-
steps
1. Create a Maven project to introduce dependencies
<! -- Inherit springboot parent project -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7. RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! --mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<! --mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<! SetTemplateEngine (new FreemarkerTemplateEngine()); -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<! -- Template Engine -->
<! --<dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> The < version > 2.1 < / version > < / dependency > -- >
<! -- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Copy the code
The core dependencies of Mybatis- Plus integration with SpringBoot are Mybatis- Plus-boot-starter and Freemaker template engine (or Velocity template engine).
Note: If you do not inherit the Springboot parent project, be sure to include the version number, otherwise you do not know which version dependency will download errors
2. Use a code generator to generate code
Create a parent package com.xxx under SRC /main/ Java, create a code generator class mpGenerater.java under the parent package, paste in the template code click here, modify the template code slightly, and then execute the main method.
3. The test
Test use of Mybatis-plus general CRUD method
4. Customize methods
Mybatis-plus supports custom methods. If a custom method needs to add a new attribute to the entity class, it must add such a comment on the attribute: @tableField (exist = false), otherwise an error will be reported using its generic CRUD method. Adding this annotation will tell Mybatis -Plus that this attribute always has no field in the database. The default is true, so the new attribute will need to be manually changed to False.
Entity class
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@TableName("cmfz_article")
public class Article extends Model<Article> {
private static final long serialVersionUID = 1L;
private String id;
private String title;
@TableField("publish_date")
@JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss",timezone = "GMT+8")
private Date publishDate;
@TableField("guru_id")
private String guruId;
private String content;
// Omit table fields while querying
@TableField(exist = false)
private Guru guru;
@Override
protected Serializable pkVal(a) {
return this.id; }}Copy the code
mapper.xml
<! -- Query mapping result -->
<resultMap id="BaseResultMap" type="com.baizhi.entity.Article">
<id column="id" property="id" />
<result column="title" property="title" />
<result column="publish_date" property="publishDate" />
<result column="guru_id" property="guruId" />
<result column="content" property="content" />
<association property="guru" javaType="com.baizhi.entity.Guru">
<id column="gid" property="id" />
<result column="dharma" property="dharma" />
</association>
</resultMap>
<! -- Generic query result column -->
<sql id="Base_Column_List">
id, title, publish_date AS publishDate, guru_id AS guruId, content
</sql>
<! Table join query all articles and article authors and pagination; -->
<select id="findAllArticle" resultMap="BaseResultMap">
select a.id, a.title, a.publish_date, a.guru_id, a.content, g.id gid,g.dharma from cmfz_article a
left join cmfz_guru g
on a.guru_id = g.id
limit #{start},#{rows}
</select>
Copy the code