Bailong banyan tree uses Spring combined with MyBatis to realize the separation of database read and write
Bailong Banyan Intelligence Network AI technology, according to users' personal comprehensive credit situation, recommend suitable users' products and services. When users log on to the banyan tree interface, everyone can see different product information, including interest rate, term and quota, etc. Compared with the standard service, Banyan Tree has helped users to screen and provide customized loan information services for users.
Bailong banyan adopts the technology of Spring + mybatis
First define an annotation
Import java.lang.annotation.ElementType; import java.lang.annotation.Target; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @ Retention (RetentionPolicy.RUNTIME) @ Target (ElementType.METHOD) public @ interface DataSource {public String value ();}
Bailong banyan tree defines another HandleDataSource
Public class HandleDataSource {public static final ThreadLocal holder = new ThreadLocal (); public static void putDataSource (String datasource) {holder.set (datasource);} public static String getDataSource () {return holder.get ();}}
Bailong banyan tree defines a section
Import java.lang.reflect.Method; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.reflect.MethodSignature; public class DataSourceAspect {public void pointCut () {}; public void before (JoinPoint point) {Objecttarget = point.getTarget (); / / intercepted entity class String method = point.getSignature () .getName (); / / intercepted method name Class [] classz = target.getClasss () .getInterfaces (); Class [] parameterTypes = (MethodSignature) point.getSignature () .getMethod () .getParameterTypes () / / method parameter type try {Method m = classz [0] .getMethod (method, parameterTypes); if (m! = null & & m.isAnnotationPresent (DataSource.class)) {DataSource data = m.getAnnotation (DataSource.class); HandleDataSource.putDataSource (data.value ());}} catch (Exception e) {e.printStackTrace ();}
Bailong banyan tree acquires data source
Import org.springfcamework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class ChooseDataSource extends AbstractRoutingDataSource {protected Object determineCurrentLookupKey () {return HandleDataSource.getDataSource ();}}
Bailong banyan tree configuration XMl
Classpath*:mysql.propertiescom.mysql.jdbc.Driver$ {jdbc.url} ${jdbc.user} ${jdbc.passsword} SELECT 1 FROM DUAL32510010000true 60com.mysql.jdbc.Driver$ {jdbc.url.read} ${jdbc.user.read} ${jdbc.password.read} SELECT 1 FROM DUAL32510010000true60
Banyan trees are annotated on the service interface.