Java实体类中封装其他实体类并引用

在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>
  • 7
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值