公司springcloud框架中使用的持久层是mybatis,为了与时俱进,最近决定采用更先进的技术提高生产力,最近打算把mybatis替换成加强版mybatis-plus,以下是本次的升级过程,以及过程中遇到的坑。
1、本次做了一个common,把controller、entity、mapper、service的基本功能进行一下简单的封装
首先追加mybatis-plus的依赖
com.baomidou
mybatis-plus-boot-starter
entity、mapper、service的框架基类都是mybatis-plus中使用的,没有做什么特殊处理,留好基类,为了以后继续拓展用,都是只做了一下继承mybatis-plus。
BaseEntity.java
package com.rcyj.common.base.entity;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author YaoHuaJun
* @since 2019-12-14
*/
@EqualsAndHashCode(callSuper = true)
@Data
public abstract class BaseEntity> extends BaseCreatedEntity {
// /**
// * 删除状态
// */
// @TableLogic
// private int deleteState;
}
接口BaseMapper.java
package com.rcyj.common.base.mapper;
/**
* @author YaoHuaJun
* @since 2019-12-14
*/
public interface BaseMapper extends com.baomidou.mybatisplus.core.mapper.BaseMapper {
}
接口BaseService.java
package com.rcyj.common.base.service;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author YaoHuaJun
* @since 2019-12-14
*/
public interface BaseService extends IService {
}
实体类BaseServiceImpl.java
package com.rcyj.common.base.service.impl;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rcyj.common.base.service.BaseService;
/**
* @author YaoHuaJun
* @since 2019-12-14
*/
public class BaseServiceImpl, T> extends ServiceImpl implements BaseService {
}
2、本次追加了做业务模块
追加依赖
com.baomidou
mybatis-plus-boot-starter
${mybatis-plus.version}
yml文件中追加(mybatis-plus不实用xml配置sql,注释掉就可以了)
# mybatis-plus
mybatis-plus:
#mapper-locations: classpath*:mapper/**/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
type-aliases-package: com.rcyj.systemex.entity
global-config:
sql-parser-cache: true
db-config:
id-type: id_worker_str
logic-delete-value: 1
logic-not-delete-value: 0
banner: false
configuration:
map-underscore-to-camel-case: true
cache-enabled: true
# 打印SQL
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
controller文件中追加
/**
* 获取单个详情
*
* @param id
* @return
*/
@GetMapping(API_PREFIX + PATH + "/select")
public ResponseEntity select(String id) {
return ResponseEntity.ok(this.dictionaryService.getById(id));
}
entity文件
package com.rcyj.systemex.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.rcyj.common.base.entity.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.time.LocalDateTime;
/**
* @author: YaoHuaJun
* @create: 2019-12-14 11:21
**/
@EqualsAndHashCode(callSuper = true)
@Data
@TableName("sys_dict")
public class Dictionary extends BaseEntity {
private String typeCode;
private String name;
private Byte value;
private String parentTypeCode;
private Byte isDeleted;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
mapper文件追加
package com.rcyj.systemex.mapper;
import com.rcyj.common.base.mapper.BaseMapper;
import com.rcyj.systemex.entity.Dictionary;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @author: YaoHuaJun
* @create: 2019-12-14 11:25
**/
@Repository
public interface DictionaryMapper extends BaseMapper {
}
service文件
接口
package com.rcyj.systemex.service;
import com.rcyj.common.base.service.BaseService;
import com.rcyj.systemex.entity.Dictionary;
import java.util.List;
/**
* @author: YaoHuaJun
* @create: 2019-12-14 11:25
**/
public interface DictionaryService extends BaseService {
}
实体类
package com.rcyj.systemex.service.impl;
import com.rcyj.common.base.service.impl.BaseServiceImpl;
import com.rcyj.systemex.entity.Dictionary;
import com.rcyj.systemex.mapper.DictionaryMapper;
import com.rcyj.systemex.service.DictionaryService;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author: YaoHuaJun
* @create: 2019-12-14 11:25
**/
@Service
public class DictionaryServiceImpl extends BaseServiceImpl implements DictionaryService {
}
注意点:因为本次使用了阿里的druid连接池,这玩意和mybatis-plus有版本不容的问题,试了好多版本,最终才实验出来用哪一对比较合适,先分享给大家。
3.0.6
1.1.18