springboot项目集成mybatis,且第三方依赖jar包service层需配置SqlSessionFactory或SqlSessionTemplate属性
application.yml文件需配置如下:
server:
port: 8081
servlet:
context-path: /项目名
logging:
level:
root: info
mybatis:
mapper-locations: classpath:mybatis/**/*.xml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://1.1.1.127:3306/数据库名?useUnicode=true&characterEncoding=UTF-8
username:用户名
password: 密码
type: com.alibaba.druid.pool.DruidDataSource
test-datasource:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: jdbc:mysql://1.1.1.128:3306/数据库名?useUnicode=true&characterEncoding=UTF-8
username: 用户名
password: 密码
type: com.alibaba.druid.pool.DruidDataSource
由于使用了两个数据库,所以要配置两个datasource配置类
DataSourceConfig.java
package com.test.common.datasource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
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.core.JdbcTemplate;
@Configuration
public class DataSourceConfig {
@Value("${mybatis.mapper-locations}")
private String mapperLocations;
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
/**
* 返回数据库的会话工厂
* @param ds
* @return
* @throws Exception
*/
@Bean
@Primary
public SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(ds);
// 加载MyBatis配置文件
PathMatchingResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
// 能加载多个,所以可以配置通配符(如:classpath*:mapper/**/*.xml)
bean.setMapperLocations(resourcePatternResolver.getResources(mapperLocations));
// 配置mybatis的config文件
// sqlSessionFactoryBean.setConfigLocation("mybatis-config.xml");
return bean.getObject();
}
/**
* 返回数据库的会话模板
* @param sessionFactory
* @return
* @throws Exception
*/
@Bean
@Primary
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sessionFactory) throws Exception{
return new SqlSessionTemplate(sessionFactory);
}
}
TestDataSourceConfig.java
package com.test.common.datasource;
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.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
@Configuration
@MapperScan(basePackages = "com.test.api.dao",sqlSessionFactoryRef = "testSqlSessionFactory")
public class TestDataSourceConfig {
@Bean(name = "testDatasource")
@ConfigurationProperties(prefix = "spring.test-datasource")
public DataSource testDatasource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "testJdbcTemplate")
public JdbcTemplate testJdbcTemplate(@Qualifier("basementDatasource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
/**
* 返回test数据库的会话工厂
* @param ds
* @return
* @throws Exception
*/
@Bean(name = "testSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDatasource") DataSource ds,@Value("classpath:mybatis/test/mybatis-test-apiMapper.xml")Resource[] mapperLocations) throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(ds);
bean.setMapperLocations(mapperLocations);
return bean.getObject();
}
/**
* 返回test数据库的会话模板
* @param sessionFactory
* @return
* @throws Exception
*/
@Bean(name = "testSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("testSqlSessionFactory") SqlSessionFactory sessionFactory) throws Exception{
return new SqlSessionTemplate(sessionFactory);
}
}
配置引入第三方依赖jar包service
testConfig.java
package com.test.common.utils;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.test.api.TestService;
import com.test.api.impl.TestServiceImpl;
import com.test.common.IdBuilder;
@Configuration
public class TestConfig {
@Bean
public TestService testService() {
return new TestServiceImpl();
}
@Bean
public IdBuilder idBuilder(DataSource dataSource) {
IdBuilder bean = new IdBuilder();
bean.setDataSource(dataSource);
bean.setIncrementerName("T_SEQUENCE");
bean.setColumnName("CURRENT_VAL");
bean.setCacheSize(10);
return bean;
}
}