JAVA 泛型BaseDao BaseService

model 层

package com.maomao.model;

import java.io.Serializable;

public class User implements Serializable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;
	private String name;
	private String age;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	
}

package com.maomao.model;

import java.io.Serializable;

public class Student implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;
	private String name;
	private String age;
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
}

BaseDao

package com.maomao.dao;

import java.io.Serializable;
import java.util.List;

public interface BaseDao {
	
	void add(T o);
	
	void del(Class c, Serializable id);
	
	List findAll();
	
	public T getById(Serializable id);
	
	public void update(T o);

}

BaseDaoImpl

package com.maomao.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import com.maomao.dao.BaseDao;

public class BaseDaoImpl implements BaseDao{

	@Override
	public void add(T o) {
		System.out.println(o.getClass().getName()+":保存方法");
		
	}

	@Override
	public void del(Class c, Serializable id) {
		System.out.println(c.getClass().getName()+",id="+id+","+":删除方法");
		
	}

	@Override
	public List findAll() {
		Class clazz = (Class)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		System.out.println(clazz.getName()+":查询所有方法");
		return null;
	}

	@Override
	public T getById(Serializable id) {
		Class clazz = (Class)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		System.out.println(clazz.getName()+",id="+id+","+":根据主键ID查询方法");
		return null;
	}

	@Override
	public void update(T o) {
		System.out.println(o.getClass().getName()+":修改方法");
		
	}

}

BaseService

package com.maomao.service;

import java.io.Serializable;
import java.util.List;

public interface BaseService {
	
	void add(T o);
	
	void del(Serializable id);
	
	List findAll();
	
	public T getById(Serializable id);
	
	public void update(T o);

}

BaseServiceImpl


package com.maomao.service.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import com.maomao.dao.BaseDao;
import com.maomao.dao.impl.BaseDaoImpl;
import com.maomao.service.BaseService;

public class BaseServiceImpl implements BaseService {

	private BaseDao baseDao = new BaseDaoImpl();
	
	
	
	@Override
	public void add(T o) {
		baseDao.add(o);
	}

	@Override
	public void del(Serializable id) {
		Class clazz = (Class)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
		baseDao.del(clazz, id);
		
	}

	@Override
	public List findAll() {
		baseDao.findAll();
		return null;
	}

	@Override
	public T getById(Serializable id) {
		baseDao.getById(id);
		return null;
	}

	@Override
	public void update(T o) {
		baseDao.update(o);
		
	}

}

UserService

package com.maomao.service;

import com.maomao.model.User;

public interface UserService extends BaseService{

}

UserServiceImpl

package com.maomao.service.impl;

import com.maomao.model.User;
import com.maomao.service.UserService;

public class UserServiceImpl  extends BaseServiceImpl implements UserService {

}

StudentService

package com.maomao.service;

import com.maomao.model.Student;

public interface StudentService extends BaseService{

}

StudentServiceImpl

package com.maomao.service.impl;

import com.maomao.model.Student;
import com.maomao.service.StudentService;

public class StudentServiceImpl extends BaseServiceImpl implements
		StudentService {
}

TestDemo

package com.maomao.test;

import org.junit.Test;

import com.maomao.model.Student;
import com.maomao.model.User;
import com.maomao.service.StudentService;
import com.maomao.service.UserService;
import com.maomao.service.impl.StudentServiceImpl;
import com.maomao.service.impl.UserServiceImpl;

public class TestDemo {

	@Test
	public void test1(){
		UserService userService = new UserServiceImpl();
		
		User u = new User();
		userService.add(u);
		
		Student student = new Student();
		
		StudentService studentService = new StudentServiceImpl();
		studentService.add(student);
		
	}
}


你可能感兴趣的:(JAVA)