MyBatis-Plus的基本CRUD操作大全
一、BaseMapper
MyBatis-Plus中的基本CRUD在內(nèi)置的BaseMapper中都已得到了實(shí)現(xiàn),我們可以直接使用,接口如下
public interface BaseMapper<T> extends Mapper<T> {
int insert(T entity);
int deleteById(Serializable id);
int deleteById(T entity);
int deleteByMap(@Param("cm") Map<String, Object> columnMap);
int delete(@Param("ew") Wrapper<T> queryWrapper);
int deleteBatchIds(@Param("coll") Collection<?> idList);
int updateById(@Param("et") T entity);
int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
T selectById(Serializable id);
List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
default T selectOne(@Param("ew") Wrapper<T> queryWrapper) {
List<T> ts = this.selectList(queryWrapper);
if (CollectionUtils.isNotEmpty(ts)) {
if (ts.size() != 1) {
throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records", new Object[0]);
} else {
return ts.get(0);
}
} else {
return null;
}
}
default boolean exists(Wrapper<T> queryWrapper) {
Long count = this.selectCount(queryWrapper);
return null != count && count > 0L;
}
Long selectCount(@Param("ew") Wrapper<T> queryWrapper);
List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
<P extends IPage<T>> P selectPage(P page, @Param("ew") Wrapper<T> queryWrapper);
<P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param("ew") Wrapper<T> queryWrapper);
}1. 插入
注意:MyBatis-Plus在實(shí)現(xiàn)插入數(shù)據(jù)時(shí),會(huì)默認(rèn)基于雪花算法的策略生成id
/**
* 用戶插入功能的方法。
* 該方法創(chuàng)建一個(gè)User對(duì)象并將其插入數(shù)據(jù)庫(kù),驗(yàn)證插入操作是否成功,
* 并輸出受影響的行數(shù)以及自動(dòng)生成的用戶ID。
*/
@Test
public void testInsert(){
// 創(chuàng)建一個(gè)新的User對(duì)象,ID為null表示由數(shù)據(jù)庫(kù)自動(dòng)生成
User user = new User(null, "張三", 23, "zhangsan@qcby.com",null,null);
// 執(zhí)行插入操作,將用戶數(shù)據(jù)保存到數(shù)據(jù)庫(kù)中
//INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
int result = userMapper.insert(user);
// 輸出插入操作影響的行數(shù),正常情況下應(yīng)為1
System.out.println("受影響行數(shù):"+result);
// 輸出數(shù)據(jù)庫(kù)自動(dòng)生成的用戶ID,MyBatis-Plus在實(shí)現(xiàn)插入數(shù)據(jù)時(shí),會(huì)默認(rèn)基于雪花算法的策略生成id
//2020478658078167042
System.out.println("id自動(dòng)獲?。?+user.getId());
}2. 刪除
/**
* 通過ID刪除用戶信息的功能。
* 該方法調(diào)用userMapper的deleteById方法,傳入指定的用戶ID進(jìn)行刪除操作,
* 并輸出受影響的行數(shù)以驗(yàn)證刪除結(jié)果。
*/
@Test
public void testDeleteById(){
//通過id刪除用戶信息
//DELETE FROM user WHERE id=?
int result = userMapper.deleteById(2020488576042545170L);
System.out.println("受影響行數(shù):"+result);
}
@Test
public void testDeleteBatchIds(){
//通過多個(gè)id批量刪除
//DELETE FROM user WHERE id IN ( ? , ? )
List<Long> idList = Arrays.asList(1L, 3L);
int result = userMapper.deleteBatchIds(idList);
System.out.println("受影響行數(shù):"+result);
}
/**
* 通過Map條件刪除用戶記錄的功能。
* 該方法構(gòu)造一個(gè)包含刪除條件的Map對(duì)象,并調(diào)用userMapper的deleteByMap方法執(zhí)行刪除操作,
* 最后輸出受影響的行數(shù)。
* @param map 包含刪除條件的鍵值對(duì)集合,其中key為字段名,value為對(duì)應(yīng)的條件值。。
* @return 受影響的行數(shù),表示成功刪除的記錄數(shù)量。
*/
@Test
public void testDeleteByMap(){
//根據(jù)map集合中所設(shè)置的條件刪除記錄
// 構(gòu)造刪除條件的Map集合
//DELETE FROM user WHERE name = ? AND age = ?
Map<String, Object> map = new HashMap<>();
map.put("age", 23);
map.put("name", "張三");
int result = userMapper.deleteByMap(map);
System.out.println("受影響行數(shù):"+result);
}3. 修改
/**
* 通過ID更新用戶信息的功能。
* 該方法創(chuàng)建一個(gè)User對(duì)象,并調(diào)用userMapper的updateById方法來(lái)更新數(shù)據(jù)庫(kù)中的對(duì)應(yīng)記錄。
* 最后輸出受影響的行數(shù)。
*/
@Test
public void testUpdateById(){
//創(chuàng)建User對(duì)象,并設(shè)置要修改的屬性,null不傳參
User user = new User(4L, "test", null, null, null, null);
//UPDATE user SET name=?, age=? WHERE id=?
int result = userMapper.updateById(user);
System.out.println("受影響行數(shù):"+result);
}4. 查詢
/**
* 通過ID查詢用戶信息的功能。
* 該方法調(diào)用userMapper的selectById方法,傳入指定的用戶ID進(jìn)行查詢,
* 并將查詢結(jié)果打印到控制臺(tái)。
*/
@Test
public void testSelectById(){
//根據(jù)id查詢用戶信息
//SELECT id,name,age,email FROM user WHERE id=?
User user = userMapper.selectById(4L);
System.out.println(user);
}
/**
* 通過多個(gè)ID批量查詢用戶信息的功能。
* 該方法調(diào)用userMapper的selectBatchIds方法,傳入指定的用戶ID列表進(jìn)行批量查詢,
* 并將查詢結(jié)果打印到控制臺(tái)。
*/
@Test
public void testSelectBatchIds(){
//根據(jù)id批量查詢
//SELECT id,name,age,email FROM user WHERE id IN ( ? , ? )
List<User> list = userMapper.selectBatchIds(Arrays.asList(4, 2));
list.forEach(System.out::println);
}
/**
* 通過Map條件查詢用戶信息列表的功能。
* 該方法構(gòu)造一個(gè)包含查詢條件的Map對(duì)象,并調(diào)用userMapper的selectByMap方法執(zhí)行查詢操作,
* 最后將查詢結(jié)果打印到控制臺(tái)。
*/
@Test
public void testSelectByMap(){
//根據(jù)map查詢用戶信息,此時(shí)null傳參
//SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
Map<String, Object> map = new HashMap<>();
map.put("name", "Jack");
map.put("age", 20);
List<User> list = userMapper.selectByMap(map);
list.forEach(System.out::println);
}
/**
* 通過條件查詢用戶信息列表的功能。
* 該方法創(chuàng)建一個(gè)User對(duì)象,并調(diào)用userMapper的selectOne方法來(lái)查詢數(shù)據(jù)庫(kù)中的對(duì)應(yīng)記錄。
* 最后將查詢結(jié)果打印到控制臺(tái)。
*/
@Test
public void testSelectOne(){
//查詢用戶信息,根據(jù)條件查詢,最多只能查詢一條
//SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
User user = new User();
user.setName("Jack");
user.setAge(20);
User one = userMapper.selectOne(new QueryWrapper<>(user));
System.out.println(one);
}
/**
* 查詢所有數(shù)據(jù)selectList()功能。
* 該方法通過調(diào)用userMapper的selectList()方法查詢數(shù)據(jù),
* 并將查詢結(jié)果打印到控制臺(tái)。
* selectList()方法根據(jù)MyBatis-Plus內(nèi)置的條件構(gòu)造器查詢一個(gè)list集合。
* 傳入null表示沒有查詢條件,即查詢所有數(shù)據(jù)。
*/
@Test
public void testSelectList(){
//selectList()根據(jù)MP內(nèi)置的條件構(gòu)造器查詢一個(gè)list集合,null表示沒有條件,即查詢所有
userMapper.selectList( null).forEach(System.out::println);
}5. 其他重要接口
// selectPage(分頁(yè)查詢 + 復(fù)雜條件)
@Test
public void testSelectPage() {
// 分頁(yè)參數(shù):第2頁(yè),每頁(yè)3條
Page<User> page = new Page<>(2, 3);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.gt("age", 18) // 年齡>18
.like("email", "@test.com") // 郵箱包含@test.com
.orderByDesc("age"); // 按年齡降序
IPage<User> userIPage = userMapper.selectPage(page, queryWrapper);
System.out.println("selectPage-總條數(shù):" + userIPage.getTotal());
System.out.println("selectPage-當(dāng)前頁(yè)數(shù)據(jù):");
userIPage.getRecords().forEach(System.out::println);
}
// selectMapsPage(分頁(yè)查詢,返回Map集合,僅查指定字段)
@Test
public void testSelectMapsPage() {
Page<Map<String, Object>> page = new Page<>(1, 5);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("uid", "name", "age") // 只查3個(gè)字段
.between("age", 18, 30); // 年齡在18-30之間
IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(page, queryWrapper);
System.out.println("selectMapsPage-總頁(yè)數(shù):" + mapIPage.getPages());
System.out.println("selectMapsPage-當(dāng)前頁(yè)Map數(shù)據(jù):");
mapIPage.getRecords().forEach(System.out::println);
}
// selectObjs(僅查詢指定字段,返回Object集合,適合單字段查詢)
@Test
public void testSelectObjs() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("name") // 只查name字段
.eq("age", 20);
List<Object> nameList = userMapper.selectObjs(queryWrapper);
System.out.println("selectObjs-僅查詢name字段結(jié)果:");
nameList.forEach(System.out::println);
}
// update(UpdateWrapper條件更新,非ID更新)
@Test
public void testUpdateWithWrapper() {
User user = new User();
user.setEmail("update_wrapper@test.com"); // 要更新的字段
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("name", "張三")
.gt("age", 18); // 條件:name=張三 且 age>18
int rows = userMapper.update(user, updateWrapper);
System.out.println("testUpdateWithWrapper-影響行數(shù):" + rows);
}
// update(鏈?zhǔn)経pdateWrapper,直接設(shè)置更新字段,無(wú)需new User)
@Test
public void testUpdateForSet() {
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.eq("uid", 1L)
.set("age", 28) // 直接設(shè)置要更新的字段和值
.set("email", "update_set@test.com");
int rows = userMapper.update(null, updateWrapper);
System.out.println("testUpdateForSet-影響行數(shù):" + rows);
}
// delete(QueryWrapper條件刪除,支持復(fù)雜條件)
@Test
public void testDeleteWithWrapper() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name", "測(cè)試刪除")
.lt("age", 18); // 條件:name含測(cè)試刪除 且 age<18
int rows = userMapper.delete(queryWrapper);
System.out.println("testDeleteWithWrapper-影響行數(shù):" + rows);
}二、IServce
MyBatis-Plus中有一個(gè)接口 IService和其實(shí)現(xiàn)類 ServiceImpl,封裝了常見的業(yè)務(wù)層邏輯。采用get查詢單行、remove刪除、list查詢集合、page分頁(yè)前綴命名方式,區(qū)分mapper層,避免混淆。接口如下
public interface IService<T> {
int DEFAULT_BATCH_SIZE = 1000;
default boolean save(T entity) {
return SqlHelper.retBool(this.getBaseMapper().insert(entity));
}
@Transactional(
rollbackFor = {Exception.class}
)
default boolean saveBatch(Collection<T> entityList) {
return this.saveBatch(entityList, 1000);
}
boolean saveBatch(Collection<T> entityList, int batchSize);
@Transactional(
rollbackFor = {Exception.class}
)
default boolean saveOrUpdateBatch(Collection<T> entityList) {
return this.saveOrUpdateBatch(entityList, 1000);
}
boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
default boolean removeById(Serializable id) {
return SqlHelper.retBool(this.getBaseMapper().deleteById(id));
}
default boolean removeById(Serializable id, boolean useFill) {
throw new UnsupportedOperationException("不支持的方法!");
}
default boolean removeById(T entity) {
return SqlHelper.retBool(this.getBaseMapper().deleteById(entity));
}
default boolean removeByMap(Map<String, Object> columnMap) {
Assert.notEmpty(columnMap, "error: columnMap must not be empty", new Object[0]);
return SqlHelper.retBool(this.getBaseMapper().deleteByMap(columnMap));
}
default boolean remove(Wrapper<T> queryWrapper) {
return SqlHelper.retBool(this.getBaseMapper().delete(queryWrapper));
}
default boolean removeByIds(Collection<?> list) {
return CollectionUtils.isEmpty(list) ? false : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(list));
}
@Transactional(
rollbackFor = {Exception.class}
)
default boolean removeByIds(Collection<?> list, boolean useFill) {
if (CollectionUtils.isEmpty(list)) {
return false;
} else {
return useFill ? this.removeBatchByIds(list, true) : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(list));
}
}
@Transactional(
rollbackFor = {Exception.class}
)
default boolean removeBatchByIds(Collection<?> list) {
return this.removeBatchByIds(list, 1000);
}
@Transactional(
rollbackFor = {Exception.class}
)
default boolean removeBatchByIds(Collection<?> list, boolean useFill) {
return this.removeBatchByIds(list, 1000, useFill);
}
default boolean removeBatchByIds(Collection<?> list, int batchSize) {
throw new UnsupportedOperationException("不支持的方法!");
}
default boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
throw new UnsupportedOperationException("不支持的方法!");
}
default boolean updateById(T entity) {
return SqlHelper.retBool(this.getBaseMapper().updateById(entity));
}
default boolean update(Wrapper<T> updateWrapper) {
return this.update((Object)null, updateWrapper);
}
default boolean update(T entity, Wrapper<T> updateWrapper) {
return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
}
@Transactional(
rollbackFor = {Exception.class}
)
default boolean updateBatchById(Collection<T> entityList) {
return this.updateBatchById(entityList, 1000);
}
boolean updateBatchById(Collection<T> entityList, int batchSize);
boolean saveOrUpdate(T entity);
default T getById(Serializable id) {
return this.getBaseMapper().selectById(id);
}
default List<T> listByIds(Collection<? extends Serializable> idList) {
return this.getBaseMapper().selectBatchIds(idList);
}
default List<T> listByMap(Map<String, Object> columnMap) {
return this.getBaseMapper().selectByMap(columnMap);
}
default T getOne(Wrapper<T> queryWrapper) {
return this.getOne(queryWrapper, true);
}
T getOne(Wrapper<T> queryWrapper, boolean throwEx);
Map<String, Object> getMap(Wrapper<T> queryWrapper);
<V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
default long count() {
return this.count(Wrappers.emptyWrapper());
}
default long count(Wrapper<T> queryWrapper) {
return SqlHelper.retCount(this.getBaseMapper().selectCount(queryWrapper));
}
default List<T> list(Wrapper<T> queryWrapper) {
return this.getBaseMapper().selectList(queryWrapper);
}
default List<T> list() {
return this.list(Wrappers.emptyWrapper());
}
default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
return this.getBaseMapper().selectPage(page, queryWrapper);
}
default <E extends IPage<T>> E page(E page) {
return this.page(page, Wrappers.emptyWrapper());
}
default List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
return this.getBaseMapper().selectMaps(queryWrapper);
}
default List<Map<String, Object>> listMaps() {
return this.listMaps(Wrappers.emptyWrapper());
}
default List<Object> listObjs() {
return this.listObjs(Function.identity());
}
default <V> List<V> listObjs(Function<? super Object, V> mapper) {
return this.listObjs(Wrappers.emptyWrapper(), mapper);
}
default List<Object> listObjs(Wrapper<T> queryWrapper) {
return this.listObjs(queryWrapper, Function.identity());
}
default <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
return (List)this.getBaseMapper().selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
}
default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper) {
return this.getBaseMapper().selectMapsPage(page, queryWrapper);
}
default <E extends IPage<Map<String, Object>>> E pageMaps(E page) {
return this.pageMaps(page, Wrappers.emptyWrapper());
}
BaseMapper<T> getBaseMapper();
Class<T> getEntityClass();
default QueryChainWrapper<T> query() {
return ChainWrappers.queryChain(this.getBaseMapper());
}
default LambdaQueryChainWrapper<T> lambdaQuery() {
return ChainWrappers.lambdaQueryChain(this.getBaseMapper());
}
default KtQueryChainWrapper<T> ktQuery() {
return ChainWrappers.ktQueryChain(this.getBaseMapper(), this.getEntityClass());
}
default KtUpdateChainWrapper<T> ktUpdate() {
return ChainWrappers.ktUpdateChain(this.getBaseMapper(), this.getEntityClass());
}
default UpdateChainWrapper<T> update() {
return ChainWrappers.updateChain(this.getBaseMapper());
}
default LambdaUpdateChainWrapper<T> lambdaUpdate() {
return ChainWrappers.lambdaUpdateChain(this.getBaseMapper());
}
default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
return this.update(entity, updateWrapper) || this.saveOrUpdate(entity);
}
}1. 創(chuàng)建Service接口和實(shí)現(xiàn)類
/**
* UserService繼承IService模板提供的基礎(chǔ)功能
*/
public interface UserService extends IService<User> {}/**
* ServiceImpl實(shí)現(xiàn)了IService,提供了IService中基礎(chǔ)功能的實(shí)現(xiàn)
* 若ServiceImpl無(wú)法滿足業(yè)務(wù)需求,則可以使用自定的UserService定義方法,并在實(shí)現(xiàn)類中實(shí)現(xiàn) */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {}2. 測(cè)試查詢記錄數(shù)
@Autowired
private UserService userService;
@Test
public void testGetCount(){
long count = userService.count();
System.out.println("總記錄數(shù):" + count);
}3. 測(cè)試批量插入
/**
* 批量插入
*/
@Test
public void testSaveBatch(){
// SQL長(zhǎng)度有限制,海量數(shù)據(jù)插入單條SQL無(wú)法實(shí)行,
// 因此MP將批量插入放在了通用Service中實(shí)現(xiàn),而不是通用Mapper
ArrayList<User> users = new ArrayList<>();
// 未傳參的字段默認(rèn)為null,但是id雪花自增
for (int i = 0; i < 5; i++) {
User user = new User();
// 字符串拼接
user.setName("kk" + i);
// 加法運(yùn)算
user.setAge(20 + i);
users.add(user);
}
//SQL:INSERT INTO user ( username, age ) VALUES ( ?, ? )
userService.saveBatch(users);
}4. 其他常用接口
// 測(cè)試saveOrUpdate(新增或更新:有ID則更新,無(wú)則新增)
@Test
public void testSaveOrUpdate() {
// 場(chǎng)景1:無(wú)ID → 新增
User user1 = new User();
user1.setName("新增或更新1");
user1.setAge(23);
user1.setEmail("saveOrUpdate1@test.com");
userService.saveOrUpdate(user1);
// 場(chǎng)景2:有ID → 更新
User user2 = new User();
user2.setId(1L);
user2.setName("新增或更新2");
user2.setAge(24);
userService.saveOrUpdate(user2);
}
// 測(cè)試saveOrUpdateBatch(批量新增或更新)
@Test
public void testSaveOrUpdateBatch() {
User user1 = new User();
user1.setId(1L); // 有ID → 更新
user1.setName("批量更新");
user1.setAge(25);
User user2 = new User(); // 無(wú)ID → 新增
user2.setName("批量新增");
user2.setAge(26);
List<User> userList = Arrays.asList(user1, user2);
boolean result = userService.saveOrUpdateBatch(userList);
System.out.println("批量新增/更新是否成功:" + result);
}
// 測(cè)試updateBatchById(批量更新(按ID))
@Test
public void testUpdateBatchById() {
User user1 = new User();
user1.setId(1L);
user1.setAge(30);
User user2 = new User();
user2.setId(2L);
user2.setAge(31);
List<User> userList = Arrays.asList(user1, user2);
boolean result = userService.updateBatchById(userList);
System.out.println("批量更新是否成功:" + result);
}
// 測(cè)試removeById(根據(jù)ID刪除)
@Test
public void testRemoveById() {
boolean result = userService.removeById(1L);
System.out.println("刪除是否成功:" + result);
}
// 測(cè)試removeByIds(批量刪除)
@Test
public void testRemoveByIds() {
List<Long> idList = Arrays.asList(7L, 8L);
boolean result = userService.removeByIds(idList);
System.out.println("批量刪除是否成功:" + result);
}
// 測(cè)試remove(按條件刪除)
@Test
public void testRemove() {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getName, "測(cè)試刪除").eq(User::getAge, 0);
boolean result = userService.remove(queryWrapper);
System.out.println("條件刪除是否成功:" + result);
}
// 測(cè)試getById(根據(jù)ID查詢)
@Test
public void testGetById() {
User user = userService.getById(1L);
System.out.println(user);
}
// 測(cè)試listByIds(批量查詢)
@Test
public void testListByIds() {
List<Long> idList = Arrays.asList(1L, 2L);
List<User> userList = userService.listByIds(idList);
userList.forEach(System.out::println);
}
// 測(cè)試listPage(分頁(yè)查詢)
@Test
public void testListPage() {
Page<User> page = new Page<>(1, 5);
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.gt(User::getAge, 18);
IPage<User> userIPage = userService.page(page, queryWrapper);
System.out.println("總條數(shù):" + userIPage.getTotal());
userIPage.getRecords().forEach(System.out::println);
}
// 測(cè)試count(統(tǒng)計(jì)總數(shù))
@Test
public void testCount() {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.gt(User::getAge, 20);
long count = userService.count(queryWrapper);
System.out.println("年齡大于20的用戶數(shù):" + count);
}
// 測(cè)試鏈?zhǔn)讲樵儯↙ambdaQueryChainWrapper)
@Test
public void testLambdaQueryChain() {
List<User> userList = new LambdaQueryChainWrapper<>(userService.getBaseMapper())
.eq(User::getAge, 20)
.like(User::getName, "張三")
.list();
userList.forEach(System.out::println);
}
// 測(cè)試事務(wù)性批量操作(@Transactional)
@Test
@Transactional // 測(cè)試完成后回滾,避免污染數(shù)據(jù)
public void testTransactionalBatch() {
// 批量新增
List<User> saveList = Arrays.asList(
new User(null, "事務(wù)測(cè)試1", 28, "tx1@test.com", SexEnum.MALE, null),
new User(null, "事務(wù)測(cè)試2", 29, "tx2@test.com", SexEnum.FEMALE,null)
);
userService.saveBatch(saveList);
// 批量更新
List<User> updateList = Arrays.asList(
new User(2026L, "事務(wù)更新1", 30, null, SexEnum.MALE, null),
new User(2027L, "事務(wù)更新2", 31, null, SexEnum.FEMALE, null)
);
userService.updateBatchById(updateList);
// 驗(yàn)證(事務(wù)內(nèi)可查,事務(wù)外回滾)
System.out.println("事務(wù)內(nèi)查詢:" + userService.getById(2026L));
}到此這篇關(guān)于MyBatis-Plus的基本CRUD的文章就介紹到這了,更多相關(guān)mybatisplus crud操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MyBatisPlus如何優(yōu)化千萬(wàn)級(jí)數(shù)據(jù)的CRUD
- MyBatis-Plus通用CRUD操作的實(shí)現(xiàn)
- MyBatis Plus Mapper CRUD接口測(cè)試方式
- Mybatis-Plus?CRUD操作方法
- MyBatisPlus中CRUD使用方法詳解
- MyBatisPlus標(biāo)準(zhǔn)數(shù)據(jù)層CRUD的使用詳解
- mybatisplus?復(fù)合主鍵(多主鍵)?CRUD示例詳解
- MyBatis-Plus使用ActiveRecord(AR)實(shí)現(xiàn)CRUD
- MyBatis-Plus 實(shí)現(xiàn)單表 CRUD的示例代碼
相關(guān)文章
mybatis一對(duì)多兩種mapper寫法實(shí)例
這篇文章主要介紹了mybatis一對(duì)多兩種mapper寫法實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-12-12
Springboot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)代碼實(shí)例
這篇文章主要介紹了Springboot集成Quartz實(shí)現(xiàn)定時(shí)任務(wù)代碼實(shí)例,任務(wù)是有可能并發(fā)執(zhí)行的,若Scheduler直接使用Job,就會(huì)存在對(duì)同一個(gè)Job實(shí)例并發(fā)訪問的問題,而JobDetail?&?Job方式,Scheduler都會(huì)根據(jù)JobDetail創(chuàng)建一個(gè)新的Job實(shí)例,這樣就可以規(guī)避并發(fā)訪問問題2023-09-09
Spring與Mybatis相結(jié)合實(shí)現(xiàn)多數(shù)據(jù)源切換功能
這篇文章主要介紹了Spring與Mybatis相結(jié)合實(shí)現(xiàn)多數(shù)據(jù)源切換功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
java后臺(tái)本地文件轉(zhuǎn)為MultipartFile類型的實(shí)現(xiàn)方式
在Java后臺(tái)將本地文件轉(zhuǎn)換為MultipartFile類型,可以通過使用FileItemFactory創(chuàng)建FileItem,然后使用CommonsMultipartFile類構(gòu)造一個(gè)MultipartFile對(duì)象,將本地文件流轉(zhuǎn)換為MultipartFile,getMultipartFiles()和getMultipartFiles()方法2025-02-02
IDEA的默認(rèn)快捷鍵設(shè)置與Eclipse的常用快捷鍵的設(shè)置方法
這篇文章主要介紹了IDEA的默認(rèn)快捷鍵設(shè)置與Eclipse的常用快捷鍵的設(shè)置方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
SpringBoot服務(wù)訪問路徑動(dòng)態(tài)處理方式
這篇文章主要介紹了SpringBoot服務(wù)訪問路徑動(dòng)態(tài)處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

