ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2020-01-23 15:01:54  阅读:304  来源: 互联网

标签:语态 MyBatis3 person resultType ResultType 参数 类型 id select


输出参数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,person,resultType,ResultType,参数,类型,id,select
来源: https://www.cnblogs.com/ghlz/p/12230619.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有