码迷,mamicode.com
首页 > 数据库 > 详细

mybatis常用操作数据库知识

时间:2017-09-04 13:16:02      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:hsi   val   tab   元素   selectkey   使用   uuid   大小   erp   

  •  mybatis生成uuid        
<insert id="insert" parameterType="自定义的属性类名">
<selectKey keyProperty="userId//你的主键属性名" resultType="String" order="BEFORE">
  select replace(uuid(),-,‘‘) from dual //原样照抄
</selectKey>
  insert into sys_user (user_id, user_name, login_name,
    user_pwd)
values (#{userId,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR},
  #{loginName,jdbcType=VARCHAR},
  #{userPwd,jdbcType=VARCHAR})
</insert>
  •  select

           

<insert id="addBean" parameterType="com.test.bean.ScenicPunish">
       <!--生成uuid  -->
      <selectKey keyProperty="punish_id" resultType="java.lang.String" order="BEFORE">
        select replace(uuid(),-,‘‘) from dual
      </selectKey>
      insert into t_scenic_punish(punish_id,punish_prov,punish_city,punish_country,punish_scenic,punish_org,
         punish_type,punish_date,punish_title,punish_content,punish_url)
      values(#{punish_id},#{punish_prov},#{punish_city},#{punish_country},#{punish_scenic},#{punish_org},#{punish_type},#{punish_date},#{punish_title},#{punish_content},#{punish_url})
  </insert>

select 语句属性配置细节:
属性描述取值默认
id 在这个模式下唯一的标识符,可被其它语句引用    
parameterType 传给此语句的参数的完整类名或别名    
resultType 语句返回值类型的整类名或别名。注意,如果是集合,那么这里填写的是集合的项的整类名或别名,而不是集合本身的类名。(resultType 与resultMap 不能并用)    
resultMap 引用的外部resultMap 名。结果集映射是MyBatis 中最强大的特性。许多复杂的映射都可以轻松解决。(resultType 与resultMap 不能并用)    
flushCache 如果设为true,则会在每次语句调用的时候就会清空缓存。select 语句默认设为false true/false false
useCache 如果设为true,则语句的结果集将被缓存。select 语句默认设为false true/false false
timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 正整数 未设置
fetchSize 设置一个值后,驱动器会在结果集数目达到此数值后,激发返回,默认为不设值,由驱动器自己决定 正整数 驱动器决定
statementType statement,preparedstatement,callablestatement。预准备语句、可调用语句 STATEMENT、PREPARED、CALLABLE PREPARED
resultSetType forward_only、scroll_sensitive、scroll_insensitive 只转发,滚动敏感,不区分大小写的滚动 FORWARD_ONLY、SCROLL_SENSITIVE、SCROLL_INSENSITIVE 驱动器决定
insert可以使用数据库支持的自动生成主键策略,设置useGeneratedKeys=”true”,然后把keyProperty 设成对应的列,就搞定了。比如说上面的StudentEntity 使用auto-generated 为id 列生成主键.
也可以在数据库中定义自动更新。
或者使用uuid进行更新id值


<!-- 插入学生 自动主键--> <insert id="insertStudentAutoKey" parameterType="StudentEntity"> <selectKey keyProperty="studentID" resultType="String" order="BEFORE"> select nextval(‘student‘) </selectKey> INSERT INTO STUDENT_TBL (STUDENT_ID, STUDENT_NAME, STUDENT_SEX, STUDENT_BIRTHDAY, CLASS_ID) VALUES (#{studentID}, #{studentName}, #{studentSex}, #{studentBirthday}, #{classEntity.classID}) </insert>


    
  • insert

       <insert id="insertStudent" parameterType="StudentEntity" useGeneratedKeys="true" keyProperty="studentID">

 

           

id 在这个模式下唯一的标识符,可被其它语句引用    
parameterType 传给此语句的参数的完整类名或别名    
flushCache 如果设为true,则会在每次语句调用的时候就会清空缓存。select 语句默认设为false true/false false
useCache 如果设为true,则语句的结果集将被缓存。select 语句默认设为false true/false false
timeout 设置驱动器在抛出异常前等待回应的最长时间,默认为不设值,由驱动器自己决定 正整数 未设置
fetchSize 设置一个值后,驱动器会在结果集数目达到此数值后,激发返回,默认为不设值,由驱动器自己决定 正整数 驱动器决定
statementType statement、preparedstatement、callablestatement。预准备语句、可调用语句 STATEMENT、PREPARED、CALLABLE PREPARED
useGeneratedKeys 告诉MyBatis 使用JDBC 的getGeneratedKeys 方法来获取数据库自己生成的主键(MySQL、SQLSERVER 等关系型数据库会有自动生成的字段)。默认:false true/false false
keyProperty 标识一个将要被MyBatis设置进getGeneratedKeys的key 所返回的值,或者为insert 语句使用一个selectKey子元素。    

selectKey语句属性配置细节:

 

mybatis常用操作数据库知识

标签:hsi   val   tab   元素   selectkey   使用   uuid   大小   erp   

原文地址:http://www.cnblogs.com/llforeverlove/p/7472911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!