MyBatis3——输出参数ResultType、动语态sql

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

MyBatis3——输出参数ResultType、动语态sql

若雨eclipse   2020-01-23 我要评论
输出参数ResultType
1、输出参数为简单类型(8个基本+String)
2、输出参数为对象类型
3、输出参数为实体对象类型的集合:虽然输出类型为集合,但是resultType依然写集合的元素类型,eg:resultType="person"
4、输出参数类型为HashMap          --->一个HashMap对应一个人的多个元素(多个属性);查询所有人的属性:List<HashMap<String, Object>>
 
resultType和resultMap的区别:
resultType:实体类的属性、数据表的字段:类型、名字相同时
resultMap:实体类的属性、数据表的字段:类型、名字不同时
注意:当属性名和字段名不一致时,除了使用resultMap外还可以使用resultType+HashMap
resultType+HashMap方法:select 表的字段名 “类的属性名” from 
eg:<select id="queryPersonOutByHashMap" resultType="HashMap">
      select id "pid",name "pname" from person where id=1
   </select>
 
动语态sql:
动态查询
<select id="queryPersonbyNameorAgeWithSqlTag" parameterType="Person" resultType="Person">
        select id,name,age from person
        <where> <!-- <where>只能解决第一个<if>里面的and -->
            <if test="name!=null and name!=''">
                and name=#{name}        
            </if>
            <if test="age!=null and age!=0">
                and age=#{age}
            </if>
        </where>
    </select>
foreach:
<foreach>迭代的类型:数组、对象数组、集合、属性
查询语句:select * from person WHERE id in(1,2,3);
<!-- foreach查询   #{id}填补item separator指定分隔符-->
    <select id="queryPersonsbyIds" parameterType="grade" resultType="Person">
        select * from person
        <where>
            <if test="ids!=null and ids.size>0">
                <foreach collection="ids" open="id in (" close=")"
                item="id" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>
简单类型的array数组:
无论传递什么参数名,都用array代替。
<!-- 将多个元素放到数组里进行查询  必须是array代替数组-->
<select id="queryPersonsWithArray" parameterType="int[]" resultType="Person">
        select * from person
        <where>
            <if test="array!=null and array.length>0">
                <foreach collection="array" open="id in (" close=")"
                item="id" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>
list集合:
无论传递什么参数名,都用list代替。
<!-- 将多个元素放到list集合进行查询  必须是list-->
    <select id="queryPersonsWithList" parameterType="list" resultType="Person">
        select * from person
        <where>
            <if test="list!=null and list.size>0">
                <foreach collection="list" open="id in (" close=")"
                item="id" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>
对象数组:
<!-- 将多个元素放到对象数组里进行查询 ,必须是array ,parameterType="Object[]"-->
    <select id="queryPersonsWithObjectArray" parameterType="Object[]" resultType="Person">
        select * from person
        <where>
            <if test="array!=null and array.length>0">
                <foreach collection="array" open="id in (" close=")"
                item="person" separator=",">
                    #{person.id}
                </foreach>
            </if>
        </where>
    </select>
 
SQL片段:
    将相似功能代码提取出来,再进行引用。
步骤1、将代码提取出来;
<sql id="ObjectArrayIds">
    代码片段
</sql>
步骤2、用到时用id引用。
<include refid="ObjectArrayIds"></include>
注意:sql片段与引用处不在一个文件里的话refid前面加上namespace的值。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们