use of com.maxqiu.demo.entity.User in project demo-SpringBoot by Max-Qiu.
the class TestUserService method getById.
/**
* 根据ID获取
*/
@Test
void getById() {
// SELECT id,username,age,email FROM smp_user WHERE id=?
User one = userService.getById(1);
System.out.println(one);
}
use of com.maxqiu.demo.entity.User in project demo-SpringBoot by Max-Qiu.
the class TestUserService method saveOrUpdateBatch.
/**
* 批量保存或更新
*
* 有ID和没ID会自动分段
*
* 如果没有ID,则直接保存
*
* 如果有ID,则会先检查,再决定执行 INSERT 或 UPDATE
*/
@Test
void saveOrUpdateBatch() {
List<User> userList = new ArrayList<>();
// INSERT INTO smp_user ( id, username, age ) VALUES ( ?, ?, ? )
userList.add(new User().setUsername("max").setAge(12));
userList.add(new User().setUsername("Vicky").setAge(10));
// SELECT id,username,age,email FROM smp_user WHERE id=?
// INSERT INTO smp_user ( id, username, age ) VALUES ( ?, ?, ? )
userList.add(new User().setId(5L).setUsername("Tom").setAge(13));
// SELECT id,username,age,email FROM smp_user WHERE id=?
// UPDATE smp_user SET username=?, age=? WHERE id=?
userList.add(new User().setId(5L).setUsername("Jerry").setAge(16));
// INSERT INTO smp_user ( id, username, age ) VALUES ( ?, ?, ? )
userList.add(new User().setUsername("张三").setAge(16));
userService.saveOrUpdateBatch(userList);
}
use of com.maxqiu.demo.entity.User in project demo-SpringBoot by Max-Qiu.
the class TestUserService method getOne1.
/**
* 条件查询获取第一个,并设置是否抛出异常
*/
@Test
void getOne1() {
// SELECT id,name,age,email,pid,createtime FROM user WHERE (age > ?)
// false 查出多个时,仅返回第一个,并日志记录警告
User one = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getAge, 24), false);
System.out.println(one);
}
use of com.maxqiu.demo.entity.User in project demo-SpringBoot by Max-Qiu.
the class TestUserService method saveOrUpdateWithWrapper.
/**
* 更新或保存,带条件查询
*/
@Test
void saveOrUpdateWithWrapper() {
// update(entity, updateWrapper) || saveOrUpdate(entity);
// 先执行 update ,根据update结果执行 saveOrUpdate
// UPDATE smp_user SET age=? WHERE (age = ?)
// SELECT id,username,age,email FROM smp_user WHERE id=?
// INSERT INTO smp_user ( id, age ) VALUES ( ?, ? )
boolean save1 = userService.saveOrUpdate(new User().setId(2L).setAge(11), new LambdaUpdateWrapper<User>().eq(User::getAge, 18));
System.out.println(save1);
// UPDATE smp_user SET age=? WHERE (age = ?)
boolean save2 = userService.saveOrUpdate(new User().setId(2L).setAge(18), new LambdaUpdateWrapper<User>().eq(User::getAge, 11));
System.out.println(save2);
// UPDATE smp_user SET username=?, age=? WHERE (age = ?)
// SELECT id,username,age,email FROM smp_user WHERE id=?
// UPDATE smp_user SET age=? WHERE id=?
boolean save3 = userService.saveOrUpdate(new User().setId(2L).setAge(18), new LambdaUpdateWrapper<User>().eq(User::getAge, 1));
System.out.println(save3);
}
use of com.maxqiu.demo.entity.User in project demo-SpringBoot by Max-Qiu.
the class TestUserService method save.
/**
* 单条对象保存
*
* 如果设置ID,则存在相同ID时抛出异常
*/
@Test
void save() {
// INSERT INTO smp_user ( id, username, age ) VALUES ( ?, ?, ? )
boolean save1 = userService.save(new User().setUsername("Amy").setAge(11));
System.out.println(save1);
}
Aggregations