此处拦截SQL语句,目的是为了拼接,分页条件
@Configuration
public class MpConfig {@Beanpublic MybatisPlusInterceptor mpInterceptor(){//1.定义Mp拦截器 ,创建MybatisPlusInterceptor拦截器对象MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();//2.添加具体的拦截器、添加分页拦截器mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());return mpInterceptor;}
}
实现BaseMapper<>接口
@Mapper
public interface UserDao extends BaseMapper {
}
@Testpublic void testPage() {IPage page = new Page(2, 5);// 分页构造器:设置 当前第几页 一页多少条IPage pageResult = userDao.selectPage(page, null);System.out.println(JSON.toJSONString(page));System.out.println("数据列表" + JSON.toJSONString(pageResult.getRecords()));System.out.println("当前页码" + pageResult.getCurrent());System.out.println("每页条数" + pageResult.getSize());System.out.println("总记录数" + pageResult.getTotal());System.out.println("总页数" + pageResult.getPages());}
}
# 开启mp的日志(输出到控制台)
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
做法:在resources下新建ogback.xml文件,内容如下:
spring:main:banner-mode: off # 关闭SpringBoot启动图标(banner)
# mybatis-plus日志控制台输出
mybatis-plus:configuration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:banner: off # 关闭mybatisplus启动图标
//方式一:按条件查询@Testpublic void test1(){// select * from user where age >= 18 and age < 65QueryWrapper qw = new QueryWrapper<>();qw.ge("age",18);qw.lt("age",65);List userList = userDao.selectList(qw);System.out.println(JSON.toJSONString(userList)); //[{"age":28,"id":5,"name":"snake","password":"123456","tel":"12345678910"},{"age":22,"id":6,"name":"张益达","password":"123456","tel":"12345678910"}]/*System.out.println(userList);不进行JSON转换输出[User(id=5, name=snake, password=123456, age=28, tel=12345678910), User(id=6, name=张益达, password=123456, age=22, tel=12345678910)]* */}
查阅源码优化全局变量处声明对象操作
3.2.2.1全局变量声明
LambdaQueryWrapper userLambdaQueryWrapper = Wrappers.lambdaQuery();//lambdaQuery 可用屏蔽底层的具体实现,未来会有变化上层代码无需过多的调整。而且不用new对象减少内存@Autowiredprivate UserDao userDao;
3.2.2.2 select * from user where age >= 18 and age < 65
传统语法 | MyBatisPlus语法 | 说明 |
---|---|---|
< | lt | less than |
<= | le | less equal |
> | gt | greater than |
>= | ge | greater equal |
= | eq | equal |
between and | 范围 | |
like | 模糊查询 | |
in | 在in之后的列表中的值,多选一 |
//lambda格式@Testpublic void test2(){// select * from user where age >= 18 and age < 65//LambdaQueryWrapper userLambdaQueryWrapper1 = new LambdaQueryWrapper<>();// LambdaQueryWrapper userLambdaQueryWrapper = Wrappers.lambdaQuery();userLambdaQueryWrapper.ge(User::getAge,18).lt(User::getAge,65);List userList = userDao.selectList(userLambdaQueryWrapper);System.out.println(JSON.toJSONString(userList));System.out.println("=================");System.out.println(userList);}
// 等于@Testpublic void test3(){//select * from user where name = "tom"userLambdaQueryWrapper.eq(User::getName,"tom");List userList = userDao.selectList(userLambdaQueryWrapper);System.out.println(JSON.toJSONString(userList));}
/*** null值判断* 判断 字段值是否为null 不为null才拼接查询条件*/@Testpublic void test31(){//模拟前端传的参数//select * from user where name = nullUser user = new User();/* if (user.getName()!=null){userLambdaQueryWrapper.eq(User::getName,user.getName());}健壮性判断*/userLambdaQueryWrapper.eq(user.getName()!= null,User::getName,user.getName());//此处user.getName()为空,User::getName与user.getName()作对比,// 即 在数据库实体类中的name属性="null" 做判断条件List userList = userDao.selectList(userLambdaQueryWrapper);System.out.println(JSON.toJSONString(userList));}
// like模糊查询@Testpublic void test4(){//select * from user where name like "j%"userLambdaQueryWrapper.like(User::getName,"j");List list = userDao.selectList(userLambdaQueryWrapper);System.out.println(list);}
/*** between*/@Testpublic void test5(){//select * from user where age between 16 and 28userLambdaQueryWrapper.between(User::getAge, 16, 28);List list = userDao.selectList(userLambdaQueryWrapper);System.out.println(list);}
/*** in*/@Testpublic void test6(){//select * from user where id in (1,2,3)List inList = Arrays.asList(1, 2, 3);userLambdaQueryWrapper.in(User::getId, inList);List list = userDao.selectList(userLambdaQueryWrapper);System.out.println(list);}
/*** 根据id列表批量查询*/@Testpublic void test1(){List ids = Arrays.asList(1, 2, 3);//将一个变长参数或者数组转换成ListList userList = userDao.selectBatchIds(ids);System.out.println(userList);}
/*** 根据id列表批量删除*/@Testpublic void test2(){List ids = Arrays.asList(1, 2, 3);int count = userDao.deleteBatchIds(ids);System.out.println(count);}
/*** 聚合查询一般用: selectMaps*/@Testpublic void test(){//select tel, count(*) as cnt from user group by tel order by cnt;QueryWrapper userQueryWrapper = new QueryWrapper<>();userQueryWrapper.select("tel","count(*) as cnt");userQueryWrapper.groupBy("tel");userQueryWrapper.orderByAsc("cnt");List
注解 | 说明 |
---|---|
@TableField | 通过value属性(value="数据库中实际字段名") ,设置当前属性对应的数据库表中的字段关系 |
@TableField | 通过exist属性(true存在;false不存在) ,设置属性在数据库表字段中是否存在,默认为true。不能与value合并使用 |
@TableField | 通过select属性(true参与;false不参与) :设置该属性是否参与查询。此属性与select()映射配置不冲突。 |
@TableName | 通过value属性@TableName("数据库中实际表名") ,设置当前类对应的数据库表名称 |
注解 | 说明 |
---|---|
@TableId | 1、AUTO(0): 使用数据库id自增策略控制id生成; 2、NONE(1):不设置id生成策略;3、INPUT(2):用户手工输入id;4、ASSIGN_ID(3):雪花算法生成id (可兼容数值型与字符串型); 5、ASSIGN UUID(4):以UUID生成算法作为id生成策略 |
4.1.1 全局配置
mybatis-plus:global-config:db-config:id-type: assign_idtable-prefix: tbl_
package com.itheima.domain;import com.baomidou.mybatisplus.annotation.*;import lombok.Data;@Data
public class User {private Long id;//逻辑删除字段,标记当前记录是否被删除@TableLogicprivate Integer deleted;}
4.2.2.1 全局配置逻辑删除字面值(不建议)
mybatis-plus:global-config:db-config:table-prefix: tbl_# 逻辑删除字段名logic-delete-field: deleted# 逻辑删除字面值:未删除为0logic-not-delete-value: 0# 逻辑删除字面值:删除为1logic-delete-value: 1
乐观锁的解决思想:
首先,在多个线程访问共享资源(数据库)时,给数据库添加一个version(int)的标记字段,然后,当某一个线程首先获取到数据库中资源是,version发生改变(常规操作自动加一),