wjj多数据源,spring 、springMVC、mybatis、MySQL、MySQL。。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <artifactId>wjj</artifactId>
        <groupId>com.hstd</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>mds</artifactId>
    <packaging>war</packaging>
    <name>mds Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <finalName>mds</finalName>
    </build>
</project>
#============================#
#===== Database sttings =====#
#============================#


jdbc1.driver=com.mysql.jdbc.Driver
jdbc1.url=jdbc:mysql://172.17.43.156:3306/dlsqwjj?useUnicode=true&characterEncoding=utf-8zeroDateTimeBehavior=convertToNull
jdbc1.username=dlsqwjj
jdbc1.password=wjj1234

jdbc1.pool.init=1
jdbc1.pool.minIdle=3
jdbc1.pool.maxActive=20

jdbc1.testSql=SELECT 'x' FROM DUAL
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd


      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"
       default-lazy-init="true">

    <description>Spring Configuration</description>

    <!-- 加载配置属性文件 -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:mds.properties"/>

    <!-- 数据源配置, 使用 BoneCP 数据库连接池 -->
    <bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->
        <property name="driverClassName" value="${jdbc1.driver}"/>

        <!-- 基本属性 url、user、password -->
        <property name="url" value="${jdbc1.url}"/>
        <property name="username" value="${jdbc1.username}"/>
        <property name="password" value="${jdbc1.password}"/>

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="${jdbc1.pool.init}"/>
        <property name="minIdle" value="${jdbc1.pool.minIdle}"/>
        <property name="maxActive" value="${jdbc1.pool.maxActive}"/>

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000"/>

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>

        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000"/>

        <property name="validationQuery" value="${jdbc1.testSql}"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>

        <!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用)
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->

        <!-- 配置监控统计拦截的filters -->
        <property name="filters" value="stat"/>
    </bean>

    <bean id="dataSourceAspect" class="com.hstd.jeeh5.modules.mds.service.DataSourceAspect"/>
    <aop:config>
        <aop:aspect ref="dataSourceAspect" order="-1">
            <!-- 拦截所有service方法 -->
            <aop:pointcut id="dataSourcePointcut" expression="execution(* com.hstd.jeeh5.modules.*.dao.*.*(..))"/>
            <aop:before pointcut-ref="dataSourcePointcut" method="intercept"/>
            <aop:after pointcut-ref="dataSourcePointcut" method="after"/>
        </aop:aspect>
    </aop:config>
    <!--多数据源要删除 spring-context-shiro.xml aop 配置-->
    <aop:aspectj-autoproxy/>


    <bean id="dynamicDataSource" class="com.hstd.jeeh5.modules.mds.utils.DynamicDataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <!-- 指定lookupKey和与之对应的数据源 -->
                <entry key="dataSource" value-ref="dataSource"></entry>
                <entry key="dataSource1" value-ref="dataSource1"></entry>
            </map>
        </property>
        <!-- 这里可以指定默认的数据源 -->
        <property name="defaultTargetDataSource" ref="dataSource" />
    </bean>
</beans>
package com.hstd.jeeh5.modules.mds.service;

import com.hstd.jeeh5.modules.mds.utils.DataSource;
import com.hstd.jeeh5.modules.mds.utils.DynamicDataSourceHolder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;


public class DataSourceAspect {

    /**
     * 拦截目标方法,获取由@DataSource指定的数据源标识,设置到线程存储中以便切换数据源
     *
     * @param point
     * @throws Exception
     */
    public void intercept(JoinPoint point) throws Exception {
        System.out.println("==========================================================================================:"+point.getTarget().getClass());
        Class<?> target = point.getTarget().getClass();
        MethodSignature signature = (MethodSignature) point.getSignature();
        // 默认使用目标类型的注解,如果没有则使用其实现接口的注解
        for (Class<?> clazz : target.getInterfaces()) {
            resolveDataSource(clazz, signature.getMethod());
        }
        resolveDataSource(target, signature.getMethod());
    }

    /**
     * 提取目标对象方法注解和类型注解中的数据源标识
     *
     * @param clazz
     * @param method
     */
    private void resolveDataSource(Class<?> clazz, Method method) {
        try {
            Class<?>[] types = method.getParameterTypes();
            // 默认使用类型注解
            if (clazz.isAnnotationPresent(DataSource.class)) {
                DataSource source = clazz.getAnnotation(DataSource.class);
                DynamicDataSourceHolder.setDataSource(source.value());
            }
            // 方法注解可以覆盖类型注解
            Method m = clazz.getMethod(method.getName(), types);
            if (m != null && m.isAnnotationPresent(DataSource.class)) {
                DataSource source = m.getAnnotation(DataSource.class);
                DynamicDataSourceHolder.setDataSource(source.value());
            }
        } catch (Exception e) {
            System.out.println(clazz + ":" + e.getMessage());
        }
    }


    public void after(){
        System.out.println("after");
        DynamicDataSourceHolder.clearDataSource();
    }
}
package com.hstd.jeeh5.modules.mds.utils;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({TYPE,METHOD})
@Retention(RUNTIME)
public @interface DataSource {
    String value();
}
package com.hstd.jeeh5.modules.mds.utils;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        // 从自定义的位置获取数据源标识
        System.out.println("00000:"+DynamicDataSourceHolder.getDataSource());
        return DynamicDataSourceHolder.getDataSource();
    }

}
package com.hstd.jeeh5.modules.mds.utils;

public class DynamicDataSourceHolder {
    /**
     * 注意:数据源标识保存在线程变量中,避免多线程操作数据源时互相干扰
     */
    private static final ThreadLocal<String> THREAD_DATA_SOURCE = new ThreadLocal<String>();

    public static String getDataSource() {
        return THREAD_DATA_SOURCE.get();
    }

    public static void setDataSource(String dataSource) {
        THREAD_DATA_SOURCE.set(dataSource);
    }

    public static void clearDataSource() {
        THREAD_DATA_SOURCE.remove();
    }

}
#============================#
#===== Database sttings =====#
#============================#

#mysql database setting
jdbc.type=mysql
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://10.25.101.234:3306/wjj?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root


jdbc1.driver=com.mysql.jdbc.Driver
jdbc1.url=jdbc:mysql://172.17.43.156:3306/dlsqwjj?useUnicode=true&characterEncoding=utf-8
jdbc1.username=dlsqwjj
jdbc1.password=wjj1234



#####dasdjjddnncnncnnnnnnnnnnnnnnnnnnnnnnnnnnasdjd!
#pool settings
jdbc.pool.init=1
jdbc.pool.minIdle=3
jdbc.pool.maxActive=20

#jdbc.testSql=SELECT 'x'
jdbc.testSql=SELECT 'x' FROM DUAL

#redis settings
redis.keyPrefix==JEEH5
redis.host=127.0.0.1
redis.port=6379

#============================#
#===== System settings ======#
#============================#

#\u4EA7\u54C1\u4FE1\u606F\u8BBE\u7F6E
productName=JEEH5
copyrightYear=2015
version=V1.2.6

#\u6F14\u793A\u6A21\u5F0F: \u4E0D\u80FD\u64CD\u4F5C\u548C\u4FDD\u5B58\u7684\u6A21\u5757\uFF1A sys: area/office/user/role/menu/dict, cms: site/category
demoMode=false

#\u7BA1\u7406\u57FA\u7840\u8DEF\u5F84, \u9700\u540C\u6B65\u4FEE\u6539\uFF1Aweb.xml
adminPath=/a

#\u524D\u7AEF\u57FA\u7840\u8DEF\u5F84
frontPath=

#\u7F51\u7AD9URL\u540E\u7F00
urlSuffix=.jhtml

#\u662F\u5426\u4E0D\u5141\u8BB8\u5237\u65B0\u4E3B\u9875\uFF0C\u4E0D\u5141\u8BB8\u60C5\u51B5\u4E0B\uFF0C\u5237\u65B0\u4E3B\u9875\u4F1A\u5BFC\u81F4\u91CD\u65B0\u767B\u5F55
notAllowRefreshIndex=false

#\u662F\u5426\u5141\u8BB8\u591A\u8D26\u53F7\u540C\u65F6\u767B\u5F55
user.multiAccountLogin=true

#\u5206\u9875\u914D\u7F6E
page.pageSize=30

#\u7855\u6B63\u7EC4\u4EF6\u662F\u5426\u4F7F\u7528\u7F13\u5B58
supcan.useCache=false

#\u901A\u77E5\u95F4\u9694\u65F6\u95F4\u8BBE\u7F6E, \u5355\u4F4D\uFF1A\u6BEB\u79D2, 30s=30000ms, 60s=60000ms
oa.notify.remind.interval=60000

#============================#
#==== Framework settings ====#
#============================#

#\u4F1A\u8BDD\u8D85\u65F6\uFF0C \u5355\u4F4D\uFF1A\u6BEB\u79D2\uFF0C 20m=1200000ms, 30m=1800000ms, 60m=3600000ms
session.sessionTimeout=1800000
#\u4F1A\u8BDD\u6E05\u7406\u95F4\u9694\u65F6\u95F4\uFF0C \u5355\u4F4D\uFF1A\u6BEB\u79D2\uFF0C2m=120000ms\u3002
session.sessionTimeoutClean=120000

#\u7F13\u5B58\u8BBE\u7F6E
ehcache.configFile=cache/ehcache-local.xml
#ehcache.configFile=cache/ehcache-rmi.xml

#\u7D22\u5F15\u9875\u8DEF\u5F84
web.view.index=/a

#\u89C6\u56FE\u6587\u4EF6\u5B58\u653E\u8DEF\u5F84
web.view.prefix=/WEB-INF/views/
web.view.suffix=.jsp

#\u6700\u5927\u6587\u4EF6\u4E0A\u4F20\u9650\u5236\uFF0C\u5355\u4F4D\u5B57\u8282. 10M=10*1024*1024(B)=10485760 bytes\uFF0C\u9700\u540C\u6B65\u4FEE\u6539\uFF1Ackfinder.xml
web.maxUploadSize=10485760

#\u65E5\u5FD7\u62E6\u622A\u8BBE\u7F6E\uFF0C\u6392\u9664\u7684URI\uFF1B\u5305\u542B @RequestMapping\u6CE8\u89E3\u7684value\u3002\uFF08\u5DF2\u4F5C\u5E9F\uFF09
#web.logInterceptExcludeUri=/, /login, /sys/menu/tree, /sys/menu/treeData, /oa/oaNotify/self/count
#web.logInterceptIncludeRequestMapping=save, delete, import, updateSort

#\u9759\u6001\u6587\u4EF6\u540E\u7F00
web.staticFile=.css,.js,.png,.jpg,.gif,.jpeg,.bmp,.ico,.swf,.psd,.htc,.htm,.html,.crx,.xpi,.exe,.ipa,.apk

#\u5355\u70B9\u767B\u5F55CAS\u8BBE\u7F6E
cas.server.url=http://127.0.0.1:8180/cas
cas.project.url=http://127.0.0.1:8080/jeeh5


#\u4E0A\u4F20\u6587\u4EF6\u7EDD\u5BF9\u8DEF\u5F84, \u8DEF\u5F84\u4E2D\u4E0D\u5141\u8BB8\u5305\u542B\u201Cuserfiles\u201D
#userfiles.basedir=/usr/local/web_root/

#\u5DE5\u7A0B\u8DEF\u5F84\uFF0C\u5728\u4EE3\u7801\u751F\u6210\u65F6\u83B7\u53D6\u4E0D\u5230\u5DE5\u7A0B\u8DEF\u5F84\u65F6\uFF0C\u53EF\u518D\u6B64\u6307\u5B9A\u7EDD\u5BF9\u8DEF\u5F84\u3002
#projectPath=D\:\\workspace\\jeeh5


ftp.server.flag=true
ftp.server.url=127.0.0.1
ftp.server.username=zkp
ftp.server.password=123
ftp.server.port=2121
ftp.server.home=/zkp
ftp.server.nginx.prefix=http://127.0.0.1:8888/zkp
upload.file.type=ai,psd,jiff,doc,docx,xls,xlsx,pdf,gif,jpg,jpeg,png,bmp,rar,zip,bz
package com.hstd.jeeh5.modules.wjj.dm;

import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Component
public class ConnectDm {
    //定义DMJdbc驱动串
    static String jdbcString = "dm.jdbc.driver.DmDriver";
    // 定义DMUrl 链接串
    static String urlStirng = "jdbc:dm://192.168.3.4:5236/DMTEST?useUnicode=true&characterEncoding=utf-8";
    // 定义连接用户名
    static String urlName = "SYSDBA";
    // 定义连接用户口令 密码
    static String password = "123456789";
    // 定义连接对象
    static Connection connection = null;
    // 静态代码块 负责加载驱动

     static{
        try {
            Class.forName(jdbcString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 单例模式返回数据库链接对象 这里会获得对象,不会显示问题
    public static Connection getConnection() throws SQLException {
        if (connection == null) {
            connection = DriverManager.getConnection(urlStirng, urlName, password);
            return connection;
        }
        return connection;
    }


//    /**
//     * 加载驱动
//     *
//     * @throws SQLException
//     */
//    public void loadJdbcDriver() throws SQLException {
//
//        try {
            System.out.println("Loading JDBC Driver...");
//            // 加载jdbc驱动
//            Class.forName(jdbcString); // 隐士注册
            DriverManager.registerDriver(  new dm.jdbc.driver.DmDriver()); //显示注册
//        } catch (ClassNotFoundException e) {
//            throw new SQLException("Loading JDBC Driver Error:" + e.getMessage());
//        } catch (Exception e) {
//            throw new SQLException("Loading JDBC Driver Error:" + e.getMessage());
//        }
//
//    }

    /**
     * 关闭连接
     *
     * @throws SQLException
     */
    public void disConnect() throws SQLException {

        try {
            //关闭连接
            connection.close();

        } catch (SQLException e) {
            throw new SQLException("close connection error:" + e.getMessage());
        }
    }


    //     测试调用 这里看链接是否生成
//    public static void main(String[] args) {
//
//        try {
//            Connection connection=ConnectDm.getConnection();
//            if (connection!=null){
//                System.out.println("成功获取connection");
//            }else {
//                System.out.println("数据库链接异常");
//            }
//
//        }catch (SQLException e){
//            e.printStackTrace();
//        }
//        int i = 129 & 128;// 转为2进制的先 参与运算的两个为都为1 是1,否则为零 ,在转为10进制
//        System.out.println(i);
//    }

}

转载于:https://my.oschina.net/fairing/blog/1590041

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值