欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring集成MyBatis进行项目开发(三)

程序员文章站 2022-07-12 13:34:48
...

 

     Spring集成MyBatis进行项目开发(一)

     Spring集成MyBatis进行项目开发(二)

    上两章进行了相关环境的配置和测试,接下来是将继续这个样例系统的Dao层,service层和controller层。

    AppDao.java

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;

@Repository
public class AppDaoImpl implements AppDao {

	@Autowired
	private ApplicationsMapper mapper;
	
	@Override
	public boolean add(Application app) {
		try {
			mapper.add(app);
			return true;
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean delete(String appAccount) {
		try {
			mapper.delete(appAccount);
			return true;
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean update(Application app) {
		try {
			mapper.update(app);
			return true;
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public Application findByAppAccount(String appAccount) {
		try {
			Application findByAppAccount = mapper.findByAppAccount(appAccount);
			return findByAppAccount;
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public 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;

@Repository
public class BaseDAL {

	private static final Logger logger = LoggerFactory.getLogger(BaseDAL.class);
	
	@Autowired
	private AppDao _appDao;
	public static AppDao appDao;

	@Autowired
	private MetaDataDao _metaDataDao;
	public static MetaDataDao metaDataDao;

	@Autowired
	private DDLManager _DDLManager;
	public static DDLManager DDLManager;
	
	@Autowired
	private AnalyzeDao _analyzeDao;
	public static AnalyzeDao analyzeDao;
	
	@Autowired
	private DialstatisticsDao _dialstatisticsDao;
	public static DialstatisticsDao dialstatisticsDao;

	@PostConstruct
	public 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),只需在这个容器中添加一个这样的声明:

	@Autowired
	private 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
 */
@Service
public class AppServiceImpl implements AppService {

	private static final Logger logger = LoggerFactory
			.getLogger(AppServiceImpl.class);

	@Override
	public Application find(String appAccount) {
		return BaseDAL.appDao.findByAppAccount(appAccount);
	}

	
	@Override
	public boolean update(Application app) {
		String appAccount = app.getAppAccount();
		if (appAccount == null && "".equals(appAccount)) {
			return true;
		}
		return BaseDAL.appDao.update(app);
	}

	@Override
	public boolean setDisable(String appAccount) {
		Application app = new Application();
		app.setAppAccount(appAccount);
		app.setIsDisable(Application.APP_DISABLE);
		return BaseDAL.appDao.update(app);
	}

	@Override
	public boolean setEnable(String appAccount) {
		Application app = new Application();
		app.setAppAccount(appAccount);
		app.setIsDisable(Application.APP_ENABLE);
		return BaseDAL.appDao.update(app);
	}

	@Override
	public 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
 */
@Service
public class BaseBLL {
	
	private static final Logger logger = LoggerFactory.getLogger(BaseBLL.class);
	
	@Autowired
	private AnalyzeService _analyzeService;
	public static AnalyzeService analyzeService;

	@Autowired
	private AppService _appService;
	public static AppService appService;

	@Autowired
	private MetaDataService _metaDataService;
	public static MetaDataService metaDataService;

	@PostConstruct
	public 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,开发效率一级赞。