论坛首页 Java企业应用论坛

如何换数据库(未使用任何框架)

浏览 7337 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (8) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-12-18   最后修改:2009-12-30

 

因为之前使用的mysql 现在想转为Oracle实现

如何改能降低最大的工作量

DaoFactory 类 里面组合了 Dao的实例

ServiceFactory 类 里面 组合了 Service实例

 ps:这两个类不算工厂类 应将类名Factory改成Contextg更合适

具体下面贴代码

 

 

 

public class DaoFactory
{
    private static IAccountDao accountDao = new AccountDaoImpl();
    private static IAffairDao affairDao = new AffairDaoImpl();
    private static IStateDao stateDao = new StateDaoImpl();
    private static IPersonnelDao personnelDao = new PersonnelDaoImpl();
    private static IAnnouncementDao announcementDao = new AnnouncementDaoImpl();
    private static IStatDao statDao = new StatDaoImpl();

    private static IDepartmentDao departmentDao = new DepartmentDaoImpl();
    private static IPositionDao positionDao = new PositionDaoImpl();
    private static IInformationDao informationDao = new InformationDaoImpl();
    private static IDaysDao daysDao = new DaysDaoImpl();
    private static IWeekDao weekDao = new WeekDaoImpl();
    private static IWorkContentDao workContentDao = new WorkContentDaoImpl();

    private static IEmployeeManageDao employeeManageDao = new EmployeeManageDaoImpl();
    private static IHolidayDao holidayDao = new HolidayDaoImpl();
    private static IHolidayTypeDao holidayTypeDao = new HolidayTypeDaoImpl();

    private static IMessageDao messageDao = new MessageDaoImpl();

    
    
    
    public static IMessageDao getMessageDao()
    {
        return messageDao;
    }

    public static IAccountDao getAccountDao()
    {
        return accountDao;
    }

    public static IAffairDao getAffairDao()
    {
        return affairDao;
    }


    public static IStateDao getStateDao()
    {
        return stateDao;
    }

    public static IPersonnelDao getPersonnelDao()
    {
        return personnelDao;
    }

    public static IAnnouncementDao getAnnouncementDao()
    {
        return announcementDao;
    }

    public static IStatDao getStatDao()
    {
        return statDao;
    }

    public static IDepartmentDao getDepartmentDao()
    {
        return departmentDao;
    }

    public static IPositionDao getPositionDao()
    {
        return positionDao;
    }

    public static IInformationDao getInformationDao()
    {
        return informationDao;
    }

    public static IDaysDao getDaysDao()
    {
        return daysDao;
    }

    public static IWeekDao getWeekDao()
    {
        return weekDao;
    }

    public static IWorkContentDao getWorkContentDao()
    {
        return workContentDao;
    }

    public static IEmployeeManageDao getEmployeeManageDao()
    {
        return employeeManageDao;
    }

    public static IHolidayDao getHolidayDao()
    {
        return holidayDao;
    }

    public static IHolidayTypeDao getHolidayTypeDao()
    {
        return holidayTypeDao;
    }

}

 

 

public class ServiceFactory
{
    private static IAccountService accountService = new AccountServiceImpl();
    private static IAffixService affixService = new AffixServiceImpl();
    private static IAffairService affairService = new AffairServiceImpl();
    private static IStateService stateService = new StateServiceImpl();
    private static IPersonnelService personnelService = new PersonnelServiceImpl();
    private static IAnnouncementService announcementService = new AnnouncementServiceImpl();
    private static IStatService statService = new StatServiceImpl();
    private static IDepartmentService departmentService = new DepartmentServiceImpl();
    private static IWorkContentService workContentService = new WorkContentServiceImpl();
    private static IInformationService informationService = new InformationServiceImpl();
    private static IPositionService positionService = new PositionServiceImpl();
    private static IDaysService daysService = new DaysServiceImpl();
    private static IWeekService weekService = new WeekServiceImpl();
    private static IEmployeeManageService employeeManageService = new EmployeeServiceImpl();
    private static IHolidayService holidayService = new HolidayServiceImpl();
    private static IHolidayTypeService holidayTypeService = new HolidayTypeServiceImpl();
    private static IMessageService messageService = new MessageServiceImpl();
    
   
    
    public static IMessageService getMessageService()
    {
        return messageService;
    }

    public static IEmployeeManageService getEmployeeManageService()
    {
        return employeeManageService;
    }

    public static IAccountService getAccountService()
    {
        return accountService;
    }
    
    public static IAffixService getAffixService()
    {
        return affixService;
    }
    
    public static IAffairService getAffairService()
    {
        return affairService;
    }
    
    public static IStateService getStateService()
    {
        return stateService;
    }
    
    public static IPersonnelService getPersonnelService()
    {
        return personnelService;
    }
    
    public static IAnnouncementService getAnnouncementService()
    {
        return announcementService;
    }
    
    public static IStatService getStatService()
    {
        return statService;
    }
    
    public static IDepartmentService getDepartmentService()
    {
        return departmentService;
    }
    
    public static IWorkContentService getWorkContentService()
    {
        return workContentService;
    }
    
    public static IInformationService getInformationService()
    {
        return informationService;
    }
    
    public static IPositionService getPositionService()
    {
        return positionService;
    }
    
    public static IDaysService getDaysService()
    {
        return daysService;
    }
    
    public static IWeekService getWeekService()
    {
        return weekService;
    }

    
    public static IHolidayService getHolidayService()
    {
        return holidayService;
    }

    
    public static IHolidayTypeService getHolidayTypeService()
    {
        return holidayTypeService;
    }
    
    
    
}

 

  例如 AccountServiceImpl 的实现

 

 

public class AccountServiceImpl implements IAccountService
{ 
    public AccountBean login(String name, String password)
    {
        return DaoFactory.getAccountDao().login(name, password);
    }
}

 

   发表时间:2009-12-18  
ServiceFactory   类改不了了,因为servlet里面充斥着这样的代码
0 请登录后投票
   发表时间:2009-12-18  
  

public class DaoFactory   
{   
    private static IAccountDao accountDao ;
    private static IAffairDao affairDao ;  
    private static IStateDao stateDao ; 
    private static IPersonnelDao personnelDao ;   
    private static IAnnouncementDao announcementDao;
    private static IStatDao statDao;
  
    private static IDepartmentDao departmentDao ;
    private static IPositionDao positionDao  ;
    private static IInformationDao informationDao ;
    private static IDaysDao daysDao ;
    private static IWeekDao weekDao ;
    private static IWorkContentDao workContentDao ;  
    private static IEmployeeManageDao employeeManageDao;
    private static IHolidayDao holidayDao ;
    private static IHolidayTypeDao holidayTypeDao  ;

  
    private static IMessageDao messageDao ;

     static{
       //读取配置文件 获得数据库类型

        //下面实例化
       
        if(TYPE==mysql ){ 
         // 实例化
           IAccountService accountService = new AccountServiceImpl();   
...
          }else if(TYPE==oracle ){

          IAccountService accountService = new AccountServiceOracleImpl();   

...
          }



     }  
       
       
    public static IMessageDao getMessageDao()   
    {   
        return messageDao;   
    }   
  
    public static IAccountDao getAccountDao()   
    {   
        return accountDao;   
    }   
  
    public static IAffairDao getAffairDao()   
    {   
        return affairDao;   
    }   
  
  
    public static IStateDao getStateDao()   
    {   
        return stateDao;   
    }   
  
    public static IPersonnelDao getPersonnelDao()   
    {   
        return personnelDao;   
    }   
  
    public static IAnnouncementDao getAnnouncementDao()   
    {   
        return announcementDao;   
    }   
  
    public static IStatDao getStatDao()   
    {   
        return statDao;   
    }   
  
    public static IDepartmentDao getDepartmentDao()   
    {   
        return departmentDao;   
    }   
  
    public static IPositionDao getPositionDao()   
    {   
        return positionDao;   
    }   
  
    public static IInformationDao getInformationDao()   
    {   
        return informationDao;   
    }   
  
    public static IDaysDao getDaysDao()   
    {   
        return daysDao;   
    }   
  
    public static IWeekDao getWeekDao()   
    {   
        return weekDao;   
    }   
  
    public static IWorkContentDao getWorkContentDao()   
    {   
        return workContentDao;   
    }   
  
    public static IEmployeeManageDao getEmployeeManageDao()   
    {   
        return employeeManageDao;   
    }   
  
    public static IHolidayDao getHolidayDao()   
    {   
        return holidayDao;   
    }   
  
    public static IHolidayTypeDao getHolidayTypeDao()   
    {   
        return holidayTypeDao;   
    }   
  
}  



但是却有if else 破坏了封装性

0 请登录后投票
   发表时间:2009-12-18  
如果你要换数据库首先如果有框架ibtais你肯定不要选,hibernate对数据量不大的情况下还是可行
另外 spring jdbcTemple自己封装下也一般可行,如果不选任何框架,那只有自己封装了,我个人愚见,项目里使用spring还是不错的,最起码new这个词在我的代码里,看不到的,感觉极其丑陋。
1 请登录后投票
   发表时间:2009-12-18  
重新实现DAO,然后再DAOFactory里面修改获取代码
0 请登录后投票
   发表时间:2009-12-18  
spring 的bean 配置太多了也烦啊
0 请登录后投票
   发表时间:2009-12-18  
不知道楼主想问什么,现在的代码已经是接口依赖了,替换DAO实现是很自然的事情,难道LZ是想不该daoimpl来实现?
0 请登录后投票
   发表时间:2009-12-18  
LZ 看下 spring 的scan 扫描 就不用写那么多配置了
0 请登录后投票
   发表时间:2009-12-18  
DAO都没有用接口直接写的类?没有框架的情况下,直接替换实现是理所当然的吧。

像楼主这样,我完全看不出如果有方言冲突的DAO实现你如何解决?

而且即便以前没用数据库方面的框架,替换数据库DAO以后用Digest把DAO实现和Service实现用配置的方式拼装起来不是更简单吗?

要隔离数据库这一层,没有注入实在想不出能够在不改代码的情况下有很好的办法。
0 请登录后投票
   发表时间:2009-12-19  
不支持,会有很多code duplication
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics