最近新建项目,碰到很多坑,于是乎写个总结记录一下。
1.参数匹配
mapper参数:常用的有String,list<xx>,HashMap<String, Object>,自定义对象等。
但是参数有的时候要带@Param("xx"),有的时候不带。
//参数为String对象,在xml中直接使用,不做非空判断,就不用添加@param("id")
User selectByPrimaryKey(String id);
<select id="selectByPrimaryKey" parameterType="java.lang.String"
resultType="com.xx.User">
select *
from user
where id = #{id,jdbcType=VARCHAR}
</select>
//如果判断id是否为空,则要加上@param("id") (import org.apache.ibatis.annotations.Param;)
User selectByPrimaryKey(String id);
<select id="selectByPrimaryKey" parameterType="java.lang.String"
resultType="com.xx.User">
select *
from user
<if test="id !=null and id != ''">
where id = #{id,jdbcType=VARCHAR}
</if>
</select>
//更新,或者新建的时候,判断id,userName,但是却不用加@Param("xx")
//因为xml中没有对insertSelective的参数user进行非空判断,而是对user
//的属性进行了判断
int insertSelective(User user);
<insert id="insertSelective" parameterType="com.xx.User">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="userName != null">
user_name,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="userName != null">
#{userName,jdbcType=VARCHAR},
</if>
</trim>
</insert>
//list类型的变量会默认一个key,名字就叫"list",如果需要变成自定义的集合名,需要加Param()注解
int insertUserRoles(@Param("userRoles") List<UserRole> userRoles);
<insert id="insertUserRoles" parameterType="java.util.List">
replace into user_role (user_id,role_id) values
<foreach collection="userRoles" item="it" separator=",">
(#{it.userId},#{it.roleId})
</foreach>
</insert>
//如果使用HashMap当参数,在xml中直接使用map里面的key就行,不用添加@Param("xx")
List<Map<String, Object>> selectUsers(Map<String, Object> searchMap);
//别忘了在service层给map添加key-value
searchMap.put("userName", userName);
<select id="selectUsers" parameterType="java.util.Map"
resultType="com.xx.User">
select *
from user u
WHERE 1=1
<if test="userName !=null and userName !=''">
AND u.department IN
<foreach collection="department" open="(" close=")" separator="," item="value">
#{value, jdbcType=VARCHAR}
</foreach>
</if>
</select>
2.返回值
<!--insert,update默认返回int-->
<insert id="insertSelective" parameterType="com.xx.User">
insert into user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="userName != null">
user_name,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=VARCHAR},
</if>
<if test="userName != null">
#{userName,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<!--返回自定义对象-->
<select id="selectByPrimaryKey" parameterType="java.lang.String"
resultType="com.xx.User">
select *
from user
where id = #{id,jdbcType=VARCHAR}
</select>
<!--返回Map对象-->
<select id="getUsers" parameterType="java.util.Map"
resultType="java.util.Map">
select *
from user
where id = #{id,jdbcType=VARCHAR}
</select>
这里应该都会碰到一个问题,驼峰转换。我目前的项目是springboot+mybatis。因此配驼峰转换的方式和mybatis有些出入。
之前SSM的项目要在mybatis的配置文件里加上
<setting name="mapUnderscoreToCamelCase" value="true"/>
springboot的项目要在application.properties加上
mybatis.configuration.map-underscore-to-camel-case=true
这里有个特殊情况,如果resultType="java.util.Map" mybatis并不会实现驼峰转换,这时有两种解决方式:一是给字段取别名,例如:user表的user_name字段,select user_name as userName from user.二是自己写个工具类,对map的key进行驼峰转换。