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