我已经阅读了有关 Mybatis 的书籍和文档,XML 和 Annotation 都可以满足我的要求,但是从 myBatis 官方网站上,他们声称 XML 是一种更好的映射器方式,因为 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 所说的嵌套连接映射之上,resultMap
XML格式支持继承,注解无法实现,每次都要rewrite。还有 @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/