大家日常mybatis开发的时候,有没有发现好多操作基本都差不多,比如通过id进行select、update、delete等等操作,虽然差不多,但是xml文件和mapper接口中也得写这一堆一模一样的代码。那我们有什么工具可以帮我完成这些重复性的代码工作呢?别着急下面就来给大家一一详解。
一、pom文件中加入tk.mybatis依赖
<!--tk.mybatis依赖-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
引入依赖的时候要特别注意是否会和springboot相互冲突,比如我这边用的springboot版本是2.1.3.RELEASE和2.1.5的tk.mybatis是不冲突的。
二、mapper接口中继承Mapper(tk.mybatis.mapper.common.Mapper)
import com.hzcard.crm.mybatis.domain.SampleMain;
import tk.mybatis.mapper.common.Mapper;
public interface SampleMainMapper extends Mapper<SampleMain> {
}
我们可以看到这个SampleMainMapper接口中都没有写任何的方法,只是继承一个Mapping接口,这边需要注意的是Mapper不是mybatis中的mapper(org.apache.ibatis.annotations.Mapper),而是tk.mybatis.mapper.common.Mapper中的。
三、.mapper对应的xml文件
<?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="com.hzcard.crm.mybatis.mapper.SampleMainMapper">
<resultMap id="BaseResultMap" type="com.hzcard.crm.mybatis.domain.SampleMain">
<!--
WARNING - @mbg.generated
-->
<id column="id_" jdbcType="BIGINT" property="id" />
<result column="gmt_create" jdbcType="TIMESTAMP" property="gmtCreate" />
<result column="gmt_modified" jdbcType="TIMESTAMP" property="gmtModified" />
<result column="is_deleted" jdbcType="BIT" property="deleted" />
<result column="first_name" jdbcType="VARCHAR" property="firstName" />
<result column="last_name" jdbcType="VARCHAR" property="lastName" />
<result column="age_" jdbcType="TINYINT" property="age" />
</resultMap>
</mapper>
我们可以看到也没有对应的增删改查配置信息出现,因为tk.mybatis已经全部帮我们做好了。
四、实际代码调用过程详解
1.控制类代码
@RestController
public class SampleController implements SampleApi{
private static final Logger logger = LoggerFactory.getLogger(SampleController.class);
@Autowired
private SampleService sampleService;
@Override
public SampleDto echo(SampleDto dto) {
logger.info("#echo {}", dto);
return dto;
}
@Override
public SampleDto getById(@PathVariable("id") Long id) {
logger.info("#getById {}", id);
return sampleService.getSample(id);
}
@Override
public Long create(@RequestBody SampleDto dto) {
logger.info("#create {}", dto);
return sampleService.createSample(dto);
}
@Override
public boolean update(@PathVariable("id") Long id, @RequestBody SampleDto dto) {
logger.info("#update id={}, dto={}", id, dto);
dto.setId(id);
return sampleService.updateSample(dto);
}
@Override
public boolean delete(@PathVariable("id") Long id) {
logger.info("#delete {}", id);
return sampleService.deleteSample(id);
}
@Override
public Page<SampleDto> page(@RequestParam("index") int index, @RequestParam(value = "size", defaultValue = "20") int size) {
logger.info("#page index={}, size={}", index, size);
return sampleService.pageSample(index, size);
}
}
2.服务接口代码
public interface SampleService {
/**
* 创建
* @param dto {@link SampleDto}
* @return ID
*/
Long createSample(SampleDto dto);
/**
* 更新
* @param dto {@link SampleDto}
* @return 是否更新
*/
boolean updateSample(SampleDto dto);
/**
* 删除
* @param id ID
* @return 是否删除
*/
boolean deleteSample(Long id);
/**
* 查找
* @param id ID
* @return {@link SampleDto},没有相应对象则返回{@code null}
*/
SampleDto getSample(Long id);
/**
* 分页查找
* @param index 页码下标
* @param size 每页尺寸
* @return 分页对象
*/
Page<SampleDto> pageSample(int index, int size);
}
3.服务实现类代码
@Service
public class SampleServiceImpl implements SampleService {
private static final Logger logger = LoggerFactory.getLogger(SampleServiceImpl.class);
@Autowired
private IdGenerator idGenerator;
@Autowired
private SampleMainMapper sampleMainMapper;
@Override
@Transactional
public Long createSample(SampleDto dto) {
logger.info("#createSample {}", dto);
Date currentDate = DateUtils.getCurrentDate();
long id = idGenerator.nextId();
SampleMain sampleMain = BeanUtils.copyProps(dto, SampleMain.class);
sampleMain.setId(id);
sampleMain.setGmtCreate(currentDate);
sampleMain.setGmtModified(currentDate);
sampleMain.setDeleted(Boolean.FALSE);
sampleMainMapper.insertSelective(sampleMain);
return id;
}
@Override
@Transactional
public boolean updateSample(SampleDto dto) {
logger.info("#updateSample {}", dto);
Date currentDate = DateUtils.getCurrentDate();
SampleMain sampleMain = BeanUtils.copyProps(dto, SampleMain.class);
sampleMain.setGmtModified(currentDate);
int rows = sampleMainMapper.updateByPrimaryKeySelective(sampleMain);
return rows > 0;
}
@Override
@Transactional
public boolean deleteSample(Long id) {
logger.info("#deleteSample {}", id);
int rows = sampleMainMapper.deleteByPrimaryKey(id);
return rows > 0;
}
@Override
public SampleDto getSample(Long id) {
logger.info("#getSample {}", id);
SampleMain sampleMain = sampleMainMapper.selectByPrimaryKey(id);
return BeanUtils.copyProps(sampleMain, SampleDto.class);
}
@Override
public Page<SampleDto> pageSample(int index, int size) {
logger.info("#pageSample index={}, size={}", index, size);
PageInfo<SampleMain> pageInfo = PageHelper.offsetPage(index * size, size)// 分页
.setOrderBy("GMT_CREATE DESC")// 排序
.doSelectPageInfo(new ISelect() {
@Override
public void doSelect() {
sampleMainMapper.selectAll();
}
});
return PageCopier.copy(pageInfo, SampleDto.class);
}
}
我们可以看到sampleMainMapper中的insertSelective、deleteByPrimaryKey、updateByPrimaryKeySelective、selectByPrimaryKey这些常见的方法都已经帮我们集成好了,我们只需要调用接口了,完全没必要自己写,可谓简单又高效。
参考文章:https://blog.csdn.net/linzhiqiang0316/article/details/88314026