myBatis3之SQL映射的XML文件(insert,update,delete 元素)

myBatis3之SQL映射的XML文件(insert,update,delete 元素)

----------

 

insert,update,delete 

 

数据变更语句insert,update和delete在它们的实现中非常相似:

<insert 
	id="insertAuthor" 
	parameterType="domain.blog.Author" 
	flushCache="true" 
	statementType="PREPARED" 
	keyProperty="" 
	useGeneratedKeys="" 
	timeout="20000">

 

<update 
	id="insertAuthor" 
	parameterType="domain.blog.Author" 
	flushCache="true" 
	statementType="PREPARED" 
	timeout="20000">
  
<delete 
	id="insertAuthor" 
	parameterType="domain.blog.Author" 
	flushCache="true" 
	statementType="PREPARED" 
	timeout="20000">
  
属性 描述 
id 在命名空间中唯一的标识符,可以被用来引用这条语句。
parameterType 将会传入这条语句的参数类的完全限定名或别名。
flushCache 将其设置为 true,不论语句什么时候被调用,都会导致缓存被清空。默认值:false。
timeout  

这个设置驱动程序等待数据库返回请求结果,并抛出异常时间的最大等待值。默认不设置

(驱动自行处理)

statementType 

STATEMENT,PREPARED 或 CALLABLE 的一种。这会让 MyBat is使用选择使用 Statement,,PreparedStatement 或 CallableStatement。默认值:PREPARED。

useGeneratedKeys 

(仅对insert有用)这会告诉MyBatis使用JDBC的getGeneratedKeys方法来取出由数据

比如:像MySQL和SQL Server 这样的数据库管理系统的自动递增字段)内部生成的主键。

默认值:false。

keyProperty (仅对insert有用)标记一个属性, MyBatis会通过getGeneratedKeys或者通过 insert 语句的selectKey子元素设置它的值。默认:不设置。

下面就是 insert,update 和 delete 语句的示例:

<insert id="insertAuthor" parameterType="domain.blog.Author"> 
	insert into Author (id,username,password,email,bio) 
	values (#{id},#{username},#{password},#{email},#{bio}) 
</insert> 
<update id="updateAuthor" parameterType="domain.blog.Author"> 
	update Author set 
		username = #{username}, 
		password = #{password}, 
		email = #{email}, 
		bio = #{bio} 
	where id = #{id} 
</update> 
<delete id="deleteAuthor” parameterType="int"> 
	delete from Author where id = #{id} 
</delete>  

它有一些属性和子元素用来处理主键的生成。首先,如果你的数据库支持自动生成主键的字段(比如MySQL和SQL Server),那么你可以设置useGeneratedKeys=”true”,而且设置keyProperty到你已经做好的目标属性上。例如,如果上面的Author表已经对id使用了自动生成的列类型,那么语句可以修改为:

<insert id="insertAuthor" parameterType="domain.blog.Author" 
	useGeneratedKeys=”true” keyProperty=”id”> 
	insert into Author (username,password,email,bio) 
	values (#{username},#{password},#{email},#{bio}) 
</insert>  

MyBatis有另外一种方法来处理数据库不支持自动生成类型,或者可能JDBC驱动不支持自动生成主键时的主键生成问题。 

这里有一个简单(甚至很傻)的示例,它可以生成一个随机ID可能你不会这么做,但是这展示了MyBatis处理问题的灵活性,

因为它并不真的关心ID的生成: 

<insert id="insertAuthor" parameterType="domain.blog.Author"> 
	<selectKey keyProperty="id" resultType="int" order="BEFORE"> 
		select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1 
	</selectKey> 
	insert into Author(id, username, password, email,bio, favourite_section) 
	values(#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR}) 
</insert>  

在上面的示例中,selectKey元素将会首先运行,Author的id会被设置,然后插入语句会被调用。这给你了一个简单的行为在你的数据库中来处理自动生成的主键,而不需要使你的Java代码变得复杂。 selectKey 元素描述如下: 

<selectKey 
	keyProperty="id" 
	resultType="int" 
	order="BEFORE" 
	statementType="PREPARED"> 
属性描述
keyProperty selectKey 语句结果应该被设置的目标属性。 
resultType 

结果的类型。MyBatis通常可以算出来,但是写上也没有问题。MyBatis允许任何简单类型用作

主键的类型,包括字符串。

order 

这可以被设置为 BEFORE 或 AFTER。如果设置为 BEFORE,那么它会首先选择主键,

设置 keyProperty 然后执行插入语句。如果设置为 AFTER,那么先执行插入语句,然后是

selectKey 元素,这和Oracle 数据库相似,可以在插入语句中嵌入序列调用。

statementType 

和前面的相同,MyBatis支持STATEMENT,PREPARED 和CALLABLE 语句的映射类型

,分别代表 PreparedStatement 和CallableStatement 类型。

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值