打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Mybatis使用之增删改详解

 

一:简介

 

        主要记录Mybatis的增删改使用到的标签属性及意义。参数方面没有过多涉及、后面还是会有单独章节来记录参数的类型及使用。

        这里没有查询、因为查询算是比较重要的使用方式、同时也比较复杂、牵涉到ResultMap的配置、关联查询等。分开的目的是想让博客每一篇都有重点、调理清晰一点。

 

二:新增

 

       2.1 insert标签属性及意义:

      

       2.2 insert之返回生成主键:

 

              2.2.1对于MySql或者SQL Server这种主键自增长的数据库可以使用如下方式获取主键(其中两个关键标签属性useGeneratedKeys 和keyProperty的意义见上面insert标签属性):

  1. <insert id="addAuthorWithAutoGeneratedKey" useGeneratedKeys="true" keyProperty="id">
  2. INSERT INTO author (username, password, email, bio, favourite_section)
  3. VALUES (#{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
  4. </insert>


              2.2.2对于Oracle这种不支持主键自增长的数据库可以使用下面这种先生成一个主键再插入表并将生成主键返回的方式来笨处理:

 

  1. <insert id="addAuthorWithoutAutoGeneratedKey">
  2. <selectKey keyProperty="id" resultType="int" order="BEFORE">
  3. select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1
  4. </selectKey>
  5. insert into Author
  6. (id, username, password, email,bio, favourite_section)
  7. values
  8. (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
  9. </insert>

              2.2.3对于Oracle一般主键都会使用Sequence来实现、此时返回新增记录的主键的方式如下:

             

  1. <insert id="addAuthor" parameterType="author">
  2. INSERT INTO author(id, username) VALUES (seq_author.nextval, #{username})
  3. <selectKey keyProperty="id" resultType="int">
  4. SELECT max(id) FROM author
  5. </selectKey>
  6. </insert>

 

              2.2.4 selectKey:

 

       2.3 实例:

              2.3.1 Mysql中向Author表中插入一条数据并返回主键

 

  1. <insert id="addAuthorWithAutoGeneratedKey" useGeneratedKeys="true" keyProperty="id">
  2. INSERT INTO author (username, password, email, bio, favourite_section)
  3. VALUES (#{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR})
  4. </insert>

              2.3.2Oracle中向Author表中插入一条数据并返回主键

 

  1. <insert id="addAuthor" parameterType="author">
  2. INSERT INTO author(id, username) VALUES (seq_author.nextval, #{username})
  3. <selectKey keyProperty="id" resultType="int">
  4. SELECT max(id) FROM author
  5. </selectKey>
  6. </insert>

三:修改、删除

 

       将两个放在一起的原因是两者使用的方式比较简单、其中修改使用方式基本和新增差不多。

 

       3.1 修改

 

              3.1.1update标签及标签属性

  1. <update
  2. id=""
  3. parameterType="author"
  4. statementType="PREPARED"
  5. flushCache="true"
  6. timeout="20"
  7. databaseId=""
  8. keyProperty=""
  9. keyColumn=""
  10. useGeneratedKeys="true"/>
             
              具体标签属性意义见2.2

              3.1.2修改实例

             当其未设定主键为返回值时、返回的是数据库中影响的行数。比如修改一条记录、则方法的返回结果为1.

  1. <update id="updateAuthor" parameterType="author" flushCache="true" statementType="PREPARED">
  2. UPDATE author
  3. SET username = #{username}
  4. WHERE id = #{id}
  5. </update>

       3.2 删除

 

              3.2.1delete标签及标签属性

              返回的是数据库中影响的行数。比如删除10条记录、则方法的返回结果为10.

  1. <delete
  2. id=""
  3. parameterType="author"
  4. statementType="PREPARED"
  5. flushCache="true"
  6. timeout="20"/>


              3.2.2实例

  1. <delete id="deleteAuthor" parameterType="int">
  2. DELETE FROM author
  3. WHERE id = #{id}
  4. </delete>

四:补充


       

        github地址:https://github.com/andyChenHuaYing/scattered-items/tree/master/items-mybatis

        源码下载地址:http://download.csdn.net/detail/chenghuaying/8713311

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
myBatis3之SQL映射的XML文件(insert,update,delete 元素)
mybatis初步
MySQL如何自动获取主键(MyBatis执行Insert操作返回自增主键)
Mybatis中,当插入数据后,返回最新主键id的几种方法,及具体用法
mybatis 自动增长id保存后得到id
Mybatis插入数据行ID生成策略
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服