目录
1.MyBatisPlus标准数据层开发
1.1 开发步骤
1.2 lombok编辑
安装插件
导坐标
使用
1.3 标准数据层CRUD功能——基本增删改查
1.4 标准数据层CRUD功能——分页
配置拦截器
测试
开日志
2.DQL控制(条件查询)
2.1 条件查询方式
条件查询null值处理
2.2 查询投影
2.3 查询条件设定
2.4 字段映射与表名映射 编辑
3.DML控制
3.1 Insert
3.1.1 id生成策略控制
1. 指定id
2. 采用数据库的id策略
3. 雪花算法生成id
3.1.2 全局配置id生成策略
3.2 Delete
3.2.1 批量删除
3.2.2 逻辑删除(实际上是update语句)MP中逻辑删除后的数据,不参与查询!!!!!
3.3 Update
3.3.1 乐观锁实现
4.代码生成器
org.projectlombok
lombok
1.18.22
provided
@Data
public class Book {
private long id;
private String type;
private String name;
private String description;
}
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
@Data //lombok的功能
@TableName("tbl_book") //表的名称
public class Book {
private long id;
private String type;
private String name;
private String description;
}
@Mapper()
public interface BookDao extends BaseMapper {
}
@SpringBootTest
class DemoApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void testGetAll() {
List books = bookDao.selectList(null);
System.out.println(books);
}
@Test
void testGetById() {
Book book = bookDao.selectById(3L);
System.out.println(book);
}
@Test
void testInser() {
Book book = new Book();
book.setName("111");
book.setName("123");
book.setDescription("546");
bookDao.insert(book);
}
@Test
void testDelete() {
bookDao.deleteById(27L);
}
@Test
void testUpdate() {
Book book = new Book();
book.setId(3L);
book.setName("12345");
bookDao.updateById(book);
}
}
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
//1.定义拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//2.添加具体的拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
@Test
void testSelectPage() {
IPage page = new Page(1, 5);
bookDao.selectPage(page, null);
System.out.println("当前页数据:"+page.getRecords());
System.out.println("当前页码:"+page.getCurrent());
System.out.println("每页显示条数:"+page.getSize());
System.out.println("总记录数:"+page.getTotal());
System.out.println("总页数:"+page.getPages());
}
#开启mp日志,控制台输出
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
@SpringBootTest
class DemoApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void testGetAll() {
//方式1:按条件查询
// QueryWrapper wrapper = new QueryWrapper();
// wrapper.lt("id",10);//查询id小于10的数据
// List books = bookDao.selectList(wrapper);
// System.out.println(books);
//方式2:Lambda格式按条件查询
// QueryWrapper wrapper = new QueryWrapper<>();
// wrapper.lambda().lt(Book::getId, 10);
// List books = bookDao.selectList(wrapper);
// System.out.println(books);
//方式3:Lambda格式按条件查询
LambdaQueryWrapper wrapper = new LambdaQueryWrapper();
//大于5且小于10
// wrapper.gt(Book::getId,5).lt(Book::getId,10);
wrapper.gt(Book::getId,5);//大于5
wrapper.lt(Book::getId,10);//小于10
//小于5或大于10
// wrapper.lt(Book::getId,5).or().gt(Book::getId,10);
List books = bookDao.selectList(wrapper);
System.out.println(books);
}
}
@Test
void testGetAll2() {
//查询投影,只查询id和name字段
//方式1:Lambda格式按条件查询
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.select(Book::getId,Book::getName);
List books = bookDao.selectList(wrapper);
System.out.println(books);
//方式2
QueryWrapper wrapper2 = new QueryWrapper<>();
wrapper2.select("id","name");
List books2 = bookDao.selectList(wrapper2);
System.out.println(books2);
}
@Test
void testGetAll3() {
//查询投影:查询总记录数
QueryWrapper wrapper3 = new QueryWrapper<>();
wrapper3.select("count(*) as count");//Lambda格式只能查询内部属性,这里不能用
List
条件构造器 | MyBatis-Plushttps://www.mybatis-plus.com/guide/wrapper.html
@Test
void testGetAll5() {
//多条件查询
LambdaQueryWrapper wrapper5 = new LambdaQueryWrapper<>();
wrapper5.eq(Book::getName, "深入理解Java虚拟机").eq(Book::getType, "计算机理论");
Book book = bookDao.selectOne(wrapper5);
System.out.println(book);
}
@Test
void testGetAll6() {
//范围查询
LambdaQueryWrapper wrapper6 = new LambdaQueryWrapper<>();
//lt(小于)、gt(大于)、eq(等于)、le(小于等于)、ge(大于等于)
wrapper6.between(Book::getId, 5, 8);
List books = bookDao.selectList(wrapper6);
System.out.println(books);
}
@Test
void testGetAll7() {
//模糊匹配
LambdaQueryWrapper wrapper7 = new LambdaQueryWrapper<>();
// wrapper7.like(Book::getName, "计算机");// %计算机%
// wrapper7.likeLeft(Book::getName, "计算机");// %计算机
wrapper7.likeRight(Book::getType, "计算机");// 计算机%
List books = bookDao.selectList(wrapper7);
System.out.println(books);
}
@Data //lombok的功能
@TableName("tbl_book") //表的名称
public class Book {
@TableId(type = IdType.INPUT)//指定id
private Long id;
private String type;
private String name;
private String description;
}
@SpringBootTest
class DemoApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void testInser() {
Book book = new Book();
book.setId(100L);
book.setType("111");
book.setName("123");
book.setDescription("546");
bookDao.insert(book);
}
}
@Data //lombok的功能
@TableName("tbl_book") //表的名称
public class Book {
@TableId(type = IdType.AUTO)//采用数据库的id策略
private Long id;
private String type;
private String name;
private String description;
}
@SpringBootTest
class DemoApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void testInser() {
//数据库采用的是id自增
Book book = new Book();
book.setType("111");
book.setName("123");
book.setDescription("546");
bookDao.insert(book);
}
}
如果指定了id按照指定的id,如果没有指定id用雪花算法生成id
用雪花算法生成id是一个64位的二进制,因此数据库一定要用bigint来存
@Data //lombok的功能
@TableName("tbl_book") //表的名称
public class Book {
@TableId(type = IdType.ASSIGN_ID)//默认,雪花算法生成id
//如果指定了id按照指定的id,如果没有指定id用雪花算法生成id
//用雪花算法生成id是一个64位的二进制,因此数据库一定要用bigint来存
private Long id;
private String type;
private String name;
private String description;
}
mybatis-plus:
global-config:
db-config:
id-type: auto #给所有实体类设置id生成策略
table-prefix: tbl_ #给所有实体类名前面拼上tbl_,不用再写表名映射
@Test
void testDeleteBatchIds() {
ArrayList list = new ArrayList<>();
list.add(100L);
list.add(101L);
bookDao.deleteBatchIds(list);
}