JAVA_WEBプロジェクトのサービス層をBaseServiceとBaseServiceImplに抽出


Javaweb開発で3つのフレームワーク開発に使用される場合は、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();
	}}