Mybatis(二)返回值、表查询

作者: 其实我很菜啊 | 来源:发表于2017-12-02 16:55 被阅读96次

    之前对Mybatis有了初步的学习与使用,最近学习ssh发现ssh太重,我个人觉得,像hibernate事务,很多情况下没有使用,而缓存,大部分时候也用不上,需要用到的场景,完全可以自已开发,更轻,而数据关系变得复杂后,hibernate越难驾驭,相比而言mybatis更灵活,必要时,直接写SQL。所以再次对Mybatis进一步的进行学习。

    返回值

    Mybatis 中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部resultMap的引用,但是resultMap和resultType不能同时存在。
    在Mybatis进行查询映射时,其实查询出的每一个属性都是放在一个对应的Map里面的,其中key是属性名,values则是其对应的值。

    • 当提供的返回值类型属性是resultType时,Mybatis会将Map里面的key的value取出给resultType所指定对应的属性,其实Mybatis的每一个查询映射的返回值类型都是resultMap,只是当提供的返回值类型属性resultType的时候,Mybatis对其自动的给把对应的值献给resultType所指定对象的属性。
    • 当提供的返回值类型是resultMap时,因为Map不能很好表示,就需要自己再进行一步把它转换为对应的对象,这常常在复杂的查询中很有作用如:assoiation、collection。
    使用resultType类型
    <!-- resultType对应JavaBean类型 -->
        <select id="selectAll" resultType="cn.softjx.modle.User">
            select t_id id,t_username username,t_password password from t_user;
        </select>
    

    可以看到在select语句中都进行了字段名的替换,因为在javabean中属性的命名与在数据库中真实的字段名是不一样的,所以需要在sql中进行手动的代换,这是字段比较少的情况如果数据多就会使sql语句变得复杂,不便于维护修改。

    使用resultMap类型
    <resultMap type="cn.softjx.modle.User" id="UserMap">
            <result column="t_id" property="id"/>
            <result column="t_username" property="username"/>
            <result column="t_password" property="password"/>
        </resultMap>
        
        <select id="selectAll2" resultMap="UserMap" >
            select t_id,t_username,t_password from t_user;
        </select>
    

    使用resultMap在进行sql语句查询时不用在进行手动的替换,交给Mybatis自动映射完成,得到的结果是一致的。

    • column 字段对应的应当是数据库查询结果字段,而不是数据库中的字段
    • property 与Javabean中属性值一致

    查询标签中的resultMap要与表映射的resultMap标签中的id一致。

    表查询

    • 多对一

    多对一在实际项目中经常用到,比如多个学生对应一个学校多个工人对应一个工厂。可以通过在数据库中进行外键的关联物理上的实现也可以通过sql语句实现逻辑上的关联,在此不再赘述。无论通过那种关联在Mybatis的映射文件配置基本上是一置的。
    本例举多个学生对一个学校的例子:

    • User.java
    public class User {
        private Integer id;
        private String username;
        private String password;
        private Integer sid;
        
        private School school;
    }
    

    当然还要提供相应的set、get方法,在UserBean里面声明了一个School对象,要取出学校名称时可以通过这个schoo对象的get方法将名字取出。

    实现方式一
    • UserMapping.xml
    <resultMap type="cn.softjx.modle.User" id="UserMap">
            <result column="t_id" property="id"/>
            <result column="t_username" property="username"/>
            <result column="t_password" property="password"/>
            <result column="t_sid" property="sid"/>
            <result column="s_name" property="school.name"/>
        </resultMap>
    <select id="getSchoolName" resultMap="UserMap" >
            select t_id,t_username,t_password,s_name FROM t_user,school where t_sid=s_id AND s_id=1;
        </select>
    

    可以看到在resultMap中不配置了User自身的属性还多了<result column="s_name" property="school.name"/>,即我们需要哪个附表的值就要在resultMap中配置相应的附表映射。

    实现方式二
    <resultMap type="cn.softjx.modle.User" id="UserMap">
            <result column="t_id" property="id"/>
            <result column="t_username" property="username"/>
            <result column="t_password" property="password"/>
            <result column="t_sid" property="sid"/>
            <association property="school" javaType="cn.softjx.modle.School">
                <result column="s_name" property="name" />
            </association>
        </resultMap>
    <select id="getSchoolName" resultMap="UserMap" >
            select t_id,t_username,t_password,s_name FROM t_user,school where t_sid=s_id AND s_id=1;
        </select>
    

    方式二引入了association标签,不再在原有user映射关系上进行补充,此标签中的property属性值为UserBean中附表对象的引用名,javaType即为Schooljava类的的全路径。sql语句不变

    多对一结果.png

    可以看到通过mybatis查询出的结果与在数据库中查询的结果是一致的。

    • 一对多

    一对多与多对一的配置有许多相似点,一对多返回的类型是一个集合所以要在SchoolBean中加入学生的集合
    School.java

    public class School {
        private Integer id;
        private String  name;
        
        private List<User> user;
    }
    

    学校映射文件配置

    <resultMap type="cn.softjx.modle.School" id="SchoolMap">
            <result column="s_id" property="id"/>
            <result column="s_name" property="name"/>
            <collection property="user" ofType="cn.softjx.modle.User">
                <result column="t_id" property="id"/>
                <result column="t_username" property="username"/>
                <result column="t_password" property="password"/>
                <result column="t_sid" property="sid"/>         
            </collection>
    
        </resultMap>
        
        <select id="getSchool" resultMap="SchoolMap">
            select s_id,s_name,t_id,t_username,t_password
            FROM school,t_user WHERE s_id=t_sid AND s_id=1;
        </select>
    

    标签属性含义与多对一配置差不多不再详说

    相关文章

      网友评论

        本文标题:Mybatis(二)返回值、表查询

        本文链接:https://www.haomeiwen.com/subject/pcrgbxtx.html