項目主要採用:SpringMVC4.3.2.RELEASE +Spring4.3.2.RELEASE + Maven 3.3.3 + druid 1.0.29 + Mybatis 3.2.8 + Mysql 5.1.21 + Jquery 1.7.2 + JSPjava
開發工具以下:IDEA 2016mysql
參考文章:https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFiltergit
ConfigFilter的做用包括:github
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="filters" value="config" /> <property name="connectionProperties" value="config.file=file:///home/admin/druid-pool.properties" /> </bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="filters" value="config" /> <property name="connectionProperties" value="config.file=http://127.0.0.1/druid-pool.properties" /> </bean>
這種配置方式,使得一個應用集羣中,多個實例能夠從同一個地方讀取配置,集中配置,集中修改,部署更簡單。redis
DruidDataSource支持jvm啓動參數配置filters,因此你能夠:spring
java -Ddruid.filters=config ....
數據庫密碼直接寫在配置中,對運維安全來講,是一個很大的挑戰。Druid爲此提供一種數據庫密碼加密的手段ConfigFilter。sql
在命令行中執行以下命令:數據庫
java -cp druid-1.0.16.jar com.alibaba.druid.filter.config.ConfigTools you_password
輸出express
privateKey:MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA6+4avFnQKP+O7bu5YnxWoOZjv3no4aFV558HTPDoXs6EGD0HP7RzzhGPOKmpLQ1BbA5viSht+aDdaxXp6SvtMQIDAQABAkAeQt4fBo4SlCTrDUcMANLDtIlax/I87oqsONOg5M2JS0jNSbZuAXDv7/YEGEtMKuIESBZh7pvVG8FV531/fyOZAiEA+POkE+QwVbUfGyeugR6IGvnt4yeOwkC3bUoATScsN98CIQDynBXC8YngDNwZ62QPX+ONpqCel6g8NO9VKC+ETaS87wIhAKRouxZL38PqfqV/WlZ5ZGd0YS9gA360IK8zbOmHEkO/AiEAsES3iuvzQNYXFL3x9Tm2GzT1fkSx9wx+12BbJcVD7AECIQCD3Tv9S+AgRhQoNcuaSDNluVrL/B/wOmJRLqaOVJLQGg==
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOvuGrxZ0Cj/ju27uWJ8VqDmY7956OGhVeefB0zw6F7OhBg9Bz+0c84RjzipqS0NQWwOb4kobfmg3WsV6ekr7TECAwEAAQ==
password:PNak4Yui0+2Ft6JSoKBsgNPl+A033rdLhFw+L0np1o+HDRrCo9VkCuiiXviEMYwUgpHZUFxb2FpE0YmSguuRww==
輸入你的數據庫密碼,輸出的是加密後的結果。spring-mvc
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="jdbc:derby:memory:spring-test;create=true" /> <property name="username" value="sa" /> <property name="password" value="${password}" /> <property name="filters" value="config" /> <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${publickey}" /> </bean>
有三種方式配置:
=======================================================================================
本人本身的文件配置以下:
jdbc.properties
#JDBCdriver #jdbc.driverClassName=oracle.jdbc.driver.OracleDriver jdbc.driverClassName=com.mysql.jdbc.Driver #JDBC jdbc.url=jdbc\:mysql\://127.0.0.1:3306/ssmredis?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true #jdbc.url=jdbc:oracle:thin:@91.4.0.76:15521:sjbb #oracle account/password #jdbc.username=edpdata #jdbc.password=edpdata#123 #mysql account/password jdbc.username=root jdbc.password=root #jdbc.publickey= publicKey = MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIqAfobn0atBXjrAIWKVxh1iIy+haKqRhNwUrHN6rT4OcJs3oWSgqbPuBpjhdZUQF9MX6T6TWiLWuA6vE8qQyHcCAwEAAQ== password = LiWSDbtVp6vEzadIgCnxlsC74tyNIT0q35OVzqiYPAa8PMKidNAklYj5YZfEdxtC/MNhW+GbxA/ZA9ib3OQyTA== jdbc.dbcp.initialSize=10 jdbc.dbcp.maxActive=500 jdbc.dbcp.maxIdle=10 jdbc.dbcp.minIdle=1 jdbc.dbcp.maxWait=120000
spring-mybatis.xml 配置信息以下:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 自動掃描 --> <context:component-scan base-package="com.tydic.www" /> <!-- 引入配置文件 --> <context:property-placeholder location="classpath:*.properties" /> <!--Druid 鏈接池配置 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本屬性 url、user、password --> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${password}" /> <property name="filters" value="config,log4j" /> <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${publicKey}" /> <!-- <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${jdbc.publickey}" /> --> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="1" /> <property name="minIdle" value="1" /> <property name="maxActive" value="40" /> <!-- 配置獲取鏈接等待超時的時間 --> <property name="maxWait" value="60000" /> <!-- 配置間隔多久才進行一次檢測,檢測須要關閉的空閒鏈接,單位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一個鏈接在池中最小生存的時間,單位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x' FROM DUAL" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <!-- 打開PSCache,而且指定每一個鏈接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <!-- 超過期間限制是否回收 --> <property name="removeAbandoned" value="true" /> <!-- 超時時間;單位爲秒。180秒=3分鐘 --> <property name="removeAbandonedTimeout" value="1800" /> <!-- 關閉abanded鏈接時輸出錯誤日誌 --> <property name="logAbandoned" value="true" /> <!-- 配置監控統計攔截的filters,去掉後監控界面sql沒法統計 --> <!-- property name="filters" value="stat" / --> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/myBatis.xml"></property> <property name="dataSource" ref="dataSource"></property> <property name="mapperLocations"> <list> <value>classpath:com/tydic/www/mapper/*.xml</value> </list> </property> </bean> <!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.tydic.www.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- advice:通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="select*" read-only="true"/> <tx:method name="query*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/> <tx:method name="*" propagation="REQUIRES_NEW" rollback-for="Exception"/> </tx:attributes> </tx:advice> <!-- 切入 --> <aop:config expose-proxy="true"> <!-- 只對業務邏輯層實施事務 --> <aop:pointcut id="txPointcut" expression="execution(* com.tydic.www.service..*.*(..))" /> <!-- Advisor定義,切入點和通知分別爲txPointcut、txAdvice --> <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/> </aop:config> </beans>
<property name="filters" value="config,log4j" />
<property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${publicKey}" />
紅色的配置信息主要功能以下:
加載加密的密碼密文並解析密文。log4j這個是打印mybatis的sql語句的功能