疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

利用spring AOP和Annotation来简化DAO实现—软件培训


 

通常在数据库DAO层的查询中,我们会定义一个DAO接口,而在实现中我们只是拼接查询参数并且指定一个ibatissqlmap中的sqlid进行查询,Dao的实现很类似,而且非常简单,其实可以简化这种的实现,不需要这些实现代码,下面我们通过annotation机制来简化这块的实现。

  比如

  [java]

  public class TestDaoImpl extends SqlMapClientDaoSupport implements TestDao {

  @Override

  public int updateBrandOfferStatusByBrandMemberIdLong brandMemberId, String

 

operator, String status {

  Map<String, Object> map = new HashMap<String, Object>();

  map.put"brandMemberId", brandMemberId);

  map.put"operator", operator);

  map.put"status", status);

  return this.getSqlMapClientTemplate()。update"BRANDOFFER.UPDATE-BRANDOFFER-BY-BRANDMEMBERID", map);

  }

  @Override

  public List<Long> queryOfferIdsByBrandMemberIdLong brandMemberId, Integer start,

 

Integer end {

  Map<String, Object> map = new HashMap<String, Object>();

  map.put"brandMemberId", brandMemberId);

  map.put"start", start);

  map.put"end", end);

  return this.getSqlMapClientTemplate()。queryForList"BRANDOFFER.SELECT-

 

OFFERIDLIST-BY-BRANDMEMBERID", map);

  }

  ……

  }

  首先,我们使用建立一个spring的工程,依赖如下:

  [java]

  <?xml version="1.0" encoding="UTF-8"?>

  <project xmlns=http://maven.apache.org/POM/4.0.0

 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0

http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>mySpringWeb</groupId>

  <artifactId>springDemo</artifactId>

  <packaging>jar</packaging>

  <version>1.0.0-SNAPSHOT</version>

  <name>Started with Laurel</name>

  <dependencies>

  <dependency>

  <groupId>org.springframework</groupId>

  <artifactId>spring-context</artifactId>

  <version>3.2.0.RELEASE</version>

  </dependency>

  </dependencies>

  <repositories>

  <repository>

  <id>springsource-repo</id>

  <name>SpringSource Repository</name>

  <url>http://repo.springsource.org/release</url>

  </repository>

  </repositories>

  </project>

  我们定义两个annotation

DAO是用来加在DAO接口的方法上的annotaion,可以通过name指定ibatis中的sql id,这个annotation可以添加type之类的参数,可以用来指定dao查询的类型,

inserti,update,query,delete等类型。

  [java]

  package mySpringWeb;

  import java.lang.annotation.ElementType;

  import java.lang.annotation.Inherited;

  import java.lang.annotation.Retention;

  import java.lang.annotation.RetentionPolicy;

  import java.lang.annotation.Target;

  @RetentionRetentionPolicy.RUNTIME

  @Inherited

  @Target{ ElementType.METHOD, ElementType.TYPE}

  public @interface Dao {

  String name() default "[defaultMethod]";

  }

  DaoPara

  package mySpringWeb;

  import java.lang.annotation.ElementType;

  import java.lang.annotation.Inherited;

  import java.lang.annotation.Retention;

  import java.lang.annotation.RetentionPolicy;

  import java.lang.annotation.Target;

  @RetentionRetentionPolicy.RUNTIME

  @Inherited

       @Target{ElementType.FIELD, ElementType.METHOD, ElementType.LOCAL_VARIABLE,

ElementType.PARAMETER}

  public @interface DaoParam {

  String name() default "paramName";

  }

  然后定义一个DAO接口,里面有一个方法,

  [java]

  package mySpringWeb;

  import java.util.List;

  public interface MyDao {

  @Daoname="MyDaoAnnotataion"

  public List<Object> query@DaoParamname="param1"String param1, @DaoParam

name="param2"int param2);

  }

  我们写一个空的DAO实现类,

  [java]

  package mySpringWeb;

  import java.util.List;

  public class MyDaoImpl implements MyDao{

  @Override

  public List<Object> query@DaoParamname="param1"String param1, @DaoParam

name="param2"int param2 {

  // TODO Auto-generated method stub

  return null;

  }

  }

  然后就是写一个spring AOP的 拦截器类了,

  [java]

  package mySpringWeb;

  import java.lang.annotation.Annotation;

  import java.util.ArrayList;

  import java.util.HashMap;

  import java.util.List;

  import java.util.Map;

  import org.aopalliance.intercept.MethodInterceptor;

  import org.aopalliance.intercept.MethodInvocation;

  import org.springframework.core.annotation.AnnotationUtils;

  public class MyIntercept implements MethodInterceptor{

  static{

  try {

  Class.forName"mySpringWeb.Dao");

  } catch ClassNotFoundException e {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  @Override

  public Object invokeMethodInvocation invocation throws Throwable {

  Dao dao = AnnotationUtils.findAnnotationinvocation.getMethod(), Dao.class);

 

//递归查找该方法是否定义了这个annotation

  ifdao != null{

  List<String> list = new ArrayList<String>();

  list.adddao.name());

  System.out.printlndao.name());

  //method name就是ibatis sqlid,这里可以注入一个dao,然后传入sqlid,执行返回

  System.out.printlninvocation.getMethod()。getName());

  Map<String,Object> paraMap = new HashMap<String,Object>();

  Annotation[][] annotations = invocation.getMethod()。getParameterAnnotations();

  Object[] object = invocation.getArguments();

  forint i = 0; i < annotations.length;i++{

  forAnnotation an: annotations[i]{

  ifan.annotationType()。isAssignableFromDaoParam.class)){

  System.out.printlnan.toString());

  paraMap.put(((DaoParaman)。name(), object[i]);

  }

  }

  }

  //dao查询参数map

  System.out.printlnparaMap.toString());

  //这里ibatis sqlid和查询参数map都知道,那么就可以进行数据库查询,然后返回了,对于返回类型也可以通过定义返回的annotation进行返回参数类型的几种定义

  //当前这里需要注入springDAO的一个实现,这里根据annotation的类型和参数以及sqlid就可以进行数据库查询,然后返回,这里就比较简单了。

  return list;

  }

  System.out.println"go to here error");

  return null;

  }

  }