目录
实体类
@TableId
主键专属
@TableId(value = "typeId",type = IdType.AUTO)
private Long typeId;
值 | 描述 | 注意 |
AUTO | 数据库自增 | |
NONE | MP set主键,雪花算法实现 | 注意生成的长度否则会报错 |
INPUT | 需要开发者手动赋值 | 没有对id进行赋值,数据库一般会采取自增id的方式处理 |
ASSIGN_ID | ||
ASSIGN_UUID | 要求主键类型必须是String类型,会自动生成UUID进行赋值 |
@TableField
非主键字段使用
public class User{
private Integer id;
@TableField(value = "name")
private String UserName;
@TableField(exist = false)
private Integer gender;
@Version
private Integer version;
}
value:映射非主键字段 value映射字段名
exist:表示是否为数据库字段,false表示非数据库字段,查询时不会报错
select 表示是否查询该字段,false表示不查询该字段,返回null
@Version
标记乐观锁 -主修改操作
一、LambdaQueryWrapper
https://blog.csdn.net/weixin_43841924/article/details/125736206
1、servie 层和实现类要集成mybatisPlus的
service 继承IService<>
public interface OpLogService extends IService<OpLog> {
实现类中要继承IService的实现类ServiceImpl<mapper,实体类>
@Service
public class OpLogServiceImpl extends ServiceImpl<OpLogMapper, OpLog> implements OpLogService {
2、如果你想要mapper中的一些方法的话,mapper 要继承BaseMapper<实体类>
public interface OpLogMapper extends BaseMapper<OpLog> {
3、常用的方法
LambdaQueryWrapper (方便)
1)、分页 Page
/*参数1是分页起始位置,一般是1,参数2:结束位置,一般都是前端传过来的,查询的数量*/
Page<LandLossAllowance> page1 = new Page<LandLossAllowance>(参数1,参数2);
LambdaQueryWrapper<LandLossAllowance> queryWrapper = new LambdaQueryWrapper<>();
/*
*条件 eq 指的是相等
*/
queryWrapper.eq(实体类::属性,值);
....
/*执行page方法 page(参数1,参数2)
* 参数1:上面的new的分页对象,参数2是lambda表达式查询条件*/
IPage<LandLossAllowance> iPage = page(page1, queryWrapper);
2)、常用的lambda表达式的一些定义
QueryWrapper
select(String… sqlSelect); // 用于定义需要返回的字段。
例: select(“id”, “name”, “age”) —> select id, name, age
select(Predicate predicate); // Lambda 表达式,过滤需要的字段。
lambda(); // 返回一个 LambdaQueryWrapper
private List<HwSecuritiesInfo> query(QueryParam param) {
QueryWrapper<HwSecuritiesInfo> wrapper = new QueryWrapper<>();
wrapper.lambda().gt(HwSecuritiesInfo::getId, param.getJsid())
.orderByAsc(HwSecuritiesInfo::getId);
wrapper.lambda().select(HwSecuritiesInfo::getId, HwSecuritiesInfo::getBusinessId, HwSecuritiesInfo::getCompanyCode);
wrapper.last("limit " + param.getCount());
return hwSecuritiesInfoService.list(wrapper);
UpdateWrapper
set(String column, Object val); // 用于设置 set 字段值。
例: set(“name”, null) —> set name = null
etSql(String sql); // 用于设置 set 字段值。
例: setSql(“name = ‘老公’”) —> set name = ‘老公’
lambda(); // 返回一个 LambdaUpdateWrapper
public List<GelonghuiNews> selectGelonghuiNews(Long lastId) {
QueryWrapper<GelonghuiNews> wrapper = new QueryWrapper<>();
if (null == lastId) {
wrapper.lambda().gt(GelonghuiNews::getJsid, lastId).eq(GelonghuiNews::getImageStatus,OssImageStatus.YES.getStatus());
return super.baseMapper.selectList(wrapper);
二、BaseMapper
public interface OpLogMapper extends BaseMapper<OpLog> {
@Insert("insert into dedmo_user(id,name) values(null,#{name})")
void insertUser(User user);
@update("update dedmo_user set name = #{newName} where name = #{oldName}")
void updateUser(String newName,String oldName);
}
增删查改操作
1、查询操作
1 selectById
查询id为1的用户信息
BaseMapper.selectById(1)
2 selectList
查询全部的用户信息
BaseMapper.selectList(null)
判断queryWrapper条件是否为空
QueryWrapper queryWrapper=new QueryWrapper();
queryWrapper.eq("name",adminName);
List<Student> list = studentMapper.selectList(queryWrapper);
3 selectBatchIds
查询id为1和3的用户信息
BaseMapper.selectBatchIds(Arrays.asList(1,3));
4 selectByMap
需求:查询name为yixin,并且年龄为18岁的用户信息。
HashMap<String ,Object> map=new HashMap<>();
map.put("name","yixin");
map.put("age",18);
BaseMapper.selectByMap(map);
5 selectOne
结合queryMapper
LambdaQueryWrapper queryWrapper=new LambdaQueryWrapper();
queryWrapper.eq("name",adminName);
studentMapper.selectOne(queryWrapper);
2、增加操作
insert
需求:插入用户的信息如下
Person person =new Person();
person.setName("张三");
person.setAge(21);
person.setEmail("test6@qq.com");
BaseMapper.insert(person);
3、 删除操作
deleteById
需求:删除id为5的用户信息。
BaseMapper.deleteById(5);
deleteByMap
需求:删除姓名为 Sandy,并且年龄为21的用户信息
HashMap<String, Object> map=new HashMap<>();
map.put("name","Sandy");
map.put("age",21);
BaseMapper..deleteByMap(map);
4、更新操作
updateById
需求:将id为2的用户的姓名更改为"一心同学"
Person person =new Person();
person.setId(2L);
person.setName("一心同学");
person.setAge(20);
person.setEmail("test2@qq.com");
BaseMapper.updateById(person);