Guns学习笔记(一) mybatis-plus
作者:互联网
官网:https://mp.baomidou.com/guide/
优点:支持lambda表达式,内置分页插件,支持多种数据库,强大的CRUD 操作损耗小无入侵 等等等等。。
pom:
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>3.3.0</version> </dependency>
-
常用注解:
@TableName("sys_user")//表名注解
@TableId(value = "id", type = IdType.AUTO) //主键注解
@TableField("name")// 字段注解(非主键)
@TableField(exist = false)// 不是数据库表字段
private String remark;
-
Service:
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
public interface UserMapper extends BaseMapper<User> {
}
-
条件查询
例子:参考:https://mp.baomidou.com/guide/wrapper.html#abstractwrapper
方法一:
QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.like("name","一").lt("age",60);// name LIKE '%一%' AND age<60 this.baseMapper.selectList(wrapper);
like ("name","hehe") 相当于 like '%hehe%'
likeRight("name","王") 相当于 like '%王'
between("age",10,15) 相当于 between 10 and 15
isNotNull("name") 相当于 name is not null
or() 相当于 or
ge("age",25) 相当于 >=;lt <;le <=;gt >;
orderByAsc("id") 相当于 order by id asc
orderByDesc("id") 相当于 order by id desc
inSql("id", "select id from table where id < 3") 相当于 in
... ...
调用数据库函数:
apply("date_format(dateColumn,'%Y-%m-%d') = '2008-08-08'")
嵌套:
// name like '张%' and (age <40 or email in not null )
and (i-> i.lt("age",40).or().isNotNull("email"))
// (age <40 or email in not null ) and name like '张%'
nested(i -> i.lt("age",40).or().isNotNull("email")).likeRight("name","王")
// age in (1,2,3)
in("age",Arrays.asList(1,2,3));
// Limit 1 如果想在最后面加一个limit 1
last(" limit 1 ")
// 不列全字段
select("id", "name", "age")
// 分组
groupBy("groupId")
方法二:实体作为条件
User user = new User(); user.setName("张三"); QueryWrapper<User> wrapper = new QueryWrapper<>(user); this.baseMapper.selectList(wrapper);
map做条件
Map<String,Object> map = new HashMap<>(); map.put("name","张三"); map.put("age",25); QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.allEq(map); this.baseMapper.selectList(wrapper);
Map<String,Object> map = new HashMap<>(); map.put("name","张三"); map.put("age",null); QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.allEq(map,false);// 没有 略View Code
lambda
LambdaQueryWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery(); lambdaQueryWrapper.like(User::getName,"张").lt(User::getDeptId,5); List<User> userList = this.baseMapper.selectList(lambdaQueryWrapper);
自定义(自己写mapper)
@Select("select * from ${ew.customSqlSegment}") List<User> selectAll(@Param(Constants.WRAPPER) Wrapper<User> wrapper);
分页
自定义:
IPage <User> selectUserPage(Page<User>page,@Param(Constants.WRAPPER) Wrapper<User> wrapper);
更新操作:
UpdateWrapper<User> uw= new UpdateWrapper<>(); uw.eq("name","张三").eq("age",25); User user = new User(); user.setEmail("xx123@xx.com"); int rows= this.baseMapper.update(user,uw);
UpdateWrapper<User> uw= new UpdateWrapper<>(); uw.eq("name","张三").eq("age",25).set("age",26); int rows= this.baseMapper.update(null,uw);
删除操作
id删除:
int deleteById(Serializable id);
条件删除:
int deleteByMap(@Param("cm") Map<String, Object> columnMap);
Map<String,Object> map = new HashMap<>(); map.put("name","张三"); map.put("age",25); this.baseMapper.deleteByMap(map);View Code
int delete(@Param("ew") Wrapper<T> wrapper);
LambdaQueryWrapper<User> lambdaQuery =Wrappers.lambdaQuery(); lambdaQuery.eq(User::getName,"张三").or().gt(User::getDeptId,5); this.baseMapper.delete(lambdaQuery);View Code
批量删除:
int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);
this.baseMapper.deleteBatchIds(Arrays.asList(1L,2L));View Code
标签:map,name,age,wrapper,id,plus,mybatis,new,Guns 来源: https://www.cnblogs.com/DarGi2019/p/12187125.html