1.parameterType="string"或者int/long等单个参数,则在获取参数时,分两种情况:
如果是要进行非null的判断,则不可以在if后直接那变量名进行判空,因为mybatis会默认变量名为_parameter,否则会报no getter/setter错误。要写成< if test="_ parameter!=null and _parameter!='' ">
User getUserById(String uid); <select id="getUserById" resultTypeb ="com.apple.entity.User"> SELECT <include refid="BASE_COLUMN"/> FROM <include refid="BASE_TABLE"/> where 1=1 <if test="_parameter!=null and _parameter!='' "> and uid=#{uid} </if> <!--不加上@Param直接使用会报no getter/setter错误--> <!--<if test="uid!=null">--> <!--uid=#{uid}--> <!--</if>--> <!--如果不要进行非null的判断,可以直接使用--> <!--and uid=#{uid}--> </select>上面情况是针对于string类型参数直接传入,如果不想在判断时使用,mybatis默认的变量名,则需要在dao层后台传入时加上@Param参数可以直接使用变量判断
如果不要进行非null的判断,则可以不用_parameter,直接使用where uid=#{uid}
_parameter:代表整个参数
单个参数:_parameter就是这个参数多个参数:参数会被封装为一个map;_parameter就是代表这个map2、对于多个String类型,或者String和int的组合传入,则无法使用Mybatis默认的_parameterType。
1)使用@Param注解,在mybatis中可以直接将此作为变量名判断和使用:
User getUserById4(@Param("uid")Integer uid,@Param("password")String password); <select id="getUserById4" resultType="com.apple.entity.User"> SELECT <include refid="BASE_COLUMN"/> FROM <include refid="BASE_TABLE"/> where 1=1 <if test="uid!=null and uid!='' "> and uid=#{uid} </if> <if test="password!=null and password!='' "> and password=#{password} </if> </select>2)不用@param注解,使用下标索引的方式,这里我用#{0}会报错,要用#{arg0}:
User getUserById4(Integer uid,String password); <select id="getUserById4" resultType="com.apple.entity.User"> SELECT <include refid="BASE_COLUMN"/> FROM <include refid="BASE_TABLE"/> where 1=1 <if test="arg0!=null and arg0!='' "> and uid=#{arg0} </if> <if test="arg1!=null and arg1!='' "> and password=#{arg1} </if> </select>3、传参为map,直接使用key判断,跟上面多个string类似
User getUserById5(Map<String, String> paramMap); Map<String, String> paramMap = new HashMap<>(); paramMap.put("uid", "1"); //主键id可以将int 当做string 传入也能查询 paramMap.put("password", "123"); <select id="getUserById5" resultType="com.apple.entity.User"> SELECT <include refid="BASE_COLUMN"/> FROM <include refid="BASE_TABLE"/> where 1=1 <if test="uid!=null and uid!='' "> and uid=#{uid} </if> <if test="password!=null and password!='' "> and password=#{password} </if> </select>4、传参为list,parameterType不用写,分两种,List< String/Integer/Long>和List< User/Map>
第一种List< String/Integer/Long>
注意:
如果参数不加@param注解,if和foreach标签里面是用list进行判断!!
如果想用自己写的的参数名称判断,就需要加@param,不然识别不了!!
List<User> getUserById6(List<Integer> list); //这里写成下面这种方式,if和foreach里面也是用list进行判断,而不是用idList,不然会报错,要写成idList要加上@param List<User> getUserById6(List<Integer> idList); <select id="getUserById6" resultType="com.apple.entity.User"> SELECT <include refid="BASE_COLUMN"/> FROM <include refid="BASE_TABLE"/> where 1=1 <if test="list!=null "> and uid in <foreach collection="list" index="index" separator="," item="item" open="(" close=")"> #{item} </foreach> </if> </select>第二种List< User/Map>
void addUserBatch(List<User> list); <!--注意批量插入这种情况下foreach标签不要open="(" close=")"--> <insert id="addUserBatch"> insert into t_user(username,password) values <foreach collection="list" item="item" index="index" separator=","> ( #{item.username}, #{item.password} ) </foreach> </insert>5、传参为javabean,传参分两种
传参不加上@Param:
void addUser(User user); <insert id="addUser"> insert into t_user(username,password) values (#{username},#{password}) </insert>如果传参加上@Param,则xml文件要加上user.
void addUser(@Param("user") User user); <insert id="addUser"> insert into t_user(username,password) values (#{user.username},#{user.password}) </insert>以上几种传参:
如果是select查询都不用写paramType,可以直接找到对应的类型,而resultType必须写;
如果是insert/update/delete的时候,paramType和resultType都不用写!
foreach标签的open 里面可以写and in 这种 不一定只有(
推荐单个或者多个参数(除了map这种)的时候都写上@param防止出错!!!