@mapper个人理解

@mapper的作用是可以给mapper接口自动生成一个实现类,让spring对mapper接口的bean进行管理,并且可以省略去写复杂的xml文件

此处借用一段代码

/UserDAO
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import entity.User;

/**
 * 添加了@Mapper注解之后这个接口在编译时会生成相应的实现类
 * 
 * 需要注意的是:这个接口中不可以定义同名的方法,因为会生成相同的id
 * 也就是说这个接口是不支持重载的
 */
@Mapper
public interface UserDAO {

    @Select("select * from user where name = #{name}")
    public User find(String name);

    @Select("select * from user where name = #{name} and pwd = #{pwd}")
    /**
      * 对于多个参数来说,每个参数之前都要加上@Param注解,
      * 要不然会找不到对应的参数进而报错
      */
    public User login(@Param("name")String name, @Param("pwd")String pwd);
}
View Code

不过进过自己测试,用过写了xml,使用@mapper也会走到xml文件中,但是如果xml和@select同时存在,会报错,只能使用其中一种

原文地址:https://www.cnblogs.com/tianphone/p/10930605.html