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的值。
  • 相关阅读:
    java基础
    JAVASE 安装步骤
    MySQL 45道练习题
    MySQL 多表查询
    2018-08-07JDBC连接MySQL+增删改表格+SQL注入问题及其预处理对象PreparedStatement解决方案
    2018-08-06Java中的异常捕获和Throw详细细节
    2018-08-03List接口方法+LinkedList方法+Vector集合+Set接口下HashSet和LinkedHashSet集合+HashCode()+equals()方法对于Set接口判断重复的详细细节
    2018-08-01集合Collection+Iterator迭代器+泛型+增强For循环
    2018-07-31包装类与基本数据类型String的转换+System类详细知识+Arrays类+大数据(BigInteger+BigDecimal)运算
    2018-07-27Final+Static+匿名对象+3中代码块
  • 原文地址:https://www.cnblogs.com/ghlz/p/12230619.html
Copyright © 2011-2022 走看看