查询数据:
示例,通过构建实体,查询,有点类似于mybatis-generate的Example查询
@Test
public void testCondition2() {
User user = new User();
user.setName("梅长苏");
user.setAge(30);
LambdaQueryWrapper<User> userQueryWrapper = Wrappers.<User>lambdaQuery(user);
List<User> users = userMapper.selectList(userQueryWrapper);
System.out.println(users);
//[User(id=1, name=梅长苏, age=30, email=mei@qq.com, parentId=null, createTime=Fri Jan 11 14:20:20 CST 2019)]
//查询结果并不会填充user
System.out.println(user);
//User(id=null, name=梅长苏, age=30, email=null, parentId=null, createTime=null)
}
但是要注意,使用了实体查询之后,在使用.eq等方法,会导致sql重复
@Test
public void testCondition2() {
//这是错误示例
User user = new User();
user.setName("梅长苏");
user.setAge(30);
LambdaQueryWrapper<User> userQueryWrapper = Wrappers.<User>lambdaQuery(user);
userQueryWrapper.eq(User::getName,"梅长苏").eq(User::getAge,30);
List<User> users = userMapper.selectList(userQueryWrapper);
}
正常:SELECT id,name,age,email,parent_id,create_time FROM user WHERE name=? AND age=?
重复:SELECT id,name,age,email,parent_id,create_time FROM user WHERE name=? AND age=? AND (name = ? AND age = ?)
附上打印出mybatis-plus的sql的配置
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl