同一个网站买多家cdn,微信开发网站制作,大连h5开发公司,优是是什么网站摘要#xff1a;本文围绕 MyBatis-Plus 数据操作展开#xff0c;涵盖标准数据层 CRUD 与分页查询#xff1b;以及各种的复杂 SQL 查询#xff1b;映射匹配#xff08;TableField、TableName 注解#xff09;与 ID 生成策略#xff08;TableId 五种类型及全局配置#x…摘要本文围绕 MyBatis-Plus 数据操作展开涵盖标准数据层 CRUD 与分页查询以及各种的复杂 SQL 查询映射匹配TableField、TableName 注解与 ID 生成策略TableId 五种类型及全局配置多数据批量操作以及逻辑删除标记字段、注解、配置和乐观锁字段、注解、拦截器机制全面介绍 MP 核心数据操作功能。思维导图1. 标准数据层CRUD与分页查询基础增删改查//新增方法 Testvoid testSave() {User usernew User();user.setId(1L);user.setUsername(tom);userMapper.insert(user);}//删除方法Testvoid testDelete(int id) {userMapper.deleteById(id);}//修改方法Testvoid testUpdateById() {User usernew User();user.setId(1L);user.setUsername(Tom);userMapper.updateById(user);}//根据id查询数据Testvoid testGetById(int id) {userMapper.selectById(id);}//查询全部Testvoid testGetAll() {ListUser userList userMapper.selectList(null);System.out.println(userList);}
分页查询1.配置MP拦截器
Configuration
public class MpConfig {Beanpublic MybatisPlusInterceptor mpInterceptor(){MybatisPlusInterceptor mpInterceptornew MybatisPlusInterceptor();mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());return mpInterceptor;}
}2.分页查询业务代码
//分页查询Testvoid testSelectPage() {int current 1;int size 2;IPage page new Page(current, size);userMapper.selectPage(page,null);System.out.println(当前页码值:page.getCurrent());System.out.println(每页显示数:page.getSize());System.out.println(一共多少页:page.getPages());System.out.println(一共多少条:page.getTotal());System.out.println(所有记录数:page.getRecords());}3.开启MP日志推荐开启
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl返回结果2. 复杂SQL查询方法合集WrapperMyBatisPlus将书写复杂的SQL查询条件进行了封装使用编程的形式完成查询条件的组合1.条件查询 - 设置查询条件//按条件查询Testvoid SelectByCondition() {//第一种:间接使用lamda格式按条件查询QueryWrapperUser wrappernew QueryWrapper();//设置条件wrapper.lambda().lt(User::getId,2);ListUser userList1 userMapper.selectList(wrapper);System.out.println(userList1);//第二种:直接使用lamda格式按条件查询LambdaQueryWrapperUser lqWrappernew LambdaQueryWrapperUser();//设置条件lqWrapper.lt(User::getId,2);ListUser userList2 userMapper.selectList(wrapper);System.out.println(userList2);}
2.条件查询 - 组合条件查询
//按条件查询Testvoid SelectByCondition() {LambdaQueryWrapperUser lqWrapper1new LambdaQueryWrapperUser();//设置条件(并列关系)lqWrapper1.lt(User::getId,3).gt(User::getId,1);//设置条件(或者关系)lqWrapper1.gt(User::getId,3).or().lt(User::getId,1);//使用betweenlqWrapper1.between(User::getAge,16,24);//模糊查询lqWrapper1.like(userName,o);queryWrapper1.likeLeft(userName,R);queryWrapper1.likeRight(userName,e);}
3.条件查询 - NULL空值
//按条件查询 - 动态SQLTestvoid SelectByCondition() {//模拟查询请求的数据UserQuery query new UserQuery();query.setAge(18);query.setId(5L);//null值判定LambdaQueryWrapperUser lqWrapper new LambdaQueryWrapperUser();lqWrapper.gt(query.getAge()!null,User::getAge,query.getAge());}
4.条件查询 - 查询投影
先按设定条件筛选出满足要求的数据行再从这些行中提取所需的特定列最终得到既符合条件限制又仅包含目标字段的数据结果。
查询投影
//条件查询 - 查询投影Testvoid SelectByCondition() {//写法一:这种写法只适用于LambdaLambdaQueryWrapperUser lqWrapper new LambdaQueryWrapperUser();lqWrapper.select(User::getId,User::getAge,User::getUsername);//写法二:使用两次查询QueryWrapperUser queryWrappernew QueryWrapper();queryWrapper.select(id,userName,age);//若结果为单个对象使用selectOneListUser userList userMapper.selectList(queryWrapper);}查询数量查询分组
//条件查询 - 查询投影Testvoid SelectByCondition() {//查询数量QueryWrapperUser queryWrappernew QueryWrapper();queryWrapper.select(count(*) as count);//按年龄分组queryWrapper.groupBy(age);ListMapString, Object maps userMapper.selectMaps(queryWrapper);System.out.println(maps);}3. 映射匹配兼容性字段映射与表名映射使用TableField注解解决1.value属性
2.exist属性
2.select属性
使用TableName注解4.id生成策略使用TableId注解
总共五大ID生成策略使用方法
Data
TableName(value user)
public class User {//五选一TableId(type IdType.AUTO)TableId(type IdType.ASSIGN_ID)TableId(type IdType.NONE)TableId(type IdType.INPUT)TableId(type IdType.ASSIGN_UUID)private Long id;private String username;private Integer age;private String phone;
}全局配置方法全局配置5.多数据操作 - 删除和查询//批量删除Testvoid testDelete() {ListLong listnew ArrayList();for (long i 1; i 5; i) {list.add(i);}userMapper.deleteBatchIds(list);}//批量查询Testvoid testDelete() {ListLong listnew ArrayList();for (long i 1; i 5; i) {list.add(i);}userMapper.selectBatchIds(list);}6.逻辑删除1.添加逻辑删除标记字段2.实体类加TableLogic注解3.修改配置文件
mybatis-plus:global-config:db-config:logic-delete-field: isDeletelogic-delete-value: 1logic-not-delete-value: 07.乐观锁1.添加锁标记字段2.添加版本注解3.配置乐观锁拦截器实现锁机制对应的动态SQL语句拼装
至此大功告成