---
title: "整体架构dao、service、action篇"
author: "microye"
date: "2012-12-31T14:15:31Z"
tags: ["Hibernate", "Spring", "Struts", "dbutils"]
categories: ["开源软件"]
slug: "dao-service-action"
draft: false
lastmod: "2012-12-31T14:15:31Z"
id: 1757451
url: "https://yeshujun.iteye.com/blog/1757451"
---

1、一个spring配置文件，它是核心。这个xml配置的东东有：

     1）、一个号称目前最快的数据源：BoneCPDataSource。

     2）、数据库连接properties文件。

     3）、spring 自动扫描注册在application context中的组件：&lt;context:component-scan base-package="com.jroo" /&gt;

     4）、aop、注解事务管理：&lt;aop:aspectj-autoproxy/&gt;、&lt;tx:annotation-driven/&gt;

     5）、增删改查，就仅此一个spring配置文件，架构搭好了，开发人员不用管任何spring配置文件了。

     6）、见下图：

![图片](http://static.oschina.net/uploads/img/201212/29220123_yjm6.jpg)

 

2、实体层：使用jpa注解或hibernate注解标注每个实体，如：

@Entity  
@Table(name = "SYS\_DEPT")  
@Cache(usage=CacheConcurrencyStrategy.READ\_WRITE)  
public class SysDept extends BaseEntity implements java.io.Serializable {  
        private static final long serialVersionUID = -7969250614159287629L;  
        //略去属性  
        //略去构造方法  
        //下面是getter方法示例  
       @ManyToOne(fetch = FetchType.LAZY)  
       @JoinColumn(name = "PARENT\_ID")  
       public SysDept getSysDept() { return this.sysDept;}  
       @Column(name = "DEPT\_NAME", length = 20)  
       public String getDeptName() { return this.deptName;}    

        ..........  
}   
3、dao层性能：使用ehcache作为查询缓存。

4、dao层：

      1）、hibernate实现：推荐使用hibernate。

      2）、dbutils实现：如果是复杂的查询，如实体类没有建立关联关系，需建立左连接、右连接、union等负载查询逻辑， 或者您喜欢直接 写sql，推荐使用dbutils，它能完全满足您写sql的欲望。

      3）、一个dao原则：一个系统只有一个dao，不管您开发的多少个模块，都不再需要写dao。一个dao管理所有模块与数据库的操作。

      4）、hql怎么写的，在哪里写的？   
      4.1）、使用HqlBuilder或SqlBuilder封装查询语句及查询参数。HqlBuilder和StringBuilder、stringBuffer的使用方法类似。

             如分页查询：

             HqlBuilder hqlBuilder = new HqlBuilder("select log from SysLoginLog as log where 1=1");  
             if(StringUtils.isNotEmpty(loginUserId)){  
                  hqlBuilder.append(" and log.loginUser.id=:loginUserId ").setParam("loginUserId", loginUserId);

              }  
           if(StringUtils.isNotEmpty(loginUserName)){  
               hqlBuilder.append(" and log.loginUser.userName like:loginUserName ").setParam("loginUserName", "%"+loginUserName+"%");

            }

          page = pageQueryHqlBuilder(page, hqlBuilder);

 

     4.2）、写在外部xml中：  
![图片](http://static.oschina.net/uploads/img/201212/29220124_UsqS.jpg)  
     4.3）、在页面上写，并存入数据库：

     ![](http://static.oschina.net/uploads/space/2012/1229/215842_yqqI_921630.png)

    ![](http://static.oschina.net/uploads/space/2012/1229/215904_MLOO_921630.png)

 

5、service层：每个功能模块都应该有一个service层：一个接口和它的实现。

接口如：public interface UserService extends BaseService&lt;SysUser&gt; ，需继承接口BaseService，

BaseService接口中定义了一般增删改查方法。

实现类如：

        @Service   
        @Transactional   
         public class UserServiceImpl extends BaseServiceImpl&lt;SysUser&gt; implements UserService {}

       注意注解：@Service和@Transactional，实现需继承BaseServiceImpl类，

6、action层，如：

        @Controller("system.action.AccessLogAction")   
        @Scope("prototype")   
        public class AccessLogAction extends BaseCrudAction&lt;SysAccessLog&gt;

       注意两个注解和父类BaseCrudAction，如果该功能有附件上传，请继承BaseUploadFileAction。