Spring集成MyBatis进行项目开发(三)
package com.pinche.statistic.dao;import java.util.List;import com.pinche.statistic.domain.Application;public interface AppDao {boolean add(Application app);boolean delete(String appAccount);boolean update(Application app);Application findByAppAccount(String appAccount);List<Application> findAll();}
?AppDaoImpl.java
package com.pinche.statistic.dao.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.dao.DataAccessException;import org.springframework.stereotype.Repository;import com.pinche.statistic.dao.AppDao;import com.pinche.statistic.domain.Application;import com.pinche.statistic.mapper.ApplicationsMapper;@Repositorypublic class AppDaoImpl implements AppDao {@Autowiredprivate ApplicationsMapper mapper;@Overridepublic boolean add(Application app) {try {mapper.add(app);return true;} catch (DataAccessException e) {e.printStackTrace();}return false;}@Overridepublic boolean delete(String appAccount) {try {mapper.delete(appAccount);return true;} catch (DataAccessException e) {e.printStackTrace();}return false;}@Overridepublic boolean update(Application app) {try {mapper.update(app);return true;} catch (DataAccessException e) {e.printStackTrace();}return false;}@Overridepublic Application findByAppAccount(String appAccount) {try {Application findByAppAccount = mapper.findByAppAccount(appAccount);return findByAppAccount;} catch (DataAccessException e) {e.printStackTrace();}return null;}@Overridepublic List<Application> findAll() {try {return mapper.findAll();} catch (DataAccessException e) {e.printStackTrace();}return null;}}
?
? ? 自行设计的DAO层对象容器(在DAO对象很多时,如果在service层要调用对应的DAO还得手动注入,通过引用这个DAO层对象容器,可以实现在需要使用DAO时迅速找需要的DAO,省去了繁杂的手动注入,而且spring默认的bean都是单例的,无论在何处注入一个实体bean其实都是同一个。这样做更方便):
package com.pinche.statistic.dao;import java.lang.reflect.Field;import javax.annotation.PostConstruct;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Repository;@Repositorypublic class BaseDAL {private static final Logger logger = LoggerFactory.getLogger(BaseDAL.class);@Autowiredprivate AppDao _appDao;public static AppDao appDao;@Autowiredprivate MetaDataDao _metaDataDao;public static MetaDataDao metaDataDao;@Autowiredprivate DDLManager _DDLManager;public static DDLManager DDLManager;@Autowiredprivate AnalyzeDao _analyzeDao;public static AnalyzeDao analyzeDao;@Autowiredprivate DialstatisticsDao _dialstatisticsDao;public static DialstatisticsDao dialstatisticsDao;@PostConstructpublic void init() {long start = System.currentTimeMillis();logger.debug("start init BaseDAL ...");try {Field[] fields = this.getClass().getDeclaredFields();for (int i = 0; i < fields.length; i++) {String fieldname = fields[i].getName();if (fieldname.startsWith("_")) {String sfieldname = fieldname.substring(1);Field sfield = this.getClass().getDeclaredField(sfieldname);sfield.setAccessible(true);sfield.set(this, fields[i].get(this));}}logger.debug("init BaseDAL OVER, consume = {}ms",System.currentTimeMillis() - start);} catch (IllegalArgumentException e) {e.printStackTrace();} catch (NoSuchFieldException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}}
?
如果使用了以上的层管理容器,如果要在容器中添加一个DAO(例如:DemoDao),只需在这个容器中添加一个这样的声明:
@Autowiredprivate DemoDao _demoDao;public static DemoDao demoDao;
?
? ? 好了下面是service层定义的接口:
? ? AppService.java
package com.pinche.statistic.service;import java.util.List;import com.pinche.statistic.domain.Application;/** * @author JACKWANG * @since Dec 23, 2013 */public interface AppService {Application find(String appAccount);boolean update(Application app);boolean setDisable(String appAccount);boolean setEnable(String appAccount);List<Application> findAll();}
?
? ??AppServiceImpl.java : AppService的实现类:
package com.pinche.statistic.service.impl;import java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Service;import com.pinche.statistic.dao.BaseDAL;import com.pinche.statistic.domain.Application;import com.pinche.statistic.service.AppService;import com.pinche.statistic.utils.SystemUtils;/** * @author JACKWANG * @since Dec 23, 2013 */@Servicepublic class AppServiceImpl implements AppService {private static final Logger logger = LoggerFactory.getLogger(AppServiceImpl.class);@Overridepublic Application find(String appAccount) {return BaseDAL.appDao.findByAppAccount(appAccount);}@Overridepublic boolean update(Application app) {String appAccount = app.getAppAccount();if (appAccount == null && "".equals(appAccount)) {return true;}return BaseDAL.appDao.update(app);}@Overridepublic boolean setDisable(String appAccount) {Application app = new Application();app.setAppAccount(appAccount);app.setIsDisable(Application.APP_DISABLE);return BaseDAL.appDao.update(app);}@Overridepublic boolean setEnable(String appAccount) {Application app = new Application();app.setAppAccount(appAccount);app.setIsDisable(Application.APP_ENABLE);return BaseDAL.appDao.update(app);}@Overridepublic List<Application> findAll() {return BaseDAL.appDao.findAll();}}
? ?哈哈,使用层对象管理容器是不是很方便。通过一个引用就能获得所有的DAO支持。所以我在service层也构建了一个service层对象管理容器BaseBLL:
BaseBLL.java:
package com.pinche.statistic.service;import java.lang.reflect.Field;import javax.annotation.PostConstruct;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/** * @author JACKWANG * @since Dec 23, 2013 */@Servicepublic class BaseBLL {private static final Logger logger = LoggerFactory.getLogger(BaseBLL.class);@Autowiredprivate AnalyzeService _analyzeService;public static AnalyzeService analyzeService;@Autowiredprivate AppService _appService;public static AppService appService;@Autowiredprivate MetaDataService _metaDataService;public static MetaDataService metaDataService;@PostConstructpublic void init() {long start = System.currentTimeMillis();logger.debug("start init BaseBLL ...");try {Field[] fields = this.getClass().getDeclaredFields();for (int i = 0; i < fields.length; i++) {String fieldname = fields[i].getName();if (fieldname.startsWith("_")) {String sfieldname = fieldname.substring(1);Field sfield = this.getClass().getDeclaredField(sfieldname);sfield.setAccessible(true);sfield.set(this, fields[i].get(this));}}logger.debug("init BaseBLL OVER, consume = {}ms",System.currentTimeMillis() - start);} catch (IllegalArgumentException e) {e.printStackTrace();} catch (NoSuchFieldException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}}}
? ?
? ? 好了下面应该是controller层的编写了,但是由于笔者以上的代码只是摘录了系统中的部分,而在controller中涉及到其他的内容,如果直接摘录可能和以上的代码衔接不上。所以这里就不进行了controller层的具体介绍了。本系统controller层使用的是SpringMVC,开发效率一级赞。