Lambda条件构造器
@Test
public void selectByWrapperLambda() {
// LambdaQueryWrapper<User> queryWrapper = new QueryWrapper<User>().lambda();
// LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
LambdaQueryWrapper<User> queryWrapper = Wrappers.<User>lambdaQuery();
queryWrapper.like(User::getName, 22).lt(User::getAge, 22);
}
List<User> list = new LambdaQueryChainWrapper<User>(userMapper)
.like(User::getName, "雨").ge(User::getAge, 20).list();
分页
@Test
public void selectPage() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
Page<User> page = new Page<>(1,2);
// IPage<User> userIPage = userMapper.selectPage(page, queryWrapper);
// customSqlSegment
IPage<User> userIPage = userMapper.selectUserPage(page, queryWrapper);
System.out.println(userIPage.getPages());
System.out.println(userIPage.getTotal());
System.out.println(userIPage.getRecords());
}
更新
@Test
public void updateByWrapper() {
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.eq("name", "小李");
User user = new User();
user.setAge(20);
Integer rows = userMapper.update(user, userUpdateWrapper);
}
@Test
public void updateByWrapper2() {
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.eq("name", "小李").set("age", 21);
Integer rows = userMapper.update(null, userUpdateWrapper);
}
@Test
public void updateByWrapperLambda() {
LambdaUpdateWrapper userUpdateWrapper = Wrappers.<User>lambdaUpdate();
userUpdateWrapper.eq(User::getAge, 1).set(User::getAge, 21);
Integer rows = userMapper.update(null, userUpdateWrapper);
}
delete
@Test
public void delete() {
userMapper.deleteById("id");
Map<String, Object> map = new HashMap<>();
map.put("name", "小李");
userMapper.deleteByMap(map);
userMapper.deleteBatchIds(Arrays.asList("1","2"));
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(User::getName, "小李");
userMapper.delete(queryWrapper);
}
主键策略
@TableId(type = IdType.AUTO)
/**
* 生成ID类型枚举类
*
* @author hubin
* @since 2015-11-10
*/
@Getter
public enum IdType {
/**
* 数据库ID自增
*/
AUTO(0),
/**
* 该类型为未设置主键类型(将跟随全局)
*/
NONE(1),
/**
* 用户输入ID
* <p>该类型可以通过自己注册自动填充插件进行填充</p>
*/
INPUT(2),
/* 以下3种类型、只有当插入对象ID 为空,才自动填充。 */
/**
* 全局唯一ID (idWorker)
*/
ID_WORKER(3),
/**
* 全局唯一ID (UUID)
*/
UUID(4),
/**
* 字符串全局唯一ID (idWorker 的字符串表示)
*/
ID_WORKER_STR(5);
private final int key;
IdType(int key) {
this.key = key;
}
}
全局配置id策略
#mybatis plus 设置
mybatis-plus:
mapper-locations: classpath*:org/jeecg/modules/**/xml/*Mapper.xml
global-config:
# 关闭MP3.0自带的banner
banner: false
db-config:
#主键类型 0:"数据库ID自增",1:"该类型为未设置主键类型", 2:"用户输入ID",3:"全局唯一ID (数字类型唯一ID)", 4:"全局唯一ID UUID",5:"字符串全局唯一ID (idWorker 的字符串表示)";
id-type: 4
# 默认数据库表下划线命名
table-underline: true
#configuration:
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
局部策略 优先级 大于 全局策略