This is the 21st day of my participation in the More text Challenge. For more details, see more text Challenge

MyBatis is an excellent persistence layer framework that supports common SQL queries, stored procedures and advanced mappings. MyBatis eliminates almost all manual setup of JDBC code and parameters and retrieval encapsulation of result sets. MyBatis maps interfaces and Java POJos (Plain Old Java Objects) to records in the database using simple XML or annotations for configuration and raw mapping.

  1. Rely on
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> The < version > 2.1.0 < / version > < / dependency >Copy the code

Other dependencies, Web, Lombok,mysql

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>  <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>Copy the code
  1. Create the database table as follows and configure the connection

Set the ID to increment

Configuration file Properties

#mysql spring.datasource.url=jdbc:mysql://localhost:3306/test? serverTimezone=UTC spring.datasource.username=root spring.datasource.password=Copy the code

3. Create MVC layer 3 (Domain and DAO, Service, CTRL)

domain

@Data public class Test { private int id; private String name; private int age; @jsonFormat (pattern = "YYYY-MM-dd ") private Date workDate; }Copy the code

Dao, add, delete, change and check

// Add method, I'm passing in an object, @param @insert (" Insert into mybatis (name,age,work_date) values(#{name},#{age},#{workDate})") void Insert(Test test); // @Insert("insert into mybatis (name,age,work_date) values(#{test.name},#{test.age},#{test.workDate})") // void insert(@Param("test") Test test); @delete (" Delete from mybatis where id = #{id}") void Delete(int id); @update (" Update mybatis set age =#{age} where id =#{id}") void Update(int id,int age); Update(" Update mybatis set age =#{age} where id =#{id}") void Update(int id,int age); @select (" Select * from mybatis") List<Test> listTest();Copy the code

1) Pass multiple parameters. The parameters are passed using the wrapper type Integer

@Select("<script>select * from mybatis m " +" where 1 = 1" +"<if test='name! =null and name ! =\"\"'>" +"and m.name = #{name,jdbcType=VARCHAR}" +"</if>" +"<if test='age! =null and age ! =\"\"'>" +"and m.age = #{age,jdbcType=INTEGER}" +"</if> </script>") List<Test> listTestQuery(String name,Integer age);Copy the code

2) Pass the object, the same as when the object is added

@Select("<script>select * from mybatis m " +" where 1 = 1" +"<if test='name! =null and name ! =\"\"'>" +"and m.name = #{name,jdbcType=VARCHAR}" +"</if>" +"<if test='age! =null and age ! =\"\"'>" +"and m.age = #{age,jdbcType=INTEGER}" +"</if> " +"</script>") List<Test> listTestQuery2(Test test);Copy the code

5. Page 1) SQL page is passed to the current page and page size, when called currIndex-1

@Select("select * from mybatis limit #{currIndex},#{pageSize}")
    List<Test> listTest(int currIndex,int pageSize);
Copy the code

2) Array pagination, query all returned parts

    @RequestMapping("/findall")
    public List<Test> findall(int currIndex,int pageSize) {
        List<Test> tests = testDao1.listTest();
        return tests.subList(currIndex-1,pageSize);
    }
Copy the code

6. Others: Use @requestBody to receive myBatis configurations when receiving objects

# mybatis hump mapping, shows the SQL (com. Mybatis. Demo. Dao need to display the package). Mybatis configuration. The map - the underscore - to - camel case = true logging.level.com.mybatis.demo.dao=debugCopy the code