官网:http://mp.baomidou.com/
参考教程:http://mp.baomidou.com/guide/
MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
步骤一:创建项目:test-mybatis-plus
步骤二:修改pom.xml,添加依赖
步骤三:创建yml文件,配置数据库相关
步骤一:创建项目:test-mybatis-plus
步骤二:修改pom.xml,添加依赖
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.5.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.0version>
dependency>
dependencies>
步骤三:创建yml文件,配置数据库相关
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/cloud_db1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 1234
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #输出日志
CREATE TABLE `tmp_customer` (
`cid` int(11) NOT NULL AUTO_INCREMENT,
`cname` varchar(50) DEFAULT NULL,
`password` varchar(32) DEFAULT NULL,
`telephone` varchar(11) DEFAULT NULL,
`money` double DEFAULT NULL,
`version` int(11) DEFAULT NULL,
`create_time` date DEFAULT NULL,
`update_time` date DEFAULT NULL,
PRIMARY KEY (`cid`)
);
insert into `tmp_customer`(`cid`,`cname`,`password`,`telephone`,`money`,`version`,`create_time`,`update_time`)
values (1,'jack','1234','110',1000,NULL,NULL,NULL),(2,'rose','1234','112',1000,NULL,NULL,NULL),(3,'tom','1234','119',1000,NULL,NULL,NULL);
步骤1:配置JavaBean
步骤2:编写dao
步骤3:编写启动类
步骤4:编写测试类
步骤1:配置JavaBean
@TableName
表名注解,value属性设置表名package com.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.List;
@Data
@TableName("tmp_customer")
public class Customer {
@TableId(type = IdType.AUTO)
private Integer cid;
private String cname;
private String password;
private String telephone;
private String money;
private Integer version;
@TableField(exist = false)
private List<Integer> ids;
}
@TableId
标签参数
写法:@TableId(value=“数据库主键字段”,type = IdType.六种类型之一)
例如:@TableId(value=“user_id”,type = IdType.AUTO )
类型 | sex |
---|---|
AUTO | 数据库自增ID |
NONE | 数据库未设置主键类型(将会跟随全局) |
INPUT | 用户输入ID(该类型可以通过自己注册自动填充插件进行填充 |
ID_WORKER | 默认的全局唯一ID(idWorker) |
UUID | 全局唯一ID(UUID) |
ID_WORKER_STR | 字符串全局唯一ID(ID_WORKER的字符串表示) |
@TableField
标签参数
写法:@TableField(参数类型)
例如:@TableField(exist = false)
值 | 描述 |
---|---|
value | 字段值(驼峰命名方式,该值可无) |
update | 预处理set字段自定义注入 |
condition | 预处理WHERE实体条件自定义运算规则 |
el | 详看注解说明 |
exist | 是否为数据库表字段(默认true存在,false不存在) |
strategy | 字段验证(默认非null判断,查看com.baomidou.mybatisplus.enums.FieldStrategy) |
fill | 字段填充标记(FieldFill,配合自动填充使用) |
package com.mp.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.czxy.mp.domain.Customer;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface CustomerMapper extends BaseMapper<Customer> {
}
package com.mp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
package com.czxy;
import com.czxy.mp.MybatisPlusApplication;
import com.czxy.mp.domain.Customer;
import com.czxy.mp.mapper.CustomerMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybatisPlusApplication.class)
public class TestDemo01 {
@Resource
private CustomerMapper customerMapper;
@Test
public void testFindAll() {
List<Customer> list = customerMapper.selectList(null);
list.forEach(System.out::println);
}
}
BaseMapper 封装CRUD操作,泛型 T
为任意实体对象
方法名 | 描述 |
---|---|
int insert(T entity) | 插入一条记录,entity 为 实体对象 |
int delete(Wrapper wrapper) | 根据 entity 条件,删除记录,wrapper 可以为 null |
int deleteBatchIds(Collection idList) | 根据ID 批量删除 |
int deleteById(Serializable id) | 根据 ID 删除 |
int deleteByMap(Map |
根据 columnMap 条件,删除记录 |
int update(T entity, Wrapper updateWrapper) | 根据 whereEntity 条件,更新记录 |
int updateById(T entity); | 根据 ID 修改 |
方法名 | 描述 |
---|---|
T selectById(Serializable id) | 根据 ID 查询 |
T selectOne(Wrapper queryWrapper) | 根据 entity 条件,查询一条记录 |
List selectBatchIds(Collection idList) | 根据ID 批量查询 |
List selectList(Wrapper queryWrapper) | 根据 entity 条件,查询全部记录 |
List selectByMap(Map |
根据 columnMap 条件 |
List | 根据 Wrapper 条件,查询全部记录 |
List selectObjs( Wrapper queryWrapper) | 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值 |
IPage selectPage(IPage page, Wrapper queryWrapper) | 根据 entity 条件,查询全部记录(并翻页) |
IPage | 根据 Wrapper 条件,查询全部记录(并翻页) |
Integer selectCount(@Param(Constants.WRAPPER) Wrapper queryWrapper) | 根据 Wrapper 条件,查询总记录数 |
@Test
public void testInsert() {
Customer customer = new Customer();
customer.setCname("测试");
System.out.println(customer);
customerMapper.insert(customer);
System.out.println(customer);
}
@Test
public void testUpdate() {
Customer customer = new Customer();
customer.setCid(4);
customer.setCname("测试777");
customer.setPassword("777");
// 需要给Customer设置@TableId
customerMapper.updateById(customer);
}
@Test
public void testUpdate2() {
Customer customer = new Customer();
customer.setCname("测试777");
customer.setPassword("777");
// 更新筛选过后的所有数据,此处没有过滤条件
customerMapper.update(customer,null);
}
@Test
public void testDelete() {
// 需要给Customer设置@TableId
int i = customerMapper.deleteById(11);
System.out.println(i);
}
@Test
public void testBatchDelete() {
// 需要给Customer设置@TableId
int i = customerMapper.deleteBatchIds(Arrays.asList(9,10));
System.out.println(i);
}