日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL高级 - 案例 - 系统性能优化 - 数据源配置

發布時間:2024/4/14 数据库 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL高级 - 案例 - 系统性能优化 - 数据源配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

實現方式

db.properties

jdbc.write.driver=com.mysql.jdbc.Driver jdbc.write.url=jdbc:mysql://192.168.142.128:3306/mysql_demo jdbc.write.username=root jdbc.write.password=leonjdbc.read.driver=com.mysql.jdbc.Driver jdbc.read.url=jdbc:mysql://192.168.142.129:3306/mysql_demo jdbc.read.username=root jdbc.read.password=itcast

applicationContext-datasource.xml

<?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:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- 配置數據源 - Read --><bean id="readDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" lazy-init="true"><property name="driverClass" value="${jdbc.read.driver}"></property><property name="jdbcUrl" value="${jdbc.read.url}"></property><property name="user" value="${jdbc.read.username}"></property><property name="password" value="${jdbc.read.password}"></property></bean><!-- 配置數據源 - Write --><bean id="writeDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" lazy-init="true"><property name="driverClass" value="${jdbc.write.driver}"></property><property name="jdbcUrl" value="${jdbc.write.url}"></property><property name="user" value="${jdbc.write.username}"></property><property name="password" value="${jdbc.write.password}"></property></bean><!-- 配置動態分配的讀寫 數據源 --><bean id="dataSource" class="cn.leon.aop.datasource.ChooseDataSource" lazy-init="true"><property name="targetDataSources"><map key-type="java.lang.String" value-type="javax.sql.DataSource"><entry key="write" value-ref="writeDataSource"/><entry key="read" value-ref="readDataSource"/></map></property><property name="defaultTargetDataSource" ref="writeDataSource"/><property name="methodType"><map key-type="java.lang.String"><entry key="read" value=",get,select,count,list,query,find"/><entry key="write" value=",add,create,update,delete,remove,insert"/></map></property></bean></beans>

ChooseDataSource

public class ChooseDataSource extends AbstractRoutingDataSource {public static Map<String, List<String>> METHOD_TYPE_MAP = new HashMap<String, List<String>>();/*** 實現父類中的抽象方法,獲取數據源名稱* @return*/protected Object determineCurrentLookupKey() {return DataSourceHandler.getDataSource();}// 設置方法名前綴對應的數據源public void setMethodType(Map<String, String> map) {for (String key : map.keySet()) {List<String> v = new ArrayList<String>();String[] types = map.get(key).split(",");for (String type : types) {if (!StringUtils.isEmpty(type)) {v.add(type);}}METHOD_TYPE_MAP.put(key, v);}System.out.println("METHOD_TYPE_MAP : "+METHOD_TYPE_MAP);} }

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的MySQL高级 - 案例 - 系统性能优化 - 数据源配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。