在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>