[One step is close, one step is Tianya]
Next, let's demonstrate how to configure and use Resultmap to implement a one-to-many query in real-world development.
Preparatory work:
A. Operating system: Win7 x64
B. Basic software: Mysql,mybatis,spring,sqlyog,tomcat,web Foundation
In particular, as a demonstration program, please crossing don't tangle with the details of the database.
--------------------------------------------------------------------------------------------------------------- ----------------------
1. First, copy the above project, rename it to MYBATIS05, the project structure is as follows:
2. Above, we demonstrate that a person corresponds to a personal information. However, if we query the user according to the department, it is a typical one-to-many relationship. To demonstrate the implementation of a one-to-many relationship query, we need to create a departmental table in the database now. All table structures are as follows:
The data in each table is as follows:
3. In view of the length of the relationship, the duplicate documents please crossing reference to the above creation, here we only explain the modified documents.
4. Add the Departments.java file with the following details:
Package Com.csdn.ingo.entity;import Java.io.serializable;import java.util.list;/*** @author author e-mail:ingo* @version Created: April 21, 2016 PM 9:07:15* class description */@SuppressWarnings ("Serial") public class departments implements Serializable{private String Id;private string departmentname;private list<userinfo> userinfos;public string getId () {return ID;} public void SetId (String id) {this.id = ID;} Public String Getdepartmentname () {return departmentname;} public void Setdepartmentname (String departmentname) {this.departmentname = Departmentname;} Public list<userinfo> Getuserinfos () {return userinfos;} public void Setuserinfos (list<userinfo> userinfos) {This.userinfos = Userinfos;} Public departments (string ID, String departmentname, list<userinfo> Userinfos) {super (); this.id = ID; This.departmentname = Departmentname;this.userinfos = Userinfos;} Public departments () {super ();//TODO auto-generated constructor stub} @Overridepublic String toString () {return ' Departments [id= "+ ID +", DEPArtmentname= "+ Departmentname +", userinfos= "+ userinfos.tostring () +"] ";}} 5. Add the Departmentsdao.java file with the following details:
Package Com.csdn.ingo.dao;import com.csdn.ingo.entity.departments;/*** @author author e-mail:ingo* @version Created: April 21, 2016 PM 9:09:33* class Description */public interface Departmentsdao {departments Finddepartmentbyid (String ID);}
6. Add the Departmentsdaomapper.xml file with the following details:
7. Modify the Userinfodao.java as follows:
Public interface Userinfodao {UserInfo Finduserinfobyid (String ID); UserInfo Finduserinfobydepartmentid (String ID);}
8. In Userinfomapper.xml, add the following:
9. New unit test method:
@Testpublic void Testseletdepartment () {Departmentsdao Userdao = Sqlsession.getmapper (Departmentsdao.class); String id = "2";D epartments Curuser = Userdao.finddepartmentbyid (ID), if (curuser!=null) {log.info ("User successfully found:" + Curuser.tostring ());}
10. Test method, run the unit test method, observe the console has the following output:
User successfully found: departments [id=2, Departmentname= Sales, Userinfos=[userinfo [Userid=customer, department=2, position= pre-sales, mobile =33334444, gender=1, [email protected]], UserInfo [Userid=customer2, department=2, position= after sale, mobile=55556666, gender =1, [email protected]]] INFO [main]-called after method execution
Given the size of the relationship, we give the results of the operation alone, please refer to the readers.
--------------------------------------------------------------------------------------------------------------- -----------------------------------------
Special: Please pay special attention to the relationship between the table, Resultmap writing style, collection writing style, column configuration, if in doubt, please refer to the previous "MyBatis---resultmaps advanced usage (above)" In the content
--------------------------------------------------------------------------------------------------------------- -----------------------------------------
If the reader is careful, we may have found that the table structure relationship we designed is the user table--->userinfo Table--->departments table. In other words, we cannot find the information for the department table by using the variables in the user table. In addition, we have actually encountered such an example, there are multiple sets of attributes in a person's properties, such as the basic information is a collection, hobbies are a collection and so on. Sometimes for a complete description of an object, you need to use multiple collection properties to describe it. Therefore, we are here in the implementation of a query operation that demonstrates multiple collections in an object.
1. First, we want to add the department attribute to user, as follows: "NOTE: We only modify the object and do not modify the database"
@SuppressWarnings ("Serial") public class User implements Serializable{private string Id;private string password;private UserInfo userinfo;private Departments department;//....set//...get//... constructor//.. ToString ()}
2. Modify the Usermapper.xml in the Resultmap, after the modified content is:
<resultmap type= "user" id= "userresult" ><id property= "id" column= "id"/><result property= "password" column= "Password"/><association property= "UserInfo" column= "userid" select= " Com.csdn.ingo.dao.UserInfoDao.findUserInfoById "></association><association property=" department " Column= "department" select= "Com.csdn.ingo.dao.DepartmentsDao.findDepartmentById" ></association></ Resultmap>
3. New unit test method, as follows:
@Testpublic void Testseletdepartmentandinfo () {Try{userdao Userdao = Sqlsession.getmapper (Userdao.class); String id = "admin"; User Curuser = Userdao.finduserinfobyid (ID), if (curuser!=null) {Log.info ("successfully found User:" +curuser.tostring ());}} catch (Exception e) {e.printstacktrace ();}}
4. Test method: Run unit test, observe console output, have the following results;
The full output is:
the output is for reference only, you crossing just see similar output.
--------------------------------------------------------------------------------------------------------------- ----------------------
At this point, MyBatis get started---resultmaps instance (one-to-many queries) end
Special remark:
These two examples, please crossing must be manually knocked over, encountered do not understand the configuration of the place please see the above detailed description.
MyBatis Getting Started---resultmaps instances (one-to-many queries)