1.MybatisPlus常用注解:
1.1 当数据库、表名和字段名和实体类完全一致时无需加注解,不一致时:
@TableName指定库名
@TableId指定表名
@TableField指定字段名
1.2 自增主键:
@TableId(type=IdType.AUTO)
private Long id;
1.3 实体类中属性不是表字段:
@TableField(exist=false)
2.内置增删改查:
这里如果加了@Data注解但无法生效,应该是没有安装Lombok插件,在plugin中添加即可
2.1 增:
@Test
public void testInsert(){
User user=new User();
user.setName("lxj");
user.setEmail("lxj@163.com");
user.setAge(30);
Assert.assertTrue(userMapper.insert(user)>0);
userMapper.selectList(null).forEach(System.out::println);
}
2.1 删(3种方式):
@Test
public void testDelete(){
//主键删除
// userMapper.deleteById(1l);//长整型需添加l
// userMapper.selectList(null).forEach(System.out::println);
//批量删除
//userMapper.delete(new QueryWrapper<User>().like("name","J"));
//userMapper.delete(Wrappers.<User>query().like("name","J"));
userMapper.delete(Wrappers.<User>query().lambda().like(User::getName,"J"));
userMapper.selectList(null).forEach(System.out::println);
}
2.3 改:
这里可以在实体类中添加@Accessors(chain=true)注解使set方法返回一个当前对象。
@Test
public void testUpdate(){
//基本修改
// userMapper.updateById(new User().setId(1l).setName("wayaya"));
// userMapper.selectList(null).forEach(System.out::println);
//批量修改
// userMapper.update(null,Wrappers.<User>update().set("email","ppp@163.com").like("name","J"));
// userMapper.selectList(null).forEach(System.out::println);
//批量修改
userMapper.update(new User().setEmail("ppp@163.com"),Wrappers.<User>update().like("name","J"));
userMapper.selectList(null).forEach(System.out::println);
}
2.4 查(两种方式):
@Test
public void testSelectNew(){
//System.out.println(userMapper.selectOne(Wrappers.<User>query().eq("name","Tom")));
userMapper.selectList(new QueryWrapper<User>().select("id","name")).forEach(user -> {
System.out.println(user);
});
}
3.分页
原理一样都是通过分页拦截器,查询前先查询总行数,然后再查询当前页记录。
先添加一个分页拦截器:MybatisPlusConfig
package com.lxj.quickstart.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor().setCountSqlParser(new JsqlParserCountOptimize(true));//可优化1对1连接查询效率
}
}
3.1内置分页查询:
@Test
public void testPage(){
IPage<User> page=new Page<>(2,2);
IPage<User> pr = userMapper.selectPage(page, Wrappers.<User>query());
System.out.println("总行数"+pr.getTotal());
System.out.println("总页数"+pr.getPages());
System.out.println("每页行数"+pr.getSize());
pr.getRecords().forEach(user -> {
System.out.println(user);
});
}
3.2自定义xml分页查询:
添加配置项:
#mybatisplus
mybatis-plus:
type-aliases-package: com.lxj.quickstart.entity #别名搜索
mapper-locations: classpath:/mappers/*.xml #加载映射文件
添加xml查询:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lxj.quickstart.mapper.UserMapper">
<sql id="selectSql">
SELECT
*
FROM
user
</sql>
<select id="selectUserByPage" resultType="user">
<include refid="selectSql"></include>
<where>
<if test="u.age != null">
age = #{u.age}
</if>
<if test="u.email != null">
and email like '%${u.email}%'
</if>
</where>
</select>
</mapper>
添加mapper接口:
package com.lxj.quickstart.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lxj.quickstart.entity.User;
import org.apache.ibatis.annotations.Param;
public interface UserMapper extends BaseMapper<User> {
//映射的接口中有2个参数需要@Param定义参数名,映射文件中使用p.和c.调用属性
public IPage<User> selectUserByPage(@Param("p") IPage<User> page, @Param("u") User condition);
}
这里注意第二个参数’u‘必须和xml中的u一致。
添加测试:
@Test
public void testPage2(){
IPage<User> page=new Page<>(1,2);
//条件对象
User u=new User();
u.setAge(18);
u.setEmail("@163.com");
IPage<User> pr = userMapper.selectUserByPage(page, u);
System.out.println("总行数"+pr.getTotal());
System.out.println("总页数"+pr.getPages());
System.out.println("每页行数"+pr.getSize());
pr.getRecords().forEach(user -> {
System.out.println(user);
});
}
3.3 pageHelper分页
添加依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
添加拦截器:
//两个分页插件不冲突
@Bean
public PageInterceptor pageInterceptor(){
return new PageInterceptor();
}
映射文件 :
<select id="selectUserByPage2" resultType="user">
<include refid="selectSql"></include>
<where>
<if test="age != null">
age = #{age}
</if>
<if test="email != null">
and email like '%${email}%'
</if>
</where>
</select>
映射文件对呀接口:
public List<User> selectUserByPage2(User condition);
测试:
@Test
public void testPageHelper(){
//条件对象
User u=new User();
u.setAge(18);
u.setEmail("@163.com");
PageInfo<User> page=PageHelper.startPage(1,2).doSelectPageInfo(()->{
//映射文件
userMapper.selectUserByPage2(u);
//内置方法
userMapper.selectList(Wrappers.<User>query());
});
List<User> list = page.getList();
page.getList().forEach(System.out :: println);
System.out.println("总行数"+page.getTotal());
System.out.println("总页数"+page.getPages());
System.out.println("每页行数"+page.getPageSize());
System.out.println("当前页数"+page.getPageNum());
System.out.println("起始行数"+page.getStartRow());
System.out.println("每页行数"+page.getSize());
System.out.println("是第一页"+page.isIsFirstPage());
System.out.println("是最后一页"+page.isIsLastPage());
System.out.println("有上一页"+page.isHasPreviousPage());
System.out.println("有下一页"+page.isHasNextPage());
System.out.println("页码列表"+Arrays.toString(page.getNavigatepageNums()));
}