The question is raised:
In the process of development, we will encounter such problems, in order to the owner of the order, this data design has a user ID, user name, contact information and so on. The benefit of this is that you do not have to associate to the System User table for data correlation.
The problem is that if the basic information of the user table has been modified, then this time, want to modify the order corresponding user information.
Solution Solutions
Scenario 1.
With database triggers, this modification is relatively straightforward and does not have to modify the Java code.
Disadvantages:
Sometimes developers may not have direct access to the database and cannot be implemented in this way.
Scenario 2:
With code processing, it is certainly unreasonable to modify the business table directly when the user edits it, and the problem is that we need this user code to modify each time we add a business. This does not satisfy the closed open principle.
The solution now is:
1. When the user edits, publish an event.
2. Write the listener to monitor the event.
3. Call this code to publish the event when the information is modified.
This allows the user to edit the class without having to make any changes, and the event is published, which can be monitored by the system or not, so the code
Achieve understanding decoupling.
The specific code is as follows:
1. Define the Event object:
public class Datamodel {
Private String pk= "";
Private String tablename= "";
Private String action= "";
}
2. Defining events
public class Upddataevent extends Applicationevent {
/**
*
*/
Private static final long serialversionuid = -656796814656037536l;
Public upddataevent (Object source) {
Super (source);
}
}
3. Define the event listener.
public class Updatedatalistener implements applicationlistener<upddataevent> {
@Resource (name= "JdbcTemplate")
JdbcTemplate JdbcTemplate;
Private map<string,list<string>> sqlmap=new hashmap<string, list<string>> ();
@Override
public void Onapplicationevent (Upddataevent event) {
Datamodel datamodel= (Datamodel) Event.getsource ();
String ID=DATAMODEL.GETPK ();
String Tablename=datamodel.gettablename ();
List<string> Sqllist=sqlmap.get (tableName);
for (String sql:sqllist) {
Jdbctemplate.update (SQL, id);
}
}
public void Setsqlmap (map<string,list<string>> Map) {
This.sqlmap=map;
}
}
Inject SQL map from outside.
The map key is the table name, and the value is list, which corresponds to the SQL statement.
The SQL statement under Oracle is written as:
UPDATE Qingjia a SET (username) = (SELECT b.fullname from Sys_user b where B.userid = A.userid) where a.userid=?
Must be a parameter.
4. The configuration file:
<bean id= "Updatedatalistener" class= "Com.hotent.core.datahandler.UpdateDataListener" >
<property name= "Sqlmap" >
<map>
<!--This is the name of the table, you need to keep it when you publish the event, please unify lowercase -
<entry key= "Biaoming" >
<list>
<!--The SQL statement to be updated, you can configure multiple
<value type= "Java.lang.String" >
UPDATE Qingjia a SET (username) = (SELECT b.fullname from Sys_user b where B.userid = A.userid) where a.userid=?
</value>
</list>
</entry>
</map>
</property>
</bean>
5. Publishing Events
public class Datapublishutil {
/**
* Publish events.
* @param model
*/
public static void Publishdata (Datamodel model) {
Apputil.publishevent (new Upddataevent (model));
}
/**
* Publish events.
* @param tableName
* @param PK
*/
public static void Publishdata (String tableName, string pk) {
Datamodel model=new Datamodel ();
MODEL.SETPK (PK);
Model.settablename (TableName);
Apputil.publishevent (new Upddataevent (model));
}
}
Call the relevant method where needed, such as when the user edits.
Publishdata (String tableName, String pk)
Parameter description:
TableName: Table name This needs to be agreed and consistent in the configuration file.
PK: PRIMARY Key data
Schema implementation for modifying business data when basic data is modified