Commit 8055e953 by fengshuonan

修复多数据源不能使用的bug

parent 0ae277b9
package com.stylefeng.guns.core.config; package com.stylefeng.guns.config.datasource;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor; import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor;
......
package com.stylefeng.guns.core.config; package com.stylefeng.guns.config.datasource;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor; import com.baomidou.mybatisplus.plugins.OptimisticLockerInterceptor;
......
...@@ -30,7 +30,7 @@ mybatis-plus: ...@@ -30,7 +30,7 @@ mybatis-plus:
typeAliasesPackage: com.stylefeng.guns.modular.system.model typeAliasesPackage: com.stylefeng.guns.modular.system.model
log: log:
path: /opt/guns/logs path: guns-logs
--- ---
...@@ -57,8 +57,8 @@ spring: ...@@ -57,8 +57,8 @@ spring:
#多数据源情况的配置 #多数据源情况的配置
guns: guns:
muti-datasource: muti-datasource:
open: false open: true
url: jdbc:mysql://127.0.0.1:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=UTC url: jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=UTC
username: root username: root
password: root password: root
dataSourceNames: dataSourceNames:
......
package com.stylefeng.guns.multi.entity;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.enums.IdType;
import java.io.Serializable;
/**
* <p>
*
* </p>
*
* @author fengshuonan
* @since 2018-07-10
*/
public class Test implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "aaa", type = IdType.AUTO)
private Integer aaa;
private String bbb;
public Integer getAaa() {
return aaa;
}
public void setAaa(Integer aaa) {
this.aaa = aaa;
}
public String getBbb() {
return bbb;
}
public void setBbb(String bbb) {
this.bbb = bbb;
}
@Override
public String toString() {
return "Test{" +
"aaa=" + aaa +
", bbb=" + bbb +
"}";
}
}
package com.stylefeng.guns.multi.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.stylefeng.guns.multi.entity.Test;
/**
* <p>
* Mapper 接口
* </p>
*
* @author fengshuonan
* @since 2018-07-10
*/
public interface TestMapper extends BaseMapper<Test> {
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="share.example.modular.mapper.TestMapper">
<!-- 开启二级缓存 -->
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.stylefeng.guns.multi.entity.Test">
<id column="aaa" property="aaa" />
<result column="bbb" property="bbb" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
aaa, bbb
</sql>
</mapper>
package com.stylefeng.guns.multi.service;
/**
* <p>
* 服务类
* </p>
*
* @author fengshuonan
* @since 2018-07-10
*/
public interface TestService {
/**
* 测试多数据源的业务
*
* @author stylefeng
* @Date 2017/6/23 23:02
*/
void testBiz();
/**
* 测试多数据源的业务
*
* @author stylefeng
* @Date 2017/6/23 23:02
*/
void testGuns();
/**
* 测试多数据源的业务
*
* @author stylefeng
* @Date 2017/6/23 23:02
*/
void testAll();
}
package com.stylefeng.guns.multi.service.impl;
import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.multi.entity.Test;
import com.stylefeng.guns.multi.mapper.TestMapper;
import com.stylefeng.guns.multi.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* <p>
* 服务实现类
* </p>
*
* @author fengshuonan
* @since 2018-07-10
*/
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestMapper testMapper;
@Override
@DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
@Transactional
public void testBiz() {
Test test = new Test();
test.setBbb("bizTest");
testMapper.insert(test);
}
@Override
@DataSource(name = DatasourceEnum.DATA_SOURCE_GUNS)
@Transactional
public void testGuns() {
Test test = new Test();
test.setBbb("gunsTest");
testMapper.insert(test);
}
@Override
@Transactional
public void testAll() {
testBiz();
testGuns();
//int i = 1 / 0;
}
}
package com.stylefeng.guns.multi.test;
import com.stylefeng.guns.base.BaseJunit;
import com.stylefeng.guns.multi.service.TestService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* 业务测试
*
* @author fengshuonan
* @date 2017-06-23 23:12
*/
public class BizTest extends BaseJunit {
@Autowired
TestService testService;
@Test
public void test() {
//testService.testGuns();
testService.testBiz();
//testService.testAll();
}
}
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50719
Source Host : localhost:3306
Source Schema : guns
Target Server Type : MySQL
Target Server Version : 50719
File Encoding : 65001
Date: 10/07/2018 14:09:19
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
`aaa` int(11) NOT NULL AUTO_INCREMENT,
`bbb` varchar(255) DEFAULT NULL,
PRIMARY KEY (`aaa`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
SET FOREIGN_KEY_CHECKS = 1;
...@@ -2,7 +2,5 @@ ...@@ -2,7 +2,5 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.stylefeng.guns.core.base.controller.GlobalController,\ com.stylefeng.guns.core.base.controller.GlobalController,\
com.stylefeng.guns.core.config.DefaultFastjsonConfig,\ com.stylefeng.guns.core.config.DefaultFastjsonConfig,\
com.stylefeng.guns.core.config.MultiDataSourceConfig,\
com.stylefeng.guns.core.config.SingleDataSourceConfig,\
com.stylefeng.guns.core.config.DefaultProperties,\ com.stylefeng.guns.core.config.DefaultProperties,\
com.stylefeng.guns.core.config.DefaultWebConfig com.stylefeng.guns.core.config.DefaultWebConfig
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment