MyBatis is an excellent persistence layer framework. Although Spring Boot does not integrate MyBatis officially, the MyBatis team ADAPTS the corresponding launcher to further simplify the operation of data using MyBatis

Because of the convenience of Spring Boot framework development, it is very simple to realize the integration of Spring Boot and data access layer framework (such as MyBatis). It is mainly to introduce the corresponding dependent initiator and set the parameters related to the database

Basic environment construction:

1. Data preparation

In MySQL, a database, SpringBootData, is created, and then two tables, T_ARTICLE and T_COMMENT, are created and inserted into the tables. Where the a_id of the comment table T_comment is associated with the primary key ID of the article table T_article

CREATEDATABASE springbootData; # select USEspringbootdata; DROPTABLE IF EXISTS t_article; CREATETABLE t_article (id int(20) NOT NULL AUTO_INCREMENT COMMENT 'increment ', Title varchar(200) DEFAULT NULL COMMENT '主 体 ', content longtext COMMENT' 主 体 ', PRIMARY KEY (id) )ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; INSERTINTO T_article VALUES ('1', 'Spring Boot basics ', 'From beginner to master... '); INSERTINTO T_article VALUES ('2', 'Spring Cloud Basics ', 'From beginner to master... '); Create table t_comment DROPTABLE IF EXISTS t_comment; CREATETABLE t_COMMENT (id int(20) NOT NULL AUTO_INCREMENT COMMENT 'increment ', content longtext COMMENT' increment ', Author varchar(200) DEFAULT NULL COMMENT '标 题 ', a_idint(20) DEFAULT NULL COMMENT' 标 题 ', PRIMARY KEY (id) )ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; INSERTINTO T_comment VALUES ('1', 'luccy', '1'); INSERTINTO t_comment VALUES ('2', 'Tom ', '1'); INSERTINTO T_comment VALUES ('3', 'Eric ', '1'); INSERTINTO t_comment VALUES ('4', '0 ',' 0 ', '1'); INSERTINTO T_comment VALUES ('5', '0 ',' 1 ', '2');Copy the code

2. Create a project and import an initiator

<imgsrc=”./images/image-20191227142727497.png”alt=”image-20191227142727497″ style=”zoom:67%;” />

3. Write the entity class Comment and Article corresponding to the database table T_COMMENT and T_article

publicclass Comment {
private Integer id;
privateString content;
private String author;
private Integer aId;
// Omit the property getXX() and setXX() methods
// omit the toString() method
}
Copy the code
public class Article {

private Integer id;
private String title;
private String content;
// Omit the property getXX() and setXX() methods
// omit the toString() method
}
Copy the code

4. Write a configuration file

(1) Configure the database connection in the application.properties configuration file

# MySQL database connection configuration spring. The datasource. Url = JDBC: MySQL: / / localhost: 3306 / springbootdata? serverTimezone=UTC spring.datasource.username=root spring.datasource.password=rootCopy the code

Annotation mode integration Mybatis

(1) Create a CommentMapper interface for t_COMMENT data operations on the database table

@Mapper
publicinterface CommentMapper {
@Select("SELECT * FROM t_comment WHEREid =#{id}")
public Comment findById(Integer id);
}
Copy the code
@mapper annotation indicates that this class is a MyBatis interface file, and it is guaranteed to be automatically scanned by Spring Boot to the corresponding interface class in Spring container. To solve this problem, add @mapperscan directly to the Spring Boot project startup class."xxx") annotations, no longer need to add @mapper annotations one by one, @mapperscan ("xxx"The annotation is similar to the @mapper annotation, but it must specify the package name to scanCopy the code
These contents, from the pull education "Java engineers high salary training camp" to learn, the course content is very comprehensive, as well as pull internal push large factory services, recommend you also have a look.Copy the code