Mybatis--表关联one-to-one

来源:互联网 发布:西北师大知行学院贴吧 编辑:程序博客网 时间:2024/04/30 10:55

【1】两个表结构,其中t_class表拥有外键 t_id,对应t_teacher表主键,t_id

表结构如下:

这里写图片描述

这里写图片描述

【2】对应的model

Classes:

public class Classes {    private int id;    private String name;    private Teacher teacher;    ...}

Teacher:

public class Teacher {    private int id;    private String name;    ...}

【3】classMapper.xml配置

<mapper namespace="com.web.mapper.classMapper"><!-- ************the first way--嵌套结果************ -->    <select id="getClass" parameterType="int" resultMap="classResultMap">        select * from t_class c,t_teacher t        where c.t_id = t.t_id         and c.c_id = #{id}    </select>     <!-- 可以解决model属性名与数据表中column列名不一致问题 jdbcType一定要大写 -->        <resultMap type="Classes" id="classResultMap">        <id property="id" column="c_id" javaType="int" jdbcType="INTEGER"/>        <result property="name" column="c_name" javaType="string" jdbcType="VARCHAR"/>        <!-- one to one or one to many ,the many side !!  here,use association not collection-->        <association property="teacher" column="t_id" javaType="Teacher" >        <!--here,the column='t_id' is not necessary !-->            <!-- javaType=Teacher||teacher type=Classes||classes the two all right !!! -->            <id property="id" column="t_id"  javaType="int" jdbcType="INTEGER"/>            <result property="name" column="t_name"  javaType="string" jdbcType="VARCHAR"/>        </association>     </resultMap> <!-- ********the two way--return resultMap********** -->     <select id="getClassById" parameterType="int" resultMap="classResultMap2">        select * from t_class c where c.c_id = #{id}    </select>     <resultMap type="java.util.Map" id="classResultMap2">        <result property="id" column="c_id" javaType="int" jdbcType="INTEGER"/>        <result property="name" column="c_name" javaType="string" jdbcType="VARCHAR"/>     </resultMap> <!-- *********the three way--嵌套查询*************** -->     <select id="getClass3" parameterType="int" resultMap="classResultMap3">        select * from t_class c where c.c_id = #{id}    </select>    <!-- resultType should use column alias !!! -->     <select id="getTeacher" parameterType="int" resultType="Teacher">        select t_id id,t_name name from t_teacher where t_id = #{id}    </select>     <resultMap type="Classes" id="classResultMap3">        <result property="id" column="c_id" javaType="int" jdbcType="INTEGER"/>        <result property="name" column="c_name" javaType="string" jdbcType="VARCHAR"/>        <association property="teacher" javaType="Teacher" select="getTeacher" column="t_id">        <!-- here,the javaType is not necessary !-->        </association>     </resultMap></mapper>

【4】Test

    • test—getClass:
@Test    public void testSelect(){        /*set auto commit ,which equals to the above*/        SqlSession session = MybatisUtils.getFactory().openSession(true);        String statement = "com.web.mapper.classMapper.getClass";        /*return the effect rows*/        Classes c = session.selectOne(statement, 1);        System.out.println("result.."+c);        Teacher teacher = c.getTeacher();        System.out.println("get from class..."+teacher);    }

result as follows :

这里写图片描述

    • test-getClassById:

测试返回为map,controller中手动封装为Classes对象–这里没有附带teacher

@Test    public void testSelect2(){        /*set auto commit ,which equals to the above*/        SqlSession session = MybatisUtils.getFactory().openSession(true);        String statement = "com.web.mapper.classMapper.getClassById";        /*return the effect rows*/        HashMap<String, Object> classMap = session.selectOne(statement, 1);        Classes classes = new Classes(classMap.get("name")+"");        System.out.println("result.."+classes+','+classes.getClass());    }

result as follows :

这里写图片描述

    • test-getClass3 :

嵌套查询

    @Test    public void testSelect3(){        /*set auto commit ,which equals to the above*/        SqlSession session = MybatisUtils.getFactory().openSession(true);        String statement = "com.web.mapper.classMapper.getClass3";        /*return the effect rows*/        Classes classes = session.selectOne(statement, 1);        System.out.println("result.."+classes+','+classes.getClass());    }

result as follows :

 result..Classes [id=1, name=计算机, teacher=Teacher [id=1, name=李明], list=null],class com.web.model.Classes

【5】标签说明

association : 用于一对一的关联查询 ;property : 对象属性名称 ;javaType : 对象属性类型 ;column : 所对应的外键字段名称 ;select : 使用另一个查询封装的结果 ; parameterType : 参数类型 ;resultType : 返回的结果类型 ;jdbcType : 对应的jdbc字段类型,请注意要大写 !另外与使用数据库字段类型一致!!
0 0
原创粉丝点击