1.0 BaseMapper
我们先来查看下 BaseMapper 接口中方法有哪些?(大家也可以查看自己调用的mapper里面方法是一样的 我贴出来是为了方便大家查看)
/* * Copyright (c) 2011-2020, baomidou (jobob@qq.com). * <p> * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * <p> * https://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.baomidou.mybatisplus.core.mapper; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Constants; import org.apache.ibatis.annotations.Param; import java.io.Serializable; import java.util.Collection; import java.util.List; import java.util.Map; /* :` .:, :::,,. :: `:::::: ::` `,:,` .:` `:: `::::::::.:` `:';,` ::::, .:::` `@++++++++: `` :::` @+++++++++++# :::, #++++++++++++++` ,: `::::::;'##++++++++++ .@#@;` ::::::::::::::::::::; #@####@, :::::::::::::::+#;::. @@######+@:::::::::::::. #@:; , @@########':::::::::::: .#''':` ;##@@@+:##########@::::::::::: @#;.,:. #@@@######++++#####'::::::::: .##+,:#` @@@@@#####+++++'#####+::::::::` ,`::@#:` `@@@@#####++++++'#####+#':::::::::::@. @@@@######+++++''#######+##';::::;':,` @@@@#####+++++'''#######++++++++++` #@@#####++++++''########++++++++' `#@######+++++''+########+++++++; `@@#####+++++''##########++++++, @@######+++++'##########+++++#` @@@@#####+++++############++++; ;#@@@@@####++++##############+++, @@@@@@@@@@@###@###############++' @#@@@@@@@@@@@@###################+: `@#@@@@@@@@@@@@@@###################'` :@#@@@@@@@@@@@@@@@@@##################, ,@@@@@@@@@@@@@@@@@@@@################; ,#@@@@@@@@@@@@@@@@@@@##############+` .#@@@@@@@@@@@@@@@@@@#############@, @@@@@@@@@@@@@@@@@@@###########@, :#@@@@@@@@@@@@@@@@##########@, `##@@@@@@@@@@@@@@@########+, `+@@@@@@@@@@@@@@@#####@:` `:@@@@@@@@@@@@@@##@;. `,'@@@@##@@@+;,` ``...`` _ _ /_ _ _/_. ____ / _ / / //_//_//_|/ /_\ /_///_/_\ Talk is cheap. Show me the code. _/ / */ /** * Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能 * <p>这个 Mapper 支持 id 泛型</p> * * @author hubin * @since 2016-01-23 */ public interface BaseMapper<T> extends Mapper<T> { /** * 插入一条记录 * * @param entity 实体对象 */ int insert(T entity); /** * 根据 ID 删除 * * @param id 主键ID */ int deleteById(Serializable id); /** * 根据 columnMap 条件,删除记录 * * @param columnMap 表字段 map 对象 */ int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); /** * 根据 entity 条件,删除记录 * * @param wrapper 实体对象封装操作类(可以为 null) */ int delete(@Param(Constants.WRAPPER) Wrapper<T> wrapper); /** * 删除(根据ID 批量删除) * * @param idList 主键ID列表(不能为 null 以及 empty) */ int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); /** * 根据 ID 修改 * * @param entity 实体对象 */ int updateById(@Param(Constants.ENTITY) T entity); /** * 根据 whereEntity 条件,更新记录 * * @param entity 实体对象 (set 条件值,可以为 null) * @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句) */ int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper); /** * 根据 ID 查询 * * @param id 主键ID */ T selectById(Serializable id); /** * 查询(根据ID 批量查询) * * @param idList 主键ID列表(不能为 null 以及 empty) */ List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); /** * 查询(根据 columnMap 条件) * * @param columnMap 表字段 map 对象 */ List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); /** * 根据 entity 条件,查询一条记录 * * @param queryWrapper 实体对象封装操作类(可以为 null) */ T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /** * 根据 Wrapper 条件,查询总记录数 * * @param queryWrapper 实体对象封装操作类(可以为 null) */ Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /** * 根据 entity 条件,查询全部记录 * * @param queryWrapper 实体对象封装操作类(可以为 null) */ List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /** * 根据 Wrapper 条件,查询全部记录 * * @param queryWrapper 实体对象封装操作类(可以为 null) */ List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /** * 根据 Wrapper 条件,查询全部记录 * <p>注意: 只返回第一个字段的值</p> * * @param queryWrapper 实体对象封装操作类(可以为 null) */ List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /** * 根据 entity 条件,查询全部记录(并翻页) * * @param page 分页查询条件(可以为 RowBounds.DEFAULT) * @param queryWrapper 实体对象封装操作类(可以为 null) */ <E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /** * 根据 Wrapper 条件,查询全部记录(并翻页) * * @param page 分页查询条件 * @param queryWrapper 实体对象封装操作类 */ <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); }
2.0 对 BaseMapper中的方式进行使用
2.1 新增用户信息 (insert)
@Test public void testInsert(){ User user = new User(); user.setName("小龙"); user.setAge(22); user.setEmail("757631644@qq.com"); int insert = userMapper.insert(user); System.out.println("insert"+insert); //获取mapper新增的id 使用雪花算法生成的id System.out.println("id"+user.getId()); }
测试结果如下:
2.2 删除用户信息
2.2.1 deleteById 根据id删除
@Test public void testDelectById(){ //通过id删除 DELETE FROM user WHERE id=? int deleteById = userMapper.deleteById(1576958456307400705L); if (deleteById>0){ System.out.println("删除成功"); }else { System.out.println("删除失败"); } System.out.println("deleteById"+deleteById); }
运行结果如下:
==> Preparing: DELETE FROM user WHERE id=? ==> Parameters: 1576958456307400705(Long) <== Updates: 1
2.2.2 deleteByMap根据map条件删除
@Test public void testDelectByMap(){ //通过map中设置的条件进行删除 HashMap<String, Object> map = new HashMap<>(); map.put("name","小龙"); map.put("age",22); int deleteByMap = userMapper.deleteByMap(map); if (deleteByMap>0){ System.out.println("删除成功"); }else { System.out.println("删除失败"); } System.out.println("deleteByMap"+deleteByMap); }
运行结果如下:
==> Preparing: DELETE FROM user WHERE name = ? AND age = ? ==> Parameters: 小龙(String), 22(Integer) <== Updates: 1
2.2.3 deleteBatchIds 批量删除
@Test public void testDelectBatchIds(){ //通过id中批量删除 List<Long> longs = Arrays.asList(1L, 2L, 3L); int deleteBatchIds = userMapper.deleteBatchIds(longs); if (deleteBatchIds>0){ System.out.println("删除成功"); }else { System.out.println("删除失败"); } System.out.println("deleteBatchIds"+deleteBatchIds); }
运行结果如下:
==> Preparing: DELETE FROM user WHERE id IN ( ? , ? , ? ) ==> Parameters: 1(Long), 2(Long), 3(Long) <== Updates: 3
3.0 修改用户信息
3.1 根据id进行修改 updateById
@Test public void testUpdate(){ User user = new User(); user.setId(4L); user.setName("我是老王"); user.setAge(322); int updateById = userMapper.updateById(user); if (updateById>0){ System.out.println("修改成功"); }else { System.out.println("修改失败"); } System.out.println("updateById"+updateById); }
运行结果如下:
==> Preparing: UPDATE user SET name=?, age=? WHERE id=? ==> Parameters: 我是老王(String), 322(Integer), 4(Long) <== Updates: 1
4.0 查询用户信息
4.1 selectById 根据id查询用户
@Test public void testSelect(){ //通过id查询用户信息 User user = userMapper.selectById(1L); System.out.println(user); }
运行结构如下:
==> Parameters: 1(Long) <== Columns: id, name, age, email <== Row: 1, Jone, 18, test1@baomidou.com <== Total: 1
4.2 selectBatchIds根据多个id查询用户
@Test public void testSelectBatchIds(){ List<Long> longs = Arrays.asList(1L, 2L); //通过多个id查询用户信息 List<User> user = userMapper.selectBatchIds(longs); System.out.println(user); System.out.println("---------------------"); user.forEach(System.out::println); }
运行结果如下:
==> Preparing: SELECT id,name,age,email FROM user WHERE id IN ( ? , ? ) ==> Parameters: 1(Long), 2(Long) <== Columns: id, name, age, email <== Row: 1, Jone, 18, test1@baomidou.com <== Row: 2, Jack, 20, test2@baomidou.com <== Total: 2 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3f9f71ff] [User(id=1, name=Jone, age=18, email=test1@baomidou.com), User(id=2, name=Jack, age=20, email=test2@baomidou.com)] --------------------- User(id=1, name=Jone, age=18, email=test1@baomidou.com) User(id=2, name=Jack, age=20, email=test2@baomidou.com)
4.3 selectBatchIds 通过map集合中的条件查询用户信息
@Test public void testSelectByMap(){ HashMap<String, Object> hashMap = new HashMap<>(); hashMap.put("name","Jack"); hashMap.put("age",20); //通过map集合中的条件查询用户信息 List<User> user = userMapper.selectByMap(hashMap); user.forEach(System.out::println); }
运行结果如下:
==> Preparing: SELECT id,name,age,email FROM user WHERE name = ? AND age = ? ==> Parameters: Jack(String), 20(Integer) <== Columns: id, name, age, email <== Row: 2, Jack, 20, test2@baomidou.com <== Total: 1
4.3 selectList 通过map集合中的条件查询用户信息
@Test public void testselectList(){ //通过条件构造器查询一个list集合,若没有条件则可设置null List<User> users = userMapper.selectList(null); users.forEach(System.out::println); }
运行结果如下:
==> Preparing: SELECT id,name,age,email FROM user ==> Parameters: <== Columns: id, name, age, email <== Row: 1, Jone, 18, test1@baomidou.com <== Row: 2, Jack, 20, test2@baomidou.com <== Row: 3, Tom, 28, test3@baomidou.com <== Row: 4, Sandy, 21, test4@baomidou.com <== Row: 5, Billie, 24, test5@baomidou.com <== Total: 5 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3f9f71ff] User(id=1, name=Jone, age=18, email=test1@baomidou.com) User(id=2, name=Jack, age=20, email=test2@baomidou.com) User(id=3, name=Tom, age=28, email=test3@baomidou.com) User(id=4, name=Sandy, age=21, email=test4@baomidou.com) User(id=5, name=Billie, age=24, email=test5@baomidou.com)
基本的CRUD基本到这里就完成了,大家也可尝试其他的一些方法;
5.0 扩展 Test代码集合整理
package com.example; import com.example.mapper.UserMapper; import com.example.pojo.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.Arrays; import java.util.HashMap; import java.util.List; @SpringBootTest public class MyBatisPlusTest { @Autowired private UserMapper userMapper; /** * 查询全部 */ @Test public void testSelectList(){ //通过条件构造器查询一个list集合,若没有条件则可设置null List<User> users = userMapper.selectList(null); users.forEach(System.out::println); } @Test public void testInsert(){ User user = new User(); user.setName("小龙"); user.setAge(22); user.setEmail("757631644@qq.com"); int insert = userMapper.insert(user); System.out.println("insert"+insert); //获取mapper新增的id 使用雪花算法生成的id System.out.println("id"+user.getId()); } @Test public void testDelectById(){ //通过id删除 DELETE FROM user WHERE id=? int deleteById = userMapper.deleteById(1576958456307400705L); if (deleteById>0){ System.out.println("删除成功"); }else { System.out.println("删除失败"); } System.out.println("deleteById"+deleteById); } @Test public void testDelectByMap(){ //通过map中设置的条件进行删除 HashMap<String, Object> map = new HashMap<>(); map.put("name","小龙"); map.put("age",22); int deleteByMap = userMapper.deleteByMap(map); if (deleteByMap>0){ System.out.println("删除成功"); }else { System.out.println("删除失败"); } System.out.println("deleteByMap"+deleteByMap); } @Test public void testDelectBatchIds(){ //通过id中批量删除 List<Long> longs = Arrays.asList(1L, 2L, 3L); int deleteBatchIds = userMapper.deleteBatchIds(longs); if (deleteBatchIds>0){ System.out.println("删除成功"); }else { System.out.println("删除失败"); } System.out.println("deleteBatchIds"+deleteBatchIds); } /** * 根据id修改 */ @Test public void testUpdate(){ User user = new User(); user.setId(4L); user.setName("我是老王"); user.setAge(322); int updateById = userMapper.updateById(user); if (updateById>0){ System.out.println("修改成功"); }else { System.out.println("修改失败"); } System.out.println("updateById"+updateById); } @Test public void testSelect(){ //通过id查询用户信息 User user = userMapper.selectById(1L); System.out.println(user); } @Test public void testSelectBatchIds(){ List<Long> longs = Arrays.asList(1L, 2L); //通过多个id查询用户信息 List<User> user = userMapper.selectBatchIds(longs); System.out.println(user); System.out.println("---------------------"); user.forEach(System.out::println); } @Test public void testselectList(){ //通过条件构造器查询一个list集合,若没有条件则可设置null List<User> users = userMapper.selectList(null); users.forEach(System.out::println); } }