Java Dynamic Operations Database

Source: Internet
Author: User

Problem Description: For example, the project is now using 8 of the district in Nanjing, then the 8 districts have built a database, but only one project, each district users use this project to log in

Problem: How to verify which area the login belongs to, then confirm how to switch the database;

Problem thinking: In addition to 8 databases, a database is built: several tables contained in the database: storage of logged-in user information, etc., directly understand

First, the establishment of the database

H_right:

H_role: Red indicates which area the login belongs to.

H_role_right: Assigning the displayed menu

H_role_sysuser: For allocation area number

H_sysuser: User Information

The above is a separate database, plus 8 after a total of 9 databases, so this database problem first

Ii. How Java code is implemented

1, the project uses is Shrio carries on the safe processing

2, the database configuration file editing

Two configuration files: first: Configuration information specifically for all databases:

The most important thing is the configuration of the following database key value

3, how to dynamically switch the database, Java has provided these interfaces:

Abstractroutingdatasource

This is the time to use spring's dependency injection and control to flip the

@Aspect
@Component
public class LOGAOP implements ordered{
@Pointcut ("Execution (* Com.tangbo). *(..))")
public void Recordlog () {}
@Pointcut ("Execution (* com.tangbo.esmsys). *.*(..))")
The execution of any method defined in the service package:
@Pointcut ("Execution (* com.tangbo.esmsys). *.*(..)) || Execution (* com.tangbo.oprm.context). *.*(..)) || Execution (* com.tangbo.oprm.institution). *.*(..))")
public void RecordLog1 () {}
@Pointcut ("Execution (* com.tangbo.oprm.right). *.*(..)) || Execution (* com.tangbo.oprm.role). *.*(..)) || Execution (* com.tangbo.oprm.sysuser). *.*(..)) ")
public void Recordlogbysysuser () {}
@Before ("RecordLog1 ()")
public void before (Joinpoint call) {
String className = Call.gettarget (). GetClass (). GetName ();
String methodName = Call.getsignature (). GetName ();
String sessionId = (string) securityutils.getsubject (). GetSession (). GetId ();
String dataSource = (string) securityutils.getsubject (). GetSession (). getattribute (sessionid+ "DataSource");
if (DataSource = = null) {
DataSource = (String) securityutils.getsubject (). GetSession (). getattribute ("Datas");

}
Datasourcecontextholder.setdbtype (DataSource);
String db =datasourcecontextholder.getdbtype ();
System.out.println ("Start execution:" +classname+ ".") +methodname+ "() method ..." + "selects the database as:" + db);
}
@AfterThrowing ("Recordlog ()")
public void afterthrowing (Joinpoint call) {
String className = Call.gettarget (). GetClass (). GetName ();
String methodName = Call.getsignature (). GetName ();
System.out.println (classname+ ".") +methodname+ "() method throws an exception ...");
}
@AfterReturning ("Recordlog ()")
public void Afterreturn (Joinpoint call) {
String className = Call.gettarget (). GetClass (). GetName ();
String methodName = Call.getsignature (). GetName ();
System.out.println (classname+ ".") +methodname+ "() method normal execution end ...");
}
@After ("Recordlog ()")
public void after (Joinpoint call) {
String className = Call.gettarget (). GetClass (). GetName ();
String methodName = Call.getsignature (). GetName ();
Datasourcecontextholder.cleardbtype ();
System.out.println (classname+ ".") +methodname+ "() Final execution step (finally) ...");
}
@Before ("Recordlogbysysuser ()")
public void Beforebysystem (Joinpoint call) {
String className = Call.gettarget (). GetClass (). GetName ();
String methodName = Call.getsignature (). GetName ();
Datasourcecontextholder.setdbtype ("Huawenchuan1");
String db =datasourcecontextholder.getdbtype ();
System.out.println ("Start execution:" +classname+ ".") +methodname+ "() method ..." + "selects the database as:" + db);
}
@Override
public int GetOrder () {
TODO auto-generated Method Stub
return 1;
}

}

Explanation: When spring annotations are implemented, which database is used when logging in, and after verifying which zone the login belongs to, start setting up the database to be used before invoking each interface

Java Dynamic Operations Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.