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.yml
mybatis: # 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.yml
mybatis-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
的使用:官网常用方法 说明 栗子 栗子对应 sql
eq
等于 =
eq("name", "老王")
name = '老王'
ne
不等于 !=
ne("name", "老王")
name <> '老王'
gt
大于 >
gt("age", 18)
age > 18
ge
≥
ge("age", 18)
age >= 18
lt
小于 <
lt("age", 18)
age < 18
le
≤
le("age", 18)
age <= 18
between
在...之间 between("age", 18, 30)
age between 18 and 30
notBetween
不在...之间 notBetween("age", 18, 30)
age not between 18 and 30
like
LIKE '%值%' like("name", "王")
name like '%王%'
notLike
NOT 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,name
orderByAsc
Asc
升序排序orderByAsc("id", "name")
order by id ASC,name ASC
orderByDesc
Desc
降序排序orderByDesc("id", "name")
order by id DESC,name DESC
or
下一个 方法
不是用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);
-
一般
逻辑删除
,不怎么使用
-
网友评论