MyBatis使用问题记载

一、前言

之前除了实习的时候用了oracle之外,一直都是在使用mysql的,但是对于批量插入oracle的支持还是挺特别,其实最后看来,都是支持,只是我们不常这么写。下面进入重点;

二、mybatis中oracle的批量操作

1、oracle批量插入
最初写的时候如下:

<insert id="insertBatchInfo" parameterType="java.util.List">
  insert into t_share_time (id,com_id,week,start_time,end_time,create_time)  values
  <foreach collection="list" item="item" index="index" separator=",">
    (#{item.id},#{item.pileId},#{item.week},#{item.startTime},#{item.endTime},now())
  </foreach>
</insert>

所以很明显,照葫芦画瓢,oracle下也这么写,结果很明显,就是报错

org.springframework.jdbc.BadSqlGrammarException:
### Error updating database. Cause: java.sql.SQLException: ORA-00933: SQL 命令未正确结束
###the error  occured while setting parameters
###SQL:insert t_custom(custom_id,  create_time, state,  remark,name,custom_type) values (?,?,?,   ?,?,?),(?,?,?,?,?,?),(?,?,?,?,?,?)
Caused by: java.sql.SQLException: ORA-00933: SQL 命令未正确结束

这可就把我给弄到了,最后搜呀,得到的结果是

    由于oracle不支持insert多个values的方式

最后写的方式如下:

  <insert id="insertCommonCustom" parameterType="java.util.List">
           insert  into t_custom(custom_id,  create_time, state,  remark,name,custom_type,
           select seq__custom.nextval, a.*  from(
           <foreach collection="list" item="item" separator=" union all ">
              select 
              #{item.createTime,jdbcType=DATE},
              #{item.state,jdbcType=VARCHAR},
              #{item.remark,jdbcType=VARCHAR},
              #{item.name,jdbcType=VARCHAR},
              #{item.customType,jdbcType=VARCHAR}
               from dual
           </foreach>
           ) a
  </insert>

实际上就是语法,insert into table(**) select ….。
2:mysql对于批量插入的支持注意点

Error code 1064, SQL state 42000: You have an error in your SQL syntax;

此时需要修改你的数据库url,需要加url上加上

allowMultiQueries=true

三,oracel中null的处理

mapper.java层
public Integer updateStus(@Param("sn") String sn,@Param("stus") String stus);

mapper.xml
<update id="updateStus">
    update t_user  t set t.STUS=#{stus} where t.sn=#{sn}
</update>

经过测试
如果传入的sn或者stus都是此””,则不用指定jdbcType;如果传入null则需要指定jdbcType。但是在mysql中使用mybatis处理则不需要如此的问题。
null报错如下:

org.mybatis.spring.MyBatisSystemException: nested exception is 
org.apache.ibatis.type.TypeException: Could not set parameters for 
mapping: ParameterMapping{property='types', mode=IN, 
javaType=class java.lang.Long, jdbcType=null, numericScale=null, 
resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: 
org.apache.ibatis.type.TypeException: Error setting null for parameter 
#2 with JdbcType OTHER . Try setting a different JdbcType for this 
parameter or a different jdbcTypeForNull configuration property. Cause: 
java.sql.SQLException: 无效的列类型

四:oracle中插入并返回主键数据

<insert id="EmailCreate"  parameterType="com.mouse.pojo.Email">
    <selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="emailId">
        SELECT SEQ_MOUSE_EMAIL_ID.NEXTVAL FROM DUAL
    </selectKey>
    insert into t_email_validate (email_id,
                sign_code, status, type, create_by,
                create_time, update_by, update_time) 
                values (#{emailId,jdbcType=BIGINT},
                 #{signCode,jdbcType=VARCHAR},
                #{status,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR},
                #{createBy,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
                #{updateBy,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP})
</insert>   
使用selectKey来对主键赋值插入到数据库,最后会将这个值赋值到对象中去,所以在代码中用Email的对象email.getEmailId()来获取数据。

五:nested exception is java.sql.SQLDataException: ORA-01810: 格式代码出现两次
我得插入代码中写的sql是to_date(‘${create_date}’,’YYYY-MM-DD HH:mm:ss’),
oracle MM和mm是一样的,所以使用mi去代mm,然后我传入数据”2017-01-09 13:12:00″,结果又提示错误

; nested exception is java.sql.SQLDataException: ORA-01849: 小时值必须介于 1 和 12 之间

to_date函数,其格式化的参数分为两类,一为12小时制,一为24小时制,默认是12小时
to_date需要做进一步修改to_date(‘${create_date}’,’YYYY-MM-DD HH24:mm:ss’),

六:无效的列类

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: Error setting null for parameter #1 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
; uncategorized SQLException for SQL []; SQL state [99999]; error code [17004]; 无效的列类型: 1111; nested exception is java.sql.SQLException: 无效的列类型: 1111

如果插入的数据有空的话需要指定数据类型

{userName,jdbcType=VARCHAR}或者使用’${userName}’

七:浩语

                                          __                                                        
                            __  _  ____ __|  |__ _____    ___
                            \ \/ \/ /  |  \  |  \\__  \  /  _ \   
                             \     /|  |  /   Y  \/ __ \(  <_> )
                              \/\_/ |____/|___|  (____  /\____/ 
                                                    \/     \/          
                         2016,To Work Hard,To Adapter,To Change Myself
                                                       
    原文作者:吴世浩
    原文地址: https://www.jianshu.com/p/fa4f12f168e9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞