JAVA_WEB项目之Service层抽取成BaseService和BaseServiceImpl

在javaweb开发中使用到三大框架开发的时候会涉及到Dao层,Service层,Action层,接下来是贴出在Service层中抽取出公共业务逻辑模块的BaseService接口和BaseServiceImpl实现类。

package com.shop.service;

import java.util.List;

import com.shop.pojo.Category;

public interface BaseService {
	public void save(T t);
	public void update(T t);
	public void delete(int id);
	public T queryById(int id);
	public List query();
}

我们定义一个CategoryService接口,继承上面的接口:

package com.shop.service;

import java.util.List;

import com.shop.pojo.Category;

public interface CategoryService extends BaseService{
//定义属于CategoryService的业务方法
	 
}

BaseServiceImpl:公共业务逻辑实现类

package com.shop.service.impl;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import com.shop.pojo.Category;
import com.shop.service.BaseService;
import com.shop.service.CategoryService;
/**
 * 
 * @author Administrator
 *实现BaseService接口定义的方法,所有模块都有公共的方法
 */
@Service(value="baseService")//没有指定value的话,默认是第一个字母小写的类名,可以看做是xml中的bean的id
@SuppressWarnings("unchecked")
@Lazy(value=true)//延迟初始化,防止自身实例化时,调用构造BaseServiceImpl方法出现object类型转换异常
public class BaseServiceImpl implements BaseService{
	@Resource(name="sessionFactory")
	protected SessionFactory sessionFactory;
	private Class clazz;//存储了具体操作的类,
	public BaseServiceImpl(){
		System.out.println(this);
		System.out.println(this.getClass());
		System.out.println(this.getClass().getSuperclass());
		System.out.println(this.getClass().getGenericSuperclass());
		Type type= this.getClass().getGenericSuperclass();
		ParameterizedType parameterizedType=(ParameterizedType) type;
		clazz= (Class) parameterizedType.getActualTypeArguments()[0];
		System.out.println(clazz);
	}
	public static void main(String[] args) {
		new ClassPathXmlApplicationContext("applicationContext-*.xml");
	}
	public void save(T t) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();//得到当前线程的sessionFactory
		session.save(t);
	}

	public void update(T t) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();//得到当前线程的sessionFactory
		session.update(t);
	}

	public void delete(int id) {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();//得到当前线程的sessionFactory
		//session.delete(session.get(Category.Class,id));//生成两天sql语句
		session.createQuery("delete From "+clazz.getSimpleName()+" c where c.id=:id")
		.setInteger("id", id)
		.executeUpdate();
	}

	public T queryById(int id) {
		Session session=sessionFactory.getCurrentSession();
		return (T) session.get(clazz, id);
	}

	public List query() {
		// TODO Auto-generated method stub
		Session session=sessionFactory.getCurrentSession();
		return session.createQuery("From "+clazz.getSimpleName()).list();
	}



}

下面写一个CategoryServiceImpl:

 package com.shop.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;

import com.shop.pojo.Category;
import com.shop.service.CategoryService;
@Service(value="categoryService")//没有指定value的话,默认是第一个字母小写的类名,可以看做是xml中的bean的id
public class CategoryServiceImpl extends BaseServiceImpl implements CategoryService {
	public CategoryServiceImpl(){
		super();
	}}



你可能感兴趣的:(JAVA_WEB项目)