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