输出参数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的值。

MyBatis3——输出参数ResultType、动语态sql的更多相关文章

  1. sql server使用sp_executesql返回拼接字符串里面的输出参数

    问题: 今天一同事请教博主,他拼接了一个语句,select表格形式数据,然后使用@@rowcount获取到行数. 但他又有这样特别的需求:想只获取行数而不返回表格数据结果,因为是while循环,不想返 ...

  2. mybatis入门系列二之输入与输出参数

    mybatis入门系列二之详解输入与输出参数   基础知识   mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回 ...

  3. 动态SQL的执行,注:exec sp_executesql 其实可以实现参数查询和输出参数的

    本文转自:http://www.cnblogs.com/hnsdwhl/archive/2011/07/23/2114730.html 当需要根据外部输入的参数来决定要执行的SQL语句时,常常需要动态 ...

  4. Oracle中使用PL/SQL如何定义参数、参数赋值、输出参数和 if 判断

    1.pl/sql如何定义参数 declare --1)定义参数 -- ban_Id number; ban_Name ); 2.pl/sql如何参数赋值 --2)参数赋值-- ban_Id :; ba ...

  5. sql server 带输入输出参数的分页存储过程(效率最高)

    create procedure proc_page_withtopmax( @pageIndex int,--页索引 @pageSize int,--每页显示数 @pageCount int out ...

  6. sql 存储过程 输出参数 输入参数

    1.简单的存储过程 create procedure porc_name as select * from 表 go 调用时: exec proc_name 2. 带参数的存储过程 create pr ...

  7. JavaWeb_(Mybatis框架)输入和输出参数_五

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  8. MyBatis - 输入和输出参数

    基础知识 mybatis规定mapp.xml中每一个SQL语句形式上只能有一个@parameterType和一个@resultType 1. 返回值是一个对象的集合,@resultType中只能写该对 ...

  9. MySql.Data.dll 不支持输出参数

    insert INTO stu(name) VALUES('maimai'); set @ReturnValue=@@IDENTITY; string sql="insert INTO st ...

随机推荐

  1. ASP.NET MVC 实现页落网资源分享网站+充值管理+后台管理(14)之会员中心管理

    源码下载地址:http://www.yealuo.com/Sccnn/Detail?KeyValue=c891ffae-7441-4afb-9a75-c5fe000e3d1c 会员中心,主要包含了会员 ...

  2. 【50.40%】【BZOJ 4553】[Tjoi2016&Heoi2016]序列

    Time Limit: 20 Sec  Memory Limit: 128 MB Submit: 371  Solved: 187 [Submit][Status][Discuss] Descript ...

  3. 装饰器&偏函数与作用域与异常处理与文件读写

    装饰器 概念:是一个闭包,把一个函数当做参数返回一个替代版的函数,本质上就是一个返回函数的函数 简单的装饰器 def func1(): print("sunck is a good man& ...

  4. 【Jenkins】pipeline-hello-world项目

    1.New Item 2.Pipeline Definition 3.Build Error 4.Solution 5.Console Output

  5. linux_jdk,zookeeper,kafka安装

    若是在虚拟机安装最好先进行下面的安装 1.1修改各个虚拟机主机名 vi /etc/sysconfig/network 1.2修改主机名和IP的映射关系 vi /etc/hosts 1.3关闭防火墙 # ...

  6. c++修改系统环境变量 (修改注册表以后,立刻使用SendMessageTimeout(HWND_BROADCAST进行广播)

    #include "stdafx.h" #include "addPath.h" #define _AFXDLL #include <afxwin.h&g ...

  7. 【萌新向】cartographer_ros最新安装指南 2019-12

    首先说明,这篇文章于2019年12月写下,cartographer现在还在不断更新,也许这篇文档内容会失效,但是大体是一样的安装流程. 一.硬件条件 使用平台 x86_64平台 ROS发行版 Indi ...

  8. 解析GMT+N时区,返回日期类型

    涉及到正则表达式,时区转换. /** * * 按格式 yyyy-MM-dd HH:mm:ss 以指定GMT时区进行解析,返回对应的当前系统时区当地时间. * @param dateString  格式 ...

  9. ubuntu conda install ERROR missing write permission错误

    报错: ondaIOError: Missing write permissions in: /usr/local/anaconda3 # # You don't appear to have the ...

  10. 【题解】P4091 [HEOI2016/TJOI2016]求和

    [题解]P4091 [HEOI2016/TJOI2016]求和 [P4091 HEOI2016/TJOI2016]求和 可以知道\(i,j\)从\(0\)开始是可以的,因为这个时候等于\(0\).这种 ...