hibernate mysql 主从_Spring+Hibernate框架下Mysql读写分离、主从数据库配

介绍下mysql数据库读写分离在spring,hibernate框架下的配置。

1.mysql连接配置文件jdbc.properties

master.*.*表示主数据库连接参数,负责增,删,改;

slave.*.*表示从数据库连接参数,只负责读取;

jdbc.properties

master.jdbc.driverclassname=com.mysql.jdbc.drivermaster.jdbc.url=********master.jdbc.username=********master.jdbc.password=********slave.jdbc.driverclassname=com.mysql.jdbc.driverslave.jdbc.url=********slave.jdbc.username=********slave.jdbc.password=********把**改成你所需的连接参数;

2.配置aop切面类 datasourceadvice.java

import java.lang.reflect.method;import org.springframework.aop.afterreturningadvice;import org.springframework.aop.methodbeforeadvice;import org.springframework.aop.throwsadvice;import com.company.datasource.datasourceswitcher;public class datasourceadvice implements methodbeforeadvice, afterreturningadvice, throwsadvice {// service方法执行之前被调用public void before(method method, object[] args, object target) throws throwable {system.out.println("切入点: " + target.getclass().getname() + "类中" + method.getname() + "方法");if(method.getname().startswith("add") || method.getname().startswith("create")|| method.getname().startswith("save")|| method.getname().startswith("edit")|| method.getname().startswith("update")|| method.getname().startswith("delete")|| method.getname().startswith("remove")){system.out.println("切换到: master");datasourceswitcher.setmaster();}else {system.out.println("切换到: slave");datasourceswitcher.setslave();}}// service方法执行完之后被调用public void afterreturning(object arg0, method method, object[] args, object target) throws throwable {}// 抛出exception之后被调用public void afterthrowing(method method, object[] args, object target, exception ex) throws throwable {datasourceswitcher.setslave();system.out.println("出现异常,切换到: slave");}}

数据源选择类 datasourceswitcher.java

package com.company.datasource;import org.springframework.util.assert;public class datasourceswitcher {@suppresswarnings("rawtypes")private static final threadlocal contextholder = new threadlocal();@suppresswarnings("unchecked")public static void setdatasource(string datasource) {assert.notnull(datasource, "datasource cannot be null");contextholder.set(datasource);}public static void setmaster(){cleardatasource(); }public static void setslave() {setdatasource("slave");}public static string getdatasource() {return (string) contextholder.get();}public static void cleardatasource() {contextholder.remove();}}

dynamicdatasource.java数据源动态切换类

package com.company.datasource;import org.springframework.jdbc.datasource.lookup.abstractroutingdatasource;public class dynamicdatasource extends abstractroutingdatasource {@overrideprotected object determinecurrentlookupkey() {return datasourceswitcher.getdatasource();}}

下面配置spring applicationcontext.xml文件

<?xml version="1.0" encoding="utf-8"?>classpath:jdbc.properties//***c3p0配置//***hibernate一些参数这里不写了

!注 applicationcontenxt.xml中

pointcut="execution(* cn.com.company.service..*service.*(..))"

advice-ref="datasourceadvice" />

一定要配置在事务aop之上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值