1. 说明
spring boot只能集成两者中的一个,mybatis-plus是对mybatis的进一步封装,省去了书写mapper.xml。mybatis-plus的封装只支持单表操作,对于多表依然可以通过xml的方式来实现
2. 集成mybatis
-
依赖
<dependencies> <!-- mybatis-starter --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> </dependencies>- 导入
starter - 使用还要配置
jdbc驱动:配置druid连接池
- 导入
-
配置文件
-
application.yml主配置文件注册spring: profiles: # 注册配置文件 active: dev,druid,mybatis -
application-mybatis.ymlmybatis: # mapper.xml文件位置 mapper-locations: classpath:mapper/**/*.xml # 别名配置 type-aliases-package: com.sheng.mybatis.domain.entity configuration: # 日志配置 log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
-
-
在
程序启动类上使用@MappenScan("")@SpringBootApplication // 自动注册mapper接口 @MapperScan("com.sheng.mybatis.mapper") public class SpringBootMybatisApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMybatisApplication.class, args); } }
3. 集成mybatis-plus
-
依赖
<dependencies> <!-- mybatis-plus-starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.2</version> </dependency> </dependencies>- 导入
starter - 使用还要配置
jdbc驱动:配置druid连接池 - 可以导入
mybatis的依赖,但是不能配置mybatis
- 导入
-
配置文件
-
application.yml主配置文件注册spring: profiles: # 注册配置文件 active: dev,druid,mybatis-plus -
application-mybatis-plus.ymlmybatis-plus: # 可配可不配,默认为 classpath*:/mapper/**/*.xml mapper-locations: classpath:mapper/**/*.xml configuration: # 配置日志 log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
-
-
在
程序启动类上使用@MappenScan("")@SpringBootApplication // 自动注册mapper接口 @MapperScan("com.sheng.mybatis.plus.mapper") public class SpringBootMybatisPlusApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMybatisPlusApplication.class, args); } }
4. mybatis-plus的基本使用
-
select-
源码
/** * 根据 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); -
常用方法栗子
@Test public void test() { // selectById() Menu menu = menuMapper.selectById(1); // selectList() QueryWrapper<Menu> wrapper = new QueryWrapper<>(); wrapper.eq(Menu.COL_MENU_NAME, "zero") .or() .eq(Menu.COL_STATUS, 0); List<Menu> menus = menuMapper.selectList(wrapper); // selectCount() Integer integer = menuMapper.selectCount(wrapper); // selectBatchIds() menus = menuMapper.selectBatchIds(Arrays.asList(1, 2, 3)); } -
QueryWrapper的使用:官网常用方法 说明 栗子 栗子对应 sqleq等于 =eq("name", "老王")name = '老王'ne不等于 !=ne("name", "老王")name <> '老王'gt大于 >gt("age", 18)age > 18ge≥ge("age", 18)age >= 18lt小于 <lt("age", 18)age < 18le≤le("age", 18)age <= 18between在…之间 between("age", 18, 30)age between 18 and 30notBetween不在…之间 notBetween("age", 18, 30)age not between 18 and 30likeLIKE ‘%值%’ like("name", "王")name like '%王%'notLikeNOT LIKE ‘%值%’ notLike("name", "王")name not like '%王%'in在某个集合内 in("age",{1,2,3})age in (1,2,3)notIn不在某个集合内 notIn("age",{1,2,3})age not in (1,2,3)groupBy分组 groupBy("id", "name")group by id,nameorderByAscAsc升序排序orderByAsc("id", "name")order by id ASC,name ASCorderByDescDesc降序排序orderByDesc("id", "name")order by id DESC,name DESCor下一个 方法不是用and连接eq("id",1).or().eq("name","老王")id = 1 or name = '老王'- 不调用
or默认使用and连接
- 不调用
-
-
update-
源码
/** * 根据 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); -
栗子// updateById() Address address = new Address(); address.setAddressId(id); address.setIsDefault(1); // 修改 int result = addressMapper.updateById(address); // update() Address address = new Address(); address.setCustomerId(useId); address.setIsDefault(0); // 条件对象 UpdateWrapper<Address> wrapper = new UpdateWrapper<>(); // 拼接条件 wrapper.eq(Address.COL_CUSTOMER_ID, useId); result = addressMapper.update(address, wrapper); -
UpdateWrapper与QueryWrapper的使用类似
-
-
insert-
源码
/** * 插入一条记录 * * @param entity 实体对象 */ int insert(T entity); -
栗子
@Test public void test() { Item item = new Item(); item.setName("zero"); item.setPassword("123456"); item.setAge(18); // 插入 int result = itemMapper.insert(item); }
-
-
delete-
源码
/** * 根据 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); -
一般
逻辑删除,不怎么使用
-
本文详细介绍了如何在SpringBoot中集成MyBatis和MyBatis-Plus,包括依赖配置、基本使用方法及示例代码。MyBatis-Plus提供了丰富的CRUD操作,简化了数据库操作流程。
5576

被折叠的 条评论
为什么被折叠?



