MyBatis高级映射:一对一

来源:互联网 发布:淘宝描述不符处罚 编辑:程序博客网 时间:2024/05/29 11:22

说一下mybatis复杂映射,一对一,定义内嵌的resultMap。一个用户对应一个地址:简单的映射用resultType就好了
用户表:
这里写图片描述
地址表:
这里写图片描述
javabean实体类:

public class Persons implements Serializable{    private static final long serialVersionUID = 1396106231553523339L;    private int ID;    private String name;    private String age;    private String sal;    private int addressID;    private Addresst address;
public class Addresst implements Serializable{    private static final long serialVersionUID = 4125186087709383897L;    private int ID;    private String city;    public int getID() {        return ID;    }

接口定义的方法:

package cn.xyl.com.dao;import org.springframework.stereotype.Repository;import cn.xyl.com.entity.Persons;@Repository("personDao")public interface PersonDao {    Persons getAll(int ID);}

mapper.xml映射文件内容:

<mapper namespace="cn.xyl.com.dao.PersonDao">    <resultMap type="cn.xyl.com.entity.Persons" id="All">    <id property="ID" column="ID"/>    <result property="name" column="name"/>    <result property="age" column="age"/>    <result property="sal" column="sal"/>    <result property="addressID" column="addressID"/>    <association property="address" javaType="cn.xyl.com.entity.Addresst">    <id property="ID" column="ID"/>    <result property="city" column="city"/>    </association>    </resultMap>    <select id="getAll" parameterType="int" resultMap="All">    SELECT p.ID,p.name,p.age,p.sal,p.addressID,a.ID ,a.city     FROM person p,addresst a     WHERE p.addressID=a.ID     AND p.addressID=#{ID}     </select></mapper>

上面peoperty指的是javabean的属性,column指的是数据库的字段。映射address(Addresst对象的引用) 实体属性使用association(关联)映射。
结果:
这里写图片描述

阅读全文
0 0
原创粉丝点击