我已经阅读了有关Mybatis的书和文档,XML和Annotation都能满足我的要求,但是在myBatis官方网站上,他们声称XML是做Mappers的更好方法,因为Java注解有局限性。

我个人更喜欢注释

public interface PersonDAO {

    String INSERT_PERSON = "insert into person (title,firstName,surName,jobTitle,dob,email,mobile,landPhone,fax,twitter,facebook,linkedin) VALUES  (#{title},#{firstName},#{surName},#{jobTitle},#{dob},#{email},#{mobile},#{landPhone},#{fax},#{twitter},#{facebook},#{linkedin})";
    String UPDATE_PERSON = "update person set title=#{title},firstName=#{firstName},surName=#{surName},jobTitle=#{jobTitle},dob=#{dob},email=#{email},mobile=#{mobile},landPhone=#{landPhone},fax=#{fax},twitter=#{twitter},facebook=#{facebook},linkedin=#{linkedin} where id=#{id}";
    String GET_PERSON_BY_ID = "SELECT * FROM vw_person WHERE id = #{personId}";
    String DELETE_PERSON = "DELETE FROM person WHERE id = #{personId}";

    @Select(GET_PERSON_BY_ID)
    public PersonVO doSelectPerson(long personId) throws Exception;

    @Update(UPDATE_PERSON)@Options(flushCache = true, useCache = true)
    public int doUpdatePerson(PersonVO vo) throws Exception;


    @Insert(INSERT_PERSON)@Options(useGeneratedKeys = true, keyProperty = "id", flushCache = true)
    public int doCreatePerson(PersonVO person) throws Exception;

    @Delete(DELETE_PERSON)@Options(flushCache = true)
    public int doDeletePerson(long personId) throws Exception;

}

我想知道限制是什么?对我来说似乎没有什么明显的。

最佳答案

Pitchers说过的嵌套连接映射(Nested Join Mapping)之上,XML格式的resultMap支持继承,而在注释中无法实现,因此每次都需要重写。另外,@Results注释与Mapper XML元素<resultMap>对应。但是,从MyBatis 3.2.2开始,我们无法为@Results注释提供ID。因此,与<resultMap> XML元素不同,我们无法在不同的映射语句之间重用@Results声明。这意味着即使相同,您也需要复制@Results配置。例如,请参见以下findStudentBy()findAllStudents()方法:

@Select("SELECT * FROM STUDENTS WHERE STUD_ID=#{studId}")
@Results({
  @Result(id=true, column="stud_id", property="studId"),
  @Result(column="name", property="name"),
  @Result(column="email", property="email"),
  @Result(column="addr_id", property="address.addrId")
})
Student findStudentById(int studId);

@Select("SELECT * FROM STUDENTS")
@Results({
  @Result(id=true, column="stud_id", property="studId"),
  @Result(column="name", property="name"),
  @Result(column="email", property="email"),
  @Result(column="addr_id", property="address.addrId")
})
List<Student> findAllStudents();

这两个语句的@Results配置相同,但是我们需要对其进行复制。也可以解决此问题。我们可以创建一个Mapper XML文件并配置<resultMap>元素,并使用resultMap批注引用该@ResultMap

<resultMap>中定义ID为StudentResult的StudentMapper.xml
<mapper namespace="com.mybatis3.mappers.StudentMapper">
  <resultMap type="Student" id="StudentResult">
    <id property="studId" column="stud_id"/>
    <result property="name" column="name"/>
    <result property="email" column="email"/>
    <result property="phone" column="phone"/>
  </resultMap>
</mapper>

使用注释的SQL映射器

StudentMapper.java中,使用resultMap引用StudentResult属性@ResultMap
public interface StudentMapper

@Select("SELECT * FROM STUDENTS WHERE STUD_ID=#{studId}")
@ResultMap("com.mybatis3.mappers.StudentMapper.StudentResult")
Student findStudentById(int studId);

@Select("SELECT * FROM STUDENTS")
@ResultMap("com.mybatis3.mappers.StudentMapper.StudentResult")
List<Student> findAllStudents();

Java-Persistence-with-MyBatis3的引文

关于java - Mybatis XML与注释,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32631438/

10-11 06:28