Mybaits中映射关系多对多(三)

多对多的话,这里用的是role跟user两个实体
1、创建实体对象
【Mybaits中映射关系多对多(三)】Role.java

package com.nieshenkuan.model; import java.util.HashSet; import java.util.List; import java.util.Set; public class Role {private Long role_id; private String role_name; private String role_memo; private List users; public List getUsers() { return users; }public void setUsers(List users) { this.users = users; }public Long getRole_id() { return role_id; }public void setRole_id(Long role_id) { this.role_id = role_id; }public String getRole_name() { return role_name; }public void setRole_name(String role_name) { this.role_name = role_name; }public String getRole_memo() { return role_memo; }public void setRole_memo(String role_memo) { this.role_memo = role_memo; }@Override public String toString() { return "Role [role_id=" + role_id + ", role_name=" + role_name + ", role_memo=" + role_memo + ", users=" + users + "]"; }}

User.java
package com.nieshenkuan.model; import java.util.List; public class User { private Long user_id; private String user_name; private String user_code; private String user_password; private List roles; public List getRoles() { return roles; }public void setRoles(List roles) { this.roles = roles; }public Long getUser_id() { return user_id; }public void setUser_id(Long user_id) { this.user_id = user_id; }public String getUser_name() { return user_name; }public void setUser_name(String user_name) { this.user_name = user_name; }public String getUser_code() { return user_code; }public void setUser_code(String user_code) { this.user_code = user_code; }public String getUser_password() { return user_password; }public void setUser_password(String user_password) { this.user_password = user_password; }@Override public String toString() { return "User [user_id=" + user_id + ", user_name=" + user_name + ", user_code=" + user_code + ", user_password=" + user_password + ", roles=" + roles + "]"; }}

2、创建RoleMapper.xml文件
select r.*,u.* from role r,user u,sys_user_role ur where r.role_id=ur.role_id and u.user_id=ur.user_id

3、创建dao接口RoleMapper.java
package com.nieshenkuan.dao; import java.util.List; import com.nieshenkuan.model.Role; public interface RoleMapper {public List selectRoleUser(); }

4、将RoleMapper.xml注册到sqlMapperConfig文件中去

5、测试
package com.nieshenkuan.test; import java.io.IOException; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import com.nieshenkuan.dao.RoleMapper; import com.nieshenkuan.model.Role; import com.nieshenkuan.model.User; import com.nieshenkuan.util.MyBatisUtil; public class TestManyToMany {@Test public void test1() throws IOException { SqlSession sqlSession = MyBatisUtil.getSqlSession(); RoleMapper roleMapper = sqlSession.getMapper(RoleMapper.class); List selectRoleUser = roleMapper.selectRoleUser(); for (Role role : selectRoleUser) { List users = role.getUsers(); for (User user : users) { System.out.println(role.getRole_id() + "" + user.getUser_id()); } // System.out.println(role); } } }

6、测试结果
11 12 21 22

    推荐阅读