简介
BaseMapper中提供的CRUD方法
MybatisPlusInterceptor分页插件
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//注意使用哪种数据库
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
IService中提供的CRUD方法
IService接口中提供
@Test
public void test1(){
// 单个查询,多个报错
User user = userService.lambdaQuery().eq(User::getName, "Tom").one();
// 集合查询
List<User> userList = userService.lambdaQuery().like(User::getName, "J").eq(User::getAge,20).list();
// 记录数查询
Integer count = userService.lambdaQuery().like(User::getAge, 20).select(User::getName, User::getAge).count();
}
@Test
public void test2() {
// set修改某些属性
userService.lambdaUpdate().eq(User::getName, "Tom").set(User::getId, 110).set(User::getAge, 15).update();
// 修改整个对象
User user = new User();
user.setId(110L);
user.setAge(15);
userService.lambdaUpdate().eq(User::getName, "Tom").update(user);
// 根据名称删除
userService.lambdaUpdate().eq(User::getName, "Jack").remove();
}
/**
* UserService继承IService模板提供的基础功能
*/
public interface UserService extends IService<User> {}
/**
* ServiceImpl实现了IService,提供了IService中基础功能的实现
* 若ServiceImpl无法满足业务需求,则可以使用自定的UserService定义方法,并在实现类中实现
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService{}
@Data
@TableName("t_user")
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
mybatis-plus:
global-config:
db-config:
# 设置实体类所对应的表的统一前缀
table-prefix: t_
@TableId的value属性
@Data
public class User {
@TableId(value = "tid")
private Long uid;
private String name;
private Integer age;
private String email;
}
@TableId的type属性
常用主键策略:
值 | 描述 |
---|---|
IdType.ASSIGN_ID(默认) | 基于雪花算法的策略生成数据id,与数据库id是否设置自增无关 |
IdType.AUTO | 使用数据库的自增策略,注意,该类型请确保数据库设置了id自增 |
@Data
public class User {
@TableId(type = IdType.AUTO)
private Long uid;
private String name;
private Integer age;
private String email;
}
public class User {
private Long id;
@TableField("username")
private String name;
private Integer age;
private String email;
}
实现逻辑删除
public void testDeleteById(){
int result = userMapper.deleteById(1527472864163348482L);
System.out.println(result > 0 ? "删除成功!" : "删除失败!");
System.out.println("受影响的行数为:" + result);
}
public void testSelectList(){
List<User> users = userMapper.selectList(null);
}
函数名 | 说明 | 说明/例子 |
---|---|---|
eq | 等于= | 例:eq(“name”,“zhangsan”) —> name = ‘zhangsan’ |
ne | 不等于<> | 例:ne(“name”,“zhangsan”) —> name <> ‘zhangsan’ |
gt | 大于> | 例:gt(“age”,18) —> age > 18 |
ge | 大于等于>= | 例:ge(“age”,18) —> age >= 18 |
lt | 小于< | 例:lt(“age”,18) —> age < 18 |
le | 小于等于<= | 例:le(“age”,18) —> age <= 18 |
between | between 值1 and 值2 | 例:between(“age”,10,20) —> age between 10 and 20 |
notBetween | not between 值1 and 值2 | 例:notBetween(“age”,10,20) —> age not between 10 and 20 |
like | like ‘%值%’ | 例:like(“name”,“强”) —> name like ‘%强%’ |
notLike | not like ‘%值%’ | 例:notLike(“name”,“强”) —> name not like ‘%强%’ |
likeLeft | like ‘%值’ | 例:like(“name”,“飞”) —> name like ‘%强’ |
likeRight | like ‘值%’ | 例:like(“name”,“王”) —> name like ‘王%’ |
isNull | 字段 is null | 例:isNull(“emal”) —> email is null |
isNotNull | 字段 is not null | 例:isNotNull(“emal”) —> email is not null |
in | 字段 in (值1,值2…) | 例:in(“age”,{10,18,30}) —> age in (10,18,30) |
notIn | 字段 not in (值1,值2…) | 例:notIn(“age”,{10,18,30}) —> age not in (10,18,30) |
inSql | 字段 in ( sql语句 ) | inSql(“id”, “select id from table where name like ‘%J%’”) —> id in (select id from table where name like ‘%J%’) |
notInSql | 字段 not in ( sql语句 ) | notInSql(“id”, “select id from table where name like ‘%J%’”) —> id not in (select id from table where name like ‘%J%’) |
groupBy | 分组 group by 字段,… | 例:groupBy(“id”,“name”) —> group by id,name |
orderBy | 排序 ordery by 字段,… | 例:orderBy(true,true,“id”,“name”) —> order by id asc,name asc |
orderByAsc | 升排序 order by 字段,… asc | 例:orderByAsc(“id”,“name”) —> order by id,name |
orderByDesc | 降排序 order by 字段,… desc | 例:orderByDesc(“id”,“name”) —> order by id desc,name desc |
having | having (sql语句) | having(“sum(age) > {0}”,18) —> having sum(age) > 18 |
or | 拼接or | 例:eq(“id”,1).or().eq(“name”,“老王”) —> id =1 or name = ‘老王’ |
and | and 嵌套 | 例:and(i -> i.eq(“name”,“李白”).ne(“status”,“活着”)) —> and (name = ’李白‘ and status <> ‘活着’) |
apply | 拼接sql | 例:apply(“date_format(date_time,‘%Y-%m-%d’) = {0}”,“2002-08-08”) —> date_fromat(date_time,‘%Y-%m-%d’) = ‘2008-08-08’ 注意事项:动态入参对应{index}部分,直接写sql语句,有sql注入风险 |
last | 拼接到sql的最后 | 例:last(“limit 5”) 注意事项:只能调用一次,多次调用以最后一次为准,有sql注入风险 |
exists | 拼接exists (sql语句) | 例:exists(“select id from table where age = 1”) |
not exists | 拼接not exists (sql语句) | 例:not exists(“select id from table where age = 1”) |
nested | 正常嵌套 不带and和or | 例:nested(i-> i.eq(“name”,“李华”).gt(“age”,20)) —> (name = “李华” and age > 20) |
条件的优先级
sql语句规则:and和or一起用,and优先级高
/** 组装修改条件 将(年龄大于20并且用户名中包含有a)或邮箱为null的用户信息修改 **/
@Test
public void test01() {
/*
UPDATE t_user SET user_name=?, email=?
age > ? AND user_name LIKE ? OR email IS NOT NULL
*/
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.gt("age", 20)
.like("user_name", "a")
.or()
.isNotNull("email");
User user = new User();
user.setName("小明");
user.setEmail("[email protected]");
int result = userMapper.update(user, queryWrapper);
System.out.println("result = " + result);
}
/** 条件优先级 将用户名中包含a并且(年龄大于20或邮箱为null)的用户信息修改 **/
@Test
public void test02() {
// lambda中条件优先级
/*
UPDATE t_user SET user_name=?, email=?
WHERE user_name LIKE ? AND (age > ? OR email IS NULL)
*/
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("user_name", "a")
.and(i -> i.gt("age", 20).or().isNull("email"));
User user = new User();
user.setName("小红");
user.setEmail("[email protected]");
int result = userMapper.update(user, queryWrapper);
System.out.println("result = " + result);
}
Lambda表达式
@Test
public void test3() {
/*
SELECT uid AS id,user_name AS name,age,email,is_deleted FROM t_user
WHERE user_name LIKE ? AND age <= ?
*/
String username = "a";
Integer ageBegin = null;
Integer ageEnd = 30;
//组装set子句
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
//避免使用字符串表示字段,防止运行时错误
queryWrapper.like(StringUtils.isNotBlank(username), User::getName, username)
.gt(ageBegin != null, User::getAge, ageBegin)
.le(ageEnd != null, User::getAge, ageEnd);
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
}
@Test
public void test4() {
/*
UPDATE t_user SET user_name=?,email=?
WHERE user_name LIKE ? AND (age > ? OR email IS NULL)
*/
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.like(User::getName, "a")
//lambda表达式内的逻辑优先运算
.and(i -> i.gt(User::getAge, 20).or().isNull(User::getEmail));
updateWrapper.set(User::getName, "小黑").set(User::getEmail, "[email protected]");
int result = userMapper.update(null, updateWrapper);
System.out.println("result = " + result);
}
UserMapper中定义接口方法
/**
* 根据年龄查询用户列表,分页显示
*/
Page<User> selectPageVo(@Param("page") Page<User> page,@Param("age") Integer age);
UserMapper.xml中编写SQL
<select id="selectPageVo" resultType="User">
select id,username as name,age,email from t_user where age > #{age}
select>
测试方法
@Test
public void testSelectPageVo(){ //设置分页参数
Page<User> page = new Page<>(1, 5);
userMapper.selectPageVo(page, 20);
//获取分页数据
List<User> list = page.getRecords();
list.forEach(System.out::println);
System.out.println("当前页:"+page.getCurrent());
System.out.println("每页显示的条数:"+page.getSize());
System.out.println("总记录数:"+page.getTotal());
System.out.println("总页数:"+page.getPages());
System.out.println("是否有上一页:"+page.hasPrevious());
System.out.println("是否有下一页:"+page.hasNext());
}
// 分页查询
Page<UserEntity> userPage = super.lambdaQuery().page(new Page<>(request.getCurrent(), request.getSize()));
// entity转换为vo
IPage<UserVO> infoResPage = userPage.convert(item -> Convert.convert(UserVO.class, item));
MyBaitsX可以根据我们在Mapper接口中输入的方法名快速帮我们生成对应的sql语句