论坛首页 Java企业应用论坛

IBatis3.0 中一点典型特性

浏览 5806 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-02-22  
在IBatis2.X中也有一些类似的特性,这里仅仅发表一些IBatis3.0中的使用。这些特性在2.X中都有,可能部分不同,需要注意。
写道
在数据可以自动生成主键是使用
<!-- mysql /mssql -->
<insert id="addBlog" parameterType="Blog" useGeneratedKeys="true"
keyProperty="blogid" >
insert into Blog(author,subject,content, publishTime,blogid)
values(#{author}#,#{subject}#,#{content}#,#{publishTime}#,#{blogid})
</insert>
针对Oracle序列的产生方式

<!-- oracle -->
<insert id="addBlog" parameterType="Blog" >
<selectKey keyProperty="blogid" resultType="int" order="BEFORE">
select seq_order.nextval() from dual
</selectKey>
insert into Blog(author,subject,content, publishTime,blogid)
values(#{author}#,#{subject}#,#{content}#,#{publishTime}#,#{blogid})
</insert>

<!--
多表查询的使用
-->
<select id="selectBlogDetails" parameterType="int" resultMap="blogResultMap" >
select
B.id as blogid,
A.author as author
from Blog B
left outer join Author A
on B.author_id=A.id
where B.id=#{id}
</select>

<!--
 动态查询的应用
if  choose when 的使用
-->
<select id="findActiveBlogWithTitleLike" parameterType="Blog" resultType="Blog">
select * from Blog
where state='ACTIVE'
<if test="title!=null">
and title like #{title}
</if>
<if test="author!=null and author.name!=null">
and title like #{author.name}
</if>
</select>


<select id="findActionBlogLike" parameterType="Blog" resultType="Blog">
select * from Blog where state='ACTIVE'
<choose>
<when test="title!=null">
and title like #{title }
</when>
<when test="author!=null and author.name!=null">
and title like #{author.name}
</when>
<otherwise>
and featured=1
</otherwise>
</choose>
</select>

 

   发表时间:2010-02-22  
useGeneratedKeys="true" keyProperty="blogid"

可以指定生成主键是个不错的特性,IBatis2.x 无法标识Entity主键的确是个设计遗憾。

0 请登录后投票
   发表时间:2010-02-23  
raymond2006k 写道
useGeneratedKeys="true" keyProperty="blogid"

可以指定生成主键是个不错的特性,IBatis2.x 无法标识Entity主键的确是个设计遗憾。



ibatis本来就是一个sql mapping,并不要求一定要有主键的,设计的目的就是处理任何的sql,执行什么sql他就不关心的
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics