Spring boot integrates tk.mybatis

tk.mybatis is a framework based on mybatis, which aims to simplify the dao layer and xml code, and provide a series of methods to easily operate the database. The integration steps are as follows:

  1. pom file
<dependency>
	  	<groupId>tk.mybatis</groupId>
	  	<artifactId>mapper-spring-boot-starter</artifactId>
	  	<version>1.2.4</version>
	</dependency>
  1. The dao layer inherits the Mapper interface of tk.mybatis, and the generic type can be specified as the corresponding model
public interface SysTableMapper extends Mapper<SysTable>{   
    int deleteByPrimaryKeys(String[] ids);
}
  1. The content in the xml file can be left blank, because it is already encapsulated by default and does not need to be written repeatedly. If there is a custom sql, you can add it.
<?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="com.zhangda.splatform.dao.SysTableMapper">
</mapper>
  1. Add annotations to model
@Table(name = "sys_table")
public class SysTable {
	@Id
    private String id;

	@Column(name = "name")
    private String name;
    
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
  1. Don't forget to generate the Example class corresponding to the model, because the dao layer can do a lot of operations according to the example class, and the generated code can be generated by the mybatis generator. After generation, you can modify it slightly according to the above steps.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325462012&siteId=291194637