-
Mapper接口和*Mapper.xml之间参数传递的问题
使用@Param("{value-name}")注解而不是@Value注解,{value-name}表示需要传递的参数的名字
示例:
java配置如下
int updateInfoByPrimaryKeyAndToken(@Param("id") String id, @Param("token") String token, @Param("head_img") String headImg, @Param("username") String username, @Param("update_time") Date updateTime);
对应xml配置如下
<update id="updateInfoByPrimaryKeyAndToken"> update user set <if test="head_img != null and head_img != ''"> head_img = #{head_img,jdbcType=LONGVARCHAR}, </if> <if test="username != null and username != ''"> username = #{username,jdbcType=VARCHAR}, </if> update_time = #{update_time,jdbcType=TIMESTAMP} where id = #{id,jdbcType=VARCHAR} and token = #{token,jdbcType=VARCHAR} limit 1 </update>
-
Mybatis传回主键自增的问题
使用
标签,需要注意,返回的自增主键值是赋给传进来的实体的,而不是方法前面的int返回类型,方法前面的int永远表示的都是修改的行数 示例:
java配置如下
int insert(Location location);
对应的xml配置如下
<insert id="insert" parameterType="cn.renoside.letsgo.model.Location"> insert into location (city) values (#{city,jdbcType=VARCHAR}) <selectKey keyColumn="id" keyProperty="id" resultType="java.lang.Integer" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey> </insert>
使用时特别注意,正确使用方法:
Location save = new Location(); save.setCity(city); locationMapper.insert(save); int location_id = save.getId();
错误使用:
Location save = new Location(); save.setCity(city); int location_id = locationMapper.insert(save);
-
Mybatis子查询中不能使用limit的问题
在子查询中使用limit时遇到"This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' "的错误,解决方案是再套一层子查询
示例:
select id, parent_id, create_by, follow_by, article_id, create_time, update_time, enable, content from comment where id in (select parent_id from (select parent_id from comment where parent_id != -1 group by parent_id limit 3) as hot)
错误使用:
select id, parent_id, create_by, follow_by, article_id, create_time, update_time, enable, content from comment where id in (select parent_id from comment where parent_id != -1 group by parent_id limit 3)
另外,特别注意下面一个可能会出错的地方:
select id, parent_id, create_by, follow_by, article_id, create_time, update_time, enable, content from comment where id in (select hot.parent_id from (select parent_id from comment where parent_id != -1 group by parent_id limit 3) as hot)
不要使用hot.parent_id,可能会报"unknown column 'hot.parent_id' in 'field list'"的错误