前言
之前写过 Mybatis Plus 的基本配置和使用。
QueryWrapper
为什么说不够友好,客官大姥爷们先看一个例子,假设数据库中,存在一个user表,表结构呢相对简单,如下所示:
CREATE TABLE user1
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
在使用mybatis-plus查询时,只需要编写pojo类,关联映射,如下所示:
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "user1")
public class User1 implements Serializable {
@TableId(value = "id")
private Long id;
@TableField(value = "name")
private String userName;
@TableField(value = "age")
private Integer userAge;
@TableField(value = "email")
private String userEmail;
}
再编写一个持久层的mapper接口:
import cn.linkpower.pojo.User1;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface User1Mapper extends BaseMapper<User1> {
}
就能进行查询操作,如下所示:
import cn.linkpower.mapper.User1Mapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LombokQueryTest {
@Autowired
private User1Mapper user1Mapper;
@Test
public void test(){
QueryWrapper<User1> user1QueryWrapper = new QueryWrapper<>();
user1QueryWrapper.eq("age",20);
user1Mapper.selectOne(user1QueryWrapper);
}
}
虽然很方便,但是这里需要注意一个问题:
QueryWrapper中的eq、in等操作,第一个参数往往是列名!
【问题】这里就引申出一个问题:
如果实际开发中表结构经常改变,岂不是涉及到变动的列名相关代码都需要改!!!!
针对上面问题的出现,可以考虑使用LambdaQueryWrapper解决!
LambdaQueryWrapper
那么现在就变更表的列名。
新建一个类似的表。
表结构如下所示:
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
user_name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
user_age INT(11) NULL DEFAULT NULL COMMENT '年龄',
user_email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
INSERT INTO user (id, user_name, user_age, user_email) VALUE
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
依旧还是编写pojo类,实现映射对应关系:
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "user")
public class User2 implements Serializable {
@TableId(value = "id")
private Long id;
@TableField(value = "user_name")
private String userName;
@TableField(value = "user_age")
private Integer userAge;
@TableField(value = "user_email")
private String userEmail;
}
编写一个持久层的接口:
import cn.linkpower.pojo.User2;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository
public interface User2Mapper extends BaseMapper<User2> {
}
编写测试类:
@Autowired
private User2Mapper user2Mapper;
@Test
public void test1(){
LambdaQueryWrapper<User2> queryWrapper = new LambdaQueryWrapper();
queryWrapper.eq(User2::getUserName,"Jack");
User2 users = user2Mapper.selectOne(queryWrapper);
}
测试运行:
总结
从上面的QueryWrapper和LambdaQueryWrapper,传递参数信息很容易发现:
LambdaQueryWrapper的传参支持User2::getUserName语法!
这么做的好处就是:
当表结构发生改变时,如@TableField(value = "user_name") 更改为 @TableField(value = "name")只要类的成员变量别名不变,就不会影响到程序结果的获取!