Mybatis 集成 MyBatis-Plus。因为项目原因,老的mybatis无法去掉,又无法忍受Mybatsi写那么多xml的痛苦,做了一个兼容方案,共存

改造前(pom、bean)

<properties>
    <mybatis.version>3.4.1</mybatis.version>
    <mybatis-spring.version>1.3.1</mybatis-spring.version>
</properties>

<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis</artifactId>
	<version>${mybatis.version}</version>
</dependency>
<dependency>
	<groupId>org.mybatis</groupId>
	<artifactId>mybatis-spring</artifactId>
	<version>${mybatis-spring.version}</version>
</dependency>
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.0</version>
</dependency>


@Bean("sqlSessionFactory")
public SqlSessionFactoryBean sqlSessionFactoryBean(@Autowired @Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {
	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
	sqlSessionFactoryBean.setDataSource(dynamicDataSource);
	sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(batisMapperLocation));
	sqlSessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(batisConfigLocation));
	sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
	return sqlSessionFactoryBean;
}

改造后(pom、bean、entity、mapper)

<properties>
    <mybatis.version>3.5.9</mybatis.version>
    <mybatis-spring.version>2.0.7</mybatis-spring.version>
    <mybatis-spring-boot-starter.version>2.2.2</mybatis-spring-boot-starter.version>
    <mybatis-plus.version>3.4.2</mybatis-plus.version>
</properties>

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>${mybatis.version}</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>${mybatis-spring.version}</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>${mybatis-spring-boot-starter.version}</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>${mybatis-plus.version}</version>
</dependency>

@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactoryBean(@Autowired @Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {
    //主要改动在这里
	MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
	sqlSessionFactoryBean.setDataSource(dynamicDataSource);
	sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(batisMapperLocation));
	sqlSessionFactoryBean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource(batisConfigLocation));
	sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
	sqlSessionFactoryBean.setGlobalConfig(globalConfig());
	return sqlSessionFactoryBean.getObject();
}

//因为项目用的是oracle数据库,注入全局配置
@Bean
public GlobalConfig globalConfig() {
	GlobalConfig conf = new GlobalConfig();
	conf.setDbConfig(new GlobalConfig.DbConfig().setKeyGenerator(new OracleKeyGenerator()));
	return conf;
}

实战(Entity,Mapper,TestController)

//实体类
@KeySequence("seq_Sys_Constant")
public class SysConstant implements Serializable {

	@TableId
	private Integer constantId;

	private String constantType;

	private String constantKey;

	private String constantValue;

	public Integer getConstantId() {
		return constantId;
	}

	public void setConstantId(Integer constantId) {
		this.constantId = constantId;
	}

	public String getConstantType() {
		return constantType;
	}

	public void setConstantType(String constantType) {
		this.constantType = constantType;
	}

	public String getConstantKey() {
		return constantKey;
	}

	public void setConstantKey(String constantKey) {
		this.constantKey = constantKey;
	}

	public String getConstantValue() {
		return constantValue;
	}

	public void setConstantValue(String constantValue) {
		this.constantValue = constantValue;
	}
}

//Mapper接口
public interface SysConstantMapper extends BaseMapper<SysConstant> {

}

//测试Controller
@Controller
@RequestMapping("/data")
public class TestPlusController {

	@Autowired
	SysConstantMapper sysConstantMapper;

	@PostMapping("/insert")
	@ResponseBody
	public ResponseModel<?> insert(@RequestBody RequestMonoModel<TestVo> model) {
		SysConstant sysConstant = new SysConstant();
		sysConstant.setConstantKey("1");
		sysConstant.setConstantValue("2");
		sysConstant.setConstantType("3");
		sysConstantMapper.insert(sysConstant);
		return Rm.ok();
	}

	@GetMapping("/get/{id}")
	@ResponseBody
	public ResponseModel<?> get(@PathVariable Integer id) {
        //根据主键查询单条记录
		SysConstant sysConstant = sysConstantMapper.selectById(id);
		System.out.println(JSON.toJSONString(sysConstant));
        //lambdaQuery查询,根据指定字段查询返回单条记录
		sysConstant = sysConstantMapper.selectOne(Wrappers.lambdaQuery(SysConstant.class).eq(SysConstant::getConstantValue, "12"));
		System.out.println(JSON.toJSONString(sysConstant));
		return Rm.ok();
	}

}

我这边老的分页插件是自定义的,用不到MybatisPlus的分页插件,如果要用到MybatisPlus的插件,需要再配置下

<plugins>
    <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"/> 
</plugins>

至此,就可以让Mybatis 和 Mybatis Plus和互相补充,更好地完成ORM操作。 新项目的话还是建议大家用MybatisPlus,因为它只是在Mybatis基础上做增加,完美兼容,告别琐碎的xml维护,当然复杂的Sql还是要写到xml里面,方便维护。