一、 在application.properties中设置数据源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#设置Tomcat端口,默认8080
server.port=8080
#设置项目ContextPath
server.context-path=/
#设置Tomcat编码
server.tomcat.uri-encoding=UTF-8
#设置视图解析器路径
spring.mvc.view.prefix=/WEB-INF/views/
#设置视图解析器后缀
spring.mvc.view.suffix=.jsp
#数据库配置mybatis generator
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?setUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
#数据库配置
spring.datasource.test.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.test.jdbc-url=jdbc:mysql://localhost:3306/mybatis
spring.datasource.test.username=root
spring.datasource.test.password=root
#配置.xml文件路径
mybatis.mapper-locations=classpath:/com/kai/demo/mapper/*.xml
#配置模型路径
mybatis.type-aliases-package=com.kai.demo.model
二、DataSource创建,DataSourceConfig.java
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
@Configuration
@MapperScan(basePackages = "com.kai.demo.dao")
@Primary
@PropertySource("classpath:application.properties")
public class DataSourceConfig {
//mybatis 的mapper配置文件地址
@Value("${mybatis.mapper-locations}")
private String mybatisMapper;
@Primary
@Bean(name = "testDataSource")
@ConfigurationProperties(prefix = "spring.datasource.test")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "testSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("testDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mybatisMapper));
try {
return bean.getObject();
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Primary
@Bean(name = "testTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("testDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "testSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("testSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
指定要扫描的Mapper类的包的路径,如果不指定,需要在每个Mapper类里添加@Mapper注解
1
@MapperScan(basePackages = "com.kai.demo.dao")
指定配置文件地址,配置文件是application.properties时,可以省略
1
@PropertySource("classpath:application.properties")
当有多个数据源配置是,使用@Primary指定当前数据库为主要的数据源
指名用的是哪个数据源,testDataSource为DataSourceConfg开始创建的数据源
1
@Qualifier("testDataSource")
进行了自定义的DataSource的话,Application.java 中需要加(exclude= {DataSourceAutoConfiguration.class})来排除掉自动配置的DataSource
1
@EnableAutoConfiguration(exclude= {DataSourceAutoConfiguration.class})
三、如果使用的Spring Boot自动配置的DataSource,只需要进行MapperLocation配置就可使用Mybatis了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Configuration
@MapperScan(basePackages = "com.kai.demo.dao")
@Primary
public class DefaultDataSource {
//mybatis 的mapper配置文件地址
@Value("${mybatis.mapper-locations}")
private String mybatisMapper;
@Bean
public SqlSessionFactory setSqlSessionFactory(DataSource dataSource) throws IOException {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(mybatisMapper));
try {
return bean.getObject();
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
这个时候Appliation.java中就不能有(exclude= {DataSourceAutoConfiguration.class})
完整环境下载地址:
https://github.com/CatherineHu/Spring-Boot-Mybatis-MVC