Spring+Mybatis+Mysql搭建分布式數(shù)據(jù)庫訪問框架的方法
一、前言
用Java開發(fā)企業(yè)應用軟件, 經常會采用Spring+MyBatis+Mysql搭建數(shù)據(jù)庫框架。如果數(shù)據(jù)量很大,一個MYSQL庫存儲數(shù)據(jù)訪問效率很低,往往會采用分庫存儲管理的方式。本文講述如何通過Spring+Mybatis構建多數(shù)據(jù)庫訪問的架構,并采用多線程提升數(shù)據(jù)庫的訪問效率。
需要說明一下,這種方式只適合數(shù)據(jù)庫數(shù)量、名稱固定,且不是特別多的情況。針對數(shù)據(jù)庫數(shù)量不固定的情況,后面再寫一篇處理方案。
二、整體方案
三、開發(fā)環(huán)境準備
3.1 下載Spring、Mybatis、Mysql組件。
3.2 Eclipse:Java開發(fā)IDE。引入如下jar包:
代碼結構如下:
四、構建數(shù)據(jù)庫集群
在MYSQL中創(chuàng)建11個數(shù)據(jù)庫(test1/2/3/4/5/6/7/8/9/10/11)創(chuàng)建一個簡單的表:
在test1的tbl_Demo表中插入5千萬條數(shù)據(jù),其它10個數(shù)據(jù)庫的tbl_Demo表中分別插入5百萬條數(shù)據(jù)(用函數(shù))。
在test1的tbl_Demo表中插入5千萬條數(shù)據(jù),其它10個數(shù)據(jù)庫的tbl_Demo表中分別插入5百萬條數(shù)據(jù)(用函數(shù))。
五、創(chuàng)建Mybatis數(shù)據(jù)庫映射接口
/** * Mybatis 映射接口 * * * @author elon * @version 1.0, 2015年10月23日 */ public interface IDemo { public void insertDemo(DemoDAO demo); public List<Integer> selectGroup(); } /** * * Mybatis 映射服務接口 * * @author elon * @version 1.0, 2015年10月23日 */ public interface IDemoService { public void insertDemo(DemoDAO demo); public List<Integer> selectGroup(); } /** * * Mybatis 映射服務實現(xiàn) * * @author elon * @version 1.0, 2015年10月23日 */ public class DemoServiceImpl implements IDemoService { private IDemo idemo = null; public void setIdemo(IDemo idemo) { this.idemo = idemo; } @Override public void insertDemo(DemoDAO demo) { idemo.insertDemo(demo); } @Override public List<Integer> selectGroup() { return idemo.selectGroup(); } }
六、創(chuàng)建數(shù)據(jù)庫標識管理和動態(tài)數(shù)據(jù)源
/** * * 保存數(shù)據(jù)庫標識。每個線程由獨立的對象存儲 * * @author elon * @version 1.0, 2015年10月23日 */ public class DBIndetifier { private static ThreadLocal<String> dbKey = new ThreadLocal<String>(); public static void setDBKey(final String dbKeyPara) { dbKey.set(dbKeyPara); } public static String getDBKey() { return dbKey.get(); } } /** * * 動態(tài)數(shù)據(jù)源??筛鶕?jù)不同的數(shù)據(jù)索引連接不同的數(shù)據(jù)庫 * * @author elon * @version 1.0, 2015年10月23日 */ public class DynamicDataSource extends AbstractRoutingDataSource { @Override public Object determineCurrentLookupKey() { return DBIndetifier.getDBKey(); } }
七、創(chuàng)建數(shù)據(jù)庫訪問對象
/** * * 數(shù)據(jù)庫訪問對象。用于插入數(shù)據(jù)。 * * @author elon * @version 1.0, 2015年10月23日 */ public class DemoDAO { private int a; private String b; private int c; public int getA() { return a; } public void setA(int a) { this.a = a; } public String getB() { return b; } public void setB(String b) { this.b = b; } public int getC() { return c; } public void setC(int c) { this.c = c; } } /** * * 映射結果定義 * * @author elon * @version 1.0, 2015年10月23日 */ public class DemoResult implements Serializable { /** * Comment for <code>serialVersionUID</code><br> * */ private static final long serialVersionUID = -413001138792531448L; private long sum; public long getSum() { return sum; } public void setSum(long sum) { this.sum = sum; } @Override public String toString() { return String.valueOf(sum); } }
八、創(chuàng)建數(shù)據(jù)庫訪問任務
/** * 數(shù)據(jù)庫訪問任務定義。將每一個對數(shù)據(jù)庫訪問的請求包裝為一個任務對象,放到任務管理中, * 然后等待任務執(zhí)行完成,取出執(zhí)行結果。 * * @author elon * @version 1.0, 2015年10月23日 */ public class DBTask implements Runnable { // 操作數(shù)據(jù)庫標識,用于指定訪問的數(shù)據(jù)庫。與spring配置文件中的數(shù)據(jù)動態(tài)數(shù)據(jù)源定義一致。 private final String dbKey; // mybatis數(shù)據(jù)庫訪問對象 private final Object dbAccessObject; // mysbatis數(shù)據(jù)庫訪問方法名稱,用于反射調用 private final String methodName; // 存儲可變參數(shù)的值 private final Object[] paraArray; // 存儲可變參數(shù)類型 @SuppressWarnings("rawtypes") private final Class[] paraClassArray; // 數(shù)據(jù)庫操作結果。查詢操作返回查詢結果; 插入、刪除、修改操作返回null。 private Object operateResult; // 操作數(shù)據(jù)庫拋出的異常信息 private Exception exception; // 標識任務是否已經執(zhí)行 private boolean finish; /** * 構造函數(shù) * @param dbKey 數(shù)據(jù)庫標識 * @param dbAccessObject 數(shù)據(jù)庫訪問對象 * @param methodName 數(shù)據(jù)庫訪問方法名稱 * @param paraArray 參數(shù)列表 */ public DBTask(final String dbKey, final Object dbAccessObject, final String methodName, final Object... paraArray) { this.dbKey = dbKey; this.dbAccessObject = dbAccessObject; this.methodName = methodName; this.paraArray = paraArray; finish = false; exception = null; paraClassArray = new Class[paraArray.length]; for (int index = 0; index < paraArray.length; ++index) { paraClassArray[index] = paraArray[index].getClass(); } operateResult = null; } /** * * 任務執(zhí)行函數(shù) * */ @Override public void run() { try { DBIndetifier.setDBKey(dbKey); Method method = dbAccessObject.getClass().getMethod(methodName, paraClassArray); // 查詢操作返回查詢結果; 插入、刪除、修改操作返回null operateResult = method.invoke(dbAccessObject, paraArray); } catch (Exception e) { exception = e; e.printStackTrace(); } finish = true; } /** * * 返回操作結果。查詢操作返回查詢結果; 插入、刪除、修改操作返回null * * @return 操作結果 */ public Object getRetValue() { return operateResult; } /** * 拋出數(shù)據(jù)庫操作異常 * * @return 異常 */ public Exception getException() { return exception; } /** * * 返回任務是否已執(zhí)行 * * @return 標記 */ public boolean isFinish() { return finish; } }
九、創(chuàng)建數(shù)據(jù)庫任務管理器
/** * 數(shù)據(jù)庫訪問任務管理。將數(shù)據(jù)庫訪問任務放到線程池中執(zhí)行。 * * * @author elon * @version 1.0, 2015年10月23日 */ public class DBTaskMgr { private static class DBTaskMgrInstance { public static final DBTaskMgr instance = new DBTaskMgr(); } public static DBTaskMgr instance() { return DBTaskMgrInstance.instance; } private ThreadPoolExecutor pool; public DBTaskMgr() { pool = new ThreadPoolExecutor(10, 50, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10000), new ThreadPoolExecutor.CallerRunsPolicy()); } public void excute(Runnable task) { pool.execute(task); } }
十、創(chuàng)建MyBatis配置文件
10.1 mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <mappers> <mapper resource="cfg/demoMapper.xml"/> </mappers> </configuration>
10.2 demoMapper.xml
<?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="com.elon.IDemo"> <insert id="insertDemo" parameterType="com.elon.DemoDAO"> insert into tbl_demo(a, b, c) values(#{a}, #, #{c}); </insert> <resultMap id="demoResult" type="com.elon.DemoResult"> <id property="sum" column="sumColum"/> </resultMap> <select id="selectGroup" resultMap="demoResult"> select sum(a) as sumColum from tbl_demo group by c; </select> </mapper>
十一、創(chuàng)建Spring配置文件
11.1 spring.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="dataSource_1" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://10.70.69.69:3306/test1"></property> <property name="username" value="user123"></property> <property name="password" value="user123"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource_2" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://10.70.69.69:3306/test2"></property> <property name="username" value="user123"></property> <property name="password" value="user123"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource_3" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://10.70.69.69:3306/test3"></property> <property name="username" value="user123"></property> <property name="password" value="user123"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource_4" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://10.70.69.69:3306/test4"></property> <property name="username" value="user123"></property> <property name="password" value="user123"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource_5" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://10.70.69.69:3306/test5"></property> <property name="username" value="user123"></property> <property name="password" value="user123"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource_6" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://10.70.69.69:3306/test6"></property> <property name="username" value="user123"></property> <property name="password" value="user123"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource_7" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://10.61.67.246:3306/test7"></property> <property name="username" value="user123"></property> <property name="password" value="user123"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource_8" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://10.61.67.246:3306/test8"></property> <property name="username" value="user123"></property> <property name="password" value="user123"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource_9" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://10.61.67.246:3306/test9"></property> <property name="username" value="user123"></property> <property name="password" value="user123"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource_10" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://10.61.67.246:3306/test10"></property> <property name="username" value="user123"></property> <property name="password" value="user123"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource_11" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://10.61.67.246:3306/test11"></property> <property name="username" value="user123"></property> <property name="password" value="user123"></property> <property name="maxActive" value="100"></property> <property name="maxIdle" value="30"></property> <property name="maxWait" value="500"></property> <property name="defaultAutoCommit" value="true"></property> </bean> <bean id="dataSource" class="com.elon.DynamicDataSource"> <property name="targetDataSources"> <map> <entry key="test1" value-ref="dataSource_1"/> <entry key="test2" value-ref="dataSource_2"/> <entry key="test3" value-ref="dataSource_3"/> <entry key="test4" value-ref="dataSource_4"/> <entry key="test5" value-ref="dataSource_5"/> <entry key="test6" value-ref="dataSource_6"/> <entry key="test7" value-ref="dataSource_7"/> <entry key="test8" value-ref="dataSource_8"/> <entry key="test9" value-ref="dataSource_9"/> <entry key="test10" value-ref="dataSource_10"/> <entry key="test11" value-ref="dataSource_11"/> </map> </property> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:cfg/mybatis.xml"></property> <property name="dataSource" ref="dataSource" /> </bean> <bean id="iDemo" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="com.elon.IDemo"></property> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> <bean id="iDemoService" class="com.elon.DemoServiceImpl"> <property name="idemo" ref="iDemo"></property> </bean> </beans>
十二、測試代碼
public class TestMain { /** * 測試代碼 * * * @param args */ public static void main(String[] args) { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext("cfg/spring.xml"); IDemoService service1 = (IDemoService)context.getBean("iDemoService"); // 創(chuàng)建任務對象 DBTask task1 = new DBTask("test1", service1, "selectGroup"); DBTask task2 = new DBTask("test2", service1, "selectGroup"); DBTask task3 = new DBTask("test3", service1, "selectGroup"); DBTask task4 = new DBTask("test4", service1, "selectGroup"); DBTask task5 = new DBTask("test5", service1, "selectGroup"); DBTask task6 = new DBTask("test6", service1, "selectGroup"); DBTask task7 = new DBTask("test7", service1, "selectGroup"); DBTask task8 = new DBTask("test8", service1, "selectGroup"); DBTask task9 = new DBTask("test9", service1, "selectGroup"); DBTask task10 = new DBTask("test10", service1, "selectGroup"); DBTask task11 = new DBTask("test11", service1, "selectGroup"); DemoDAO demo = new DemoDAO(); demo.setA(10000000); demo.setB("12121212"); demo.setC(100); DBTask taskInsert = new DBTask("test2", service1, "insertDemo", demo); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("開始插入數(shù)據(jù):" + format.format(new Date())); DBTaskMgr.instance().excute(taskInsert); while (true) { if (!taskInsert.isFinish()) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } else { break; } } System.out.println("插入數(shù)據(jù)結束:" + format.format(new Date())); System.out.println("開始查詢5千萬數(shù)據(jù)表:" + format.format(new Date())); DBTaskMgr.instance().excute(task1); while (true) { if (!task1.isFinish()) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } else { break; } } System.out.println(task1.getRetValue()); System.out.println("查詢5千萬數(shù)據(jù)表結束:" + format.format(new Date())); List<DBTask> taskList = new ArrayList<DBTask>(); taskList.add(task2); taskList.add(task3); taskList.add(task4); taskList.add(task5); taskList.add(task6); taskList.add(task7); taskList.add(task8); taskList.add(task9); taskList.add(task10); taskList.add(task11); System.out.println("開始查詢10個5百萬數(shù)據(jù)表:" + format.format(new Date())); for (DBTask task : taskList) { DBTaskMgr.instance().excute(task); } while (true) { int success = 0; for (DBTask task : taskList) { if (!task.isFinish()) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } else { ++success; } } if (success == 10) { break; } } for (DBTask task : taskList) { System.out.println(task.getRetValue());; } System.out.println("10個5百萬數(shù)據(jù)表查詢結束:" +format.format(new Date())); } }
十三、測試結果
直接查詢一個5千萬條數(shù)據(jù)的數(shù)據(jù)庫用時:45s。
多線程同步查詢10個5百萬數(shù)據(jù)的數(shù)據(jù)庫用時: 22s。
由于10個數(shù)據(jù)庫放在兩臺服務器上,一個服務器5個數(shù)據(jù)庫。如果將10個數(shù)據(jù)分別部署到10個服務器,效率將更高。
總結
以上所述是小編給大家介紹的Spring+Mybatis+Mysql搭建分布式數(shù)據(jù)庫訪問框架,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 詳解idea搭建springboot+mybatis框架的教程
- 使用IDEA搭建SSM框架的詳細教程(spring + springMVC +MyBatis)
- 詳解手把手Maven搭建SpringMVC+Spring+MyBatis框架(超級詳細版)
- Java框架搭建之Maven、Mybatis、Spring MVC整合搭建(圖文)
- 詳解MyEclipse中搭建spring-boot+mybatis+freemarker框架
- Spring MVC 4.1.3 + MyBatis零基礎搭建Web開發(fā)框架(注解模式)
- Java的MyBatis框架項目搭建與hellow world示例
- Windows下Java+MyBatis框架+MySQL的開發(fā)環(huán)境搭建教程
- MyBatis框架搭建與代碼解讀分析
相關文章
String類型轉localDate,date轉localDate的實現(xiàn)代碼
這篇文章主要介紹了String類型轉localDate,date轉localDate的實現(xiàn)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Spring?Boot?集成Redisson實現(xiàn)分布式鎖詳細案例
這篇文章主要介紹了Spring?Boot?集成Redisson實現(xiàn)分布式鎖詳細案例,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08Spring中bean的初始化和銷毀幾種實現(xiàn)方式詳解
這篇文章主要介紹了Spring中bean的初始化和銷毀幾種實現(xiàn)方式詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11SpringBoot ApplicationListener事件監(jiān)聽接口使用問題探究
這篇文章主要介紹了SpringBoot ApplicationListener事件監(jiān)聽接口使用問題,自定義監(jiān)聽器需要實現(xiàn)ApplicationListener接口,實現(xiàn)對應的方法來完成自己的業(yè)務邏輯。SpringBoot Application共支持6種事件監(jiān)聽2023-04-04java創(chuàng)建子類對象設置并調用父類的變量操作
這篇文章主要介紹了java創(chuàng)建子類對象設置并調用父類的變量操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01Spring Boot整合RabbitMQ實例(Topic模式)
Topic Exchange 轉發(fā)消息主要是根據(jù)通配符。接下來通過本文給大家分享Spring Boot整合RabbitMQ實例(Topic模式),需要的朋友參考下吧2017-04-04