资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在Spring中使用Mybatis和Mysql搭建一个分布式数据库访问框架

这篇文章将为大家详细讲解有关怎么在Spring中使用Mybatis和MySQL搭建一个分布式数据库访问框架,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联公司是一家集成都做网站、成都网站制作、网站页面设计、网站优化SEO优化为一体的专业网站设计公司,已为成都等多地近百家企业提供网站建设服务。追求良好的浏览体验,以探求精品塑造与理念升华,设计最适合用户的网站页面。 合作只是第一步,服务才是根本,我们始终坚持讲诚信,负责任的原则,为您进行细心、贴心、认真的服务,与众多客户在蓬勃发展的市场环境中,互促共生。

开发环境

3.1 下载Spring、Mybatis、Mysql组件。

3.2 Eclipse:Java开发IDE。引入如下jar包:

怎么在Spring中使用Mybatis和Mysql搭建一个分布式数据库访问框架

代码结构如下:

怎么在Spring中使用Mybatis和Mysql搭建一个分布式数据库访问框架

四、构建数据库集群

在MYSQL中创建11个数据库(test1/2/3/4/5/6/7/8/9/10/11)创建一个简单的表:

怎么在Spring中使用Mybatis和Mysql搭建一个分布式数据库访问框架

在test1的tbl_Demo表中插入5千万条数据,其它10个数据库的tbl_Demo表中分别插入5百万条数据(用函数)。

在test1的tbl_Demo表中插入5千万条数据,其它10个数据库的tbl_Demo表中分别插入5百万条数据(用函数)。

五、创建Mybatis数据库映射接口

/**
 * Mybatis 映射接口
 * 
 *
 * @author elon
 * @version 1.0, 2015年10月23日
 */
public interface IDemo 
{ 
 public void insertDemo(DemoDAO demo);
 public List selectGroup();
}
/**
 * 
 * Mybatis 映射服务接口
 *
 * @author elon
 * @version 1.0, 2015年10月23日
 */
public interface IDemoService
{ 
 public void insertDemo(DemoDAO demo);
 public List selectGroup();
}
/**
 * 
 * Mybatis 映射服务实现
 *
 * @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 selectGroup()
 { 
  return idemo.selectGroup();
 }
}

六、创建数据库标识管理和动态数据源

/**
 * 
 * 保存数据库标识。每个线程由独立的对象存储
 *
 * @author elon
 * @version 1.0, 2015年10月23日
 */
public class DBIndetifier
{ 
 private static ThreadLocal dbKey = new ThreadLocal();
 public static void setDBKey(final String dbKeyPara)
 { 
  dbKey.set(dbKeyPara);
 }
 public static String getDBKey()
 {
  return dbKey.get();
 }
}
/**
 * 
 * 动态数据源。可根据不同的数据索引连接不同的数据库
 *
 * @author elon
 * @version 1.0, 2015年10月23日
 */
public class DynamicDataSource extends AbstractRoutingDataSource
{
 @Override
 public Object determineCurrentLookupKey()
 {
  return DBIndetifier.getDBKey();
 }
}

七、创建数据库访问对象

/**
 * 
 * 数据库访问对象。用于插入数据。
 *
 * @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 serialVersionUID
  *    */  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);  } }

八、创建数据库访问任务

/**
 * 数据库访问任务定义。将每一个对数据库访问的请求包装为一个任务对象,放到任务管理中,
 * 然后等待任务执行完成,取出执行结果。
 *
 * @author elon
 * @version 1.0, 2015年10月23日
 */
public class DBTask implements Runnable
{ 
 // 操作数据库标识,用于指定访问的数据库。与spring配置文件中的数据动态数据源定义一致。
 private final String dbKey;
 // mybatis数据库访问对象
 private final Object dbAccessObject;
 // mysbatis数据库访问方法名称,用于反射调用
 private final String methodName;
 // 存储可变参数的值
 private final Object[] paraArray;
 // 存储可变参数类型
 @SuppressWarnings("rawtypes")
 private final Class[] paraClassArray;
 // 数据库操作结果。查询操作返回查询结果; 插入、删除、修改操作返回null。
 private Object operateResult;
 // 操作数据库抛出的异常信息
 private Exception exception;
 // 标识任务是否已经执行
 private boolean finish;
 /**
  * 构造函数
  * @param dbKey 数据库标识
  * @param dbAccessObject 数据库访问对象
  * @param methodName 数据库访问方法名称
  * @param paraArray 参数列表
  */
 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;
 }
 /**
  * 
  * 任务执行函数
  *
  */
 @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;
 }
 /**
  * 抛出数据库操作异常
  * 
  * @return 异常
  */
 public Exception getException()
 {
  return exception;
 }
 /**
  * 
  * 返回任务是否已执行
  *
  * @return 标记
  */
 public boolean isFinish()
 {
  return finish;
 }
}

九、创建数据库任务管理器

/**
 * 数据库访问任务管理。将数据库访问任务放到线程池中执行。
 * 
 *
 * @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(10000),
          new ThreadPoolExecutor.CallerRunsPolicy());
 }
 public void excute(Runnable task)
 {
  pool.execute(task);
 }
}

十、创建MyBatis配置文件

10.1 mybatis.xml




 
  
 

10.2 demoMapper.xml

 
 
 
 
  insert into tbl_demo(a, b, c) values(#{a}, #{b}, #{c});
 
 
  
 
 
  select sum(a) as sumColum from tbl_demo group by c;
 

十一、创建Spring配置文件

11.1 spring.xml



 
  
  
  
  
  
  
  
  
 
 
  
  
  
  
  
  
  
  
 
 
  
  
  
  
  
  
  
  
 
 
  
  
  
  
  
  
  
  
 
 
  
  
  
  
  
  
  
  
 
 
  
  
  
  
  
  
  
  
 
 
  
  
  
  
  
  
  
  
 
 
  
  
  
  
  
  
  
  
 
 
  
  
  
  
  
  
  
  
 
 
  
  
  
  
  
  
  
  
 
 
  
  
  
  
  
  
  
  
 
 
  
   
    
    
    
    
    
    
    
    
    
    
    
   
  
 
 
  
  
 
 
  
  
 
 
  
 

十二、测试代码

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");
  // 创建任务对象
  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("开始插入数据:" + 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("插入数据结束:" + format.format(new Date()));
  System.out.println("开始查询5千万数据表:" + 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千万数据表结束:" + format.format(new Date()));
  List taskList = new ArrayList();
  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百万数据表:" + 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百万数据表查询结束:" +format.format(new Date()));
 }
}

十三、测试结果

怎么在Spring中使用Mybatis和Mysql搭建一个分布式数据库访问框架

关于怎么在Spring中使用Mybatis和Mysql搭建一个分布式数据库访问框架就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网页标题:怎么在Spring中使用Mybatis和Mysql搭建一个分布式数据库访问框架
标题来源:http://cdkjz.cn/article/psdpho.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220