使用所有的框架都一样,导jar包和配置相应的文件,以下就是hibernate手动配置的代码。(熟练之后可以利用MyEclipse的自带工具直接进行框架的搭建)
一、导入hibernate的基本包
二、文件的配置(hibernate.cfg.xml),配置最基本的连接数据库信息和相应的方言
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <!-- Generated by MyEclipse Hibernate Tools. -->
- <hibernate-configuration>
- <session-factory>
- <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
- <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:ORCL</property>
- <property name="connection.username">sa</property>
- <property name="connection.password">sa</property>
- <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
- <property name="myeclipse.connection.profile">ora</property>
- <mapping resource="com/bean/Teacher.hbm.xml"/>
- <mapping resource="com/bean/ClassInfo.hbm.xml"/>
- <mapping resource="com/bean/Student.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:ORCL</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="myeclipse.connection.profile">ora</property>
<mapping resource="com/bean/Teacher.hbm.xml"/>
<mapping resource="com/bean/ClassInfo.hbm.xml"/>
<mapping resource="com/bean/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
三、项目所有包文件截图
四、配置对应bean 的xml映射文件(com.bean报下)
1.教师bean类
- package com.bean;
- import java.math.BigDecimal;
- public class Teacher {
- private BigDecimal tid;
- private String tname;
- private BigDecimal tage;
- public BigDecimal getTid() {
- return tid;
- }
- public void setTid(BigDecimal tid) {
- this.tid = tid;
- }
- public String getTname() {
- return tname;
- }
- public void setTname(String tname) {
- this.tname = tname;
- }
- public BigDecimal getTage() {
- return tage;
- }
- public void setTage(BigDecimal tage) {
- this.tage = tage;
- }
- public Teacher(BigDecimal tid, String tname, BigDecimal tage) {
- super();
- this.tid = tid;
- this.tname = tname;
- this.tage = tage;
- }
- public Teacher() {
- // TODO Auto-generated constructor stub
- }
- }
package com.bean;
import java.math.BigDecimal;
public class Teacher {
private BigDecimal tid;
private String tname;
private BigDecimal tage;
public BigDecimal getTid() {
return tid;
}
public void setTid(BigDecimal tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public BigDecimal getTage() {
return tage;
}
public void setTage(BigDecimal tage) {
this.tage = tage;
}
public Teacher(BigDecimal tid, String tname, BigDecimal tage) {
super();
this.tid = tid;
this.tname = tname;
this.tage = tage;
}
public Teacher() {
// TODO Auto-generated constructor stub
}
}
教师bean类对应的xml映射文件配置
(class中的name是bean类,table是数据库对应的表名,不要写错。id为数据表的主键,其余均为该表属性,教师表死最简单的一个表,不牵扯任何外键管理,下一张班级表就会牵扯外键关系。)
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.bean.Teacher" table="teacher" schema="sa">
- <id name="tid" column="tid" type="java.math.BigDecimal">
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.bean.Teacher" table="teacher" schema="sa">
<id name="tid" column="tid" type="java.math.BigDecimal">
- <generator class="assigned"></generator>
<generator class="assigned"></generator>
- </id>
- <property name="tname" column="tname" type="java.lang.String"></property>
- <property name="tage" column="tage" type="java.math.BigDecimal"></property>
- </class>
- </hibernate-mapping>
</id>
<property name="tname" column="tname" type="java.lang.String"></property>
<property name="tage" column="tage" type="java.math.BigDecimal"></property>
</class>
</hibernate-mapping>
=============================================================================================================================
2.班级bean类
- package com.bean;
- public class ClassInfo {
- public String clid;
- public String cltitle;
- public Teacher teacher;
- public String getClid() {
- return clid;
- }
- public void setClid(String clid) {
- this.clid = clid;
- }
- public String getCltitle() {
- return cltitle;
- }
- public void setCltitle(String cltitle) {
- this.cltitle = cltitle;
- }
- public Teacher getTeacher() {
- return teacher;
- }
- public void setTeacher(Teacher teacher) {
- this.teacher = teacher;
- }
- public ClassInfo(String clid, String cltitle, Teacher teacher) {
- this.clid = clid;
- this.cltitle = cltitle;
- this.teacher = teacher;
- }
- public ClassInfo() {
- }
- }
package com.bean;
public class ClassInfo {
public String clid;
public String cltitle;
public Teacher teacher;
public String getClid() {
return clid;
}
public void setClid(String clid) {
this.clid = clid;
}
public String getCltitle() {
return cltitle;
}
public void setCltitle(String cltitle) {
this.cltitle = cltitle;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public ClassInfo(String clid, String cltitle, Teacher teacher) {
this.clid = clid;
this.cltitle = cltitle;
this.teacher = teacher;
}
public ClassInfo() {
}
}
班级bena类对应的xml映射文件的配置
(因为每个班级有一个带班老师,因此牵扯到了外键。xml配置中外键用<many-to-one name="teacher" column="tid" class="com.bean.Teacher"></many-to-one>,name为班级bean类的属性名称,coloumn为对应数据库的字段名称)
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.bean.ClassInfo" table="classinfo" schema="sa">
- <id name="clid" column="clid" type="java.lang.String">
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.bean.ClassInfo" table="classinfo" schema="sa">
<id name="clid" column="clid" type="java.lang.String">
- <generator class="assigned"></generator>
<generator class="assigned"></generator>
- </id>
- <property name="cltitle" column="cltitle" type="java.lang.String"></property>
- <many-to-one name="teacher" column="tid" class="com.bean.Teacher"></many-to-one>
- </class>
- </hibernate-mapping>
</id>
<property name="cltitle" column="cltitle" type="java.lang.String"></property>
<many-to-one name="teacher" column="tid" class="com.bean.Teacher"></many-to-one>
</class>
</hibernate-mapping>
=================================================================================================================================
3.角色权限中间表的配置(因为实际需求中,很可能会有中间表存在,而中间表该如何配置呢?我先给大家发完bean类再发xml配置)
权限bean类(Pop)
- package com.bean;
- import java.math.BigDecimal;
- public class Pop {
- private BigDecimal pid;
- private String ptitle;
- public BigDecimal getPid() {
- return pid;
- }
- public void setPid(BigDecimal pid) {
- this.pid = pid;
- }
- public String getPtitle() {
- return ptitle;
- }
- public void setPtitle(String ptitle) {
- this.ptitle = ptitle;
- }
- public Pop(BigDecimal pid, String ptitle) {
- super();
- this.pid = pid;
- this.ptitle = ptitle;
- }
- public Pop() {
- // TODO Auto-generated constructor stub
- }
- }
package com.bean;
import java.math.BigDecimal;
public class Pop {
private BigDecimal pid;
private String ptitle;
public BigDecimal getPid() {
return pid;
}
public void setPid(BigDecimal pid) {
this.pid = pid;
}
public String getPtitle() {
return ptitle;
}
public void setPtitle(String ptitle) {
this.ptitle = ptitle;
}
public Pop(BigDecimal pid, String ptitle) {
super();
this.pid = pid;
this.ptitle = ptitle;
}
public Pop() {
// TODO Auto-generated constructor stub
}
}
角色bean类 (UserType)
- package com.bean;
- import java.math.BigDecimal;
- public class UserType {
- private BigDecimal utid;
- private String utname;
- public BigDecimal getUtid() {
- return utid;
- }
- public void setUtid(BigDecimal utid) {
- this.utid = utid;
- }
- public String getUtname() {
- return utname;
- }
- public void setUtname(String utname) {
- this.utname = utname;
- }
- public UserType(BigDecimal utid, String utname) {
- super();
- this.utid = utid;
- this.utname = utname;
- }
- public UserType() {
- // TODO Auto-generated constructor stub
- }
- }
package com.bean;
import java.math.BigDecimal;
public class UserType {
private BigDecimal utid;
private String utname;
public BigDecimal getUtid() {
return utid;
}
public void setUtid(BigDecimal utid) {
this.utid = utid;
}
public String getUtname() {
return utname;
}
public void setUtname(String utname) {
this.utname = utname;
}
public UserType(BigDecimal utid, String utname) {
super();
this.utid = utid;
this.utname = utname;
}
public UserType() {
// TODO Auto-generated constructor stub
}
}
中间表UtAndPopID类(类中属性为Pop和UserType)
- package com.bean;
- import java.io.Serializable;
- public class UtAndPopID implements Serializable {
- private UserType ut;
- private Pop p;
- public UserType getUt() {
- return ut;
- }
- public void setUt(UserType ut) {
- this.ut = ut;
- }
- public Pop getP() {
- return p;
- }
- public void setP(Pop p) {
- this.p = p;
- }
- public UtAndPopID(UserType ut, Pop p) {
- super();
- this.ut = ut;
- this.p = p;
- }
- public UtAndPopID() {
- }
- }
package com.bean;
import java.io.Serializable;
public class UtAndPopID implements Serializable {
private UserType ut;
private Pop p;
public UserType getUt() {
return ut;
}
public void setUt(UserType ut) {
this.ut = ut;
}
public Pop getP() {
return p;
}
public void setP(Pop p) {
this.p = p;
}
public UtAndPopID(UserType ut, Pop p) {
super();
this.ut = ut;
this.p = p;
}
public UtAndPopID() {
}
}
中间表UtAndPop(属性为UtAndPopID)
- package com.bean;
- public class UtAndPop {
- UtAndPopID utandpopid;
- public UtAndPopID getUtandpopid() {
- return utandpopid;
- }
- public void setUtandpopid(UtAndPopID utandpopid) {
- this.utandpopid = utandpopid;
- }
- public UtAndPop(UtAndPopID utandpopid) {
- this.utandpopid = utandpopid;
- }
- public UtAndPop() {
- // TODO Auto-generated constructor stub
- }
- }
package com.bean;
public class UtAndPop {
UtAndPopID utandpopid;
public UtAndPopID getUtandpopid() {
return utandpopid;
}
public void setUtandpopid(UtAndPopID utandpopid) {
this.utandpopid = utandpopid;
}
public UtAndPop(UtAndPopID utandpopid) {
this.utandpopid = utandpopid;
}
public UtAndPop() {
// TODO Auto-generated constructor stub
}
}
Pop和UserType的xml映射文件配置和teacher的差不多我就不发了 ,这里只发下中间表的配置
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!--
- Mapping file autogenerated by MyEclipse Persistence Tools
- -->
- <hibernate-mapping>
- <class name="com.bean.UtAndPop" table="ut_and_pop" schema="sa">
- <composite-id name="utandpopid" class="com.bean.UtAndPopID">
- <key-many-to-one name="ut" column="utid" class="com.bean.UserType"></key-many-to-one>
- <key-many-to-one name="p" column="pid" class="com.bean.Pop"></key-many-to-one>
- </composite-id>
- </class>
- </hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.bean.UtAndPop" table="ut_and_pop" schema="sa">
<composite-id name="utandpopid" class="com.bean.UtAndPopID">
<key-many-to-one name="ut" column="utid" class="com.bean.UserType"></key-many-to-one>
<key-many-to-one name="p" column="pid" class="com.bean.Pop"></key-many-to-one>
</composite-id>
</class>
</hibernate-mapping>
*************当你配置完成之后 还需要在hibernate.cfg.xml中进行相关的配置这些映射文件才会好用(在<mapping />中加入映射文件的路径)
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-configuration PUBLIC
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <!-- Generated by MyEclipse Hibernate Tools. -->
- <hibernate-configuration>
- <session-factory>
- <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
- <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:ORCL</property>
- <property name="connection.username">sa</property>
- <property name="connection.password">sa</property>
- <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
- <property name="myeclipse.connection.profile">ora</property>
- <mapping resource="com/bean/Teacher.hbm.xml"/>
- <mapping resource="com/bean/ClassInfo.hbm.xml"/>
- <mapping resource="com/bean/UtAndPop.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:ORCL</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="myeclipse.connection.profile">ora</property>
<mapping resource="com/bean/Teacher.hbm.xml"/>
<mapping resource="com/bean/ClassInfo.hbm.xml"/>
<mapping resource="com/bean/UtAndPop.hbm.xml"/>
</session-factory>
</hibernate-configuration>
五、获得session操作数据库(com.sessionmanage包下)
MySessionFactory.java
- package com.sessionmanage;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.classic.Session;
- public class MySessionFactory {
- public static SessionFactory sft=new Configuration().configure().buildSessionFactory();
- public static Session getSession(){
- //这里的session和之前的连接池差不多
- Session session=sft.openSession();
- return (session!=null)?session:null;
- }
- }
package com.sessionmanage;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
public class MySessionFactory {
public static SessionFactory sft=new Configuration().configure().buildSessionFactory();
public static Session getSession(){
//这里的session和之前的连接池差不多
Session session=sft.openSession();
return (session!=null)?session:null;
}
}
六、编写工具CriteriaPoJo,为了实现系统DAO中封装criteria方式的查询方法做准备(hibernate中有hql和criteria两种查询方式)
CriteriaPoJo.java
- package com.tool;
- public class CriteriaPoJo {
- //这里只是写了几个方法,当然以后我们可以将其所有的都写出来,虽然这个类有点麻烦,但是以后子类调用时会简单很多
- public static final Integer IDEQ=1;
- public static final Integer LIKE=2;
- public static final Integer BETWEEN=3;
- public static final Integer ISNULL=4;
- private Integer wtd;
- private String property;
- private Object[] values;
- public Integer getWtd() {
- return wtd;
- }
- public void setWtd(Integer wtd) {
- this.wtd = wtd;
- }
- public String getProperty() {
- return property;
- }
- public void setProperty(String property) {
- this.property = property;
- }
- public Object[] getValues() {
- return values;
- }
- public void setValues(Object[] values) {
- this.values = values;
- }
- public CriteriaPoJo(Integer wtd, String property, Object[] values) {
- this.wtd = wtd;
- this.property = property;
- this.values = values;
- }
- public CriteriaPoJo() {
- }
- }
package com.tool;
public class CriteriaPoJo {
//这里只是写了几个方法,当然以后我们可以将其所有的都写出来,虽然这个类有点麻烦,但是以后子类调用时会简单很多
public static final Integer IDEQ=1;
public static final Integer LIKE=2;
public static final Integer BETWEEN=3;
public static final Integer ISNULL=4;
private Integer wtd;
private String property;
private Object[] values;
public Integer getWtd() {
return wtd;
}
public void setWtd(Integer wtd) {
this.wtd = wtd;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public Object[] getValues() {
return values;
}
public void setValues(Object[] values) {
this.values = values;
}
public CriteriaPoJo(Integer wtd, String property, Object[] values) {
this.wtd = wtd;
this.property = property;
this.values = values;
}
public CriteriaPoJo() {
}
}
七、编写DAO类(com.dao包下)
SysDAO.java(抽象父类方法,供子类DAO调用)
- package com.dao;
- import java.util.List;
- import org.hibernate.Criteria;
- import org.hibernate.Query;
- import org.hibernate.Transaction;
- import org.hibernate.classic.Session;
- import org.hibernate.criterion.Criterion;
- import org.hibernate.criterion.Restrictions;
- import com.sessionmanage.MySessionFactory;
- import com.tool.CriteriaPoJo;
- public abstract class SysDAO {
- public static final int ADD=1;
- public static final int DEL=2;
- public static final int UPD=3;
- public abstract boolean insert(Object ob);
- public abstract boolean delete(Object ob);
- public abstract boolean update(Object ob);
- public abstract List findAll();
- public abstract Object findById(Object ob);
- //封装Criterion,供Criteria查询自身调用
- private Criterion getCriterion(CriteriaPoJo cp){
- if(cp.getWtd()==cp.IDEQ){
- return Restrictions.idEq(cp.getValues()[0]);
- }
- if(cp.getWtd()==cp.LIKE){
- return Restrictions.like(cp.getProperty(), "%"+cp.getValues()[0]+"%");
- }
- if(cp.getWtd()==cp.BETWEEN){
- if(cp.getValues().length<2)
- return null;
- return Restrictions.between(cp.getProperty(), cp.getValues()[0], cp.getValues()[1]);
- }
- return null;
- }
- //封装父类Criteria查询方法
- public List executeCriteria(Class cl,CriteriaPoJo[] cps){
- Session session=MySessionFactory.getSession();
- if(session==null){
- return null;
- }
- Criteria crt=null;
- try {
- crt=session.createCriteria(cl);
- if(cps!=null&&cps.length>0){
- for (int i = 0; i < cps.length; i++) {
- crt.add(getCriterion(cps[i]));
- }
- }
- return crt.list();
- } catch (Exception e) {
- // TODO: handle exception
- }finally{
- session.close();
- }
- return null;
- }
- //封装父类模糊查询方法
- public List getInfoLikePro(String cl,String[] col,String[] colvalue){
- String hql="from "+cl+" where 0=0";
- for (int i = 0; i < col.length; i++) {
- hql+=" and "+col[i]+"like ?";
- }
- return this.executeQuery(hql, colvalue);
- }
- //封装父类根据属性多条件查询方法
- public List getInfoByPro(String cl,String[] col,String[] colvalue){
- String hql="from "+cl+" where 0=0";
- for (int i = 0; i < col.length; i++) {
- hql+=" and "+col[i]+"=?";
- }
- return this.executeQuery(hql, colvalue);
- }
- //封装父类分页查询方法
- public List getInfoByPageSize(String hql,int rowcount,int pagesize){
- Session session=MySessionFactory.getSession();
- if(session==null){
- return null;
- }
- try {
- Query q=session.createQuery(hql);
- int size=rowcount*(pagesize-1);
- q.setFirstResult(size);
- q.setMaxResults(rowcount);
- return q.list();
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- session.close();
- }
- return null;
- }
- //封装父类查询方法
- public List executeQuery(String hql,String[] args){
- Session session=MySessionFactory.getSession();
- if(session==null){
- return null;
- }
- try {
- Query q=session.createQuery(hql);
- if(args!=null&&args.length>0){
- for (int i = 0; i < args.length; i++) {
- q.setString(i,args[i]);
- }
- }
- return q.list();
- } catch (Exception e) {
- // TODO: handle exception
- }finally{
- session.close();
- }
- return null;
- }
- //封装父类增删改方法
- public boolean executeUpdate(Object ob,Integer wtd){
- return this.executeUpdateBatch(new Object[]{ob}, new Integer[]{wtd});
- }
- //封装父类批处理事务,增删改方法
- public boolean executeUpdateBatch(Object[] obs,Integer[] wtd){
- Session session=MySessionFactory.getSession();
- if(session==null){
- return false;
- }
- Transaction tran=session.beginTransaction();
- try {
- for (int i = 0; i < obs.length; i++) {
- switch (wtd[i]) {
- case ADD:
- session.save(obs[i]);
- break;
- case DEL:
- session.delete(obs[i]);
- break;
- case UPD:
- session.update(obs[i]);
- break;
- default:
- return false;
- }
- if(i%20==0){
- session.flush();
- session.clear();
- }
- }
- tran.commit();
- return true;
- } catch (Exception e) {
- tran.rollback();
- }finally{
- session.close();
- }
- return false;
- }
- }
package com.dao;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import com.sessionmanage.MySessionFactory;
import com.tool.CriteriaPoJo;
public abstract class SysDAO {
public static final int ADD=1;
public static final int DEL=2;
public static final int UPD=3;
public abstract boolean insert(Object ob);
public abstract boolean delete(Object ob);
public abstract boolean update(Object ob);
public abstract List findAll();
public abstract Object findById(Object ob);
//封装Criterion,供Criteria查询自身调用
private Criterion getCriterion(CriteriaPoJo cp){
if(cp.getWtd()==cp.IDEQ){
return Restrictions.idEq(cp.getValues()[0]);
}
if(cp.getWtd()==cp.LIKE){
return Restrictions.like(cp.getProperty(), "%"+cp.getValues()[0]+"%");
}
if(cp.getWtd()==cp.BETWEEN){
if(cp.getValues().length<2)
return null;
return Restrictions.between(cp.getProperty(), cp.getValues()[0], cp.getValues()[1]);
}
return null;
}
//封装父类Criteria查询方法
public List executeCriteria(Class cl,CriteriaPoJo[] cps){
Session session=MySessionFactory.getSession();
if(session==null){
return null;
}
Criteria crt=null;
try {
crt=session.createCriteria(cl);
if(cps!=null&&cps.length>0){
for (int i = 0; i < cps.length; i++) {
crt.add(getCriterion(cps[i]));
}
}
return crt.list();
} catch (Exception e) {
// TODO: handle exception
}finally{
session.close();
}
return null;
}
//封装父类模糊查询方法
public List getInfoLikePro(String cl,String[] col,String[] colvalue){
String hql="from "+cl+" where 0=0";
for (int i = 0; i < col.length; i++) {
hql+=" and "+col[i]+"like ?";
}
return this.executeQuery(hql, colvalue);
}
//封装父类根据属性多条件查询方法
public List getInfoByPro(String cl,String[] col,String[] colvalue){
String hql="from "+cl+" where 0=0";
for (int i = 0; i < col.length; i++) {
hql+=" and "+col[i]+"=?";
}
return this.executeQuery(hql, colvalue);
}
//封装父类分页查询方法
public List getInfoByPageSize(String hql,int rowcount,int pagesize){
Session session=MySessionFactory.getSession();
if(session==null){
return null;
}
try {
Query q=session.createQuery(hql);
int size=rowcount*(pagesize-1);
q.setFirstResult(size);
q.setMaxResults(rowcount);
return q.list();
} catch (Exception e) {
e.printStackTrace();
}finally{
session.close();
}
return null;
}
//封装父类查询方法
public List executeQuery(String hql,String[] args){
Session session=MySessionFactory.getSession();
if(session==null){
return null;
}
try {
Query q=session.createQuery(hql);
if(args!=null&&args.length>0){
for (int i = 0; i < args.length; i++) {
q.setString(i,args[i]);
}
}
return q.list();
} catch (Exception e) {
// TODO: handle exception
}finally{
session.close();
}
return null;
}
//封装父类增删改方法
public boolean executeUpdate(Object ob,Integer wtd){
return this.executeUpdateBatch(new Object[]{ob}, new Integer[]{wtd});
}
//封装父类批处理事务,增删改方法
public boolean executeUpdateBatch(Object[] obs,Integer[] wtd){
Session session=MySessionFactory.getSession();
if(session==null){
return false;
}
Transaction tran=session.beginTransaction();
try {
for (int i = 0; i < obs.length; i++) {
switch (wtd[i]) {
case ADD:
session.save(obs[i]);
break;
case DEL:
session.delete(obs[i]);
break;
case UPD:
session.update(obs[i]);
break;
default:
return false;
}
if(i%20==0){
session.flush();
session.clear();
}
}
tran.commit();
return true;
} catch (Exception e) {
tran.rollback();
}finally{
session.close();
}
return false;
}
}
其他各类的DAO类继承SysDAO类进行使用,这里我就只给出TeacherDAO.java的代码
TeacherDAO.java
- package com.dao;
- import java.util.List;
- public class TeacherDAO extends SysDAO {
- @Override
- public boolean delete(Object ob) {
- return executeUpdate(ob, SysDAO.DEL);
- }
- @Override
- public boolean insert(Object ob) {
- return executeUpdate(ob, SysDAO.ADD);
- }
- @Override
- public boolean update(Object ob) {
- return executeUpdate(ob, SysDAO.UPD);
- }
- //定义批量删除操作的方法
- public boolean deleteInfos(Object[] obs){
- Integer[] wtd=new Integer[obs.length];
- for (int i = 0; i < wtd.length; i++) {
- wtd[i]=SysDAO.DEL;
- }
- return executeUpdateBatch(obs, wtd);
- }
- @Override
- public List findAll() {
- return this.executeQuery("from Teacher", null);
- }
- @Override
- public Object findById(Object ob) {
- return this.executeQuery("from Teacher where tid=?",new String[]{ob+""} );
- }
- }