In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to write mybatis-plus, hikariCP dynamic data sources", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write mybatis-plus and hikariCP dynamic data sources.
The most recent project uses multiple data sources, so I think of the previous dynamic-datasource-springboot-starter of mybatis-plus series. Since springboot2.x uses hikari as the default database connection pool, it is integrated with springboot2.X+mybatis-plus3.x+dynamic-datasource-springboot-starter3.0.0. The code is as follows: 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;@Configurationpublic class MybatisConfiguration {/ * paging plug-in Registration * @ 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;@Datapublic 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);}}
Profile application.properties
Server.port=9001mybatis-plus.global-config.db-config.id-type=automybatis-plus.global-config.db-config.logic-delete-field=delmybatis-plus.global-config.db-config.logic-delete-value=1mybatis-plus.global-config.db-config.logic-not-delete-value=0mybatis-plus.global-config.db-config.field-strategy=not_emptyspring.datasource.dynamic.primary=ds1spring.datasource.dynamic.datasource.ds1.username=rootspring.datasource.dynamic.datasource.ds1.password=123456spring.datasource.dynamic.datasource.ds1.url=jdbc: Mysql://localhost:3306/demo_ds_0spring.datasource.dynamic.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver# dynamic data Source configuration spring.datasource.dynamic.datasource.ds1.hikari.jdbc-url=jdbc:mysql://localhost:3306/demo_ds_0spring.datasource.dynamic.datasource.ds1.hikari.driver-class-name=com.mysql.jdbc.Driverspring.datasource.dynamic.datasource.ds1.hikari.username=rootspring.datasource.dynamic.datasource.ds1.hikari.password=123456spring.datasource.dynamic .datasource.ds1.hikari.connection-test-query=select 1 from dualspring.datasource.dynamic.datasource.ds1.hikari.is-auto-commit=truespring.datasource.dynamic.datasource.ds2.username=rootspring.datasource.dynamic.datasource.ds2.password=123456spring.datasource.dynamic.datasource.ds2.url=jdbc:mysql://localhost:3306/demo_ds_1spring.datasource.dynamic.datasource.ds2.driver-class-name=com.mysql.jdbc.Driverspring.datasource.dynamic.datasource.ds2.hikari.jdbc-url=jdbc:mysql://localhost:3306/demo_ Ds_1spring.datasource.dynamic.datasource.ds2.hikari.driver-class-name=com.mysql.jdbc.Driverspring.datasource.dynamic.datasource.ds2.hikari.username=rootspring.datasource.dynamic.datasource.ds2.hikari.password=123456 has come here I believe you have a deeper understanding of "how to write mybatis-plus, hikariCP dynamic data sources". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.