实现读写分离:

1.spring配置如下:spring-dataResource.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 主数据源 -->
<bean id="myDataSourceMaster" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!--从数据源-->
<bean id="myDataSourceSlave" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <bean id="sqlMapClientReader" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sql-map-config.xml" />
<property name="dataSource" ref="myDataSourceSlave" />
</bean> <bean id="sqlMapClientWriter" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:sql-map-config.xml" />
<property name="dataSource" ref="myDataSourceMaster" />
</bean> <!-- 操作数据库的基类 -->
<bean id="ibatisBaseDao" class="com.zzcm.ad.common.dao.impl.IbatisBaseDaoImpl">
<property name="ibatisDaoReader">
<bean id="ibatisDaoReader" class="com.zzcm.ad.common.dao.impl.IbatisDaoReaderImpl"></bean>
</property>
<property name="ibatisDaoWriter">
<bean id="ibatisDaoWriter" class="com.zzcm.ad.common.dao.impl.IbatisDaoWriterImpl"/>
</property>
</bean>
</beans>
2.将此配置文件加载到主配置文件 spring-applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- 加载属性文件 -->
<bean id="propertyConfig" class="com.zzcm.ad.util.spring.PropertyAnnotationPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${global_config_path}/4gad/jdbc.properties</value>
<value>file:${global_config_path}/4gad/support.properties</value>
</list>
</property>
</bean> <import resource="spring/spring-dataResources.xml"/>
<context:component-scan base-package="com.zzcm.ad"/> <!-- 添加注解驱动 -->
<mvc:annotation-driven/>
<!-- 允许对静态资源文件的访问 -->
<mvc:default-servlet-handler/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3.基本思想:
将读写数据库操作包装到一个基本操作类中,这样子对外操作就是一个基本操作类
因此如下:
基本操作接口:IbatisBaseDao
 package com.zzcm.ad.common.dao;
public interface IbatisBaseDao extends IbatisDaoReader,IbatisDaoWriter{
}
读操作接口:IbatisDaoReader
 package com.zzcm.ad.common.dao;
import java.util.List;
import java.util.Map;
public interface IbatisDaoReader{
/**
* 根据statementName查询,返回对象
* @param statementName
* @return
*/
public abstract <T> T queryForObject(String statementName);
/**
* 根据statementName,参数obj查询,返回对象
* @param statementName
* @param obj
* @return
*/
public abstract <T> T queryForObject(String statementName, Object obj);
/**
* 根据statementName查询,返回对象集合List
* @param statementName
* @return
*/
public abstract <T> List<T> queryForList(String statementName);
/**
* 根据statementName,参数obj查询,返回对象集合List
* @param statementName
* @param obj
* @return
*/
public abstract <T> List<T> queryForList(String statementName, Object obj);
/**
* 根据statementName,参数obj,返回Map key值查询,返回Map
* @param statementName
* @param obj
* @param keyProperty
* @return
*/
public abstract <T> Map queryForMap(String statementName,Object obj, String keyProperty);
/**
* 根据statementName、参数obj、返回Map key值、返回map value查询,返回Map
* @param statementName
* @param obj
* @param keyProperty
* @param valueProperty
* @return
*/
public abstract <T> Map queryForMap(String statementName,Object obj, String keyProperty,String valueProperty);
}
写操作接口:IbatisDaoWriter
 package com.zzcm.ad.common.dao;
import com.zzcm.ad.common.exception.JdbcException;
public interface IbatisDaoWriter{
/**
* 保存一个实体对象
* @param statementName
* @param entity
* @return
* @throws JdbcException
*/
public abstract Integer save(String statementName,Object obj) throws JdbcException;
/**
* 保存
* @param statementName
* @return
* @throws JdbcException
*/
public abstract Integer save(String statementName) throws JdbcException;
/**
* 更新一个实体对象
* @param statementName
* @param entity
* @return
* @throws JdbcException
*/
public abstract int update(String statementName,Object obj) throws JdbcException;
/**
* 更新
* @param statementName
* @param entity
* @return
* @throws JdbcException
*/
public abstract int update(String statementName) throws JdbcException;
/**
* 按照条件删除记录
* @param statementName
* @param paramObj
* @return
* @throws JdbcException
*/
public abstract int delete(String statementName,Object obj) throws JdbcException;
/**
* 按照条件删除记录
* @param statementName
* @return
* @throws JdbcException
*/
public abstract int delete(String statementName) throws JdbcException; }
读操作实现类:IbatisDaoReaderImpl
 package com.zzcm.ad.common.dao.impl;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.util.Assert;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.zzcm.ad.common.dao.IbatisDaoReader;
import com.zzcm.ad.common.exception.JdbcException;
/**
* 对数据库读操作的实现类
* @author shunyang
*
*/
public final class IbatisDaoReaderImpl extends SqlMapClientDaoSupport implements IbatisDaoReader {
private static final Logger logger = LoggerFactory.getLogger(IbatisDaoReaderImpl.class);
@Resource(name="sqlMapClientReader")
private SqlMapClient sqlMapClientReader;
@PostConstruct
public void initSqlMapClient(){
super.setSqlMapClient(sqlMapClientReader);
} @SuppressWarnings("unchecked")
@Override
public <T> T queryForObject(String statementName) throws JdbcException{
Assert.notNull(statementName);
try {
return (T) this.getSqlMapClientTemplate().queryForObject(statementName);
} catch (Exception e) {
logger.error("Something's wrong when query :");
logger.error("statementName:"+statementName);
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
}
@Override
public <T> T queryForObject(String statementName, Object obj) {
Assert.notNull(statementName);
Assert.notNull(obj);
try {
return (T) this.getSqlMapClientTemplate().queryForObject(statementName,obj);
} catch (Exception e) {
logger.error("Something's wrong when query :");
logger.error("param:"+obj.getClass().getName());
logger.error("statementName:"+statementName);
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
}
@Override
public <T> List<T> queryForList(String statementName) {
Assert.notNull(statementName);
try {
return (List<T>) this.getSqlMapClientTemplate().queryForList(statementName);
} catch (Exception e) {
logger.error("Something's wrong when query :");
logger.error("statementName:"+statementName);
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
}
@Override
public <T> List<T> queryForList(String statementName, Object obj) {
Assert.notNull(statementName);
Assert.notNull(obj);
try {
return (List<T>) this.getSqlMapClientTemplate().queryForList(statementName,obj);
} catch (Exception e) {
logger.error("Something's wrong when query :");
logger.error("statementName:"+statementName);
logger.error("param:"+obj.getClass().getName());
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
}
@Override
public <T> Map queryForMap(String statementName, Object obj,String keyProperty) {
Assert.notNull(statementName);
Assert.notNull(obj);
try {
return this.getSqlMapClientTemplate().queryForMap(statementName, obj, keyProperty);
} catch (Exception e) {
logger.error("Something's wrong when query :");
logger.error("statementName:"+statementName);
logger.error("param:"+obj.getClass().getName()+"--->keyProperty:"+keyProperty);
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
}
@Override
public <T> Map queryForMap(String statementName, Object obj,String keyProperty, String valueProperty) {
Assert.notNull(statementName);
Assert.notNull(obj);
try {
return this.getSqlMapClientTemplate().queryForMap(statementName, obj, keyProperty, valueProperty);
} catch (Exception e) {
logger.error("Something's wrong when query :");
logger.error("statementName:"+statementName);
logger.error("param:"+obj.getClass().getName()+"--->keyProperty:"+keyProperty+"-->valueProperty:"+valueProperty);
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
}
}
写操作实现类:IbatisDaoWriterImpl
 package com.zzcm.ad.common.dao.impl;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.util.Assert;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.zzcm.ad.common.dao.IbatisDaoWriter;
import com.zzcm.ad.common.exception.JdbcException;
/**
* 对数据库写操作的实现类
* @author shunyang
*
*/
public final class IbatisDaoWriterImpl extends SqlMapClientDaoSupport implements IbatisDaoWriter {
private static final Logger logger = LoggerFactory.getLogger(IbatisDaoReaderImpl.class);
@Resource(name = "sqlMapClientWriter")
private SqlMapClient sqlMapClientWriter;
@PostConstruct
public void initSqlMapClient(){
super.setSqlMapClient(sqlMapClientWriter);
} public Integer save(String statementName,Object obj) {
Assert.notNull(statementName);
Assert.notNull(obj);
try {
return (Integer) this.getSqlMapClientTemplate().insert(statementName, obj);
} catch (Exception e) {
logger.error("Something's wrong when save Object:");
logger.error(obj.getClass().getName());
logger.error("statementName:"+statementName);
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
}
@Override
public Integer save(String statementName) throws JdbcException {
Assert.notNull(statementName);
try {
return (Integer) this.getSqlMapClientTemplate().insert(statementName);
} catch (Exception e) {
logger.error("Something's wrong when save Object:");
logger.error("statementName:"+statementName);
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
} @Override
public int update(String statementName, Object obj)
throws JdbcException {
Assert.notNull(statementName);
Assert.notNull(obj);
try {
return (Integer) this.getSqlMapClientTemplate().update(statementName, obj);
} catch (Exception e) {
logger.error("Something's wrong when update Object:");
logger.error(obj.getClass().getName());
logger.error("statementName:"+statementName);
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
}
@Override
public int update(String statementName) throws JdbcException {
Assert.notNull(statementName);
try {
return (Integer) this.getSqlMapClientTemplate().update(statementName);
} catch (Exception e) {
logger.error("Something's wrong when update Object:");
logger.error("statementName:"+statementName);
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
}
@Override
public int delete(String statementName) throws JdbcException {
Assert.notNull(statementName);
try {
return this.getSqlMapClientTemplate().delete(statementName);
} catch (Exception e) {
logger.error("Something's wrong when delete Object:");
logger.error("statementName:"+statementName);
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
}
@Override
public int delete(String statementName, Object obj)
throws JdbcException {
Assert.notNull(statementName);
try {
return this.getSqlMapClientTemplate().delete(statementName,obj);
} catch (Exception e) {
logger.error("Something's wrong when delete Object:");
logger.error(obj.getClass().getName());
logger.error("statementName:"+statementName);
throw new JdbcException(this.getClass() + "->" + e.getMessage(), e);
}
}
}
基本的操作实现类:IbatisBaseDaoImpl
 package com.zzcm.ad.common.dao.impl;
import java.util.List;
import java.util.Map;
import com.zzcm.ad.common.dao.IbatisBaseDao;
import com.zzcm.ad.common.dao.IbatisDaoReader;
import com.zzcm.ad.common.dao.IbatisDaoWriter;
import com.zzcm.ad.common.exception.JdbcException;
/**
* 对数据库操作的基础类,实现了读写分离
* @author shunyang
*
*/
public class IbatisBaseDaoImpl implements IbatisBaseDao {
private IbatisDaoReader ibatisDaoReader;
private IbatisDaoWriter ibatisDaoWriter; public IbatisDaoReader getIbatisDaoReader() {
return ibatisDaoReader;
}
public void setIbatisDaoReader(IbatisDaoReader ibatisDaoReader) {
this.ibatisDaoReader = ibatisDaoReader;
}
public IbatisDaoWriter getIbatisDaoWriter() {
return ibatisDaoWriter;
}
public void setIbatisDaoWriter(IbatisDaoWriter ibatisDaoWriter) {
this.ibatisDaoWriter = ibatisDaoWriter;
}
@Override
public Integer save(String statementName, Object obj)throws JdbcException {
return ibatisDaoWriter.save(statementName, obj);
}
@Override
public Integer save(String statementName) throws JdbcException {
return ibatisDaoWriter.save(statementName);
}
@Override
public int update(String statementName, Object obj) throws JdbcException {
return ibatisDaoWriter.update(statementName, obj);
}
@Override
public int update(String statementName) throws JdbcException {
return ibatisDaoWriter.update(statementName);
}
@Override
public int delete(String statementName, Object obj)throws JdbcException {
return ibatisDaoWriter.delete(statementName,obj);
}
@Override
public int delete(String statementName) throws JdbcException {
return ibatisDaoWriter.delete(statementName);
}
@Override
public <T> T queryForObject(String statementName) {
return ibatisDaoReader.queryForObject(statementName);
}
@Override
public <T> T queryForObject(String statementName, Object obj) {
return ibatisDaoReader.queryForObject(statementName, obj);
}
@Override
public <T> List<T> queryForList(String statementName) {
return ibatisDaoReader.queryForList(statementName);
}
@Override
public <T> List<T> queryForList(String statementName, Object obj) {
return ibatisDaoReader.queryForList(statementName, obj);
}
@Override
public <T> Map queryForMap(String statementName, Object obj,
String keyProperty) {
return ibatisDaoReader.queryForMap(statementName, obj, keyProperty);
}
@Override
public <T> Map queryForMap(String statementName, Object obj,
String keyProperty, String valueProperty) {
return ibatisDaoReader.queryForMap(statementName, obj, keyProperty, valueProperty);
}
}
对外提供的类就是IbatisBaseDaoImpl
配置分离的工程:
jdbc.properties
 #数据库连接的方式,请根据你的项目中现有的情况配置
jdbc.driverClassName=com.mysql.jdbc.Driver #数据连接URL,请根据你的项目中现有的情况配置
jdbc.url=jdbc:mysql://idctest.mysql.xxx.com:3306/zzsupport #数据库连接的用户名,请根据你的项目中现有的情况配置
jdbc.username=zzmanager #数据库连接的密码(带“.encrypt”与否都可以,但是带“.encrypt”的密码必须加密),请根据你的项目中现有的情况配置
#jdbc.password.encrypt=2dUGTjehnpU.
jdbc.password=iadMOB-2013@0622)

support.properties

 #开发环境
upload.path=D:/apache-tomcat-6.0.30/webapps/zzupload-main
upload.url=http://192.168.0.88:8080/zzupload-main
工程源码详见github:https://github.com/shunyang/4gad
欢迎大家交流学习
 

搭建基于springmvc,ibatis的工程实现读写分离,配置分离的更多相关文章

  1. IDEA搭建基于maven的springboot工程

    ---恢复内容开始--- 基础环境:IntelliJ IDEA 2018.1.6 x64.JDK1.8 一.创建maven 填写包名.项目名 选择对应的本地maven 默认过来的project nam ...

  2. CentOS 7 搭建基于携程Apollo(阿波罗)配置中心单机模式

    Apollo(阿波罗)是携程框架部门研发的配置管理平台,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性.服务端基于Spring Boot ...

  3. Ubuntu 16.04下搭建基于携程Apollo(阿波罗)配置中心单机模式

    官网:https://github.com/ctripcorp/apollo Wiki:https://github.com/ctripcorp/apollo/wiki(一切的集成方式和使用方法都在这 ...

  4. Spring boot dubbo+zookeeper 搭建------基于gradle项目的消费端与服务端分离实战

    1. Dubbo简介 Dubbo是Alibaba开源的分布式框架,是RPC模式的一种成熟的框架,优点是可以与Spring无缝集成,应用到我们的后台程序中.具体介绍可以查看Dubbo官网. 2. Why ...

  5. springMVC用法 以及一个简单的基于springMVC hibernate spring的配置

    替代struts 1  web.xml中配置springmvc中央控制器 <?xml version="1.0" encoding="UTF-8"?> ...

  6. 十六、.net core(.NET 6)搭建基于Redis的Hangfire定时器

    搭建基于Redis的Hangfire定时器 Hangfire的定时配置信息会自动生成在指定到数据库内,包括关系型数据库或非关系型数据库内.目前为止,它在Redis.Oracle上面,可以支持最短15秒 ...

  7. 基于SpringMVC下的Rest服务框架搭建【1、集成Swagger】

    基于SpringMVC下的Rest服务框架搭建[1.集成Swagger] 1.需求背景 SpringMVC本身就可以开发出基于rest风格的服务,通过简单的配置,即可快速开发出一个可供客户端调用的re ...

  8. 搭建基于SSI(struts2,spring,ibatis)的javaEE开发环境

    搭建基于SSI(struts2,spring,ibatis)的javaEE开发环境 最近有很多人不知道如何搭建基于SSI(struts2,spring,ibatis)的J2EE开发环境,这里给大家一个 ...

  9. 搭建基于MySQL的读写分离工具Amoeba

    搭建基于MySQL的读写分离工具Amoeba: Amoeba工具是实现MySQL数据库读写分离的一个工具,前提是基于MySQL主从复制来实现的: 实验环境(虚拟机): 主机 角色 10.10.10.2 ...

随机推荐

  1. Android 数据库加密

    一 一个简短的引论   SQLite是一个轻量的.跨平台的.开源的数据库引擎.它的读写效率.资源消耗总量.延迟时间和总体简单性上具有的优越性,使其成为移动平台数据库的最佳解决方式(如Android.i ...

  2. Errors occurred during the build. Errors running builder &#39;JavaScript Validator&#39; on

    eclipse又一次编译时候就会报错Errors occurred during the build. Errors running builder 'JavaScript Validator' on ...

  3. Javscript轮播 支持平滑和渐隐两种效果(可以只有两张图)

    原文:Javscript轮播 支持平滑和渐隐两种效果(可以只有两张图) 先上两种轮播效果:渐隐和移动   效果一:渐隐 1 2 3 4 效果二:移动 1 2 3 4 接下来,我们来大致说下整个轮播的思 ...

  4. Swift入门教程:基本语法(一)

          简介:                                                                                       什么是S ...

  5. Aspose.Words:如何添加另一个WORD文档中的Node对象

    原文:Aspose.Words:如何添加另一个WORD文档中的Node对象 首先看一段代码,这段代码意图从docSource中获取第一个表格,并插入docTarget的末尾: , true); doc ...

  6. Premiere Pro CC问题集,不断更新

    1.Premiere Pro CC不好用? 是的.原因如下: 1.1 Adobe公司不注重用户体验,不注重工作流程,导致这款软件的用户体验很差,设计也很烂.对比Adobe公司当年用户体验最好的软件 F ...

  7. EventBus(事件总线)

    EventBus(事件总线) Guava在guava-libraries中为我们提供了事件总线EventBus库,它是事件发布订阅模式的实现,让我们能在领域驱动设计(DDD)中以事件的弱引用本质对我们 ...

  8. Linux下的C程序如何调用系统命令,并获取系统的输出信息到C程序中

    直接贴代码: #include <stdio.h> #include <string.h> #include <errno.h> int main(int argc ...

  9. SQL Server之记录筛选(top、ties、offset)汇总

    一.TOP 筛选 如果有 ORDER BY 子句,TOP 筛选将根据排序的结果返回指定的行数.如果没有 ORDER BY 子句,TOP 筛选将按照行的物理顺序返回指定的行数. 1. 返回指定数目的行 ...

  10. .net创建并安装windows服务案例

    1. 创建windows服务[引用博文]: 1. 将这个服务程序切换到设计视图2. 右击设计视图选择“添加安装程序”3. 切换到刚被添加的ProjectInstaller的设计视图4. 设置servi ...