Summary of the commonly used methods for advanced queries using resultmap of mybatis

Source: Internet
Author: User

The following content has been tested by the main user. From the PD database design to the test, the main user has never used mybatis to use resultmap, recently, I took the time to make a summary and systematically studied the resultmap of mybatis. I hope to share my learning experience with you. If something is wrong, please kindly advise.

Below are several database models designed by PD.

First, let's talk about the following table. Most of them use traditional and classic User Role tables. Instead of using the popular Union primary key method today, they perform unique for user table _ id and role table _ id.

For more information, see http://www.cnblogs.com/YingYue/p/3983611.html

, One-to-multiple use of the class student table (one class has more than one student, one student can only be in one class, except for the university, otherwise it is many-to-many, haha)

You can use the following methods to operate multiple tables in the resultmap of mybatis:

Method 1: In resultmap, specify the columns of other tables to point to the columns of other tables and add an ID field to the object class corresponding to the current table.

 

<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype mapper public "-// mybatis.org//dtd mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" usermapper "> <resultmap id =" baseresultmap "type =" entity. user "> <ID column =" ID "property =" ID "jdbctype =" varchar "/> <result column =" user_name "property =" username "jdbctype =" varchar "/> <result column = "password" property = "password" jdbctype = "varchar"/>
<! -- The user_id column does not exist in the User table. This Column exists in user_role and points to user_role's user_id column through flag -->
<Result column = "user_id" property = "flag" jdbctype = "varchar"/> </resultmap> <! -- Query roles that a user has --> <select id = "selectuserofrole" resultmap = "baseresultmap" parametertype = "Java. Lang. String"> select * from user uLeft join user_role ur on u. ID = Ur. user_id<If test = "ID! = NULL "> where id = # {ID, jdbctype = varchar} </If> <if test =" username! = NULL "> where user_name =#{ username, jdbctype = varchar} </If> </SELECT> </mapper>

Java

Package entity; public class user {private string ID; private string username; private string password; // This indicates the field, which does not exist in the database, instead, use user_id to point to the user_id column private string flag of user_role; Public String getflag (){
Return flag;
}

Public void setflag (string flag ){
This. Flag = flag;
} Public String GETID () {return ID;} public void setid (string ID) {This. ID = id = NULL? Null: Id. Trim ();} Public String GetUserName () {return username;} public void setusername (string username) {This. Username = username = NULL? Null: username. Trim ();} Public String GetPassword () {return password;} public void setpassword (string password) {This. Password = NULL? Null: password. Trim ();}}

Method 2:

Http://www.cnblogs.com/YingYue/diary/2014/09/18/3978422.html

 

Summary of the commonly used methods for advanced queries using resultmap of mybatis

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.