service层重构的解决方案(三种解决…

1 service层重构的关键代码;

package com.lrq.service.base;

public interface ICommonService<T> {
      public void save(T t);
}


package com.lrq.service.base.impl;

import com.ilrq.dao.DAO;
import com.lrq.service.base.ICommonService;

public abstract class CommonServiceImpl<T> implements ICommonService<T> {
      public DAO iDao;//通过子类进行注入;

      public void save(T t) {
            System.out.println("save t");
            iDao.persistent(t);
      }

      public DAO getiDao() {
            return iDao;
      }

      public void setiDao(DAO iDao) {
            this.iDao = iDao;
      }
     
     
}

某个具体的接口继承自ICommonService;
package com.lrq.service;

import java.io.Serializable;
import java.util.Collection;

import com.lrq.domain.Department;
import com.lrq.service.base.ICommonService;

public interface DepartmentService extends ICommonService<Department>{
     
      public Department findDepartmentById(Serializable id);
     
      public void deleteDepartment(Serializable id);
     
      public void updateDepartment(Department department);
     
      public void saveDepartment(Department department);
     
      public  Collection<Department> showAllDepartments();
     
     
      public Department findDepartmentByName(String dname);
}
对应的实现类;
package com.lrq.service.impl;

import java.io.Serializable;
import java.util.Collection;

import com.lrq.domain.Department;
import com.lrq.service.DepartmentService;
import com.lrq.service.base.impl.CommonServiceImpl;

public class MyDepartmentServiceImpl extends CommonServiceImpl<Department>
            implements DepartmentService {

      public Department findDepartmentById(Serializable id) {
            // TODO Auto-generated method stub
            return null;
      }

      public void deleteDepartment(Serializable id) {
            // TODO Auto-generated method stub
           
      }

      public void updateDepartment(Department department) {
            // TODO Auto-generated method stub
           
      }

      public void saveDepartment(Department department) {
            // TODO Auto-generated method stub
           
      }

      public Collection<Department> showAllDepartments() {
            // TODO Auto-generated method stub
            return null;
      }

      public Department findDepartmentByName(String dname) {
            // TODO Auto-generated method stub
            return null;
      }
     
}
spring配置文件关键配置;
<bean id="departmentService" class="com.lrq.service.impl.MyDepartmentServiceImpl">
      <property name="iDao" ref="departmentDao" />
</bean>
      ref:就是具体的dao实现类;
      ps:DAO:最好设置为protected的;
     

基于注解的方式实现:
方式一:用策略设计模式将DAO做为参数传递给service;谁调用我的service,谁给我注入对应的Dao实现;
具体代码如下;通用service接口;
package com.lrq.service.base;

import java.io.Serializable;
import java.util.Collection;

import com.lrq.dao.base.ICommonDao;


public interface ICommonService<T> {
     
      public void saveEntity(T t,ICommonDao iCommonDao);
     
      public void updateEntity(T t,ICommonDao iCommonDao);
     
      public void deleteEntity(Serializable id,ICommonDao iCommonDao);
     
      public T getEntity(Class clazz,Serializable id,ICommonDao iCommonDao);
     
      public Collection<T> getAllEntities(ICommonDao iCommonDao);
}

通用service接口实现;
package com.lrq.service.base.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;

import com.lrq.dao.IUserDao;
import com.lrq.dao.base.ICommonDao;
import com.lrq.service.base.ICommonService;

public abstract class CommonServiceImpl<T> implements ICommonService<T> {
      private Class clazz;
      public CommonServiceImpl() {
            ParameterizedType type = (ParameterizedType) this.getClass()
                        .getGenericSuperclass();
            clazz = (Class) type.getActualTypeArguments()[0];
      }

      public void saveEntity(T t, ICommonDao iCommonDao) {
            if (t != null) {
                  iCommonDao.persist(t);
            }
      }

      public void updateEntity(T t, ICommonDao iCommonDao) {
            if (t != null) {
                  iCommonDao.persist(t);
            }

      }

      public void deleteEntity(Serializable id, ICommonDao iCommonDao) {
            if (id != null) {
                  iCommonDao.delete(id);
            }
      }

      public T getEntity(Class clazz, Serializable id, ICommonDao iCommonDao) {
            return (T) (id==null? null:iCommonDao.get(clazz, id));
      }

      public Collection<T> getAllEntities(ICommonDao iCommonDao) {
            return iCommonDao.listAll();
      }
}
测试用例:
private IDepartmentService iDepartmentService;
      private IDepartmentDao iDepartmentDao;
      @Before
      public void setUp() throws Exception {
            ClassPathXmlApplicationC ontext applicationContext = new ClassPathXmlApplicationC ontext(
                        "spring/applicationContext.xml");
            iDepartmentService = applicationContext
                        .getBean(IDepartmentService.class);
            iDepartmentDao=applicationContext.getBean(IDepartmentDao.class);
      }

      @After
      public void tearDown() throws Exception {
      }

      @Test
      public void testSaveDepartment() {
            Department department=new Department();
            department.setDescription("多多");
            department.setDname("美工部");
     
            iDepartmentService.saveEntity(department,iDepartmentDao);//dao注入

      }
}
ps:注意这种方式请务必将dao做成单例的,如果是原型,每个用的dao的对象都不同;
怎么做到事务的统一管理呢?----这个不知道要不要这样做;

方式二:通过autowired注解,加在子类实现类的setDao方法上,给dao注值;
关键代码;
通用service接口;
package com.lrq.service.base;

import java.io.Serializable;
import java.util.Collection;

import com.lrq.dao.base.ICommonDao;


public interface ICommonService<T> {
     
      public void saveEntity(T t);
     
      public void updateEntity(T t);
     
      public void deleteEntity(Serializable id);
     
      public T getEntity(Class clazz,Serializable id);
     
      public Collection<T> getAllEntities();
}
通用service实现类;
package com.lrq.service.base.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;

import com.lrq.dao.base.ICommonDao;
import com.lrq.service.base.ICommonService;

public abstract class CommonServiceImpl<T> implements ICommonService<T> {
      private Class clazz;
      protected ICommonDao iCommonDao;

      public CommonServiceImpl() {
            ParameterizedType type = (ParameterizedType) this.getClass()
                        .getGenericSuperclass();
            clazz = (Class) type.getActualTypeArguments()[0];
      }

      public void saveEntity(T t) {
            if (t != null) {
                  iCommonDao.persist(t);
            }
      }

      public void updateEntity(T t) {
            if (t != null) {
                  iCommonDao.persist(t);
            }

      }

      public void deleteEntity(Serializable id) {
            if (id != null) {
                  iCommonDao.delete(id);
            }
      }

      public T getEntity(Class clazz, Serializable id) {
            return (T) (id == null ? null : iCommonDao.get(clazz, id));
      }

      public Collection<T> getAllEntities() {
            return iCommonDao.listAll();
      }

      public ICommonDao getiCommonDao() {
            return iCommonDao;
      }

      public void setiCommonDao(ICommonDao iCommonDao) {
            this.iCommonDao = iCommonDao;
      }
     
}



具体实现类;
package com.lrq.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lrq.dao.IDepartmentDao;
import com.lrq.domain.Department;
import com.lrq.service.IDepartmentService;
import com.lrq.service.base.impl.CommonServiceImpl;

@Service
public class DepartmentServiceImpl extends CommonServiceImpl<Department>
            implements IDepartmentService {

      @Autowired
      public void setIDepartmentDao(IDepartmentDao iDepartmentDao) {
            super.setiCommonDao(iDepartmentDao);//给父类注值;
      }

}
测试用例:
Department department=new Department();
department.setDescription("多多");
department.setDname("女女女");
iDepartmentService.saveEntity(department);
测试通过;
<script type="text/javascript" id="wumiiRelatedItems"> </script>
 
阅读(29) | 评论(0)
推荐
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值