1、定义

MyBatis-Plus(简称MP)是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。

2、简单实例

(1)项目结构



(2)MySpringBootApplication.java
package cn.hwd.mp; import org.springframework.boot.SpringApplication; import
org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class MySpringBootApplication { public static
void main(String[] args) { SpringApplication.run(MySpringBootApplication.class,
args); } }
(3)User.java
package cn.hwd.mp.domain; import java.util.Date; import lombok.Data; import
com.baomidou.mybatisplus.annotations.TableField; import
com.baomidou.mybatisplus.annotations.TableId; import
com.baomidou.mybatisplus.annotations.TableName; import
com.baomidou.mybatisplus.enums.IdType; import
com.fasterxml.jackson.annotation.JsonFormat; @Data @TableName(value = "user")
public class User { @TableId(value = "id", type = IdType.AUTO) // 主键自增长 private
Integer id; @TableField(value = "name") private String name; @TableField(value
= "age") private Integer age; @JsonFormat(pattern="yyyyMMddHHmmssSSS",
timezone="GMT+8") @TableField(value = "birth") private Date birth; }
(4)UserMapper.java
package cn.hwd.mp.mapper; import org.apache.ibatis.annotations.Mapper; import
com.baomidou.mybatisplus.mapper.BaseMapper; import cn.hwd.mp.domain.User;
@Mapper public interface UserMapper extends BaseMapper<User> { }
(5)UserMapperTests.java
package cn.hwd.mp.mapper; import java.util.Date; import java.util.List; import
org.junit.Test; import org.junit.runner.RunWith; import
org.springframework.beans.factory.annotation.Autowired; import
org.springframework.boot.test.context.SpringBootTest; import
org.springframework.test.context.junit4.SpringRunner; import
com.baomidou.mybatisplus.mapper.EntityWrapper; import
com.baomidou.mybatisplus.plugins.Page; import cn.hwd.mp.domain.User;
@RunWith(SpringRunner.class) @SpringBootTest public class UserMapperTests {
@Autowired private UserMapper userMapper; @Test public void testSelectList() {
List<User> userList = userMapper.selectList( new
EntityWrapper<User>().eq("name", "Tom")); System.out.println(userList); } @Test
public void testSelectPage() { List<User> userList = userMapper.selectPage( new
Page<User>(1, 10), new EntityWrapper<User>().eq("name", "Tom"));
System.out.println(userList); } @Test public void testSelectById() { User user
= userMapper.selectById(1); System.out.println(user); } @Test public void
testInsert() { User user = new User(); user.setName("Diana"); user.setAge(18);
user.setBirth(new Date()); int result = userMapper.insert(user);
System.out.println(result); } @Test public void testUpdateById() { User user =
new User(); user.setId(1); user.setName("Jack"); user.setAge(28);
user.setBirth(new Date()); int result = userMapper.updateById(user);
System.out.println(result); } @Test public void testDeleteById() { int result =
userMapper.deleteById(1); System.out.println(result); } }
(6)application.properties
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url =
jdbc:mysql://localhost:3306/hwd?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root spring.datasource.password = mysql
(7)pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cn.hwd</groupId>
<artifactId>mybatisplus01</artifactId> <version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>mybatisplus01</name>
<url>http://www.example.com</url> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.7.RELEASE</version> </dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <version>1.4.7.RELEASE</version>
</dependency> <dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>
<version>1.4.7.RELEASE</version> </dependency> <dependency>
<groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId>
<version>1.16.10</version> </dependency> <dependency> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <version>5.1.30</version>
</dependency> <dependency> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId> <version>2.2.0</version>
</dependency> </dependencies> </project>