Mybatis XML与注释

use*_*782 8 java ibatis mybatis

我已经阅读了关于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;

}
Run Code Online (Sandbox Code Playgroud)

我想知道限制是什么?对我来说似乎没什么好看的.

Jun*_*Liu 11

在Pitchers所说的嵌套连接映射之上,resultMapXML格式支持继承,这在注释中是无法实现的,每次都要重写.另外,@Results标注是映射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();
Run Code Online (Sandbox Code Playgroud)

@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>
Run Code Online (Sandbox Code Playgroud)

使用注释的SQL Mappers

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();
Run Code Online (Sandbox Code Playgroud)

引自Java-Persistence-with-MyBatis3

  • 如今,重用“@Results”实际上是可能的。如果您为“@Results”提供“id”,那么您可以从“@ResultMap”引用它。请参阅[文档](http://www.mybatis.org/mybatis-3/java-api.html#Mapper_Annotations)和[gitlab问题](https://github.com/mybatis/mybatis-3/issues/155 )。 (3认同)