Mybatis

1.org.apache.ibtatis.type.TypeException

开发时,在操作数据库时 报以下错误

org.apache.ibatis.type.TypeException:Could not set parameters for mapping Cause: 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: 无效的列类型: 1111

 当前报错是因为参数address为null,在插入数据库时报错

<insert id="insert">
    insert into test(id,name,address)
    values(#{id},#{name},#{address})
</insert>

此时需要调整一下sql语句即可

<insert id="insert">
    insert into test(id,name,address)
    values(#{id},#{name},#{address,jdbcType=VARCHAR})
</insert>

2.大于小于号

mybatis中不可直接使用><=

方法一:转义

>         &gt;

<         &lt;

>=       &gt;=

<=       &lt;=

<if test="count != null and count != ''">
	AND current_count &gt;= #{count}
</if>

方法二:用<![CDATA[ ]]>符号

<![CDATA[ 
	<if test="count != null and count != ''">
		AND current_count >= #{count}
	</if>
]]>

3.<if>、<where>标签

<where>
    <if test="name != null and name !=''">
        and name = #{name}
    </if>
</where>

4.<choose>标签

<where>
	<choose>
		<when test="name != null and name != ''">
            name = #{name}
		</when>
		<when test="age != null and age != ''">
            age = #{age}
		</when>
		<otherwise>
            id = #{id}
		</otherwise>
	</choose>
</where>

5.<foreach>标签

<!--int insert(@Param("userList") List<User> userList);-->
<insert id="insert">
    insert into user values
	<foreach collection="userList" item="user" separator=",">
            (SYS_GUID(),#{user.name},#{user.age},#{user.sex})
	</foreach>
</insert>

6.注意程序分批处理 mapper一次处理最大长度为1000

List<User> userLists = new ArrayList<>();
List<UserEntity> entityList = new ArrayList<>();
for (List<UserEntity> partition : Lists.partition(entityList, 1000)) {
      List<User> userList = userMapper.queryList(partition);
      userLists.add(userList);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值