当前位置:   article > 正文

springboot-mybatis整合多数据源之sqlserver_springboot 整合sqlserver 连接池

springboot 整合sqlserver 连接池

环境: intellij idea 2017.1.4 + spring boot 2.0

代码文档结构图:

注:此代码中的db1,db2为mysql数据源相关,db3为sqlserver数据源

2 数据库配置相关类

先在application.properties 文件中添加数据库的配置

  1. #spring.datasource.driverClassName=com.mysql.jdbc.Driver
  2. #spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
  3. #spring.datasource.username=root
  4. #spring.datasource.password=
  5. #mybatis.mapper-locations: classpath:mapper/*.xml
  6. ## db1 database
  7. spring.datasource.db1.jdbc-url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
  8. spring.datasource.db1.username=root
  9. spring.datasource.db1.password=
  10. spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
  11. ## db2 database
  12. spring.datasource.db2.jdbc-url=jdbc:mysql://localhost:3306/news?useUnicode=true&characterEncoding=UTF-8
  13. spring.datasource.db2.username=root
  14. spring.datasource.db2.password=
  15. spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
  16. ## db3 database
  17. spring.datasource.db3.jdbc-url=jdbc:sqlserver://ip:port;DatabaseName=dbname
  18. spring.datasource.db3.username=dbaccountname
  19. spring.datasource.db3.password=dbaccountpassowrd
  20. spring.datasource.db3.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
  21. ## Redis数据库索引(默认为0)
  22. spring.redis.database=0
  23. # Redis服务器地址
  24. spring.redis.host=127.0.0.1
  25. # Redis服务器连接端口
  26. spring.redis.port=6379
  27. # Redis服务器连接密码(默认为空)
  28. spring.redis.password=
  29. # 连接池最大连接数(使用负值表示没有限制)
  30. spring.redis.pool.max-active=200
  31. # 连接池最大阻塞等待时间(使用负值表示没有限制)
  32. spring.redis.pool.max-wait=-1
  33. # 连接池中的最大空闲连接
  34. spring.redis.pool.max-idle=10
  35. # 连接池中的最小空闲连接
  36. spring.redis.pool.min-idle=0
  37. # 连接超时时间(毫秒)
  38. spring.redis.timeout=3000

在datasouce目录下增加DataSourceConfig3文件,DataSourceConfig1,DataSourceConfig2略(资源代码里有),如下:

  1. package com.neo.datasource;
  2. import org.apache.ibatis.session.SqlSessionFactory;
  3. import org.mybatis.spring.SqlSessionFactoryBean;
  4. import org.mybatis.spring.annotation.MapperScan;
  5. import org.springframework.beans.factory.annotation.Qualifier;
  6. import org.springframework.boot.context.properties.ConfigurationProperties;
  7. import org.springframework.boot.jdbc.DataSourceBuilder;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  11. import org.springframework.core.io.support.ResourcePatternResolver;
  12. import org.springframework.jdbc.core.JdbcTemplate;
  13. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  14. import org.springframework.transaction.PlatformTransactionManager;
  15. import javax.sql.DataSource;
  16. @Configuration
  17. @MapperScan(basePackages = "com.neo.mapper.db3", sqlSessionFactoryRef = "db3SqlSessionTemplate")
  18. public class DataSourceConfig3 {
  19. @Bean(name = "sqlServerDataSource")
  20. @Qualifier("sqlServerDataSource")
  21. @ConfigurationProperties(prefix="spring.datasource.db3")
  22. public DataSource getMyDataSource(){
  23. return DataSourceBuilder.create().build();
  24. }
  25. @Bean(name = "secondaryJdbcTemplate")
  26. public JdbcTemplate secondaryJdbcTemplate(
  27. @Qualifier("sqlServerDataSource") DataSource dataSource) {
  28. return new JdbcTemplate(dataSource);
  29. }
  30. private static final String MAPPER_PATH = "classpath:mapper/db3/*.xml";
  31. private static final String ENTITY_PACKAGE = "com.neo.mapper.db3";
  32. @Bean(name = "db3SqlSessionTemplate")
  33. public SqlSessionFactory devSqlSessionFactory(
  34. @Qualifier("sqlServerDataSource") DataSource ddataSource)
  35. throws Exception {
  36. final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
  37. sessionFactory.setDataSource(ddataSource);
  38. ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
  39. sessionFactory.setMapperLocations(resolver.getResources(MAPPER_PATH));
  40. sessionFactory.setTypeAliasesPackage(ENTITY_PACKAGE);
  41. return sessionFactory.getObject();
  42. }
  43. @Bean
  44. public PlatformTransactionManager sqlServerTransactionManager(@Qualifier("sqlServerDataSource") DataSource sqlServerDataSource)
  45. {
  46. return new DataSourceTransactionManager(sqlServerDataSource);
  47. }
  48. }

实体表操作相关类文件

entity

  1. package com.neo.entity.db3;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class City {
  5. private int id;
  6. public int getId() {
  7. return id;
  8. }
  9. public void setId(int id) {
  10. this.id = id;
  11. }
  12. public String getName() {
  13. return Name;
  14. }
  15. public void setName(String name) {
  16. Name = name;
  17. }
  18. public int getParentId() {
  19. return ParentId;
  20. }
  21. public void setParentId(int parentId) {
  22. ParentId = parentId;
  23. }
  24. public int getSort() {
  25. return Sort;
  26. }
  27. public void setSort(int sort) {
  28. Sort = sort;
  29. }
  30. private String Name;
  31. private int ParentId;
  32. private int Sort;
  33. }

resource/mapper/db3/city.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.neo.mapper.db3.CityMapper">
  4. <select id="getCity" resultType="com.neo.entity.db3.City">
  5. select * from T_City
  6. <where>
  7. <if test="name != null">
  8. Name=#{name}
  9. </if>
  10. <if test="id!= null">
  11. and id=#{id}
  12. </if>
  13. </where>
  14. </select>
  15. <delete id="deleteCity" parameterType="Integer">
  16. delete from T_City where id =#{id}
  17. </delete>
  18. <insert id="addCity" parameterType="com.neo.entity.db3.City">
  19. insert into T_City(Name,ParentId,Sort)values(#{Name},#{ParentId},#{Sort})
  20. </insert>
  21. <update id="updateCity" parameterType="com.neo.entity.db3.City">
  22. update T_City
  23. <set>
  24. <if test = "name != null">
  25. city.name = #{name},
  26. </if>
  27. </set>
  28. where id = #{id}
  29. </update>
  30. </mapper>

mapper/db3/CityMapper

  1. package com.neo.mapper.db3;
  2. import com.neo.entity.db3.City;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import java.util.List;
  5. @Mapper
  6. public interface CityMapper {
  7. //获取列表
  8. public List<City> getCity() throws Exception;
  9. //根据id删除
  10. public void deleteCity(int id)throws Exception;
  11. //新增
  12. public void addCity(City city)throws Exception;
  13. //修改信息
  14. //public void updateCity(City city) throws Exception;
  15. }

CityService:

  1. package com.neo.service;
  2. import com.neo.entity.db3.City;
  3. import java.util.List;
  4. public interface CityService {
  5. //显示所有用户
  6. public List<City> getCity()throws Exception;
  7. //根据id删除用户
  8. public void deleteCity(int id)throws Exception;
  9. //新增用户
  10. public void addCity(City city)throws Exception;
  11. }
  1. package com.neo.service;
  2. import com.neo.entity.db3.City;
  3. import com.neo.mapper.db3.CityMapper;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import java.util.List;
  7. @Service
  8. public class CityServiceImpl implements CityService {
  9. @Autowired
  10. private CityMapper cityMapper;
  11. @Override
  12. public List<City> getCity() throws Exception {
  13. return cityMapper.getCity();
  14. }
  15. //根据id删除用户
  16. @Override
  17. public void deleteCity(int id) throws Exception {
  18. cityMapper.deleteCity(id);
  19. }
  20. //新增用户
  21. @Override
  22. public void addCity(City city) throws Exception {
  23. cityMapper.addCity(city);
  24. }
  25. }

controller

  1. package com.neo.controller;
  2. import com.neo.entity.db3.City;
  3. import com.neo.service.CityService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.List;
  8. @RestController
  9. public class CityController {
  10. @Autowired
  11. private CityService cityService;
  12. @Autowired
  13. private City city;
  14. //显示用户
  15. @RequestMapping("city/list")
  16. public List<City> index() throws Exception {
  17. return cityService.getCity();
  18. }
  19. //增加用户
  20. @RequestMapping("city/add")
  21. public String addCity() throws Exception {
  22. city.setName("test省份");
  23. city.setParentId(0);
  24. city.setSort(0);
  25. cityService.addCity(city);
  26. return "增加";
  27. }
  28. }

pom.xml如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.neo</groupId>
  6. <artifactId>spring-boot-hello</artifactId>
  7. <version>1.0</version>
  8. <packaging>jar</packaging>
  9. <name>spring-boot-hello</name>
  10. <description>Demo project for Spring Boot</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.0.RELEASE</version>
  15. </parent>
  16. <properties>
  17. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.mybatis.spring.boot</groupId>
  36. <artifactId>mybatis-spring-boot-starter</artifactId>
  37. <version>1.3.2</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>mysql</groupId>
  41. <artifactId>mysql-connector-java</artifactId>
  42. </dependency>
  43. <dependency>
  44. <groupId>junit</groupId>
  45. <artifactId>junit</artifactId>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.springframework.boot</groupId>
  49. <artifactId>spring-boot-starter-data-redis</artifactId>
  50. </dependency>
  51. <dependency>
  52. <groupId>com.microsoft.sqlserver</groupId>
  53. <artifactId>mssql-jdbc</artifactId>
  54. <version>6.4.0.jre8</version>
  55. <scope>runtime</scope>
  56. </dependency>
  57. </dependencies>
  58. <build>
  59. <plugins>
  60. <plugin>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-maven-plugin</artifactId>
  63. </plugin>
  64. </plugins>
  65. </build>
  66. </project>

至些所有的文件都ok了,编译运行 

http://localhost:8080/city/list

http://localhost:8080/list

都正常显示,如果数据库表里没有数据,可以用add方法先添加。

附完整测试项目包地址: https://download.csdn.net/download/huwei2003/11131140

--- end ---
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/520940
推荐阅读
相关标签
  

闽ICP备14008679号