MyBatis Universal Mapper Dynamic query table name

Source: Internet
Author: User

1: Give a @table annotation, give a default table name, do not write can also, but to hump turn underline match

@Table (name = "Conf_default")

2: Add non-table field parameters to accept dynamic Table name parameters
@Transient//declaring non-database fields

Private String TableName;

3: Implement interface Idynamictablename, implement the following method, the property in 2 is the return table name

 Public String Getdynamictablename () {    return  tableName;}

This is said in the source code:

/**@author   @since 2015-10-28 22:20 * * Public Interface idynamictablename {/**@return*/String Getdynamictablename ();}

4: Query the table dynamically using the following method

New Condition (Confdefault.  Class); Condition.settablename ("conf"); // dynamic configuration indicates Condition.createcriteria (). Andcondition ("' key ' like '%1% '"). Andcondition ("' value ' like '%1% '"); Condition.setorderbyclause ("' Key ' desc"); List<ConfDefault> list = confdefaultmapper.selectbyexample (condition);

This is how the generated SQL is:

'%1% ' and ' value ' like '%1% ') Order by ' key ' desc

MyBatis Universal Mapper Dynamic query table name

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.