本篇内容主要讲解“如何编写mybatis-plus、hikariCP动态数据源”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何编写mybatis-plus、hikariCP动态数据源”吧!
创新互联建站长期为千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为格尔木企业提供专业的网站建设、网站设计,格尔木网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。
最近的项目使用多数据源,于是想起之前mybatis-plus系列的dynamic-datasource-springboot-starter 由于springboot2.x使用hikari作为默认数据库连接池,就是用springboot2.X+mybatis-plus3.x+dynamic-datasource-springboot-starter3.0.0进行了集成,代码如下 MybatisConfiguration.java
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
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
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
package com.share.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.share.domain.User; /** - @desc: - @author:caifan - @date:2020/3/15 - / public interface UserMapper extends BaseMapper{ }
UserRest.java
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
package com.share.service; import com.baomidou.dynamic.datasource.annotation.DS; import com.share.domain.User; import com.share.mapper.UserMapper; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @Service @Transactional @DS("ds1") public class UserService { @Resource private UserMapper userMapper; public void saveUser1(User user) { userMapper.insert(user); } public User findById(Long id) { return userMapper.selectById(id); } @DS("ds2") public void saveUser2(User user) { userMapper.insert(user); } }
MybatisApplication.java
package com.share; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan(value = {"com.share.mapper"}) public class MybatisApplication { public static void main(String[] args) { SpringApplication.run(MybatisApplication.class, args); } }
配置文件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
到此,相信大家对“如何编写mybatis-plus、hikariCP动态数据源”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
网站栏目:如何编写mybatis-plus、hikariCP动态数据源
文章起源:http://scyingshan.cn/article/jdggsi.html