最近的项目使用多数据源,于是想起之前mybatis-plus系列的dynamic-datasource-springboot-starter
由于springboot2.x使用hikari作为默认数据库连接池,就是用springboot2.X+mybatis-plus3.x+dynamic-datasource-springboot-starter3.0.0进行了集成,代码如下
MybatisConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.share.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MybatisConfiguration { /** * 分页插件注册 * @return */ @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } |
BaseEntity.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
|
package com.share.domain; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import lombok.Data; import java.io.Serializable; import java.util.Date; @Data public class BaseEntity implements Serializable { private static final long serialVersionUID = 5741450406019549099L; @TableId (type = IdType.AUTO) private Long id; @TableField (value = "create_time" ) private Date createTime; @TableField (value = "update_time" ) private Date updateTime; @TableField (value = "delete_time" ) private Date deleteTime; } |
User.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
|
package com.share.domain; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.Version; import lombok.Data; import java.util.Date; @Data @TableName (value = "user" ) public class User extends BaseEntity { @Version private Integer version; private String username; private String nickname; private String password; private String email; private String phone; private Integer age; private Integer gender; private Integer status; @TableField (value = "last_login_time" ) private Date lastLoginTime; @TableLogic (value = "0" , delval = "1" ) private Integer del; } |
UserMapper.java
1 package com.share.mapper; 2 3 import com.baomidou.mybatisplus.core.mapper.BaseMapper; 4 import com.share.domain.User; 5 6 /** 7 * @desc: 8 * @author:caifan 9 * @date:2020/3/15 10 */ 11 public interface UserMapper extends BaseMapper<User> { 12 }
UserRest.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
|
package com.share.rest; import com.share.domain.User; import com.share.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController @RequestMapping ( "/rest/user" ) public class UserRest { @Autowired private UserService userService; @RequestMapping (value = "/save1" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public void save1( @RequestBody User user) { if ( null == user.getUpdateTime()) { user.setUpdateTime( new Date()); } userService.saveUser1(user); } @RequestMapping (value = "/save2" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) public void save2( @RequestBody User user) { if ( null == user.getUpdateTime()) { user.setUpdateTime( new Date()); } userService.saveUser2(user); } } |
UserService.java
1 package com.share.service; 2 3 import com.baomidou.dynamic.datasource.annotation.DS; 4 import com.share.domain.User; 5 import com.share.mapper.UserMapper; 6 import org.springframework.stereotype.Service; 7 import org.springframework.transaction.annotation.Transactional; 8 9 import javax.annotation.Resource; 10 11 @Service 12 @Transactional 13 @DS("ds1") 14 public class UserService { 15 @Resource 16 private UserMapper userMapper; 17 18 public void saveUser1(User user) { 19 userMapper.insert(user); 20 } 21 22 public User findById(Long id) { 23 return userMapper.selectById(id); 24 } 25 26 @DS("ds2") 27 public void saveUser2(User user) { 28 userMapper.insert(user); 29 } 30 31 }
MybatisApplication.java
1 package com.share; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 @SpringBootApplication 8 @MapperScan(value = {"com.share.mapper"}) 9 public class MybatisApplication { 10 public static void main(String[] args) { 11 SpringApplication.run(MybatisApplication.class, args); 12 } 13 }
配置文件application.properties
server.port=9001 mybatis-plus.global-config.db-config.id-type=auto mybatis-plus.global-config.db-config.logic-delete-field=del mybatis-plus.global-config.db-config.logic-delete-value=1 mybatis-plus.global-config.db-config.logic-not-delete-value=0 mybatis-plus.global-config.db-config.field-strategy=not_empty spring.datasource.dynamic.primary=ds1 spring.datasource.dynamic.datasource.ds1.username=root spring.datasource.dynamic.datasource.ds1.password=123456 spring.datasource.dynamic.datasource.ds1.url=jdbc:mysql://localhost:3306/demo_ds_0 spring.datasource.dynamic.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver #动态数据源配置 spring.datasource.dynamic.datasource.ds1.hikari.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_0 spring.datasource.dynamic.datasource.ds1.hikari.driver-class-name=com.mysql.jdbc.Driver spring.datasource.dynamic.datasource.ds1.hikari.username=root spring.datasource.dynamic.datasource.ds1.hikari.password=123456 spring.datasource.dynamic.datasource.ds1.hikari.connection-test-query=select 1 from dual spring.datasource.dynamic.datasource.ds1.hikari.is-auto-commit=true spring.datasource.dynamic.datasource.ds2.username=root spring.datasource.dynamic.datasource.ds2.password=123456 spring.datasource.dynamic.datasource.ds2.url=jdbc:mysql://localhost:3306/demo_ds_1 spring.datasource.dynamic.datasource.ds2.driver-class-name=com.mysql.jdbc.Driver spring.datasource.dynamic.datasource.ds2.hikari.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_1 spring.datasource.dynamic.datasource.ds2.hikari.driver-class-name=com.mysql.jdbc.Driver spring.datasource.dynamic.datasource.ds2.hikari.username=root spring.datasource.dynamic.datasource.ds2.hikari.password=123456