MyBatis, dynamic incoming table name, field name resolution

Source: Internet
Author: User

Reprint: http://luoyu-ds.iteye.com/blog/1517607

Today's project, the problem is the need to modify the data table records, and the field names are not fixed, that is, need to pass through the parameters,

Originally this is not a problem, according to the usual JDBC, or hibernate is also good to achieve, but the problem is now the project with the MyBatis this persistence layer framework, according to my first idea of the beginning of the SQL is so,

When I am full of joy to run only to find that no, so Baidu, the way to exhaustion, or not, someone gave me the need to put the field that a ${field}, this method to take, try it, no fruit, so I hurt eggs.

Search on the internet for half a day found that most of the proposed methods can not be used, also are similar, I gave up. Turn over the MyBatis document in the computer, in an obscure place found the update tag has a property statementtype, I think this attribute is not simple, the data is explained as follows:




See this, believe in the Java people know what the meaning of it?
This statement,preparedstatement, what is the meaning of this, I do not do more explanation, I do not understand please see:

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

Simply put is a precompiled, a non-precompiled.

If you want to dynamically pass in a field name, table name, and so on, if your SQL execution is precompiled, this obviously

is not possible, so you have to change to non-precompiled, which is the case:

XML code
    1. <update id="Editissueddata" parametertype="map" statementtype="STATEMENT">

At the same time, the values of the genus variables in SQL are best changed to ${xxxx}, not #{xxx}

When I'm done, this is my full xml:



Your sister, or not, the database error says,


This note: Class I passed to the SQL variable Val value, carefully read the wrong, is not feeling a bit different, right, is not the same, is not feeling three classes this thing less a quotation mark, in fact, so the last step, need to do in Java code, as follows:

Java 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);

In the put parameter to map, the corresponding Val I put in the parameter before and after a single quotation mark, so that I have been tested successfully!

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

I found a lot of people have encountered this problem, right here as a reference, hope can help you!

MyBatis, dynamic incoming table name, field name resolution

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.