Search in sources :

Example 36 with User

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);
}
Also used : User(com.maxqiu.demo.entity.User) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 37 with User

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);
}
Also used : User(com.maxqiu.demo.entity.User) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 38 with User

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);
}
Also used : User(com.maxqiu.demo.entity.User) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 39 with User

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);
}
Also used : LambdaUpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper) User(com.maxqiu.demo.entity.User) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 40 with User

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);
}
Also used : User(com.maxqiu.demo.entity.User) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

User (com.maxqiu.demo.entity.User)61 Test (org.junit.jupiter.api.Test)55 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)55 ArrayList (java.util.ArrayList)9 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)5 BigDecimal (java.math.BigDecimal)3 Order (org.junit.jupiter.api.Order)3 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 LambdaQueryChainWrapper (com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper)2 SearchHit (org.springframework.data.elasticsearch.core.SearchHit)2 NativeSearchQuery (org.springframework.data.elasticsearch.core.query.NativeSearchQuery)2 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)2 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)1 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 LambdaUpdateChainWrapper (com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper)1 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)1 MyPage (com.maxqiu.demo.model.MyPage)1 ParamSome (com.maxqiu.demo.model.ParamSome)1 List (java.util.List)1 RowBounds (org.apache.ibatis.session.RowBounds)1