MyBatis3.2.4映射配置:insert 、update 和 delete
来源:互联网 发布:记忆最佳时间段在 知乎 编辑:程序博客网 时间:2024/05/17 18:47
insert、update和 delete
<insert
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
keyProperty=""
keyColumn=""
useGeneratedKeys=""
timeout="20">
<update
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20">
<delete
id="insertAuthor"
parameterType="domain.blog.Author"
flushCache="true"
statementType="PREPARED"
timeout="20">
属性
属性
描述
id
在命名空间中唯一的标识符,可以被用来引用这条语句。
parameterType
将会传入这条语句的参数类的完全限定名或别名。
parameterMap
这是引用外部 parameterMap的已经被废弃的方法。使用内联参数映射和 parameterType属性。
flushCache
此设置为true,每当执行这个语句,将导致本地和二级缓存被刷新。默认值:false。
timeout
这个设置驱动程序等待数据库返回请求结果,并抛出异常时间的最大等待值。默认不设置(驱动自行处理)。
statementType
STA,TEMENT,PREPARED或 CALLABLE的一种。这会让 MyBatis使用选择使用 Statement,PreparedStatement或 CallableStatement。默认值:PREPARED。
useGeneratedKeys
(仅对 insert有用)这会告诉 MyBatis使用 JDBC的 getGeneratedKeys方法来取出由数据(比如:像 MySQL和 SQL Server这样的数据库管理系统的自动递增字段)内部生成的主键。默认值:false。
keyProperty
(仅对 insert有用)标记一个属性, MyBatis会通过 getGeneratedKeys或者通过 insert语句的 selectKey子元素设置它的值。默认:不设置。
keyColumn
(insert only) Sets the name of the column in the table with a generated key. This is only required in certain databases (like PostgreSQL) when the key column is not the first column in the table.
databaseId
In case there is a configured databaseIdProvider, MyBatis will load all statements with no databaseId attribute or with a databaseId that matches the current one. If case the same statement if found with and without the databaseId the latter will be discarded.
示例
<insertid="insertAuthor">
insert into Author (id,username,password,email,bio)
values (#{id},#{username},#{password},#{email},#{bio})
</insert>
<updateid="updateAuthor">
update Author set
username = #{username},
password = #{password},
email = #{email},
bio = #{bio}
where id = #{id}
</update>
<deleteid="deleteAuthor">
delete from Author where id = #{id}
</delete>
自动生成主键
如果数据库支持自动生成主键,可以设置 useGeneratedKeys= "true",而且设置 keyProperty到已经做好的目标属性上。例如,如果上面的 Author表已经对 id使用了自动生成的列类型,那么语句可以修改为:
<insertid="insertAuthor"useGeneratedKeys="true"keyProperty="id">
insert into Author (username,password,email,bio)
values (#{username},#{password},#{email},#{bio})
</insert>
MyBatis 有另外一种方法来处理数据库不支持自动生成类型,或者可能 JDBC 驱动不支 持自动生成主键时的主键生成问题。
<insertid="insertAuthor">
<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支持 STA TEMENT ,PREPARED和 CALLABLE语句的映射类型,分别代表 PreparedStatement和 CallableStatement类型。
- MyBatis3.2.4映射配置:insert 、update 和 delete
- MyBatis3.2.4映射配置:insert 、update 和 delete
- myBatis3之SQL映射的XML文件(insert,update,delete 元素)
- mapper映射文件配置之insert、update、delete
- Mybatis下mapper映射文件配置之insert、update、delete
- mybatis映射器三(insert,update,delete)
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
- MyBatis源码学习笔记(七)mapper映射文件配置之insert、update、delete
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
- mybatis配置insert/update/delete同一个模板
- mybatis-映射器-insert元素,update,delete元素
- MyBatis (一)configuration.xml的配置/mapper持久类的映射/sqlSession的获取/基础的select,insert,delete,update
- 一道笔试题[错排问题]
- GitHub入门与实践(一)——在widows环境下安装Git
- 开源无人机开发平台
- 写多了自定义View,可以来试一试自定义Drawable
- python获取自己的exe路径
- MyBatis3.2.4映射配置:insert 、update 和 delete
- 数据库—六种约束
- bash变量的学习笔记
- the diary of how to backup one more files in linux
- 八大排序算法之堆排序
- 湖大C语言设计练习题
- css hack基础理解
- 颜色Colours的使用
- linux 命令复习