溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Spring中使用Mybatis和Mysql搭建一個(gè)分布式數(shù)據(jù)庫(kù)訪問(wèn)框架

發(fā)布時(shí)間:2021-04-13 17:29:17 來(lái)源:億速云 閱讀:493 作者:Leah 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)怎么在Spring中使用Mybatis和Mysql搭建一個(gè)分布式數(shù)據(jù)庫(kù)訪問(wèn)框架,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

開(kāi)發(fā)環(huán)境

3.1 下載Spring、Mybatis、Mysql組件。

3.2 Eclipse:Java開(kāi)發(fā)IDE。引入如下jar包:

怎么在Spring中使用Mybatis和Mysql搭建一個(gè)分布式數(shù)據(jù)庫(kù)訪問(wèn)框架

代碼結(jié)構(gòu)如下:

怎么在Spring中使用Mybatis和Mysql搭建一個(gè)分布式數(shù)據(jù)庫(kù)訪問(wèn)框架

四、構(gòu)建數(shù)據(jù)庫(kù)集群

在MYSQL中創(chuàng)建11個(gè)數(shù)據(jù)庫(kù)(test1/2/3/4/5/6/7/8/9/10/11)創(chuàng)建一個(gè)簡(jiǎn)單的表:

怎么在Spring中使用Mybatis和Mysql搭建一個(gè)分布式數(shù)據(jù)庫(kù)訪問(wèn)框架

在test1的tbl_Demo表中插入5千萬(wàn)條數(shù)據(jù),其它10個(gè)數(shù)據(jù)庫(kù)的tbl_Demo表中分別插入5百萬(wàn)條數(shù)據(jù)(用函數(shù))。

在test1的tbl_Demo表中插入5千萬(wàn)條數(shù)據(jù),其它10個(gè)數(shù)據(jù)庫(kù)的tbl_Demo表中分別插入5百萬(wàn)條數(shù)據(jù)(用函數(shù))。

五、創(chuàng)建Mybatis數(shù)據(jù)庫(kù)映射接口

/**
 * Mybatis 映射接口
 * 
 *
 * @author elon
 * @version 1.0, 2015年10月23日
 */
public interface IDemo 
{ 
 public void insertDemo(DemoDAO demo);
 public List<Integer> selectGroup();
}
/**
 * 
 * Mybatis 映射服務(wù)接口
 *
 * @author elon
 * @version 1.0, 2015年10月23日
 */
public interface IDemoService
{ 
 public void insertDemo(DemoDAO demo);
 public List<Integer> selectGroup();
}
/**
 * 
 * Mybatis 映射服務(wù)實(shí)現(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ù)庫(kù)標(biāo)識(shí)管理和動(dòng)態(tài)數(shù)據(jù)源

/**
 * 
 * 保存數(shù)據(jù)庫(kù)標(biāo)識(shí)。每個(gè)線程由獨(dú)立的對(duì)象存儲(chǔ)
 *
 * @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();
 }
}
/**
 * 
 * 動(dòng)態(tài)數(shù)據(jù)源??筛鶕?jù)不同的數(shù)據(jù)索引連接不同的數(shù)據(jù)庫(kù)
 *
 * @author elon
 * @version 1.0, 2015年10月23日
 */
public class DynamicDataSource extends AbstractRoutingDataSource
{
 @Override
 public Object determineCurrentLookupKey()
 {
  return DBIndetifier.getDBKey();
 }
}

七、創(chuàng)建數(shù)據(jù)庫(kù)訪問(wèn)對(duì)象

/**
 * 
 * 數(shù)據(jù)庫(kù)訪問(wèn)對(duì)象。用于插入數(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;
 }
}
/**
 * 
 * 映射結(jié)果定義
 *
 * @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ù)庫(kù)訪問(wèn)任務(wù)

/**
 * 數(shù)據(jù)庫(kù)訪問(wèn)任務(wù)定義。將每一個(gè)對(duì)數(shù)據(jù)庫(kù)訪問(wèn)的請(qǐng)求包裝為一個(gè)任務(wù)對(duì)象,放到任務(wù)管理中,
 * 然后等待任務(wù)執(zhí)行完成,取出執(zhí)行結(jié)果。
 *
 * @author elon
 * @version 1.0, 2015年10月23日
 */
public class DBTask implements Runnable
{ 
 // 操作數(shù)據(jù)庫(kù)標(biāo)識(shí),用于指定訪問(wèn)的數(shù)據(jù)庫(kù)。與spring配置文件中的數(shù)據(jù)動(dòng)態(tài)數(shù)據(jù)源定義一致。
 private final String dbKey;
 // mybatis數(shù)據(jù)庫(kù)訪問(wèn)對(duì)象
 private final Object dbAccessObject;
 // mysbatis數(shù)據(jù)庫(kù)訪問(wèn)方法名稱,用于反射調(diào)用
 private final String methodName;
 // 存儲(chǔ)可變參數(shù)的值
 private final Object[] paraArray;
 // 存儲(chǔ)可變參數(shù)類型
 @SuppressWarnings("rawtypes")
 private final Class[] paraClassArray;
 // 數(shù)據(jù)庫(kù)操作結(jié)果。查詢操作返回查詢結(jié)果; 插入、刪除、修改操作返回null。
 private Object operateResult;
 // 操作數(shù)據(jù)庫(kù)拋出的異常信息
 private Exception exception;
 // 標(biāo)識(shí)任務(wù)是否已經(jīng)執(zhí)行
 private boolean finish;
 /**
  * 構(gòu)造函數(shù)
  * @param dbKey 數(shù)據(jù)庫(kù)標(biāo)識(shí)
  * @param dbAccessObject 數(shù)據(jù)庫(kù)訪問(wèn)對(duì)象
  * @param methodName 數(shù)據(jù)庫(kù)訪問(wèn)方法名稱
  * @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;
 }
 /**
  * 
  * 任務(wù)執(zhí)行函數(shù)
  *
  */
 @Override
 public void run()
 {
  try
  { 
   DBIndetifier.setDBKey(dbKey);
   Method method = dbAccessObject.getClass().getMethod(methodName, paraClassArray);
   // 查詢操作返回查詢結(jié)果; 插入、刪除、修改操作返回null
   operateResult = method.invoke(dbAccessObject, paraArray);
  }
  catch (Exception e)
  {
   exception = e;
   e.printStackTrace();
  }
  finish = true;
 }
 /**
  * 
  * 返回操作結(jié)果。查詢操作返回查詢結(jié)果; 插入、刪除、修改操作返回null
  *
  * @return 操作結(jié)果
  */
 public Object getRetValue()
 {
  return operateResult;
 }
 /**
  * 拋出數(shù)據(jù)庫(kù)操作異常
  * 
  * @return 異常
  */
 public Exception getException()
 {
  return exception;
 }
 /**
  * 
  * 返回任務(wù)是否已執(zhí)行
  *
  * @return 標(biāo)記
  */
 public boolean isFinish()
 {
  return finish;
 }
}

九、創(chuàng)建數(shù)據(jù)庫(kù)任務(wù)管理器

/**
 * 數(shù)據(jù)庫(kù)訪問(wèn)任務(wù)管理。將數(shù)據(jù)庫(kù)訪問(wèn)任務(wù)放到線程池中執(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>

十二、測(cè)試代碼

public class TestMain
{
 /**
  * 測(cè)試代碼
  * 
  *
  * @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)建任務(wù)對(duì)象
  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("開(kāi)始插入數(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ù)結(jié)束:" + format.format(new Date()));
  System.out.println("開(kāi)始查詢5千萬(wàn)數(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千萬(wàn)數(shù)據(jù)表結(jié)束:" + 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("開(kāi)始查詢10個(gè)5百萬(wàn)數(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個(gè)5百萬(wàn)數(shù)據(jù)表查詢結(jié)束:" +format.format(new Date()));
 }
}

十三、測(cè)試結(jié)果

怎么在Spring中使用Mybatis和Mysql搭建一個(gè)分布式數(shù)據(jù)庫(kù)訪問(wèn)框架

關(guān)于怎么在Spring中使用Mybatis和Mysql搭建一個(gè)分布式數(shù)據(jù)庫(kù)訪問(wèn)框架就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI