继上一篇SpringBoot集成通用Mapper之后,今天简单整理一下SpringBoot集成mybatisplus。mybatisplus是Mybatis增强工具,省去我们所有基础的增删改查方法,下面让我们一起来了解一下如何在SpringBoot项目中使用。
1.添加Maven依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
2.创建实体类
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import lombok.Data;
/**
* 地区
*
*/
@TableName("district")
@Data
public class District{
/**
* 序号,如果需要获取数据库自动生成ID,必须设置type=IdType.AUTO
*/
@TableId(type=IdType.AUTO)
private Integer id;
/**
* 地区类型
*/
private Integer type;
/**
* 父节点id
*/
private Integer parentId;
/**
* 名称
*/
private String name;
/**
* 排序
*/
private Integer sort;
/**
* 编码
*/
private Integer code;
private Boolean deleted;
}
3.创建Mapper类
import com.baomidou.mybatisplus.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 地区主数据
*
* @date 2019-03-19 16:45:38
*/
public interface DistrictMapper extends BaseMapper<District> {
//自定义的SQL
List<DistrictVo> selectList(DistrictDto districtDto);
DistrictDetailVo selectByNames(DistrictQueryDto districtQueryDto);
}
4.创建Mapper映射配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.xxx.DistrictMapper">
<!-- 可根据自己的需求,是否要使用 -->
<resultMap type="com.user.dal.entity.District" id="districtMap">
<result property="id" column="id"/>
<result property="type" column="type"/>
<result property="parentId" column="parent_id"/>
<result property="name" column="name"/>
<result property="sort" column="sort"/>
<result property="code" column="code"/>
<result property="deleted" column="deleted"/>
</resultMap>
<select id="selectList" resultType="xxx.xxx.DistrictVo">
select cdt.id, cdt.name, cdt.code ,
(select count(1) from co_district cd where cd.parent_id = cdt.id) as childCount
from co_district cdt where deleted = 0
<if test="type != null">
and cdt.type = #{type}
</if>
<if test="parentId != null">
and cdt.parent_id = #{parentId}
</if>
</select>
<select id="selectByNames" resultType="xxx.xxx.DistrictDetailVo">
select dc.id, dc.name, dc.code, dp.id cityId, dg.id provinceId
from co_district dc
LEFT JOIN co_district dp on dc.parent_id = dp.id
LEFT JOIN co_district dg on dp.parent_id = dg.id
where dc.deleted = 0
and dc.type = 2
<if test="name != null and name != ''">
and dc.name like concat('%',#{name},'%')
</if>
<if test="cityName != null and cityName != ''">
and dp.name like concat('%',#{cityName},'%')
</if>
<if test="provinceName != null and provinceName != ''">
and dg.name like concat('%',#{provinceName},'%')
</if>
</select>
</mapper>
5.创建Service
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@RestController
@Slf4j
public class DistrictService extends ServiceImpl<DistrictMapper, District> {
public List<DistrictVo> findList(@RequestBody DistrictDto districtDto) {
//直接调用组件封装的增删改查方法,具体参见BaseMapper的源码实现
return this.baseMapper.selectList(districtDto);
}
}
6.启动类增加Mapper扫描包路径
@MapperScan("xxx.dal.mapper")
7.应用配置文件中添加mybatis-plus相关配置
# mybatis-plus
mybatis-plus:
mapper-locations: classpath:mapper/**/*.xml
type-aliases-package: xxx.dal.entity
configuration:
map-underscore-to-camel-case: true