假如说有两张表,一张是student表,一张是teacher表,当我们写登录模块时,而相对应的从数据库读取的表数据,student有与之相对应的StudenDaoimp,teacher有teacerDaoimp,对于student表有固定的查询语句:"select * from student where s_name=' "+name+" ‘ and s_password= ' "+password; //查询语句1对于teacher同理可得, select * from teacher where t_name='"+name+" ‘ and t_password= ' "+password,// 查询语句2,
那么有没有一个方法可以把查询语句1和查询语句2写成一个语句呢?答案告诉你,当然可以!用到的知识是反射,反射发射!重要的事强调3次 so , 请看下面student表对应的实体类: package com.ssh.entities;
public class Student {
@Override
public String toString() {
return "Student [s_id=" + s_id + ", s_name=" + s_name + ", s_password="
+ s_password + "]";
}
private Integer s_id;
private String s_name;
private String s_password;
public Integer getS_id() {
return s_id;
}
public void setS_id(Integer s_id) {
this.s_id = s_id;
}
public String getS_name() {
return s_name;
}
public void setS_name(String s_name) {
this.s_name = s_name;
}
public String getS_password() {
return s_password;
}
public void setS_password(String s_password) {
this.s_password = s_password;
}
}
teacher对应的实体类
package com.ssh.entities;
public class Teacher {
@Override
public String toString() {
return "Teacher [t_id=" + t_id + ", t_name=" + t_name + ", t_password="
+ t_password + "]";
}
private Integer t_id;
private String t_name;
private String t_password;
public Integer getT_id() {
return t_id;
}
public void setT_id(Integer t_id) {
this.t_id = t_id;
}
public String getT_name() {
return t_name;
}
public void setT_name(String t_name) {
this.t_name = t_name;
}
public String getT_password() {
return t_password;
}
public void setT_password(String t_password) {
this.t_password = t_password;
}
}
下面就来好好的讲解BasoDao和BaseDaoImp类:
package com.ssh.daoImp;
import java.lang.reflect.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang.StringEscapeUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.ssh.dao.BaseDao;
import com.ssh.entities.Student;
public class BaseDaoImp<T> implements BaseDao<T>{
private Class<T> classbean;//反射类
public BaseDaoImp(){
ParameterizedType ptype=(ParameterizedType)getClass().getGenericSuperclass();
Type[] type=ptype.getActualTypeArguments();
classbean =(Class<T>) type[0];//得到反射类的对象
}
@Resource(name="sessionFactory")
protected SessionFactory sessionFactory;//Hibernate的SessionFactory用于连接管理数据库操作
@Override
public T login(String name,String password) {//用于查询Sutdent,Teacher表的操作
// TODO Auto-generated method stub
StringBuffer hql=new StringBuffer();
hql.append("from "+classbean.getSimpleName()+" where 1=1");
String[] buffer=new String[2];
if(classbean.getSimpleName().endsWith("Student")){
buffer[0]="s_name";
buffer[1]="s_password";
}else if(classbean.getSimpleName().endsWith("Teacher")){
buffer[0]="t_name";
buffer[1]="t_password";
}
if((name!=null&&name!="")&&(password!=null&&password!="")){
hql.append(" and "+buffer[0]+"='"+name+"'"+"and "+buffer[1]+"='"+StringEscapeUtils.escapeSql(password.toString())+"'");
}
Session session =sessionFactory.openSession();
Query query = session.createQuery(hql.toString());
List<T> result = query.list();
if (result !=null && result.size()>0) {
return result.get(0);
}
return null;
}
}
对于Student这方面,自然是要有想对应的接口IStudentDao和接口实现类StudentDaoImp;
package com.ssh.dao;
import com.ssh.entities.Student;
public interface IStudentDao extends BaseDao<Student>{
}
StudentDaoImp:
package com.ssh.daoImp;
import com.ssh.dao.IStudentDao;
import com.ssh.entities.Student;
public class StudentDaoImp extends BaseDaoImp<Student> implements IStudentDao{
}
Teacher对应的ITeacherDao接口和接口实现类TeacherDaoImp;
ITeacherDao:
package com.ssh.dao;
import com.ssh.entities.Teacher;
public interface ITeacherDao extends BaseDao<Teacher> {//继承BaseDao
}
TeacherDaoImp:
package com.ssh.daoImp;
import com.ssh.dao.ITeacherDao;
import com.ssh.entities.Teacher;
public class TeacherDaoImp extends BaseDaoImp<Teacher> implements ITeacherDao {
}
总结:其实这个模板类BaseDao主要运用到的技术是反射,泛型,继承,接口。TeacherDaoImp和StudentDaoImp要继承BaseDao类,并且注明泛型的类型