mybatis框架实现关联查询

来源:互联网 发布:大数据研究生考试科目 编辑:程序博客网 时间:2024/06/05 09:11

在关联查询中此处介绍两种情况:一对一和一对多

1.一对一

假设数据库中有表person和card;其中person表的字段有pid,pname,page,psex,cid;card表有字段cid,cnum;

如实现在person表和身份证两张表中查询某个人的姓名和身份证号码;(属于1对1)

方法1:用扩展类的方式将多表的查询转换成了单类的查询(推荐)
            1-扩展一个封装类PersonVo extends Person  ,在类中扩展person不具有的属性;此处是向personVo中加入cnum属性;
            2-定义接口方法  ,注意:返回值类型时扩展类
            3-封装sql语句

(1)mapper.xml配置文件:

<select id="selectpersonAndCardByPid" parameterType="int"resultType="personVo">SELECT p.*,c.cnum FROM person p,card c WHERE p.cid=c.cidand pid=#{value}</select>

(2)mapper接口中定义的方法:

public List<PersonVo> selectpersonAndCardByPid(int pid);

(3)使用junit测试结果:

@Testpublic void testselectpersonAndCardByPid(){//关联查询一对1List<PersonVo> l=pm.selectpersonAndCardByPid(1);System.out.println(l.size());System.out.println();for (PersonVo personVo : l) {System.out.println(personVo.getPname()+","+personVo.getCnum());}}

运行结果:


方法2:resultsMap
            1-在主类中(person) 添加一个从类(card)的引用
            2-定义接口方法(返回值就是person对象):public Person selectPersonAndCardByPId1(int pid);
            3-sql:
               1-定义sql
               <select id="selectPersonAndCardByPId1" parameterType="int" resultMap="p_c">
                    SELECT p.*,c.cnum FROM person p,card c WHERE p.cid=c.cid and p.pid=#{value}
                </select>
               2-定义resultMap

(1)mapper.xml映射文件:

<resultMap type="person" id="p_c"><id column="pid" property="pid" /><result column="pname" property="pname" /><result column="page" property="page" /><result column="psex" property="psex" /><association property="card" javaType="card"><id column="cid" property="cid" /><result column="mycard" property="cnum" /></association></resultMap><select id="selectpersonAndCardByPid2" parameterType="int"resultMap="p_c">SELECT p.*,c.cnum mycard FROM person p,card c WHEREp.cid=c.cid and pid=#{value}</select>
 1-association:作一对一映射
                       1-property:指定person中对card的引用的属性名字
                       2-javaType:指定该引用的Java类型
                       3-子标签:
                          1-id:映射主键
                              1-column:指定查询列表中的字段名
                              2-property:指定card的属性名
                          2-result:映射普通属性

(2)mapper接口中定义的方法:

public List<Person> selectpersonAndCardByPid2(int pid);

(3)使用junit测试结果:

@Test
    public void testselectpersonAndCardByPid(){//关联查询一对1
        
        List<PersonVo> l=pm.selectpersonAndCardByPid(1);
        System.out.println(l.size());
        System.out.println();
        for (PersonVo personVo : l) {
            System.out.println(personVo.getPname()+","+personVo.getCnum());
        }
        
    }

运行结果:


2.一对多

假设数据库中有表person和card;其中person表的字段有pid,pname,page,psex,cid;card表有字段cid,cnum;在加adder住址表,其中有字段aid,aname,pid

此处根据某人的pid查询姓名和住址;

1-在person中添加一个addr的集合引用
            2-方法:public Person selectPersonAndCardAndAddrByPid(int pid);
            3-sql:
               1-sql定义:
               2-resultMap定义:

(1)mapper.xml映射文件:

<resultMap type="person" id="p_c_a" extends="p_c"><collection property="adder" ofType="Adder"><id column="aid" property="aid" /><result column="ashi" property="ashi" /></collection></resultMap><select id="selectpersonAndCardAndAdderByPid" parameterType="int"resultMap="p_c_a">SELECT p.*,c.cnum mycard,a.ashi FROM person p,adder a,card cWHERE p.pid=a.pid AND p.cid=c.cid and p.pid=#{value}</select>
 1-collection:一对多映射
                      1-property:指定person中的addr集合的属性名
                      2-ofType:指定集合属性中的数据的类型(泛型)

(2)mapper接口中定义的方法:

public Person selectpersonAndCardAndAdderByPid(int pid);

(3)使用junit测试结果:

@Testpublic void testselectpersonAndCardAndAdderByPid(){//关联查询一对多Person p=pm.selectpersonAndCardAndAdderByPid(1);System.out.println(p);System.out.println(p.getPname()+","+p.getCard().getCnum());for (Adder adder : p.getAdder()) {System.out.println(adder.getAshi());}}
运行结果:


原创粉丝点击