今天在些代码的时候,使用这个注解,因为原来在mapper中添加的,所以就猜想是不是一个对象也可以使用,就直接加上想也没有像,找错一直找不到对应的实体类,看了半天,后来一个同事说是不是@Param的原因,去掉之后,批量修改的语句执行了,尼玛 真是笨死了,,,,,无言以对
所以特地整理这个注解,
实例一 @Param注解单一属性
dao层示例
Public User selectUser(@param(“userName”) String name,@param(“userpassword”) String password);
注意:采用#{}的方式把@Param注解括号内的参数进行引用(括号内参数对应的是形参如 userName对应的是name)
xml映射对应示例
<select id=" selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_password=#{userPassword,jdbcType=VARCHAR}
</select>
实例二 @Param注解JavaBean对象
dao层示例
public List getUserInformation(@Param(“user”) User user);
xml映射对应示例
<select id="getUserInformation" parameterType="com.github.demo.vo.User" resultMap="userMapper">
select
<include refid="User_Base_Column_List" />
from mo_user t where 1=1
<!-- 因为传进来的是对象所以这样写是取不到值得 -->
<if test="user.userName!=null and user.userName!=''"> and t.user_name = #{user.userName} </if>
<if test="user.userAge!=null and user.userAge!=''"> and t.user_age = #{user.userAge} </if>
</select>
本文通过实例解析MyBatis中@Param注解的使用方法,包括单个属性和JavaBean对象的处理,以及在DAO层和XML映射文件中的应用技巧。
3028

被折叠的 条评论
为什么被折叠?



