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中不可直接使用><=
方法一:转义
> >
< <
>= >=
<= <=
<if test="count != null and count != ''">
AND current_count >= #{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);
}