本文概览:配置mybatis和JPA两种数据源。
1 搭建一个工程
搭建一个springboot简单实例,可以参考
自动生成mybatis文件,可以参考
2 Mybatis配置
配置mybatis之后整个项目工程图如下:
2.1 配置文件
application.properties配置线程池的属性如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
spring.datasource.microservice.url = jdbc:mysql://localhost:3306/test?characterEncoding=gbk&zeroDateTimeBehavior=convertToNull&jdbcCompliantTruncation=false&autoReconnect=true spring.datasource.microservice.username = root spring.datasource.microservice.password = 53061208 spring.datasource.druid.driver-class-name = com.mysql.jdbc.Driver spring.datasource.druid.connect-properties.connectTimeout = 1000 spring.datasource.druid.connect-properties.socketTimeout = 5000 spring.datasource.druid.initial-size = 2 spring.datasource.druid.max-active = 10 spring.datasource.druid.min-idle = 2 spring.datasource.druid.max-wait = 1000 spring.datasource.druid.pool-prepared-statements = false spring.datasource.druid.max-pool-prepared-statement-per-connection-size = 20 spring.datasource.druid.validation-query = SELECT 'x' spring.datasource.druid.test-on-borrow = false spring.datasource.druid.test-on-return = false spring.datasource.druid.test-while-idle = true spring.datasource.druid.time-between-eviction-runs-millis = 60000 spring.datasource.druid.min-evictable-idle-time-millis = 300000 |
2.2 数据源配置类
数据源配置类似在xml配置DataSource、TransactionManager、sqlSessionFactory三个操作。如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
@Configuration @MapperScan(basePackages = {TestServiceDataSourceCofig.PACKAGE}, sqlSessionFactoryRef = "microServiceSqlSessionFactory") public class TestServiceDataSourceCofig { public static final String PACKAGE = "com.my.mapper.test"; private static final String MAPPER_LOCATION = "classpath*:/mapper/test/*.xml"; private static final String DATA_SOURCE_NAME = "microServiceDataSource"; private static final String TRANSACTION_MANAGER_NAME = "microServiceTransactionManager"; private static final String SQL_SESSION_FACTIORY_NAME = "microServiceSqlSessionFactory"; /** * 创建DataSource * @return */ @Bean(name = DATA_SOURCE_NAME, initMethod = "init", destroyMethod = "close") @ConfigurationProperties("spring.datasource.microservice") @Primary public DataSource microServiceDataSource() { return DruidDataSourceBuilder.create().build(); } /** * 创建TransactionManager * @param microServiceDataSource * @return */ @Bean(name = TRANSACTION_MANAGER_NAME) @Primary public DataSourceTransactionManager microServiceTransactionManager(@Qualifier(DATA_SOURCE_NAME) DataSource microServiceDataSource) { return new DataSourceTransactionManager(microServiceDataSource); } /** * 创建sqlSessionFactory * @param microServiceDataSource * @return * @throws Exception */ @Bean(name = SQL_SESSION_FACTIORY_NAME) @Primary public SqlSessionFactory microServiceSqlSessionFactory(@Qualifier(DATA_SOURCE_NAME) DataSource microServiceDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(microServiceDataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(MAPPER_LOCATION)); return sessionFactory.getObject(); } } |
2.3 使用mapper
1、相关的servce类如下
1 2 3 4 5 6 7 8 9 10 11 |
@Service public class StudentService { @Autowired private StudentMapper studentMapper; public List<Student> listAll(){ StudentExample example = new StudentExample(); StudentExample.Criteria criteria = example.createCriteria(); return studentMapper.selectByExample(example); } } |
2、使用该service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
@Controller public class HelloController { @Autowired private StudentService studentService; @RequestMapping("/testDao") @ResponseBody public ResponseDemo testDao(ModelMap modelMap) { ResponseDemo demo = new ResponseDemo(); demo.setMessage(studentService.listAll().toString()); demo.setResultCode("000"); return demo; } } |