Home > Database > Mysql Tutorial > 第21天dao模型二(更为精简)之泛型的反射

第21天dao模型二(更为精简)之泛型的反射

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:29:09
Original
1105 people have browsed it

1、使用带有泛型信息的类: a、两边都带有泛型的,一定要一致 ArrayListString list = new ArrayListString(); b、单边有泛型信息也是可以的 ArrayListString list = new ArrayList (); 新程序员调用老程序员的内容 ArrayList list = new ArrayListString();

1、使用带有泛型信息的类:
a、两边都带有泛型的,一定要一致
ArrayList list = new ArrayList();
b、单边有泛型信息也是可以的
ArrayList list = new ArrayList (); 新程序员调用老程序员的内容
ArrayList list = new ArrayList(); 老程序员调用新程序员的内容
2、自定义泛型:
a、可以在定义类的后面就声明泛型,类中的实例方法就可以直接使用。
public class Demo1 {//类级别的泛型定义。类中的“实例方法”就可以直接使用

//:泛型定义的声明。在返回值的前面
public T findOne(Class clazz){
return null;
}
b、静态方法必须单独定义后才能使用。在返回值得前面定义
public static void u1(T t){

}
c、可以同时生命多个泛型的定义
public static V findValue(K k){//MAP
return null;
}

3、使用带有泛型信息的类注意事项:
只有对象类型才能作为泛型方法的实际参数

dao接口不变。

主要是实现变了,不需要写那么多代码,在basedao中做了处理即对泛型进行了反射

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.hibernate.Session;

import com.itheima.dao.CustomerDao;
import com.itheima.dao.Dao;
import com.itheima.domain.Customer;
//借助Hibernate
public class BaseDao<T> implements Dao<T> {
	private Session session = null;//当做有了这个对象
	
	private Class clazz;//从哪个表中操作
	public BaseDao(){
		//泛型的反射:目的,给成员变量clazz赋值,好让查询、删除知道从哪个表中操作
		System.out.println(this.getClass().getName());
		//this:就是哪个实实在在的对象  CustomerDao cDao = new CustomerDaoImpl();  CustoemrDaoImpl的实例
		Type type = this.getClass().getGenericSuperclass();//获取带有泛型信息的父类   "BaseDao<Customer>"     Type是Class类的接口
		ParameterizedType pType = (ParameterizedType)type; 
		clazz = (Class) pType.getActualTypeArguments()[0];
	}
	
	public void add(T t) {
		session.save(t);
	}

	public void update(T t) {
		session.update(t);
	}

	public void delete(Serializable id) {
		T t  =findOne(id);
		session.delete(t);
	}

	public T findOne(Serializable id) {
		System.out.println(clazz.getName());
		return (T) session.get(clazz, id);
	}

}
Copy after login
package com.jxnu.dao.impl;

import java.util.List;

import com.itheima.dao.CustomerDao;
import com.itheima.domain.Customer;

public class CustomerDaoImpl extends BaseDao<Customer> implements CustomerDao {
	
//	public CustomerDaoImpl(){
//		super();
//	}
	public List<Customer> findRecords(int startIndex, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

}
Copy after login
package com.jxnu.dao.impl;

import java.util.List;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;

public class UserDaoImpl extends BaseDao<User> implements UserDao {
	
	@Override
	public List<User> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

}
Copy after login

实现层减少了代码。

关键代码是:

private Class clazz;//从哪个表中操作
public BaseDao(){
//泛型的反射:目的,给成员变量clazz赋值,好让查询、删除知道从哪个表中操作
System.out.println(this.getClass().getName());
//this:就是哪个实实在在的对象 CustomerDao cDao = new CustomerDaoImpl(); CustoemrDaoImpl的实例
Type type = this.getClass().getGenericSuperclass();//获取带有泛型信息的父类 "BaseDao" Type是Class类的接口
ParameterizedType pType = (ParameterizedType)type;
clazz = (Class) pType.getActualTypeArguments()[0];
}
 

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template