MyBatis, the solution to dynamically incoming table name and field name

Today's project, the problem encountered is that the records of the data table need to be modified, and the field names are not fixed, that is to say, it needs to be passed in through parameters.

Originally, this is not a problem. According to the usual JDBC, or hibernate It is also easy to implement, but the problem is that the persistence layer framework mybatis is used in the project. According to my initial idea, the sql is like this,

When I ran it with joy, I found out that it didn't work, so I searched Baidu, I ran out of methods, but it still didn't work. Someone told me that I needed to replace the field item with ${field}. , so I'm a pain in the ass.

           After searching online for a long time, I found that most of the proposed methods could not be used, and they were all similar, so I gave up. I looked through the mybatis document on the computer, and found that the update tag has an attribute statementType in an inconspicuous place. At first glance, I think this attribute is not simple. The information is explained as follows:




 Seeing this, I believe that people who know java know what it means
. What does this statement, preparedstatement, mean? I won't explain it here. I really don't understand, please refer to:

http://wenku.baidu.com/view/ccb9da020740be1e650e9abc.html

Simply put, one is precompiled and the other is non-precompiled.

If you're going to dynamically pass in field names, table names, etc. If your sql execution is precompiled, this is obviously 

doesn't work, so you have to change to non-precompiled, that's it:

Xml code    Favorite code
  1. <updateid="editIssuedData"parameterType="map"statementType="STATEMENT">     

At the same time, it is best to change the value of the attribute variable in sql to ${xxxx} instead of #{xxx}

Here is my complete xml when I change it:



 Your sister, it still doesn't work, the database reports an error saying,


Here is a note: Class three is the value I passed to the variable val in sql. If you look carefully at the error reported, do you feel a little different? Yes, it is different. Do you feel that the third class is missing a quotation mark? , in fact, this is the case, so the last step needs to be done in the java code, as follows:

Java code    Favorite code
  1. Map<String, Object> map = new HashMap<String, Object>();  
  2.         map.put("field", Constant.ISSUED_PLAN_COLUMN_NAME.get(field));  
  3.         map.put("val""'"+value+"'");  
  4.         map.put("id", id);  

 When putting a parameter into the map, I added a single quote before and after the parameter to the corresponding val when putting it, so that's ok, I have tested it successfully!

If you want to dynamically pass in the table name, it is the same as the incoming field name.

I found that many people also encountered this problem, here is the right as a reference, I hope it can help you!

Reference from: http://luoyu-ds.iteye.com/blog/1517607

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325346276&siteId=291194637