java
笔记:
* 依赖MybatisPlus 3.5.5
* 自动映射:
a) 表名和实体类的映射 -> t_stu 表 Student 实体类:
1.当表名和实体类名不一样的时候再实体类类名上加上@TableName注解@TableName("t_stu")
2.也可以在配置文件中配置数据库表名的前缀,配置之后只要类名称与表名一致就无需@TableName注解
mybatis-plus.global-config.db-config.table-prefix: tb_
b) 字段名和实体类属性名(不是变量名,属性名是get/set方法中的setStuName中的StuName)的映射
c) 字段名下划线命名方式和实体类属性小驼峰命名方式映射:
mybatis-plus.configuration.map-underscore-to-camel-case: true
d) 字段映射:
1.字段名称与实体类属性名称不一致时,使用@TableField注解指定字段的名称即可完成封装@TableField(value= "XXX")
2.字段失效,不希望该属性被查询,隐藏这个字段@TableField(select= false)
e) 视图属性:
有些字段不需要数据库存储,但需要展示,实体类中有这个有这个属性但数据库中不存在,叫视图属性。@TableField(exist = false)
* 查询:(条件构造器、等值查询、范围查询、判断为空、包含查询、分组查询、聚合查询、排序查询、
func查询、逻辑查询、自定义条件查询、last查询、exists查询、字段查询)
1.条件构造器介绍(自己看源码):Wrapper、AbstractWrapper、AbstractLambdaWrapper、QueryWrapper、LambdaQueryWrapper:
a) Wrapper
抽象类、子类是AbstractWrapper
b) AbstractWrapper
抽象类、子类是AbstractLambdaWrapper、QueryWrapper
c) AbstractLambdaWrapper
抽象类、子类是LambdaQueryWrapper
d) QueryWrapper(重点掌握)
继承于AbstractWrapper,非抽象类,字符串方式表示字段,创建该类对象。继承AbstractWrapper
e) LambdaQueryWrapper(重点掌握)
继承于AbstractLambdaWrapper,非抽象类,方法引用方式表示字段,创建该类对象
2.等值(eq)/不等值(ne)查询:
a) 单个等值条件查询:
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("stu_name", "张三");
LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper();
lambdaQueryWrapper.eq(Student::getStuName, "张三");
* 如果传递参数的值为null,将不作为查询条件(当参数不为null的时候作为查询条件)
lambdaQueryWrapper.eq("param" != null, Student::getStuName, null);
b) 多个条件查询,再加一个eq(),或者使用allEq(Map)或者allEq(Map, false),后者设置false表示为null的字段舍弃,不进行查询。
c) 范围查询:
大于(gt): lambdaQueryWrapper.gt(Student::getStuAge, 18);
大于等于(ge): lambdaQueryWrapper.ge(Student::getStuAge, 18);
小于(lt): lambdaQueryWrapper.lt(Student::getStuAge, 18);
小于等于(le): lambdaQueryWrapper.le(Student::getStuAge, 18);
范围之间(between): lambdaQueryWrapper.between(Student::getStuAge, 18, 50);
不在范围之间(notBetween): lambdaQueryWrapper.notBetween(Student::getStuAge, 18, 50);
d) 模糊查询:
like/notLike/likeLeft/likeRight/notLikeLeft/notLikeRight等
lambdaQueryWrapper.like(Student::getStuName, "张");
.......
e) 判空查询:
isNull/isNotNull
lambdaQueryWrapper.isNull(Student::getStuName, "张");
.....
f) 包含查询: in/notIn/inSql
in/notIn:
ArrayList<Integer> arrayList = new ArrayList<>();
Collections.addAll(arrayList, 18,50,12);
lambdaQueryWrapper.in(Student::getStuAge, arrayList);
lambdaQueryWrapper.notIn(Student::getStuAge, arrayList);
inSql: notInSql 反之
ArrayList<Integer> arrayList = new ArrayList<>();
Collections.addAll(arrayList, "18,50,13");
demo1:
lambdaQueryWrapper.inSql(Student::getStuAge, arrayList);
demo2: 还可以传sql语句查询的结果
lambdaQueryWrapper.inSql(Student::getStuAge, "select stu_age from t_stu where stu_age > 20");
g) 分组查询:
假设数据库的分组语句:select stu_sex,count(*) as sex_count from t_stu group by stu_sex;
QueryWrapper queryWrapper = new QueryWrapper();
// 指定分组的字段
queryWrapper.groupBy("stu_sex");
// 展示的字段
queryWrapper.select("stu_sex,count(*) as sex_count");
// 查询结果
List<Map<String, Object>> list = studentMapper.selectMaps(queryWrapper);
h) 聚合查询:
假设数据库的分组语句(不能使用where):
select stu_sex,count(*) as sex_count from t_stu group by stu_sex having sex_count > 3;
QueryWrapper queryWrapper = new QueryWrapper();
// 指定分组的字段
queryWrapper.groupBy("stu_sex");
// 展示的字段
queryWrapper.select("stu_sex,count(*) as sex_count");
// 聚合条件筛选
queryWrapper.having("sex_count >3");
// 查询结果
List<Map<String, Object>> list = studentMapper.selectMaps(queryWrapper);
i) 排序查询: asc/desc
1.orderByAsc:
LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper();
// 指定一个字段
lambdaQueryWrapper.orderByAsc(Student::getStuAge);
// 指定两个字段
//lambdaQueryWrapper.orderByAsc(Student::getStuAge, Student::getId);
studentMapper.selectList(lambdaQueryWrapper);
2.orderByDesc ===> 同上 orderByAsc改为orderByDesc
3.orderBy
orderBy(boolean condition, boolean isAsc, R column);
a) condition -- 执行条件,如果排序字段的值为null的时候, 是否还要作为排序字段
b) isAsc -- 是否是 ASC 排序
c) column -- 单个字段
lambdaQueryWrapper.orderBy(true,true,Student::getStuAge);
j) func查询:内嵌逻辑查询
LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper();
// 拼接不同的查询条件
lambdaQueryWrapper.func(studentLambdaQueryWrapper -> {
// 根据自己实际情况来做判断
if (true) {
studentLambdaQueryWrapper.gt(Student::getStuAge, 30);
} else {
studentLambdaQueryWrapper.gt(Student::getStuAge, 10);
}
});
studentMapper.selectList(lambdaQueryWrapper);
k) 逻辑查询: and/or
a) and逻辑查询,sql: select * from t_stu where stu_age > 10 and stu_age < 30;
LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper();
lambdaQueryWrapper.gt(Student::getStuAge, 10).lt(Student::getStuAge, 30);
studentMapper.selectList(lambdaQueryWrapper);
b) and嵌套查询,sql: select * from t_stu where stu_sex = '女' and (stu_age> 18 or stu_age < 25);
lambdaQueryWrapper.eq(Student::getStuSex, "女").and(studentLambdaQueryWrapper -> {
studentLambdaQueryWrapper.gt(Student::getStuAge, 18).or().lt(Student::getStuAge, 25);
});
studentMapper.selectList(lambdaQueryWrapper);
c) or逻辑查询:
lambdaQueryWrapper.lt(Student::getStuAge, 30).or().gt(Student::getStuAge, 10);
studentMapper.selectList(lambdaQueryWrapper);
d) or内嵌套查询与and嵌套查询
......
e) nested逻辑查询: where后面拼接多个条件
lambdaQueryWrapper.nested(i -> i.eq(Student::getStuName, "小舞")
.ne(Student::getStuAge, 40).eq(Student::getStuSex, "女"));
拼接后的Sql: WHERE ((stu_name = ? AND stu_age <> ? AND stu_sex = ?))
l) 自定义条件查询apply:直接写sql语句
lambdaQueryWrapper.apply("id < 5");
拼接后的Sql: WHERE (id < 5)
m) last查询: 也是拼接字符串,拼接在SQL语句的最后面
lambdaQueryWrapper.last("limit 0,3");
拼接后的Sql: FROM t_stu limit 0,3
n) exists查询:
Sql: select * from t_stu where exists (select id from t_stu where stu_age = 1000);
主查询: select * from t_stu
子查询: (select id from t_stu where stu_age = 1000)
当我们的子查询出的结果有值/结果集的时候,exists函数返回true,否者false
select * from t_stu where true // 能讲所有数据查询出来
select * from t_stu where false // 查询结果无数据
1.exists函数返回的是boolean
// 构建查询条件
lambdaQueryWrapper.exists("select id from t_stu where stu_age = 18");
拼接后的Sql: WHERE (EXISTS (select id from t_stu where stu_age = 18))
2.notExists相反
o) 字段查询:
// select 查询要展示的字段
lambdaQueryWrapper.select(Student::getId, Student::getStuAge,Student::getStuName);
打印出的Sql: SELECT id,stu_age,stu_name FROM t_stu
分页
java
1.创建一个拦截器。给sql语句增加一个拦截器,然后把分页语句/参数拼接上去
a) 创建mybatisPlus配置文件MybatisPlusConfig,在配置文件中添加分页插件
// 官网 https://baomidou.com/pages/97710a/#%E6%94%AF%E6%8C%81%E7%9A%84%E6%95%B0%E6%8D%AE%E5%BA%93
/**
* 添加分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
//interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); 如果有多数据源可以不配具体类型 否则都建议配上具体的DbType
return interceptor;
}
b) 测试:方式一
LambdaQueryWrapper<Student> lambdaQueryWrapper = new LambdaQueryWrapper<>();
// 指定一个分页对象,包含对象的分页信息 IPage
// long current 当前页, long size 每页显示条数
// Page 是 IPage的接口实现类
IPage<Student> studentPage = new Page<>(1, 3);
// 执行查询
IPage<Student> iPage = studentMapper.selectPage(studentPage, lambdaQueryWrapper);
// 获取分页查询的信息
System.out.println("当前页: " + iPage.getCurrent());
System.out.println("每页显示的条数: " + iPage.getSize());
System.out.println("总条数: " + iPage.getTotal());
System.out.println("总页数: " + iPage.getPages());
// 接受查询的结果
List<Student> records = iPage.getRecords();
records.forEach(i -> System.out.println(i));
c) 测试:方式二
1. Mapper层:
// 自定义Sql查询
IPage<Student> selectByStuName(IPage<Student> page, String stuName);
2. XML:
<select id="selectByStudentName" resultType="Student">
select * from t_stu where stu_name = #{stuName}
</select>
3.运行:
@Test
void test02() {
IPage<Student> studentPage = new Page<>(1,5);
IPage<Student> iPage = studentMapper.selectByStudentName(studentPage, "安妮");
List<Student> students = iPage.getRecords();
students.forEach(i -> System.out.println(i));
}