网站百度快照不更新,网站域名管理中心,网站设置路由器,怎样做百度推广网站常用注解
基本约定
MybatisPlus通过扫描实体类#xff0c;并基于反射获取实体类信息作为数据库表信息。可以理解为在继承BaseMapper 要指定对应的泛型
public interface UserMapper extends BaseMapperUser 实体类中#xff0c;类名驼峰转下划线作为表名、名为id的…常用注解
基本约定
MybatisPlus通过扫描实体类并基于反射获取实体类信息作为数据库表信息。可以理解为在继承BaseMapper 要指定对应的泛型
public interface UserMapper extends BaseMapperUser 实体类中类名驼峰转下划线作为表名、名为id的字段作为主键、变量名驼峰转为下划线作为表的字段名
常见注解
TableName 用于指定表名TableId用于指定表中的主键字段信息TableField用于指定表中的普通字段信息变量名与数据库字段名不一致变量名以is开头并且是布尔类型变量名与数据库关键字冲突的
当实体类中类名和字段名不一致时可以使用上面的注解进行指定。使用时需要使用双引号单引号好像会有问题
具体用法见官方文档注解 条件构造器
mybatis-plus 支持各种复杂的where 条件可以满足日常开发的所有需求这里需要使用条件构造器。
Wrapper
更新李四的年龄为20
Service
public class UserService {Autowiredprivate UserMapper userMapper;// 查询用户public ListUser selectUser() {UpdateWrapperUser updateWrapper new UpdateWrapper();// 标签好像要用双引号updateWrapper.set(age, 20);// 条件updateWrapper.eq(name, 李四);// 更新int count userMapper.update(null, updateWrapper);// 查询更新后的数据QueryWrapperUser queryWrapper new QueryWrapper();queryWrapper.eq(name, 李四);ListUser userList userMapper.selectList(queryWrapper);return userList;}
}查询年龄大于20的男生的名称、年龄 // 查询条件QueryWrapperUser queryWrapper new QueryWrapper();// 查询年龄大于20并且性别为男的用户的姓名、年龄queryWrapper.select(name, age).gt(age, 20).eq(sex, 1);// 返回结果return userMapper.selectList(queryWrapper);男生的年龄都加1 UpdateWrapperUser updateWrapper new UpdateWrapper();// 条件updateWrapper.eq(sex, 1).setSql(age age1);// 更新userMapper.update(null, updateWrapper);// 查询更新后的数据QueryWrapperUser queryWrapper new QueryWrapper();queryWrapper.eq(sex, 1);ListUser userList userMapper.selectList(queryWrapper);return userList;或者
public ListUser selectUser() {// 查询条件QueryWrapperUser queryWrapper new QueryWrapper();queryWrapper.eq(sex, 1);// 查询数据ListUser userList userMapper.selectList(queryWrapper);for (User user : userList) {// 更新年龄user.setAge(user.getAge() 1);userMapper.update(user, queryWrapper);}return userList;
}查询id为1、2、3的用户 // 查询条件QueryWrapperUser queryWrapper new QueryWrapper();ListInteger ids List.of(1, 2, 3);queryWrapper.in(id, ids);// 查询数据ListUser userList userMapper.selectList(queryWrapper);return userList;Lambda表达式
上面的操作需要指定字段名称有时候可能会写错。这里可以使用 Lambda表达式来进行操作
查询年龄大于20的男生的名称、年龄
public ListUser selectUser() {// 查询条件LambdaQueryWrapperUser queryWrapper new LambdaQueryWrapper();queryWrapper.select(User::getName, User::getAge).gt(User::getAge, 20);// 查询数据ListUser userList userMapper.selectList(queryWrapper);return userList;
}自定义sql
我们可以利用mybatis-plus 的wrapper 来构建复杂的where条件然后自己定义sql语句剩下的部分。
基本步骤
基于Wrapper 构建where 条件
// 1、构建条件
LambdaQueryWrapperUser queryWrapper new LambdaQueryWrapper();
queryWrapper.eq(User::getId, 10);
// 调用自定义方法
userMapper.updateAgeById(queryWrapper, 1);在mapper方法参数中使用Param注解声明wrapper变量名称必须是ew
void updateAgeById(Param(ew) LambdaQueryWrapperUser wrapper , Param(age) int age);自定义sql并且使用wrapper条件 !--相当于 update user set age age #{age} where id #{id}只不过where条件是通过wrapper生成的--update idupdateAgeByIdupdate userset age age #{age} ${ew.customSqlSegment}/update这里要注意后面的条件是用的${}${}获取解析后的值#{} 是占位符