在Java开发过程中有很多情况是二个表及以上的联合操作,这是需要分清楚表的主次关系。在引用的时候有人会把二个表的数据全都封装在一个实体类中,然后在对这个实体类进行操作,但如果是三个表呢?四个表呢?还都封装在一个实体类吗?这样被封装的实体类的数据是不是太多了呢?而且也会照成层次不分,大大的抬高了代码的耦合性。和实体类泛滥的情况。下面开始介绍该如何正确的封装实体类
第一个实体类
public class UsersLogin {
private Integer usersId;
private String usersName;
private String pass;
private int flag;
//省略Getter和Setter
}
第二个实体类
public class Shoopping {
private Integer shoopId;
private String goodsName;
private Double price;
private String address;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date produceDate;
private String picture;
private String categoryName;
private int num;
private int flag;
//引用第一个表的实体对象
private UsersLogin usersLogin;
//省略Getter和Setter
}
使用mapper.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.springboot.mapper.ShiroUserMapper">
<resultMap autoMapping="true" type="com.springboot.entity.UsersLogin " id="goodsMap">
<id property="usersId" column="usersId"/>
<association autoMapping="true" property="shiroUser" javaType="com.springboot.entity.Shoopping ">
<id property="shoopId" column="shoopId"/>
</association>
<!--
复杂的属性我们需要单独处理
对象使用 : association
集合使用 : collection
-->
<!-- collection中的ofType:使用ofType获取 -->
<!-- <collection property="实体类属性名" ofType="接收获取的内容">-->
<!-- <result column="employee_id" property="employeeId"></result>-->
<!-- </collection>-->
</resultMap>
<select id="findUserList" resultMap="goodsMap">
select * from UsersLogin u,Shoopping r where u.usersId=r.shoopId
</select>
</mapper>
查询结果(我这展示的结果跟上面的实体类不对应,但大致结果是这种格式的)
[
UserList{id=1, user_name='admin@shiro.con', password='admin', role=Role{id=1, role_name='admin', user_name='admin@shiro.con'}},
UserList{id=2, user_name='test@shiro.con', password='e10adc3949ba59abbe56e057f20f883e', role=Role{id=2, role_name='test', user_name='test@shiro.con'}}]
第二种:创建第三个实体类,直接把这二个实体类封装里面
第一个ShiroUser
public class ShiroUser {
private int id;
private String user_name;
private String password;
//省略里面的方法
}
第二个Shiro_user_role
public class Shiro_user_role {
private int id;
private String role_name;
private String user_name;
//省略方法
}
第三个UserList
public class UserList {
private ShiroUser shiroUser;
private Shiro_user_role role;
//省略方法
}
配置mapper.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.springboot.mapper.ShiroUserMapper">
<resultMap autoMapping="true" type="com.springboot.entity.UserList" id="goodsMap">
<association autoMapping="true" property="role" javaType="com.springboot.entity.Shiro_user_role">
<id property="user_name" column="user_name"/>
</association>
<association autoMapping="true" property="shiroUser" javaType="com.springboot.entity.ShiroUser">
<id property="user_name" column="user_name"/>
</association>
</resultMap>
<select id="findUserList" resultMap="goodsMap">
select * from shiro_user u,shiro_user_role r where u.user_name=r.user_name
</select>
</mapper>
结果:
[
UserList{shiroUser=ShiroUser{id=1, user_name='admin@shiro.con', password='admin'},
role=Role{id=1, role_name='admin', user_name='admin@shiro.con'}
},
UserList{shiroUser=ShiroUser{id=2, user_name='test@shiro.con', password='e10adc3949ba59abbe56e057f20f883e'},
role=Role{id=2, role_name='test', user_name='test@shiro.con'}
}
]
如果需要在html页面显示:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>权限管理</title>
</head>
<body>
<table>
<tr>
<td>id</td><td>用户名</td><td>角色</td>
</tr>
<th:block th:each="u:${userLists}">
<tr>
<td th:text="${u.getShiroUser().getId()}"></td>
<td th:text="${u.getShiroUser().getUser_name()}">
<td th:text="${u.getRole().getRole_name()}">
</tr>
</th:block>
</table>
</body>
</html>