java - Mybatis XML 与注解

标签 java mybatis ibatis

我已经阅读了有关 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/

相关文章:

java - mybatis 3 selectkey inside foreach

java - 与 Spring 和 Mybatis 的事务

java - 当我通过 JSON 输入时,如何在 Ibatis 中将 java.util.Date 映射到 MySql Date

iis - IIS 部署站点上的 Ingres 连接

Java循环链表,删除不一致的地方

java - 如何将数组列表打印到 JTextArea?

java - 如何使用 mybatis-string 在一个 session 中执行多个 sql 语句

java - mybatis查询中需要返回SINGLE列Date值

java - OpenJPA - 嵌套 OneToMany 关系合并问题

java - 如何在运行时设置log4j的DB Credentials