Mybatis manual ing result set

Source: Internet
Author: User

Mybatis can automatically encapsulate the query results into Bean. The precondition is that the bean's attribute name and query result column name are the same, and the corresponding storage is performed at a time.

If the column name of the query result is inconsistent with the bean attribute name, you must manually map the result set.

<! -- Manually map the result set --> <resultmap type = "cn. TEDU. mybatis. Beans. User" id = "userrm"> <! -- The primary key column must be declared, and the ID label should be used to declare --> <ID column = "uid" property = "ID"/> <! -- For non-primary key columns, if the column name and bean attribute name are the same, you do not need to configure --> <! -- <Result column = "name" property = "name"/> --> <result column = "uage" property = "Age"/> </resultmap> <select id =" querybymapping "resultmap =" userrm "> select ID as uid, name, age as uage from user; </SELECT>
Resultmap = "userrm": Used to associate the ing with the query operation.

ID tag used to configure ID ing
Use the result label When configuring other tabs

Test class:

// Create sqlsessionfactory private sqlsessionfactory = NULL Based on the configuration file; @ before public void before () throws exception {// 1. read the core configuration file inputstream in = resources. getresourceasstream ("sqlmapconfig. XML "); // 2. create sqlsessionfactory factory = new sqlsessionfactorybuilder () according to the configuration file (). build (in);}/*** manually map result set */@ test public void test12 () {// 1. create sqlsession session = factory. opensession (); // 2. execute the operation list <user> List = session. selectlist ("CN. TEDU. mybatis. beans. usermapper. querybymapping "); // 3. print the result system. out. println (list );}

 

Mybatis manual ing result set

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.