文章目录
MP这样一款强大的持久层框架处理起来复杂的SQL来也是得心应手,效率极高,快快与我一同领略Plus的独特魅力吧
一.分页处理
1.调用方法传入参数获取返回值
创建IPage分页对象,设置分页参数,1为当前页码,3为每页显示的记录数,执行分页查询并获取其结果
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#1f7199">@SpringBootTest</span>
<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Mybatisplus</strong></span>{
<span style="color:#1f7199">@Autowired</span>
<span style="color:#333333"><strong>private</strong></span> UserDao userDao;
<span style="color:#888888">//分页查询</span>
<span style="color:#1f7199">@Test</span>
void testSelectPage(){
IPage<User> page=new Page<>(<span style="color:#880000">1</span>,<span style="color:#880000">3</span>);
userDao.selectPage(page,<span style="color:#78a960">null</span>);
System.<span style="color:#333333"><strong>out</strong></span>.println(<span style="color:#880000">"当前页码值:"</span>+page.getCurrent());
System.<span style="color:#333333"><strong>out</strong></span>.println(<span style="color:#880000">"每页显示数:"</span>+page.getSize());
System.<span style="color:#333333"><strong>out</strong></span>.println(<span style="color:#880000">"一共多少页:"</span>+page.getPages());
System.<span style="color:#333333"><strong>out</strong></span>.println(<span style="color:#880000">"一共多少条数据:"</span>+page.getTotal());
System.<span style="color:#333333"><strong>out</strong></span>.println(<span style="color:#880000">"数据:"</span>+page.getRecords());
}
}</span></span>
2.设置分页拦截器
将MP提供的分页拦截器配置成Spring管理的bean对象
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#1f7199">@Configuration</span>
<span style="color:#333333"><strong>public</strong></span> <span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>MybatisPlusConfig</strong></span> {
<span style="color:#1f7199">@Bean</span>
<span style="color:#333333"><strong>public</strong></span> MybatisPlusInterceptor <span style="color:#880000"><strong>mybatisPlusInterceptor</strong></span>(){
<span style="color:#888888">//1 创建MybatisPlusInterceptor拦截器对象</span>
MybatisPlusInterceptor mpInterceptor=<span style="color:#333333"><strong>new</strong></span> MybatisPlusInterceptor();
<span style="color:#888888">//2 添加分页拦截器</span>
mpInterceptor.addInnerInterceptor(<span style="color:#333333"><strong>new</strong></span> PaginationInnerInterceptor());
<span style="color:#333333"><strong>return</strong></span> mpInterceptor;
}
}</span></span>
查询结果如下:
二.条件查询
2.1通过QueryWrapper对象来执行分页查询
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#1f7199">@SpringBootTest</span>
<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Mybatisplus</strong></span>{
<span style="color:#1f7199">@Autowired</span>
<span style="color:#333333"><strong>private</strong></span> UserDao userDao;
<span style="color:#1f7199">@Test</span>
<span style="color:#333333"><strong>void</strong></span> <span style="color:#880000"><strong>testGetAll</strong></span>(){
QueryWrapper qw = <span style="color:#333333"><strong>new</strong></span> QueryWrapper();
qw.lt(<span style="color:#880000">"age"</span>,<span style="color:#880000">18</span>);
List<User> userList = userDao.selectList(qw);
System.out.println(userList);
}
}</span></span>
注:lt()方法为小于(<) ,对应的SQL为:
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#333333"><strong>SELECT</strong></span> <span style="color:#333333"><strong>id</strong></span>,<span style="color:#333333"><strong>name</strong></span>,<span style="color:#333333"><strong>password</strong></span>,age,tel <span style="color:#333333"><strong>FROM</strong></span> <span style="color:#333333"><strong>user</strong></span> <span style="color:#333333"><strong>WHERE</strong></span> (age < ?)</span></span>
很容易发现, 以字符串形式输出作为查询条件可能会出现字符串拼写错误 ,针对此种情况,可以进行一下小改进!
2.2在QueryWrapper对象的基础上使用lambda表达式
为了解决以字符串形式作为输出而造成拼写错误的问题, 通过lambda来实现实体与属性对应进行查询,就极大地提高了查询的准确性
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#1f7199">@SpringBootTest</span>
<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Mybatisplus</strong></span>{
<span style="color:#1f7199">@Autowired</span>
<span style="color:#333333"><strong>private</strong></span> UserDao userDao;
<span style="color:#1f7199">@Test</span>
<span style="color:#333333"><strong>void</strong></span> <span style="color:#880000"><strong>testGetAll</strong></span>(){
QueryWrapper<User> qw = <span style="color:#333333"><strong>new</strong></span> QueryWrapper<User>();
qw.lambda().lt(User::getAge, <span style="color:#880000">10</span>);<span style="color:#888888">//添加条件</span>
List<User> userList = userDao.selectList(qw);
System.out.println(userList);
}
}</span></span>
与之对应的SQL语句同样也是:
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#333333"><strong>SELECT</strong></span> <span style="color:#333333"><strong>id</strong></span>,<span style="color:#333333"><strong>name</strong></span>,<span style="color:#333333"><strong>password</strong></span>,age,tel <span style="color:#333333"><strong>FROM</strong></span> <span style="color:#333333"><strong>user</strong></span> <span style="color:#333333"><strong>WHERE</strong></span> (age < ?)</span></span>
注:构建LambdaQueryWrapper的时候泛型不能省
当不使用泛型时会提示默认的Object类不是函数接口
而我们的 lambda()的底层又需要传进去一个实体 ,传进去Object显然不能与后面的查询条件相联系!
此时我们再次编写条件的时候,就不会存在写错名称的情况,但是qw后面多了一层lambda()调用
2.3直接通过LambdaQueryWrapper对象
这也是方式二的另一种写法,原理相同都是利用LambdaQueryWrapper
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#1f7199">@SpringBootTest</span>
<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Mybatisplus</strong></span>{
<span style="color:#1f7199">@Autowired</span>
<span style="color:#333333"><strong>private</strong></span> UserDao userDao;
<span style="color:#1f7199">@Test</span>
<span style="color:#333333"><strong>void</strong></span> <span style="color:#880000"><strong>testGetAll</strong></span>(){
LambdaQueryWrapper<User> lqw = <span style="color:#333333"><strong>new</strong></span> LambdaQueryWrapper<User>();
lqw.lt(User::getAge, <span style="color:#880000">10</span>);
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
}
}</span></span>
三.多条件查询
对于多条件的情景,MP依然可以简单化解,并且构建多条件的时候,可以支持链式编程
3.1且的情况
场景一:查询数据库表中,年龄在3岁到8岁之间的用户信息
@SpringBootTest
class Mybatisplus{
@Autowired
private UserDao userDao;
@Test
/**
* 多条件查询
*/
void testGetAll04() {
//方式四 (常用!)
LambdaQueryWrapper<Users> qw4 = new LambdaQueryWrapper<>();
qw4.lt(Users::getAge, 8); //上限
qw4.gt(Users::getAge, 3); //下限
// qw4.lt(Users::getAge, 8).gt(Users::getAge, 3); 链式编程!
List<Users> users = userDao.selectList(qw4);
System.out.println(users);
}
}
注:gt(),大于(>),最终的SQL语句为
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#333333"><strong>SELECT</strong></span> <span style="color:#333333"><strong>id</strong></span>,<span style="color:#333333"><strong>name</strong></span>,<span style="color:#333333"><strong>password</strong></span>,age,tel <span style="color:#333333"><strong>FROM</strong></span> <span style="color:#333333"><strong>user</strong></span> <span style="color:#333333"><strong>WHERE</strong></span> (age < ? <span style="color:#333333"><strong>AND</strong></span> age > ?)</span></span>
也是迅速查出来了结果
3.2或的情况
场景二:查询数据库表中,年龄小于3或年龄大于8的数据
@SpringBootTest
class Mybatisplus{
@Autowired
private UserDao userDao;
@Test
void testGetAll(){
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
lqw.lt(User::getAge, 3).or().gt(User::getAge, 8);
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
}
}
这里的or()就相当于sql语句中的 or
关键字,不加默认是 and
,最终的sql语句为:
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#333333"><strong>SELECT</strong></span> <span style="color:#333333"><strong>id</strong></span>,<span style="color:#333333"><strong>name</strong></span>,<span style="color:#333333"><strong>password</strong></span>,age,tel <span style="color:#333333"><strong>FROM</strong></span> <span style="color:#333333"><strong>user</strong></span> <span style="color:#333333"><strong>WHERE</strong></span> (age < ? <span style="color:#333333"><strong>OR</strong></span> age > ?)</span></span>
也是顺利的查了出来
四.null判定
以TB为例,我们购物时进行条件筛选时, 可以选择单条件,也可以选择多条件 ,如上,我的条件就变成price>3000,price<null,这种情况按照以上介绍的查询的方式就会出现问题,如图:
显然,这种情况在开发过程中时不被允许的。所以要求我们针对null的情况要解决如下问题:
用户在输入值的时候:
1.如果只输入第一个框,说明要查询大于该价格的商品
2.如果只输入第二个框,说明要查询小于该价格的商品
3.如果两个框都输入了,说明要查询价格在两个范围之间的商品
于是,我们可以
新建一个模型类,让其 继承Brand类,并在其中添加price2属性 ,Brand02 在拥有Brand属性后同时添加了price2属性
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#1f7199">@Data</span>
public <span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Brand</strong></span> {
<span style="color:#333333"><strong>private</strong></span> <span style="color:#880000">Long</span> id;
<span style="color:#333333"><strong>private</strong></span> <span style="color:#880000">String</span> name;
<span style="color:#333333"><strong>private</strong></span> <span style="color:#880000">Double</span> price;
}
<span style="color:#1f7199">@Data</span>
public <span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Brand02</strong></span> <span style="color:#333333"><strong>extends</strong></span> <span style="color:#880000"><strong>Brand</strong></span> {
<span style="color:#333333"><strong>private</strong></span> <span style="color:#880000">Integer</span> price2;
}</span></span>
解决了实体的问题,再来解决条件的问题
<span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#1f7199">@SpringBootTest</span>
<span style="color:#333333"><strong>class</strong></span> <span style="color:#880000"><strong>Mybatisplus02</strong></span>{
<span style="color:#1f7199">@Autowired</span>
<span style="color:#333333"><strong>private</strong></span> BrandDao brandDao;
<span style="color:#1f7199">@Test</span>
<span style="color:#333333"><strong>void</strong></span> testGetAll(){
BrandQuery bq = <span style="color:#333333"><strong>new</strong></span> BrandQuery();
LambdaQueryWrapper<Brand> lqw = <span style="color:#333333"><strong>new</strong></span> LambdaQueryWrapper<Brand>();
lqw.lt(<span style="color:#78a960">null</span>!=bq.getPrice2(),<span style="color:#880000">User</span><span style="color:#880000">:</span>:getPrice, bq.getPrice2());
lqw.gt(<span style="color:#78a960">null</span>!=bq.getPrice(),<span style="color:#880000">User</span><span style="color:#880000">:</span>:getPrice, bq.getPrice());
List<Brand> brands = brandDao.selectList(lqw);
System.out.println(brands);
}
}</span></span>
解读:
如果两个属性不为空,则查询price,price2区间范围内
实现的核心在于lt()、gt()方法,condition为boolean类型上述的 null!=bq.getPrice2()
与之对应, 返回true,则添加条件,返回false则不添加条件,条件的生效与否就是靠的这个设计!
最后,也是在null的条件下完成了查询: