@Param注解
这个注解主要用于SQL语句中的参数赋值。
以mybatis中的使用为例
在mapper的接口中定义方法
AdminUser getAdminUserByUserNameAndPassword(@Param("userName") String Name, @Param("passwordMD5") String pass);
//根据两个参数获取结果
在xml中的映射文件中使用方法
<select id="getAdminUserByUserNameAndPassword" resultMap="AdminUserResult">
select id,user_name,user_token
from tb_admin_user
where user_name = #{userName} and password_md5 = #{passwordMD5}
</select>
<!-- #{}中的内容使用的是@Param中定义的别名,而不是方法中的形参 -->
动态Sql之foreach
批量查询
mapper接口方法
public List<User> selectByIds(@Param("ids") List<Integer> ids);
xml映射方法
<select id="selectByIds" resultType="com.zssj.domain.User">
select * from user
where id in
<foreach collection="ids" open="(" close=")" item="id" separator="," >
#{id}
</foreach>
</select>
sql效果(假设ids的内容为[1,2,3])
select * from user where id in (1,2,3)
批量插入
mapper接口方法
int insertUsers(@Param("Users") List<User> Users);
xml映射方法
<insert id="insertUsersBatch">
insert into user(user_name,password_md5) VALUES
<foreach collection="Users" index="index" item="User" open="" separator="," close="">
(#{User.userName}, #{User.password})
</foreach>
</insert>