首页 > Linux操作系统 > Linux操作系统 > iBatis Java Mapper--A iBatis+Spring扩展

iBatis Java Mapper--A iBatis+Spring扩展

原创 Linux操作系统 作者:Java006 时间:2009-01-06 09:55:02 0 删除 编辑
iBatis Java Mapper是iBatis+Spring的一个扩展,能够让你使用Java类和接口来代替用于object-SQL映射的XML文档。



第一步把iBatis Java Mapper实现类,配置到Spring容器中。如下:
@ResultMap {
public class Account {

private String username;
private String email;

="userid", groupBy=true)
public String getUsername();
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 , then its matching properties file must be /foo/bar/。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:

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:

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 typeAccount , 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:

public abstract void insertProfile(Account account);

public abstract void updateProfile(Account account);

public abstract void deleteProfile(Account account);

来自 “ ITPUB博客 ” ,链接:,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录


  • 博文量
  • 访问量