1 多對多
用戶和角色。步驟:
- 建立2張表,用戶表、角色表
讓用戶表和角色表具有多對多的關係。需要使用中間表,中間表包含各自的主鍵 - 建立2個實體類:用戶實體類和角色實體類。
各自包含對方的集合引用 - 建立2個配置文件
- 實現配置
(1)查詢用戶時,同時得到用戶包含的角色信息
(2)查詢角色時,同時得到角色賦予的用戶信息
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`ID` int(11) NOT NULL COMMENT '編號',
`ROLE_NAME` varchar(30) default NULL COMMENT '角色名稱',
`ROLE_DESC` varchar(60) default NULL COMMENT '角色描述',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `role`(`ID`,`ROLE_NAME`,`ROLE_DESC`) values (1,'院長','管理整個學院'),(2,'總裁','管理整個公司'),(3,'校長','管理整個學校');
DROP TABLE IF EXISTS `user_role`;
CREATE TABLE `user_role` (
`UID` int(11) NOT NULL COMMENT '用戶編號',
`RID` int(11) NOT NULL COMMENT '角色編號',
PRIMARY KEY (`UID`,`RID`),
KEY `FK_Reference_10` (`RID`),
CONSTRAINT `FK_Reference_10` FOREIGN KEY (`RID`) REFERENCES `role` (`ID`),
CONSTRAINT `FK_Reference_9` FOREIGN KEY (`UID`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into `user_role`(`UID`,`RID`) values (41,1),(45,1),(41,2);
2 查詢角色,同時得到角色的用戶信息
2.1 sql測試
2.2 實體類
2.3 DAO
- 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.tzb.dao.IRoleDao">
<resultMap id="roleMap" type="Role">
<id property="roleId" column="rid"></id>
<result property="roleName" column="role_name"></result>
<result property="roleDesc" column="role_desc"></result>
<collection property="users" ofType="User">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<result property="address" column="address"></result>
</collection>
</resultMap>
<select id="findAll" resultMap="roleMap">
select u.*,r.id as rid,r.role_name,r.role_desc from role r
left outer join user_role ur on r.id = ur.rid
left outer join user u on u.id = ur.uid
</select>
</mapper>
2.4 單元測試
public class RoleTest {
private InputStream in;
private SqlSession sqlSession;
private IRoleDao roleDao;
@Before
public void init() throws IOException {
// 1.讀取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.創建 SqlSessionFactory 工廠
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
// 3.使用工廠生產 SqlSession 對象
sqlSession = factory.openSession();
// 4.使用 SqlSession 創建DAO接口的代理對象
roleDao = sqlSession.getMapper(IRoleDao.class);
}
@After
public void destory() throws IOException {
// 提交事務
sqlSession.commit();
//6.釋放資源
sqlSession.close();
in.close();
}
@Test
public void testFindAll(){
List<Role> roles = roleDao.findAll();
for (Role role : roles) {
System.out.println("----每一個角色------");
System.out.println(role);
System.out.println(role.getUsers());
System.out.println("====================");
}
}
}
3 查詢用戶,同時得到角色
3.1 sql 測試
3.2 實體類
3.3 DAO
public interface IUserDao {
List<User> findAll();
User findById(Integer userId);
}
- IUserDao.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.tzb.dao.IUserDao">
<select id="findById" parameterType="int" resultType="com.tzb.domain.User">
select * from user where id=#{userId}
</select>
<resultMap id="userMap" type="user">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<result property="address" column="address"></result>
<collection property="roles" ofType="Role">
<id property="roleId" column="rid"></id>
<result property="roleName" column="role_name"></result>
<result property="roleDesc" column="role_desc"></result>
</collection>
</resultMap>
<select id="findAll" resultMap="userMap">
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>
3.4 單元測試
@Test
public void testFindAll(){
List<User> users = userDao.findAll();
for (User user : users) {
System.out.println("---- 每個用戶信息 ----");
System.out.println(user);
System.out.println(user.getRoles());
System.out.println("======================");
}
}