mybatis传参接收问题
单个参数且为基础类型
mapper文件中
User findByEmail(String email);
mapper.xml中
<select id="findByEmail" resultMap="BaseResultMap"
parameterType="java.lang.String">
select
<include refid="BaseColumns"/>
,d.dept_name,d.dept_type
from sys_user t
left join sys_dept d on t.dept_id=d.dept_id
where t.email = #{aaa} and t.del_flag = '0'
</select>
总之 随便写都行
单个参数且为包装类型
mapper文件中
int checkLoginNameUnique(User user);
mapper.xml中
<select id="checkLoginNameUnique" resultType="java.lang.Integer"
parameterType="com.yss.base.model.user.User">
select count(1)
from sys_user t
<where>
t.del_flag = '0'
<if test="userId !=null and userId !=''">
and t.user_id != #{userId}
</if>
<if test="loginName !=null and loginName !=''">
and t.login_name = #{loginName}
</if>
</where>
</select>
直接写成员变量名
多个参数且为基础类型
mapper文件中
void updateAvatar(@Param("userId") String userId, @Param("url") String url);
mapper.xml中
update sys_user set avatar = #{url} where user_id = #{userId}
加上@Param(“xx”) 参数名需对应
或者
mapper文件中
void updateAvatar( String userId, String url);
mapper.xml中(jdk1.7)
update sys_user set avatar = #{1} where user_id = #{0}
mapper.xml中(jdk1.8)
update sys_user set avatar = #{param1} where user_id = #{param0}
mapper.xml中(jdk1.8也可以)
update sys_user set avatar = #{arg1} where user_id = #{arg0}
多个参数且为封装类型
mapper文件中
List<User> getMyDeptUser(Page<User> page, @Param("bean") User user);
mapper.xml中
<select id="getMyDeptUser"
parameterType="com.yss.base.model.user.User" resultMap="BaseResultMap">
select
<include refid="BaseColumns"/>
,d.dept_name
from sys_user t
left join sys_dept d on t.dept_id=d.dept_id
<where>
t.del_flag = '0'
<if test="bean.userId !=null and bean.userId !=''">
and t.user_id != #{bean.userId}
</if>
<if test="bean.username !=null and bean.username !=''">
and t.username like
<if test="_databaseId == 'oracle'">'%'||#{bean.username}||'%'</if>
<if test="_databaseId == 'mssql'">'%'+#{bean.username}+'%'</if>
<if test="_databaseId == 'mysql'">concat('%',#{bean.username},'%')</if>
</if>
<if test="bean.loginName !=null and bean.loginName !=''">
and t.login_name like
<if test="_databaseId == 'oracle'">'%'||#{bean.loginName}||'%'</if>
<if test="_databaseId == 'mssql'">'%'+#{bean.loginName}+'%'</if>
<if test="_databaseId == 'mysql'">concat('%',#{bean.loginName},'%')</if>
</if>
<if test="bean.email !=null and bean.email !=''">
and t.email like
<if test="_databaseId == 'oracle'">'%'||#{bean.email}||'%'</if>
<if test="_databaseId == 'mssql'">'%'+#{bean.email}+'%'</if>
<if test="_databaseId == 'mysql'">concat('%',#{bean.email},'%')</if>
</if>
<if test="bean.deptId !=null and bean.deptId !=''">
and t.dept_id=#{bean.deptId}
</if>
<if test="bean.deptIds !=null and bean.deptIds.size()>0 ">
and t.dept_id in
<foreach collection="bean.deptIds" item="item" index="index"
open="(" close=")" separator=",">
#{item}
</foreach>
</if>
</where>
order by t.crt_time desc
</select>
加上@Param(“xx”) 参数名需对应