管理后台(guns的获取与修改)

背景

最近有个小项目,需要一个管理后台,以前的由于技术层太旧了而且其它依赖比较多,不想带过来,于是想趁着新项目,试一下一些开源的业务框架.

后台管理系统相关介绍与guns的说明

jeecg,jeesite以前经常听到,去下载来看了,发现jeecg设计的过于自动了,都不太清楚怎么开发新的业务
jeesite还不错,有明显暴露的web编写,其界面和使用体验也真不错,不过其表要按其标准来设计.而且底层代码不开源
guns 比较新,也比较简单,其代码全部开源,没有上面的两个那么完善(其实还差得有点远),不过重在简单易看好修改,所以选择这个

其它还有基于spring cloud等的开源框架,考虑到我们只是刚开始,所以没去选择

guns分析与其适应性修改

现在时间是2018/12,guns的作者把其功能 拆分了多个模块依赖
管理后台的业务主要是 https://gitee.com/stylefeng/guns
底层依赖是rose-kernel https://gitee.com/stylefeng-Roses/roses-kernel
其次,是生成器模块https://gitee.com/stylefeng/guns-generator

其代码比较 手动,均需要自己编写.
在这里插入图片描述

适应性修改

如果在使用过程中,希望项目能更扩展性好一些,可以把其 依赖的 底层业务模块加到 系统中.
新建一个空的maven project
把下面几个项目引进来

采用maven的父子模块方式,把各依赖均加到自己的服务中

drwxr-xr-x 1 aguns 1049089    0 12月 14 14:26 guns/
drwxr-xr-x 1 aguns 1049089    0 12月 14 14:26 guns-generator/
drwxr-xr-x 1 aguns 1049089    0 12月 14 14:26 kernel-core/
drwxr-xr-x 1 aguns 1049089    0 12月 14 14:26 kernel-model/
-rw-r--r-- 1 aguns 1049089 6840 12月 14 10:11 pom.xml

/pom.xml 中,这一部分是copyhttps://gitee.com/stylefeng-Roses/roses-kernel.git 中的/pom.xml,有所修改,

   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <groupId>com.mytest.manage</groupId>
    <artifactId>backguns</artifactId>
    <packaging>pom</packaging>

    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>kernel-model</module>
        <module>kernel-core</module>
        <module>guns</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
        <mp.springboot.version>2.3</mp.springboot.version>
        <fastjson.version>1.2.47</fastjson.version>
        <druid.version>1.1.10</druid.version>
        <kaptcha.version>2.3.2</kaptcha.version>
        <jwt.version>0.9.1</jwt.version>
        <hutool.version>4.1.2</hutool.version>
        <spring.boot.admin>2.0.1</spring.boot.admin>
        <lombok.versin>1.18.0</lombok.versin>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--spring cloud-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!--数据库-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>${mp.springboot.version}</version>
            </dependency>
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus</artifactId>
                <version>${mp.springboot.version}</version>
            </dependency>

            <!--工具类-->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-core</artifactId>
                <version>${hutool.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>

            <!--验证码-->
            <dependency>
                <groupId>com.github.penggle</groupId>
                <artifactId>kaptcha</artifactId>
                <version>${kaptcha.version}</version>
            </dependency>

            <!--jwt-->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>${jwt.version}</version>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.versin}</version>
            </dependency>

            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-server</artifactId>
                <version>${spring.boot.admin}</version>
            </dependency>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-server-ui</artifactId>
                <version>${spring.boot.admin}</version>
            </dependency>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-starter-client</artifactId>
                <version>${spring.boot.admin}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <additionalparam>-Xdoclint:none</additionalparam>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

kernel-model/pom.xml

<parent>
        <artifactId>backguns</artifactId>
        <groupId>com.mytest.manage</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>kernel-model</artifactId>
    <packaging>jar</packaging>
    <version>v201812</version>

    <description>一些通用实体,枚举,异常等规范</description>

    <dependencies>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--注意provided-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

kernel-core/pom.xml

<parent>
        <artifactId>backguns</artifactId>
        <groupId>com.mytest.manage</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>kernel-core</artifactId>
    <version>v201812</version>

    <packaging>jar</packaging>
    <description>核心包,被其他模块引用</description>

    <dependencies>
        <dependency>
            <groupId>com.mytest.manage</groupId>
            <artifactId>kernel-model</artifactId>
            <version>v201812</version>
        </dependency>

        <!--工具类-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-core</artifactId>
        </dependency>

        <!--数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>

        <!--工具类-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>

        <!--aop-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!--feign远程调用,**注意可选**-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--缓存,**注意可选**-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--web ,**注意可选**-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--缓存,**注意可选**-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

    </dependencies>

guns-generator/pom.xml

<groupId>cn.stylefeng.guns</groupId>
    <artifactId>guns-generator</artifactId>
    <version>v201812</version>

    <packaging>jar</packaging>

    <name>guns-generator</name>
    <description>代码生成器</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.mytest.manage</groupId>
            <artifactId>kernel-core</artifactId>
            <version>201812</version>
        </dependency>

        <dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetl</artifactId>
            <version>2.9.3</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generate</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.0.1</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <additionalparam>-Xdoclint:none</additionalparam>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

guns/pom.xml

<parent>
        <artifactId>backguns</artifactId>
        <groupId>com.mytest.manage</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>cn.stylefeng.guns</groupId>
    <artifactId>guns</artifactId>
    <version>1.0.0</version>

    <name>guns-admin</name>
    <description>mytest后台(guns 的spring boot版本)</description>

    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <shiro.version>1.4.0</shiro.version>
        <kaptcha.version>2.3.2</kaptcha.version>
        <ehcache.version>3.3.1</ehcache.version>
        <beetl.version>2.9.3</beetl.version>
        <swagger.version>2.9.2</swagger.version>
        <ehcache.core.version>2.6.11</ehcache.core.version>
        <mysql-connector-java.version>8.0.11</mysql-connector-java.version>
        <jwt.version>0.9.0</jwt.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>cn.stylefeng.guns</groupId>
            <artifactId>guns-generator</artifactId>
            <version>v201812</version>
        </dependency>

        <!--核心组件-->
        <dependency>
            <groupId>com.mytest.manage</groupId>
            <artifactId>kernel-core</artifactId>
            <version>v201812</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
        </dependency>

        <!--spring boot依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <!--shiro依赖和缓存-->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>${shiro.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>${shiro.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>${ehcache.version}</version>
        </dependency>

        <!--验证码-->
        <dependency>
            <groupId>com.github.penggle</groupId>
            <artifactId>kaptcha</artifactId>
            <version>${kaptcha.version}</version>
        </dependency>

        <!--beetl模板引擎-->
        <dependency>
            <groupId>com.ibeetl</groupId>
            <artifactId>beetl</artifactId>
            <version>${beetl.version}</version>
        </dependency>

        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>

        <!--jwt token-->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>${jwt.version}</version>
        </dependency>

        <!--需要分布式session的话需要放开注释-->
        <!--<dependency>-->
        <!--<groupId>org.springframework.session</groupId>-->
        <!--<artifactId>spring-session-data-redis</artifactId>-->
        <!--</dependency>-->
        <!--<dependency>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-data-redis</artifactId>-->
        <!--</dependency>-->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork><!-- 如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    </build>

    <profiles>
        <profile>
            <id>local</id>
            <properties>
                <spring.active>local</spring.active>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <spring.active>dev</spring.active>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <spring.active>test</spring.active>
            </properties>
        </profile>
        <profile>
            <id>produce</id>
            <properties>
                <spring.active>produce</spring.active>
            </properties>
        </profile>
    </profiles>

怎么使用生成代码的工具?

代码生成菜单:

在这里插入图片描述

随后,在生成的代码中找到 TUser.sql,里面有增加到菜单 的语句(建议 )
,或者,用下面的方式:增加到菜单,
在这里插入图片描述

再分配菜单到角色管理员中,然后重启刷新即可

双数据库?

guns已经有相应的数据源处理了,但我都调不通,后来发现有个要修改的地方:

如果在做多数据源的时候,有下面这样的配置,则spring会自己生成一个bean名为datasource的对象,

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
    username: root
    password: root
    filters: wall,mergeStat

解决:
可以把上面的spring: 去掉改成自定义的,即把默认数据源给去掉!!

biz:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
    username: root
    password: root
    filters: wall,mergeStat

当然,相应的properties配置也要改!!!

@Bean
@ConfigurationProperties(prefix = "biz.datasource")
public DruidProperties druidProperties() {
    return new DruidProperties();
}

原因解释:
在com.baomidou.mybatisplus.spring.boot.starter.MybatisPlusAutoConfiguration中有如下代码:

@Bean
@ConditionalOnMissingBean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
    MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();

由于使用了spring默认的配置,则这个datasource不是我们想要的 DynamicDataSource
如果自己配置的DynamicDataSource对象也叫datasource,则自己的对象不会执行导致后续请求总是会走 默认数据源!!!

更合理的双数据源修改

作者之前的双数据源不太友好,这里修改成如下配置,

#多数据源情况的配置
guns:
  muti-datasource:
    open: true
    datasources[0]:
      name: dataSourceGuns
      url: jdbc:mysql://127.0.0.1:3306/guns?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
      username: mytest
      password: mytest
      filters: wall,mergeStat
    datasources[1]:
      name: dataSourceBiz
      url: jdbc:mysql://127.0.0.1:3306/mytest?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
      username: mytest
      password: mytest

然后配置代码也有相应的修改

@Configuration
@ConditionalOnProperty(prefix = "guns.muti-datasource", name = "open", havingValue = "true")
@EnableTransactionManagement(order = 2, proxyTargetClass = true)
@MapperScan(basePackages = {"cn.stylefeng.guns.modular.*.dao", "cn.stylefeng.guns.modular.system.dao.mapping"})
public class MultiDataSourceConfig {

    private  static Logger log = LoggerFactory.getLogger(MultiDataSourceConfig.class);



    /**
     * druid配置
     */
    @Bean
    @ConfigurationProperties(prefix = "bizs.datasource")
    public DruidProperties druidProperties() {
        return new DruidProperties();
    }



    /**
     * 多数据源切换的aop
     */
    @Bean
    public MultiSourceExAop multiSourceExAop() {
        return new MultiSourceExAop();
    }

    /**
     * 生成DruidDataSource数据源
     */
    private DruidDataSource dataSource(DruidProperties druidProperties) {
        DruidDataSource dataSource = new DruidDataSource();
        druidProperties.config(dataSource);
        return dataSource;
    }


    @Autowired
    private  MutiDataSourceListProperties mutiDataSourceListProperties;

    @Bean
    @ConfigurationProperties(prefix = "guns.muti-datasource")
    public MutiDataSourceListProperties mutiDataSourceProperties() {
        return new MutiDataSourceListProperties();
    }

    /**
     * 多数据源连接池配置
     */
    @Bean
    public DynamicDataSource mutiDataSource() {

        //获取properteis 生成 datasourcelist
        HashMap<Object, Object> hashMap = new HashMap<>();
        DruidDataSource defaultDatasource = null;
        for (DruidProperties druidProperties : mutiDataSourceListProperties.getDatasources()) {
            DruidDataSource currentDatasource = dataSource(druidProperties);
            try {
                currentDatasource.init();
            } catch (SQLException sql) {
                sql.printStackTrace();
            }

            if (defaultDatasource == null) {
                log.info("这里设置数组第一个为默认,其url为:"+druidProperties.getUrl());
                defaultDatasource = currentDatasource;
            }

            hashMap.put(druidProperties.getName(), currentDatasource);
        }
        //加到 DynamicDataSource中
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        dynamicDataSource.setTargetDataSources(hashMap);
        dynamicDataSource.setDefaultTargetDataSource(defaultDatasource);
        return dynamicDataSource;
    }

...
}

多数据源配置

/**
 * 多数据源配置
 */
public class MutiDataSourceListProperties {

    private boolean open;

    public boolean isOpen() {
        return open;
    }

    public void setOpen(boolean open) {
        this.open = open;
    }

    private List<DruidProperties> datasources;

    public List<DruidProperties> getDatasources() {
        return datasources;
    }

    public void setDatasources(List<DruidProperties> datasources) {
        this.datasources = datasources;
    }
}

说明

guns是比较新的业务架子,也比较简单而且全部开源,
感谢作者的开源,非常便利,希望大家能给这个项目多多贡献
附项目链接
https://gitee.com/stylefeng/guns



























评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值