zoukankan      html  css  js  c++  java
  • MyBatis3——输出参数ResultType、动语态sql

    输出参数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的值。
  • 相关阅读:
    c# webapi 在swagger里添加全局过滤器
    axios get,post请求时带headers
    svn"重新定位"提示版本库uuid不匹配
    DevExpress XtraReport
    DevExpress GridControl List绑定方式下新增行的方法
    对Dev的GridControl/GridView控件进行分组并展开操作
    DevExpress GridControl控件行内新增、编辑、删除添加选择框
    JavaScript 中的FileReader对象(实现上传图片预览)
    数据库连接情况查询
    C#代码中实现两个表(DataTable)的关联查询(JOIN)
  • 原文地址:https://www.cnblogs.com/ghlz/p/12230619.html
Copyright © 2011-2022 走看看