#{a}
、#{b}
或者#{param1}
,但是为了开发规范,尽量使用和入参时一样。UserInfo selectByUserId(String userId);
<select id="selectByUserId" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=1
</select>
#{param1}
对应第一个参数,#{param2}
对应第二个参数.......UserInfo selectByUserIdAndStatus(String userId,Integer status);
<select id="selectByUserIdAndStatus" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{param1} and status=#{param2}
</select>
@Param
这个注解用于指定key,一旦指定了key,在SQL中即可对应的key入参。UserInfo selectByUserIdAndStatus(@Param("userId") String userId,@Param("status") Integer status);
<select id="selectByUserIdAndStatus" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select>
Map
,入参传Map当然也行,此时#{key}
中的key
就对应Map中的key
。UserInfo selectByUserIdAndStatusMap(Map<String,Object> map);
<select id="selectByUserIdAndStatusMap" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select>
@Test
void contextLoads() {
Map<String,Object> map=new HashMap<>();
map.put("userId","1222");
map.put("status",1);
UserInfo userInfo = userMapper.selectByUserIdAndStatusMap(map);
System.out.println(userInfo);
}
key
就是属性名称,注意一定要有get
方法。UserInfo selectByEntity(UserInfoReq userInfoReq);
<select id="selectByEntity" resultType="cn.cb.demo.domain.UserInfo">
select * from user_info where user_id=#{userId} and status=#{status}
</select>
@Data
public class UserInfoReq {
private String userId;
private Integer status;
}
in
。List<UserInfo> selectList( List<String> userIds);
<select id="selectList" resultMap="userResultMap">
select * from user_info where status=1
and user_id in
<foreach collection="list" item="item" open="(" separator="," close=")" >
#{item}
</foreach>
</select>
foreach
语法。List<UserInfo> selectList( String[] userIds);
<select id="selectList" resultMap="userResultMap">
select * from user_info where status=1
and user_id in
<foreach collection="array" item="item" open="(" separator="," close=")" >
#{item}
</foreach>
</select>