mybatis spring boot in depth

[size=large] The first blog post said that everyone can operate the background data by integrating spring boot and mybatis, but how to efficiently operate the database to save code. Let's talk about the general mapper of mybatis.
Chapter 1: pom file explains
what dependencies need to be introduced in maven's pom file?
<dependency>
			<!--Tool interface class is used for entity mapper inheritance-->
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper</artifactId>
			<version>3.3.1</version>
		</dependency>
		<dependency>
			<groupId>com.github.jsqlparser</groupId>
			<artifactId>jsqlparser</artifactId>
			<version>0.9.1</version>
		</dependency>

The introduction of com.github.jsqlparser is the support for tk.mybatis: for example, we do some simple complainMapper.selectAll() queries, all operations do not need to go to the xml of mybatis to write a query, and there are many ways to use mapper points directly, here The com.github.jsqlparser will help us parse these methods into sql. Of course, the mapper here must inherit some things. The following is a detailed explanation of how to inherit and how to use it.
Step 2: Base class mapper and subclass mapper
1. First we create a class to inherit the generic mapper
public interface BaseMapper<T> extends Mapper<T>, MySqlMapper<T> {

}

2. The entity mapper inherits the BaseMapper interface we created
public interface ComplainMapper extends BaseMapper<Complain>{
}

3. I give the entity class
@Table(name="complain")
public class Complain {
  @Id
  private Integer id;
  @Column
  private String openId;//Username
  @Column
  private Date complainTime;//Complaint time
  @Column
  private Date replyTime;//Reply time
  @Column
  private Integer replyName;//Reply name
  @Column
  private Date createTime;//Create time
  @Column
  private String complainContent;//Complaint content
  @Column
  private String replyContent;//reply content
  @Column
  private Date updateTime;//Update time

I won't give the get set
4. To operate ComplainMapper
, we don't need to write xml to look directly at the methods provided by ComplainMapper.



We can now clearly see that there are already many methods, and these methods are enough for us to do some simple things. It is also convenient for us to write less xml configuration and interface code. I believe that everyone can achieve simple crud as long as they follow these steps.
5. Realize fuzzy query
public List<Complain> selectComplainNoReplyContent() {
    // tk -mybatis
    Example example = new Example(Complain.class);
    // replyContent corresponds to the database field hha is to query whether there is hha equal to select *from complian where reply_complian="hha"
    example.createCriteria().andEqualTo("replyContent", "hha");
    return complainMapper.selectByExample(example);
  }

This is just an example and there are many styles of fuzzy query. You can explore it slowly
. 6. My environment was built in the environment of the first blog post, and I will also give the code below. . . . Everyone can do some cuuds. I have been a little busy with new ones recently. I hope everyone understands
[/size]

Guess you like

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