使用 <resultMap> 标签以及<association> 和<collection> 子标签,进行关联查询.
Pojo里面的User类
public class User implements Serializable {
private Integer id;
private String username;
private String address;
private Date birthday;
private String sex;
private List<Role> roles;
public List<Role> getRoles() {
return roles;
}
public void setRoles(List<Role> roles) {
this.roles = roles;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", address='" + address + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
'}';
}
}
Pojo里面的Role类
public class Role implements Serializable {
private Integer roleId;
private String roleName;
private String roleDesc;
private List<User> users;
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getRoleDesc() {
return roleDesc;
}
public void setRoleDesc(String roleDesc) {
this.roleDesc = roleDesc;
}
@Override
public String toString() {
return "Role{" +
"roleId=" + roleId +
", roleName='" + roleName + '\'' +
", roleDesc='" + roleDesc + '\'' +
'}';
}
}
Dao层的接口
List<Role> findAll();
IRoleDao.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xia.dao.IRoleDao">
<resultMap id="roleMap" type="com.xia.domo.Role">
<id property="roleId" column="rid"></id>
<result column="role_name" property="roleName"></result>
<result column="role_desc" property="roleDesc"></result>
<collection property="users" ofType="com.xia.domo.User" >
<id property="id" column="id"></id>
<result column="username" property="username" ></result>
<result column="address" property="address" ></result>
<result column="sex" property="sex" ></result>
<result column="birthday" property="birthday" ></result>
</collection>
</resultMap>
<select id="findAll" resultMap="roleMap">
SELECT u.*,r.id AS rid,r.role_name,r.role_desc FROM USER u
LEFT OUTER JOIN user_role ur ON u.id=ur.uid
LEFT OUTER JOIN role r ON r.id=ur.rid
</select>
</mapper>
user表: user_role表: role表:
此处sql查询的结果为:
查询如有不懂参考:SQL 内连接(inner join)与外连接(left outer join 、right outer join )区别.
测试代码:
public class RoleTest {
private InputStream in;
private SqlSession sqlSession;
private IRoleDao iRoleDao;
@Before
public void init() throws IOException {
in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory= new SqlSessionFactoryBuilder().build(in);
sqlSession=factory.openSession();
iRoleDao = sqlSession.getMapper(IRoleDao.class);
}
@After
public void destroy()throws Exception{
sqlSession.close();
in.close();
}
@Test
public void TestFindAll() throws IOException {
List<Role> roles = iRoleDao.findAll();
for (Role role:roles) {
System.out.println(role);
System.out.println(role.getUsers());
}
}
}
结果: 结果相符,很nice!
|