After setting up the SpringBoot environment and creating the Maven project

Create entry class:

@mapperscan (basePackages = "com.baizhi. Dao ") @springBootApplication public class Applincation {  public static void main(String[] args) { SpringApplication.run(Applincation.class,args); }}Copy the code

2, create a unified configuration application.yml

There is a TAB key between levels and a space between property names and values

Configure the springBoot port number and project name. The project name must start with /

server:
  port: 8989
  servlet:
    context-path: /userModel
Copy the code

Configure the location of the mybatis mapper. XML file

mybatis:
mapper-locations: classpath:com/baizhi/mapper/UserDao*.xml
Copy the code

B Add mubatis configuration to start batch processing of Mybatis, which can improve the efficiency of similar operations, such as delete by ID cycle

mybatis.executor-type=batch
Copy the code

The ali connection pool used to configure connection properties for the database

spring: datasource: username: root password: root type: com.alibaba.druid.pool.DruidDataSource driver-class-name: Com.mysql.jdbc.driver url: JDBC :mysql:// host name :3306/ library name? useSSL=false&characterEncoding=UTF8&serverTimezone=GMTCopy the code
Configure the log level that springBoot automatically depends onCopy the code
Logging: level: com.baizhi. Dao: DEBUG com.baizhi. Service: Warning root: info # GlobalCopy the code

Configure the time format received by the entity class

Spring: MVC: date-format: YYYY-MM-DD Jackson: date-format: YYYY-MM-DD time-zone: GMT+8 # Spring: MVC: date-format: YYYY-MM-DD Time-zone: GMT+8 #Copy the code

Post form submission garble and Tomcat garble are utF-8 by default

spring.http.encoding.charset=UTF-8
server.tomcat.uri-encoding=UTF-8
Copy the code

3, rely on

Start with the Arbitration Center in the < dependencies > TAB

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> < version > 2.1.5. RELEASE < / version > < / parent >Copy the code

Import the Web launcher for SpringBoot inside < dependencies >

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
Copy the code

SpringBoot integration Mybatis needs to import Mybatis initiator database here guide when Mysql connection pool here guide Ali

<! -- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 1.3.3 < / version > < / dependency > <! -- MSQ --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector- Java </artifactId> <version>8.0.12</version>  </dependency> <! Alibaba </groupId> <artifactId>druid</artifactId> <version>1.0.31</version> </dependency>Copy the code

It’s needed for testing

<! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>Copy the code

Some common comments to use when writing code

The service layer

@service ("IUserServiceInt") @service ("IUserServiceInt") @Transactional(Propagation = Propagation.SUPPORTS, readOnly = true) @Transactional@AutoWired // is used to inject instance beans from the Spring container into a class, usually on properties of a class, via type injection. The @resource // annotation is used to inject instance beans into the class as above, except that it is injected through the name type.Copy the code

The Controller layer

@responseBody automatically converts the return value object to a JSON string and responds back to the client to receive only json @restController @requestmapping (value = "/FormUserController") // is a RequestMapping annotation that can be used on classes or methods. Used on a class, this address is used as the parent path for all methods in the class that respond to requests. @postmapping (value = "/registerUser") // add @getMapping (value = "/queryUserByPage") // Query @deletemapping (value =" "/deleteUserByIds") // delete @putMapping (value = "/updateUser") @requestParam (value = "ids") Integer[] IDS is used before method arguments. Equivalent to request.getparameter (); @requestoart (value="") @pathVariable ("id") @requestoart (value=""Copy the code

Annotations used by SpringBoot tests

import static org.junit.Assert.*; @springbooTtest (classes = {entry class name.class}) @runwith (springrunner.class)public class UserServiceTest {@autoWired private IUserService userService; @test public void saveUserTest(){User User = new User("赵小 王 ", true, "123456", new Date(), "aa.png", "qq.com"); userService.saveUser(user); AssertNotNull (" User ID is not empty ", user.getid ()); If user.getid () is empty, an error is reportedCopy the code

supplement

@Component: a generic annotation for a Component. This annotation can be used to annotate @repository: DAO (database access layer) when components are difficult to categorizeCopy the code

Mapper. XML sets the default style in IDEA

 

Style template

<? The XML version = "1.0" encoding = "utf-8"? > <! DOCTYPE mapper PUBLIC "- / / mybatis.org//DTD mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > < mapper Namespace ="dao layer interface name "></mapper >Copy the code