Spring Boot集成JPA和MyBatis例子
先说说背景,项目之前使用的spring JPA做为数据持久化的工具,本人接手后想使用MyBatis,但是之前的又不想改变,所以有了这个需求,同一个数据源,使用spring JPA和MyBatis来做持久化。
刚开始集成出了好多问题,以下就列出正确的集成方式,参照这种方式,一定能集成成功
在之前的mybatis版本中,dao和mapper文件的文件名要特殊定义,就是末尾必须是Dao或者Mapper,但是现在不用了,现在在config文件中会配置扫描的类文件的包地址和.xml文件的地址,顺便说下mybatis版本
"org.mybatis:mybatis-spring:1.3.0",
"org.mybatis:mybatis:3.4.4",
要注意的事项:
a,xml中有个namespace是写xml文件对应的Dao类的,千万不要写错了,这个写错了mybatis也检测不出来,是个大坑
b,config文件中有如下方法,这个是读取配置文件,连接数据库的,如果配置文件是yml,可以用如下这种方式,会简结一些
@Bean @ConfigurationProperties(prefix = "spring.datasource") @Primary public DataSource idrDataSource() { return DataSourceBuilder.create().build(); }
某认为,集成主要有依赖以下几个文件,1,注册数据源文件2,配置文件3,dao.java,4,mapper.xml,下面给个正确的配置
1,注册数据源的文件,主要是项目启动时扫描dao文件和xml文件,根据配置文件连接数据源
package com.xxx.cms.boot.config; import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import com.alibaba.druid.pool.DruidDataSource; @Configuration
//此处是dao所在的包的地址 @MapperScan(basePackages = { "com.xxx.cms.repository.ibatis" }, sqlSessionTemplateRef = "idrSqlSessionTemplate") public class IDRDatasourceConfig2 { @Value("${spring.datasource.driver-class-name}") private String driverClassName; @Value("${spring.datasource.url}") private String url; @Value("${spring.datasource.username}") private String username; @Value("${spring.datasource.password}") private String password; @Value("${encrypt.seed}") private String encryptKey; @Bean @Primary public DataSource idrDataSource() throws GlobalErrorInfoException{ DruidDataSource datasource = new DruidDataSource(); datasource.setDriverClassName(driverClassName); datasource.setUrl(url); datasource.setUsername(username); datasource.setPassword(password); return datasource;// return DataSourceBuilder.create().build(); } @Bean @Primary public SqlSessionFactory idrSqlSessionFactory(@Qualifier("idrDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource);
//此处是mapper文件的位置 bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml")); bean.setTypeAliasesPackage("mapper"); return bean.getObject(); } @Bean @Primary public DataSourceTransactionManager idrTransactionManager(@Qualifier("idrDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean @Primary public SqlSessionTemplate idrSqlSessionTemplate( @Qualifier("idrSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
2,配置文件 cms.properties
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=Test;applicationIntent=ReadWrite
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.username=admin
spring.datasource.password=123456
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
3,Dao.java文件(UserDao.java)
package com.xxx.cms.repository.ibatis; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; @Mapper public interface UserDao { //这种写法就是需要在xml中配置对应方法的 boolean updateAndInsert(); //这种带注解的是不需要在对应xml文件中写相应方法的 @Select("select count(1) from BC_QR_Scene") int findCount(); }
4,Mapper.xml文件(UserMapper.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.xxx.cms.repository.ibatis.UserDao" > <update id="updateAndInsert" parameterType="com.xxx.cms.entity.goods.User"> IF EXISTS(SELECT * FROM USER WHERE USER_ID = #{userId} ) BEGIN UPDATE USER SET UPDATE_TIME = GETDATE() WHERE USER_ID = #{userId} END ELSE BEGIN INSERT INTO USER VALUES (#{userId}, #{userId}, #{userId}, '0', getdate(), NULL) END </update> </mapper>
有了以上四个文件之后,就可以在项目中集成mybatis了
哈哈哈哈,你有没有发现我们标题写的是集成JPA与MyBatis,但是我们都没有怎,但是都没有怎么写JPA如何如何吗,不是我漏写了,因为我集成完了发现,根本没有JPA什么事,保持原状就好,两个是独立的,目前没有发现在会互相干扰的情况。
引用链接: