Skip to content

Commit cc1771d

Browse files
committed
Spring Boot 2.x基础教程:MyBatis的多数据源配置
1 parent 5615863 commit cc1771d

File tree

14 files changed

+274
-65
lines changed

14 files changed

+274
-65
lines changed
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
package com.didispace.chapter36;
1+
package com.didispace.chapter39;
22

3-
import org.mybatis.spring.annotation.MapperScan;
43
import org.springframework.boot.SpringApplication;
54
import org.springframework.boot.autoconfigure.SpringBootApplication;
65

7-
@MapperScan("com.didispace.chapter36.mapper")
86
@SpringBootApplication
9-
public class Chapter36Application {
7+
public class Chapter39Application {
108

119
public static void main(String[] args) {
12-
SpringApplication.run(Chapter36Application.class, args);
10+
SpringApplication.run(Chapter39Application.class, args);
1311
}
1412

1513
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.didispace.chapter39;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.boot.jdbc.DataSourceBuilder;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.context.annotation.Primary;
8+
9+
import javax.sql.DataSource;
10+
11+
@Configuration
12+
public class DataSourceConfiguration {
13+
14+
@Primary
15+
@Bean
16+
@ConfigurationProperties(prefix = "spring.datasource.primary")
17+
public DataSource primaryDataSource() {
18+
return DataSourceBuilder.create().build();
19+
}
20+
21+
@Bean
22+
@ConfigurationProperties(prefix = "spring.datasource.secondary")
23+
public DataSource secondaryDataSource() {
24+
return DataSourceBuilder.create().build();
25+
}
26+
27+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.didispace.chapter39;
2+
3+
import org.apache.ibatis.session.SqlSessionFactory;
4+
import org.mybatis.spring.SqlSessionFactoryBean;
5+
import org.mybatis.spring.SqlSessionTemplate;
6+
import org.mybatis.spring.annotation.MapperScan;
7+
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
import javax.sql.DataSource;
12+
13+
@Configuration
14+
@MapperScan(
15+
basePackages = "com.didispace.chapter39.p",
16+
sqlSessionFactoryRef = "sqlSessionFactoryPrimary",
17+
sqlSessionTemplateRef = "sqlSessionTemplatePrimary")
18+
public class PrimaryConfig {
19+
20+
private DataSource primaryDataSource;
21+
22+
public PrimaryConfig(@Qualifier("primaryDataSource") DataSource primaryDataSource) {
23+
this.primaryDataSource = primaryDataSource;
24+
}
25+
26+
@Bean
27+
public SqlSessionFactory sqlSessionFactoryPrimary() throws Exception {
28+
SqlSessionFactory sessionFactory = null;
29+
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
30+
bean.setDataSource(primaryDataSource);
31+
return bean.getObject();
32+
}
33+
34+
@Bean
35+
public SqlSessionTemplate sqlSessionTemplatePrimary() throws Exception {
36+
return new SqlSessionTemplate(sqlSessionFactoryPrimary());
37+
}
38+
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.didispace.chapter39;
2+
3+
import org.apache.ibatis.session.SqlSessionFactory;
4+
import org.mybatis.spring.SqlSessionFactoryBean;
5+
import org.mybatis.spring.SqlSessionTemplate;
6+
import org.mybatis.spring.annotation.MapperScan;
7+
import org.springframework.beans.factory.annotation.Qualifier;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
import javax.sql.DataSource;
12+
13+
@Configuration
14+
@MapperScan(
15+
basePackages = "com.didispace.chapter39.s",
16+
sqlSessionFactoryRef = "sqlSessionFactorySecondary",
17+
sqlSessionTemplateRef = "sqlSessionTemplateSecondary")
18+
public class SecondaryConfig {
19+
20+
private DataSource secondaryDataSource;
21+
22+
public SecondaryConfig(@Qualifier("secondaryDataSource") DataSource secondaryDataSource) {
23+
this.secondaryDataSource = secondaryDataSource;
24+
}
25+
26+
@Bean
27+
public SqlSessionFactory sqlSessionFactorySecondary() throws Exception {
28+
SqlSessionFactory sessionFactory = null;
29+
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
30+
bean.setDataSource(secondaryDataSource);
31+
return bean.getObject();
32+
}
33+
34+
@Bean
35+
public SqlSessionTemplate sqlSessionTemplateSecondary() throws Exception {
36+
return new SqlSessionTemplate(sqlSessionFactorySecondary());
37+
}
38+
39+
}

2.1.x/chapter3-9/src/main/java/com/didispace/chapter39/mapper/UserMapper.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

2.1.x/chapter3-9/src/main/java/com/didispace/chapter39/entity/User.java renamed to 2.1.x/chapter3-9/src/main/java/com/didispace/chapter39/p/entity/UserPrimary.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
package com.didispace.chapter36.entity;
1+
package com.didispace.chapter39.p.entity;
22

33
import lombok.Data;
44
import lombok.NoArgsConstructor;
55

66

77
@Data
88
@NoArgsConstructor
9-
public class User {
9+
public class UserPrimary {
1010

1111
private Long id;
1212

1313
private String name;
1414
private Integer age;
1515

16-
public User(String name, Integer age) {
16+
public UserPrimary(String name, Integer age) {
1717
this.name = name;
1818
this.age = age;
1919
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.didispace.chapter39.p.mapper;
2+
3+
import com.didispace.chapter39.p.entity.UserPrimary;
4+
import org.apache.ibatis.annotations.Delete;
5+
import org.apache.ibatis.annotations.Insert;
6+
import org.apache.ibatis.annotations.Param;
7+
import org.apache.ibatis.annotations.Select;
8+
9+
/**
10+
* Created by 程序猿DD/翟永超 on 2020/2/28.
11+
* <p>
12+
* Blog: http://blog.didispace.com/
13+
* Github: https://github.com/dyc87112/
14+
*/
15+
public interface UserMapperPrimary {
16+
17+
@Select("SELECT * FROM USER WHERE NAME = #{name}")
18+
UserPrimary findByName(@Param("name") String name);
19+
20+
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
21+
int insert(@Param("name") String name, @Param("age") Integer age);
22+
23+
@Delete("DELETE FROM USER")
24+
int deleteAll();
25+
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.didispace.chapter39.s.entity;
2+
3+
import lombok.Data;
4+
import lombok.NoArgsConstructor;
5+
6+
7+
@Data
8+
@NoArgsConstructor
9+
public class UserSecondary {
10+
11+
private Long id;
12+
13+
private String name;
14+
private Integer age;
15+
16+
public UserSecondary(String name, Integer age) {
17+
this.name = name;
18+
this.age = age;
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.didispace.chapter39.s.mapper;
2+
3+
import com.didispace.chapter39.s.entity.UserSecondary;
4+
import org.apache.ibatis.annotations.Delete;
5+
import org.apache.ibatis.annotations.Insert;
6+
import org.apache.ibatis.annotations.Param;
7+
import org.apache.ibatis.annotations.Select;
8+
9+
/**
10+
* Created by 程序猿DD/翟永超 on 2020/2/28.
11+
* <p>
12+
* Blog: http://blog.didispace.com/
13+
* Github: https://github.com/dyc87112/
14+
*/
15+
public interface UserMapperSecondary {
16+
17+
@Select("SELECT * FROM USER WHERE NAME = #{name}")
18+
UserSecondary findByName(@Param("name") String name);
19+
20+
@Insert("INSERT INTO USER(NAME, AGE) VALUES(#{name}, #{age})")
21+
int insert(@Param("name") String name, @Param("age") Integer age);
22+
23+
@Delete("DELETE FROM USER")
24+
int deleteAll();
25+
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
spring.datasource.url=jdbc:mysql://localhost:3306/test
2-
spring.datasource.username=root
3-
spring.datasource.password=12345678
4-
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
1+
# pring boot 1.x的配置:spring.datasource.primary.url=jdbc:mysql://localhost:3306/test1
2+
spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/test1
3+
spring.datasource.primary.username=root
4+
spring.datasource.primary.password=12345678
5+
spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver
56

6-
mybatis.mapper-locations=classpath:mapper/*.xml
7+
# spring boot 1.x的配置:spring.datasource.secondary.url=jdbc:mysql://localhost:3306/test2
8+
spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/test2
9+
spring.datasource.secondary.username=root
10+
spring.datasource.secondary.password=12345678
11+
spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver
12+
13+
#mybatis.mapper-locations=classpath:mapper/*.xml

0 commit comments

Comments
 (0)