A stray ant can only move by itself

mybatis采坑

mybatis 获取主键自增长

两个属性:useGeneratedKeys 和 keyProperty ,获得的主键直接赋值到插入的对象中,通过插入的对象获取自增的主键

mabatis 多个参数的查询

Dao 层使用@Param 注解,xml配置文件中不需要再使用parameterType 属性
mybatis 中desc 经常作为description 的缩写使用,查询数据时引发错误,
在desc两端添加反引号(数字1 前的那个键)可以解决问题

mybatis 批量插入案例:

1
2
3
4
5
6
7
8
9
10
11
<insert id="insertBatch" parameterType="java.util.List">
INSERT INTO s_data_role_permission (id, dataRoleId, dataTypeId,
dataDepts, createUser, createTime
) VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id,jdbcType=INTEGER}, #{item.dataRoleId,jdbcType=INTEGER},
#{item.dataTypeId,jdbcType=INTEGER}, #{item.dataDepts,jdbcType=VARCHAR},
#{item.createUser,jdbcType=VARCHAR}, #{item.createTime,jdbcType=TIMESTAMP}
)
</foreach>
</insert>

mybatis 批量查询案例:

1
2
3
4
5
6
<select id="selectDeptTreeByIds" resultType="com.instant.ipms.model.vo.web.sys.dept.DeptTree">
SELECT id, parentId, name as text
FROM s_dept
WHERE id IN
<foreach collection="list" open="(" close=")" index="index" separator="," item="item">#{item}</foreach>
</select>

mybatis 批量更新案例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<update id="updateBatch" parameterType="java.util.List">
update prj_stage
<trim prefix="set" suffixOverrides=",">
<trim prefix="sort =case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=#{item.id} then #{item.sort}
</foreach>
</trim>
</trim>
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id,jdbcType=INTEGER}
</foreach>
</update>

org.apache.ibatis.reflection.ReflectionException

There is no getter for property named ‘keyword’ in ‘class java.lang.String’这样的错误
解决方法

  1. 将string类型换成map类型的,然后将该keyword 放入该map中,不会报错

  2. 在接口中使用@Param(value=”xxx”) String xxx,
    例如:public List fuzzyFind(@Param(value = “keyword”) String keyword);
    同时将配置文件中的paramType 去除,也可以解决问题

  3. 在配置文件中直接将keyword换成_parameter,问题解决。

    1
    2
    3
    4
    5
    6
    7
    8
    <select id="fuzzyFind" resultMap="articleResultMap" parameterType="string">
    <![CDATA[
    SELECT * FROM articles WHERE 1=1
    ]]>
    <if test="_parameter != null">
    <![CDATA[ AND article_title LIKE CONCAT('%','${_parameter}','%') ]]>
    </if>
    </select>
支付宝打赏 微信打赏

赞赏是不耍流氓的鼓励