MyBatis的几种应用场景
2013-10-24 00:00
453 查看
有幸在项目中用到了这个框架的大部分高级特性,比如一二级缓存,动态sql等等。真正的让我爱上了这个框架,本篇说一说一些典型的sql场景,看看这个框架是怎么来处理的。
insert语句返回oracle序列主键:
最普通的,我的表主键是序列来生成(由于是oracle),所以我需要插入时候生成序列主键并且返回,以下为配置:
selectKey中order属性的值是不同的,BEFORE先选择主键,设置keyProperty的值然后执行插入语句。 AFTER是先执行插入语句,然后执行selectKey。
条件查询时候的入参不固定 :
如果有uuid和portletid这任意其中之一的入参,则执行条件查询,否则为普通查询
update方法的不固定入参
处理clob,blob类型,第一篇博客blablabla写了一堆代码,在这里简单配置一个typehandler就搞定
blob的类型处理器为 B
lobTypeHandler(低版本的使用的是spring orm包的typehandler,只支持mybatis2.0版本,现在MyBatis已经3.2了,Spring太不给面子了,和hibernate是好基友,spring已经支持hibernate4了)
select * from 表名 where 列 in (1,2,3)
入参为cid,index为循环的index,collection定义入参类型,array代表数组,也可以为list,显而易见open,separator和close是帮你拼接sql的,碉堡了有木有!
还有更多的动态sql高级应用和关联查询,具体参看git上的官方文档!
http://mybatis.github.io/mybatis-3/zh/configuration.html#settings
insert语句返回oracle序列主键:
最普通的,我的表主键是序列来生成(由于是oracle),所以我需要插入时候生成序列主键并且返回,以下为配置:
<insert id="save" parameterType="cn.com.blossomframework.services.core.domain.Navigation"> <selectKey resultType="int" keyProperty="id" order="BEFORE"> select s_navigation.nextval as id from dual </selectKey> insert into navigation ( id,name,type,order,isshow,cid,portletid,uuid) values ( #{id},#{name},#{type},#{order},#{isshow},#{cid},#{portletid},#{uuid}) </insert>
selectKey中order属性的值是不同的,BEFORE先选择主键,设置keyProperty的值然后执行插入语句。 AFTER是先执行插入语句,然后执行selectKey。
条件查询时候的入参不固定 :
<select id="findAll" resultMap="navigations"> select * from navigation <where> <if test="portetid != null"> login_name=#{portletid} </if> <if test="uuid != null"> and name=#{uuid} </if> </where> </select>
如果有uuid和portletid这任意其中之一的入参,则执行条件查询,否则为普通查询
<update id="update" parameterType="int" > update navigation <set> <if test="name != null">name=#{name},</if> <if test="isshow != null">isshow=#{isshow},</if> <if test="cid != null">cid=#{cid}</if> </set> where id=#{id} </update>
update方法的不固定入参
处理clob,blob类型,第一篇博客blablabla写了一堆代码,在这里简单配置一个typehandler就搞定
<update id="update" parameterType="cn.com.blossomframework.services.core.domain.WebContent" > update webcontent set title=#{title},content=#{content,typeHandler=org.apache.ibatis.type.ClobTypeHandler} where id=#{id} </update>
blob的类型处理器为 B
lobTypeHandler(低版本的使用的是spring orm包的typehandler,只支持mybatis2.0版本,现在MyBatis已经3.2了,Spring太不给面子了,和hibernate是好基友,spring已经支持hibernate4了)
select * from 表名 where 列 in (1,2,3)
<select id="findAns" resultMap="announcements"> select * from navigation where idref in <foreach item="cid" index="index" collection="array" open="(" separator="," close=")"> #{cid} </foreach> </select>
入参为cid,index为循环的index,collection定义入参类型,array代表数组,也可以为list,显而易见open,separator和close是帮你拼接sql的,碉堡了有木有!
还有更多的动态sql高级应用和关联查询,具体参看git上的官方文档!
http://mybatis.github.io/mybatis-3/zh/configuration.html#settings
相关文章推荐
- mybatis和hibernate的本质区别和应用场景简介
- js判断数据类型的几种方法及应用场景分析
- Oracle Text 全文索引 几种典型应用场景
- springMVC+Mybatis 学习笔记-【2】Mybatis和Hibernate的本质区别和应用场景
- mybatis与hibernate的区别及各自应用场景
- 几种典型的MQ应用场景
- 10Mybatis_mybatis和hibernate本质区别和应用场景
- 几种数据加密方式及其应用场景(转)
- learning opencv 2 几种滤波的应用场景
- mybatis和hibernate区别和应用场景
- Android Context上下文(几种的对比,应用场景)
- RabbitMQ的几种应用场景
- 常见的几种数据加密与应用场景
- 几种智能指针及应用场景
- mybatis与hibernate的区别及各自应用场景
- mybatis和hibernate本质区别和应用场景
- Android几种数据存储方式的应用场景
- 老男孩教育每日一题-第83天-binlog是什么?记录的什么?有几种工作模式及企业应用场景
- Redis几种数据结构的应用场景
- hibernate和mybatis的本质却别和应用场景