问题描述
我已经阅读了有关Mybatis的书和文档,XML和Annotation都能满足我的要求,但是在myBatis官方网站上,他们声称XML是做Mappers的更好方法,因为Java注释有局限性.
I have read the book and documentation about Mybatis, both XML and Annotation does what I want, but from myBatis official website, they claim XML is a better way of doing Mappers, because Java annotation has limitations.
我个人更喜欢注释,例如
I personally prefer Annotations e.g
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;
}
我想知道限制是什么?对我来说似乎什么都没有.
I wonder what the limitation is? Nothing seems to appear obvious to me.
推荐答案
在Pitchers所说的嵌套连接映射的顶部,XML格式的resultMap
支持继承,这在注解中是无法实现的,每次都需要重写.同样,@Results
注释与Mapper XML元素<resultMap>
对应.但是,从MyBatis 3.2.2开始,我们无法为@Results
批注提供ID.因此,与<resultMap>
XML元素不同,我们不能在不同的映射语句之间重用@Results
声明.这意味着即使相同,您仍需要复制@Results
配置.例如,请参见以下findStudentBy()
和findAllStudents()
方法:
On top of Nested Join Mapping that Pitchers said, resultMap
in XML format supports inheritance, which can not be achieved in annotation, you have do rewrite each time. Also the @Results
annotation is a counterpart of the Mapper XML element <resultMap>
. However, as of MyBatis 3.2.2 we can't give an ID for the @Results
annotation. So unlike the <resultMap>
XML element, we can't reuse the @Results
declaration across different mapped statements. What this means is that you need to duplicate the @Results
configuration even though it is the same. For example, see the following findStudentBy()
and findAllStudents()
methods:
@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
.
Here the @Results
configuration is same for both the statements, but we need to duplicate it. There is also a work around for this problem. We can create a Mapper XML file and configure the <resultMap>
element and reference that resultMap
using the @ResultMap
annotation.
在StudentMapper.xml
中定义ID为StudentResult的<resultMap>
.
Define <resultMap>
with ID StudentResult in 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映射器
SQL Mappers Using Annotations
在StudentMapper.java
中,使用@ResultMap
引用resultMap
属性StudentResult
.
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持久性与MyBatis3中的
引用
quote from Java-Persistence-with-MyBatis3
这篇关于Mybatis XML与注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!