Example of MyBatis association query:
[Html]
<ResultMap type = "UserView" id = "userAndRoleViewResultMap">
<Id column = "user_id" property = "userId"/>
<Result column = "loginname" property = "loginname"/>
<Result column = "username" property = "username"/>
<Result column = "password" property = "password"/>
<Result column = "user_rights" property = "rights"/>
<Result column = "status" property = "status"/>
<Result column = "last_login" property = "lastLogin"/>
<Association property = "role" column = "role_id" javaType = "Role">
<Id column = "role_id" property = "roleId"/>
<Result column = "role_name" property = "roleName"/>
<Result column = "role_rights" property = "rights"/>
</Association>
</ResultMap>
[SQL]
<Select id = "listPageUser" parameterType = "User" resultMap = "userAndRoleResultMap">
Select u. user_id, u. username, u. loginname, u. password, r. role_id, r. role_name, u. last_login
From tb_user u
Left join tb_role r on u. role_id = r. role_id
Where u. status = 0
<If test = "loginname! = Null and loginname! = ''">
And u. loginname like "%" # {loginname} "%"
</If>
<If test = "roleId! = Null and roleId! = 0 ">
And u. role_id =#{ roleId}
</If>
<If test = "lastLoginStart! = Null ">
And u. last_login >=#{ lastLoginStart}
</If>
<If test = "lastLoginEnd! = Null ">
And u. last_login <=#{ lastLoginEnd}
</If>
</Select>
Author: jpr1990