2015-09-17 8 views
5

私はMybatisに関する本とドキュメントを読んでいますが、XMLと注釈の両方が欲しいですが、myBatisの公式サイトからは、Java注釈には限界があるため、XMLがマッパーを行う良い方法だと主張しています。Mybatis XML vs Annotation

私は個人的に、私は制限があるのだろうか注釈例えば

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; 

} 

を好みますか?何も私には明らかではないようです。

+0

注釈は[Xml構成と注釈ベースの構成](http://stackoverflow.com/a/183401/1793718)に限定されています。 myBatisが関心を持つ限り、[docs](https://mybatis.github.io/mybatis-3/getting-started.html)では、最も高度なマッピングではXMLマッピングが必要です。 'ネストされた結合マッピング(Nested Join Mapping)は、その一例です。 – Lucky

+0

この複雑なアプリケーションのMybatis注釈に関する関連する質問を読む(http://stackoverflow.com/questions/15352242/mybatis-annotations-in-complex-applications) – Lucky

答えて

7

ピボット氏によると、resultMapのXML形式の継承結合マッピングは継承をサポートしているため、アノテーションでは達成できないため、毎回書き換えます。また、@Resultsアノテーションは、Mapper XML要素<resultMap>の対応部分です。しかし、MyBatis 3.2.2の時点では、@Results注釈のIDを指定することはできません。したがって、<resultMap> XML要素とは異なり、@Results宣言を異なるマッピングステートメントに再利用することはできません。これが意味することは、同じでも、@Resultsの設定を複製する必要があるということです。

@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設定は文の両方で同じであるが、我々はそれを複製する必要があります。たとえば、次のようfindStudentBy()findAllStudents()方法を参照してください。この問題の回避策もあります。 Mapper XMLファイルを作成して<resultMap>要素を設定し、アノテーションを使用してresultMapを参照することができます。

ID StudentResultの<resultMap>StudentMapper.xmlに定義します。 StudentMapper.javaで注釈

を使用して

<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マッパー、@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

+0

最近、 '@ Results'の再利用が可能になりました。 '@ Results'で' id'を指定すると、 '@ ResultMap'から参照することができます。 [docs](http://www.mybatis.org/mybatis-3/java-api.html#Mapper_Annotations)と[gitlab issue](https://github.com/mybatis/mybatis-3/issues/)を参照してください。 155)。 – Stim

3

はい、Mybatisのドキュメントでは、より簡単で簡単なプロジェクトのために注釈がはるかに簡単で読みやすいように注意しています。ただし、注釈はXML構成に比べて制限されています。プロジェクトに複合オブジェクトまたは複雑なデータベース構造が含まれている場合は、の代わりにXML設定を使用することを検討してください。

XMLマッピングは、まだMyBatisの中でマッピングに参加し入れ子のように最も高度なマッピングのために必要です。

+1

注釈はネストされた結合マッピングを達成できると確信していますが、 t? – user5324782

1

からの引用の.xmlを使用すると、はるかにconsiceとクリーンなアプローチすることができたときにたくさんのユースケースがあります。 言ってやるが、あなたは、いくつかのCommon.xmlを作成するクエリの束を定義し、

<sql id="inStmt"> 
    IN 
    <foreach item="id" collection="ids" separator="," open="(" close=")"> 
     #{id} 
    </foreach> 
</sql> 

様およびプロジェクトでこのコードを再利用することができます。さらに、テンプレートクエリは、例えば、次のように定義することができる。:

<sql id="selectDistinct"> 
    SELECT DISTINCT(${column}), #{param} 
    FROM ${entityTable} 
</sql> 

をそしてあなたは

<include refid="Common.selectDistinct"> 
     <property name="column" value="id"/> 
     <property name="entityTable" value="some_table"/> 
</include> 

を経由して、それを参照することができます。このアプローチはあまりエラーが発生しやすいとはるかに少ないコピー/貼り付け。 (あなたは見てhereを持つことができます)。

条件、繰り返しなどもあわせて記載する必要があります。