風(fēng)人園

          弱水三千,只取一瓢,便能解渴;佛法無(wú)邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評(píng)論 - 56, 引用 - 0
          數(shù)據(jù)加載中……

          iBatis Java Mapper--A iBatis+Spring擴(kuò)展 (ZT)


          轉(zhuǎn)載自 http://www.aygfsteel.com/xmatthew/archive/2009/01/05/249986.html

          iBatis Java Mapper是iBatis+Spring的一個(gè)擴(kuò)展,能夠讓你使用Java類和接口來(lái)代替用于object-SQL映射的XML文檔。

          官方網(wǎng)址:http://javamapper.sourceforge.net/
          目前還沒(méi)有正式發(fā)布版本,但很值得期待。

          下面是一個(gè)示例:

          第一步把iBatis Java Mapper實(shí)現(xiàn)類,配置到Spring容器中。如下:
          @ResultMap {
          public class Account {

                  
          private String username;
                  
          private String email;

                  @Result(column
          ="userid", groupBy=true)
                  
          public String getUsername();
                  
                  @Result(column
          ="email")
                  
          public String getEmail();
                  
                  
          // Setters 
          }


          更多示例:

          Creating Mapped Statements

          iBatis Java Mapper uses methods to map statements. Statement methods can be in classes and interfaces with or without the @ResultMap annotation, it doesn't matter. Methods are used for statement mapping if they are annotated with @Select, @Insert, @Update or @Delete.

          Here is an example of a mapped select statement:

          @Select("select username as value from signon")

          public abstract String getUsernameList();

          In this case the SQL is provided as the value of the @Select annotation. It is also possible to use a properties file to hold the SQL if you don't want it in the Java source. The properties file's path and name must match the class/interface full name, for example if the class containing the mapper method is named foo.bar.MapperClass , then its matching properties file must be /foo/bar/MapperClass.properties。The entry key is the method name and the entry value is the SQL:

          getUsernameList = select username as value from signon

          Now we can write the annotation without the SQL:

          @Select
          public abstract String getUsernameList();

          Let's take a look at the method signiture and see what iBatis Java Mapper can learn from it about the required mapping. The return type is String , so this is the class which is used as the statement result class. Now let's look at another example:

          @Select(resultClassMapper=AccountMapper.class)
          public abstract Account getAccountByUsernameAndPassword(Account account);

          Here the annotation specifies the class that contains the result map definition. In this case the return value is ignored, but we left it as sort of self-documenting code. This method also has an argument of type Account , so Account is used as parameterClass. If a method has more than one argument, iBatis Java Mapper uses the first one as parameter class and ignores the rest.

          Mapping of insert, update and delete statements is similar and even a little simpler. Here are some examples:

          @Insert
          public abstract void insertProfile(Account account);

          @Update
          public abstract void updateProfile(Account account);

          @Delete
          public abstract void deleteProfile(Account account);



          posted on 2009-04-15 16:46 風(fēng)人園 閱讀(382) 評(píng)論(0)  編輯  收藏 所屬分類: Open Source

          主站蜘蛛池模板: 蓬莱市| 张家川| 定安县| 许昌市| 南昌市| 毕节市| 平邑县| 镇雄县| 二手房| 庐江县| 阳山县| 淮阳县| 溆浦县| 安溪县| 南陵县| 土默特左旗| 贞丰县| 白玉县| 周宁县| 吉林省| 萨迦县| 湘乡市| 化德县| 南江县| 双鸭山市| 沙河市| 洞口县| 恭城| 巫溪县| 尚志市| 花垣县| 离岛区| 郯城县| 呈贡县| 昭苏县| 平湖市| 英山县| 丹江口市| 高州市| 东兰县| 丹巴县|