Mybatis级联--DO、DTO、VO的区别

/用户信息
public class Person {
    private Integer id;
    private String name;
    private Integer age;
    private String cardno;//外键信息
    //关系属性 //对象体现关系
    private Info info;//定义一个身份对象接收当前用户身份信息
}

mapper接口

//查询所有用户信息
List<Person> queryAll();

mapper.xml

<!--用来处理结果封装-->
<resultMap id="personMap" type="com.baizhi.entity.Person">
  <id column="id" property="id"/>
  <result column="name" property="name"/>
  <result column="age" property="age"/>
  <result column="cardno" property="cardno"/>
  <!--处理一对一 association 用来处理一对一关联关系标签 
							 property: 用来书写封装的关系属性名 
               javaType:关系属性的java类型
   -->
  <association property="info" javaType="com.baizhi.entity.Info">
    <id column="iid" property="id"/>
    <result column="icardno" property="cardno"/>
    <result column="address" property="address"/>
  </association>
</resultMap>

<!--查询所有-->
<select id="queryAll" resultMap="personMap">
  select p.id,
  p.name,
  p.age,
  p.cardno,
  i.id     iid,
  i.cardno icardno,
  i.address
  from t_person p
  left join t_info i
  on p.cardno = i.cardno
</select>
  • 一对多
    实体
public class Emp {
    private Integer id;
    private String name;
    private Integer age;
    private Date bir;
}
public class Dept {
    private Integer id;
    private String name;
    //对象  关系属性
    private List<Emp> emps;//员工的关系属性
}

mapper接口

public interface DeptDAO {
    //查询所有部门并将每个部门的员工信息查询出来
    List<Dept> queryAll();
}

mapper.xml

<!--resultMap-->
<resultMap id="deptMap" type="com.baizhi.entity.Dept">
  <id column="id" property="id"/>
  <result column="name" property="name"/>
  <!--封装员工信息 collection用来处理一对多关联关系标签
            property: 封装关系属性名
            javaType: 关系属性类型
            ofType :  用来书写关系属性类型中泛型的类型
        -->
  <collection property="emps" javaType="list" ofType="com.baizhi.entity.Emp">
    <id column="eid" property="id"/>
    <result column="ename" property="name"/>
    <result column="age" property="age"/>
    <result column="bir" property="bir"/>
  </collection>
</resultMap>

<!--查询所有部门-->
<select id="queryAll" resultMap="deptMap">
  select
  d.id,
  d.name,
  e.id eid,
  e.name ename,
  e.age,
  e.bir
  from
  t_dept d
  left join t_emp e on d.id = e.deptid
</select>
  • 一对多
    实体
public class Dept {
    private Integer id;
    private String name;
}
public class Emp {
    private Integer id;
    private String name;
    private Integer age;
    private Date bir;
    //关系属性
    private Dept dept; //代表员工部门信息
}

mapper接口

public interface EmpDAO {
    //查询所有员工并查询每个员工的部门
    List<Emp> queryAll();
}

mapper.xml

<resultMap id="empMap" type="com.baizhi.entity.Emp">
  <id column="id" property="id"/>
  <result column="name" property="name"/>
  <result column="age" property="age"/>
  <result column="bir" property="bir"/>
  <!--封装部门信息-->
  <association property="dept" javaType="com.baizhi.entity.Dept">
    <id column="did" property="id"/>
    <result column="dname" property="name"/>
  </association>
</resultMap>

<!--查询所有-->
<select id="queryAll" resultMap="empMap">
  select
  e.id,e.name,e.age,e.bir,
  d.id did,d.name dname
  from t_emp e
  left join t_dept d
  on  e.deptid = d.id
</select>
  • DO、DTO、VO区别
DO:对应数据库表结构
VO:一般用于前端展示用
DTO:用于数据传递(接口入参和接口返回值都可以)

 public class Project{
       private String projectName;
       private String userid;      
 }

public class ProjectDTO{
     private String projectName;
     private User user;
}

public class  UserDO {
    private Integer id;                 //唯一主键
    private Date createdTime;           //创建时间
    private Date updatedTime;           //最后更新时间
    private String name;                //姓名
    private Integer age;                //年龄
    private String gender;              //性别
    private String address;             //住址
    private String password;            //密码
    private String nickName;            //昵称
    private Date birthday;              //生日
    private String education;           //学历
    private String politicalStatus;     //政治面貌,1表示群众,2表示团员,3表示党员,4表示其他,100表示未知
    private Integer companyId;          //公司的ID
    private Integer status;             //数据状态,1表示可用,0表示不可用
 
    //setter and getter
}

public class  UserVO {
    private Integer id;                 //唯一主键
    private String name;                //姓名
    private Integer age;                //年龄
    private String gender;              //性别
    private String address;             //住址
    private String nickName;            //昵称
    private Date birthday;              //生日
    private String education;           //学历
    private String politicalStatus;     //政治面貌,群众、团员、党员等
    private Company company;            //公司
 
    //setter and getter
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PH = 7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值