MyBatis Multi-table query, auto-Generate ID

Source: Internet
Author: User

Basically, in the configuration file, configure the fields that you want to include.

Class Relationship: Account----role,1 to 1

Account contains role class

Java class:

public class account{   private Integer accountId;    Private String username;    private String password;    private role role;}
public class Role {private integer id;private integer leave;private String name;}

Database tables:

----the structure of the table ' account '--CREATE TABLE IF  not EXISTS' account ' (' AccountId ' )int( +) not NULLauto_increment, ' UserName 'varchar( +) COLLATE Utf8_bin not NULL, ' password 'varchar( +) COLLATE Utf8_bin not NULL,' Roleid 'int( +) not NULL,PRIMARY KEY(' accountId ')) ENGINE=InnoDBDEFAULTCHARSET=UTF8 COLLATE=Utf8_bin auto_increment=1005;
----the structure of the table ' role '--CREATE TABLE IF  not EXISTS' role ' (' ID ' )int( +) not NULLauto_increment, ' R_leave 'int( One) not NULL, ' name 'varchar( +) COLLATE Utf8_bin not NULL,  PRIMARY KEY(' id ')) ENGINE=InnoDBDEFAULTCHARSET=UTF8 COLLATE=Utf8_bin auto_increment= A;

Since I was separated, all have two profiles. Can be written in a configuration file

account-resultmap.xml settings, in the configuration file, also contains all the corresponding role classes :

<Mappernamespace= "Accountmap"><!--ID is used for auto-fill in *-mapper.xml -    <Resultmaptype= "Com.hoo.entity.Account"ID= "Accountresultmap">        <ID Property= "AccountId"column= "AccountId"/>        <result Property= "username"column= "UserName"/>        <result Property= "Password"column= "Password"/>        <Association Property= "Role"Javatype= "Com.hoo.entity.Role">             <ID Property= "id"column= "id"/>             <result Property= "Leave"column= "R_leave"/>             <result Property= "Name"column= "Name"/>        </Association>    </Resultmap></Mapper>

Mapper.xml set, Check the time, directly check two tables, mybatis automatic corresponding data :

<SelectID= "Getallaccount"Resulttype= "List"Resultmap= "Accountmap.accountresultmap">        <! [Cdata[SELECT * FROM account a,role R where A.roleid=r.id]]>    </Select>        <!--Accountresultmap is defined in the Account-resultmap.xml resultmap -    <SelectID= "Getaccountbyid"ParameterType= "string"Resulttype= "Com.hoo.entity.Account"Resultmap= "Accountmap.accountresultmap">        <! [Cdata[SELECT * FROM account a,role r where A.accountid = #{id} and A.roleid=r.id]]>    </Select><!--Auto-Generate ID policy -    <InsertID= "AddAccount"Usegeneratedkeys= "true"Keyproperty= "AccountId"ParameterType= "Account">INSERT into account (AccountId, status, username, Password,phone,email,roleid) VALUES (#{accountid}, #{st ATUs}, #{username}, #{password},#{phone},#{email},#{role.id})</Insert>

MyBatis Multi-table query, auto-Generate ID

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.