MyBatisPlus学习二:常用注解、条件构造器、自定义sql

常用注解

基本约定

MybatisPlus通过扫描实体类,并基于反射获取实体类信息作为数据库表信息。可以理解为在继承BaseMapper 要指定对应的泛型

public interface UserMapper extends BaseMapper<User> 

实体类中,类名驼峰转下划线作为表名、名为id的字段作为主键、变量名驼峰转为下划线作为表的字段名

常见注解

  • @TableName :用于指定表名
  • @TableId:用于指定表中的主键字段信息
  • @TableField:用于指定表中的普通字段信息(变量名与数据库字段名不一致;变量名以is开头,并且是布尔类型;变量名与数据库关键字冲突的)

当实体类中类名和字段名不一致时可以使用上面的注解进行指定。使用时需要使用双引号,单引号好像会有问题

具体用法见官方文档:注解

在这里插入图片描述

条件构造器

mybatis-plus 支持各种复杂的where 条件,可以满足日常开发的所有需求,这里需要使用条件构造器。

Wrapper

更新李四的年龄为20

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    // 查询用户
    public List<User> selectUser() {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        // 标签好像要用双引号
        updateWrapper.set("age", 20);
        // 条件
        updateWrapper.eq("name", "李四");
        // 更新
        int count = userMapper.update(null, updateWrapper);
        // 查询更新后的数据
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", "李四");
        List<User> userList = userMapper.selectList(queryWrapper);
        return userList;
    }
}

查询年龄大于20的男生的名称、年龄

 // 查询条件
 QueryWrapper<User> queryWrapper = new QueryWrapper<>();
 // 查询年龄大于20并且性别为男的用户的姓名、年龄
 queryWrapper
         .select("name", "age")
         .gt("age", 20)
         .eq("sex", 1);
 // 返回结果
 return userMapper.selectList(queryWrapper);

在这里插入图片描述

男生的年龄都加1

 UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
 // 条件
 updateWrapper.eq("sex", 1)
         .setSql("age = age+1");
 // 更新
 userMapper.update(null, updateWrapper);
 // 查询更新后的数据
 QueryWrapper<User> queryWrapper = new QueryWrapper<>();
 queryWrapper.eq("sex", 1);
 List<User> userList = userMapper.selectList(queryWrapper);
 return userList;

或者

public List<User> selectUser() {
    // 查询条件
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("sex", 1);
    // 查询数据
    List<User> userList = userMapper.selectList(queryWrapper);
    for (User user : userList) {
        // 更新年龄
        user.setAge(user.getAge() + 1);
        userMapper.update(user, queryWrapper);
    }

    return userList;
}

查询id为1、2、3的用户

 // 查询条件
 QueryWrapper<User> queryWrapper = new QueryWrapper<>();
 List<Integer> ids = List.of(1, 2, 3);
 queryWrapper.in("id", ids);
 // 查询数据
 List<User> userList = userMapper.selectList(queryWrapper);
 return userList;

在这里插入图片描述

Lambda表达式

上面的操作需要指定字段名称,有时候可能会写错。这里可以使用 Lambda表达式来进行操作

查询年龄大于20的男生的名称、年龄

public List<User> selectUser() {
    // 查询条件
    LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper
            .select(User::getName, User::getAge)
            .gt(User::getAge, 20);

    // 查询数据
    List<User> userList = userMapper.selectList(queryWrapper);
    return userList;
}

自定义sql

我们可以利用mybatis-pluswrapper 来构建复杂的where条件,然后自己定义sql语句剩下的部分。

基本步骤

  • 基于Wrapper 构建where 条件
// 1、构建条件
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getId, 10);
// 调用自定义方法
userMapper.updateAgeById(queryWrapper, 1);
  • mapper方法参数中使用Param注解声明wrapper变量名称,必须是ew
void updateAgeById(@Param("ew") LambdaQueryWrapper<User> wrapper , @Param("age") int age);
  • 自定义sql,并且使用wrapper条件
 <!--
  相当于 update user set age = age + #{age} where id = #{id}
  只不过where条件是通过wrapper生成的
 -->
 <update id="updateAgeById">
     update user
        set age = age + #{age} ${ew.customSqlSegment}
 </update>

这里要注意,后面的条件是用的${}${}获取解析后的值,#{} 是占位符

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Mybatisplus 自定义sql 使用条件构造器可以在自定义 SQL 语句中使用 Mybatisplus 的条件构造器,方便快捷地构造查询条件。 使用步骤: 1. 在 Mapper 接口中定义自定义 SQL 语句的方法,方法返回值为 List 或者其他需要返回的结果类型。 2. 在自定义 SQL 语句中使用 ${} 占位符来引用条件构造器生成的 SQL 片段。 3. 在方法参数中使用 @Param 注解来指定条件构造器生成的 SQL 片段的参数名称和类型,同时在自定义 SQL 语句中使用 #{参数名} 占位符来引用参数。 4. 在方法中使用 QueryWrapper 类来构造查询条件,然后将 QueryWrapper 对象作为参数传递给自定义 SQL 语句方法即可。 示例代码如下: ``` @Mapper public interface UserMapper extends BaseMapper<User> { @Select("SELECT * FROM user ${ew.customSqlSegment}") List<User> selectByCustomSql(@Param(Constants.WRAPPER) QueryWrapper<User> wrapper); } ``` 其中,${ew.customSqlSegment} 是 Mybatisplus 条件构造器生成的 SQL 片段,@Param(Constants.WRAPPER) 指定了 wrapper 参数的名称和类型。 调用示例: ``` QueryWrapper<User> wrapper = new QueryWrapper<>(); wrapper.eq("age", 18).like("name", "张"); List<User> userList = userMapper.selectByCustomSql(wrapper); ``` 以上代码中,使用 QueryWrapper 构造了查询条件,然后将 QueryWrapper 对象作为参数传递给 selectByCustomSql 方法,该方法会根据传入的 QueryWrapper 对象生成自定义 SQL 语句,并返回符合条件的用户列表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无知的小菜鸡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值