1.hibernate.cfg.xmljava
1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 <hibernate-configuration> 6 <session-factory> 7 <property name="dialect"> 8 org.hibernate.dialect.MySQLDialect</property> <!-- 數據庫方言 --> 9 <property name="connection.url"> 10 jdbc:mysql://localhost:3306/db_examsystem</property><!-- 數據庫鏈接URL --> 11 <property name="connection.username">root</property> <!-- 數據庫用戶名 --> 12 <property name="connection.password">123456</property> <!-- 數據庫用戶密碼 --> 13 <property name="connection.driver_class"> <!-- 數據庫驅動類 --> 14 com.mysql.jdbc.Driver</property> 15 <mapping resource="com/sanqing/po/Student.hbm.xml"/> 16 <mapping resource="com/sanqing/po/Teacher.hbm.xml"/> 17 <mapping resource="com/sanqing/po/Subject.hbm.xml"/> 18 </session-factory> 19 </hibernate-configuration>
2.hibernateSessionFactory類mysql
1 package com.sanqing.hibernate; 2 3 import org.hibernate.HibernateException; 4 import org.hibernate.Session; 5 import org.hibernate.cfg.Configuration; 6 import org.junit.Test; 7 8 public class HibernateSessionFactory { 9 private static String CONFIG_FILE_LOCATION 10 = "/hibernate.cfg.xml"; //指定配置文件路徑 11 private static final ThreadLocal<Session> threadLocal 12 = new ThreadLocal<Session>(); //定義ThreadLocal對象 13 private static Configuration configuration 14 = new Configuration(); //定義Configuration對象 15 private static org.hibernate.SessionFactory sessionFactory;//定義SessionFactory對象 16 private static String configFile = CONFIG_FILE_LOCATION; 17 static { 18 try { 19 configuration.configure(configFile);//讀取配置文件 20 sessionFactory = 21 configuration.buildSessionFactory();//根據配置文件建立SessionFactory對象 22 } catch (Exception e) { 23 System.err 24 .println("%%%% Error Creating SessionFactory %%%%"); 25 e.printStackTrace(); 26 } 27 } 28 private HibernateSessionFactory() { 29 } 30 public static Session getSession() throws HibernateException { 31 Session session = (Session) threadLocal.get();//從ThreadLocal對象中得到Session對象 32 if (session == null || !session.isOpen()) {//判斷得到的Session對象是否爲空或者未打開 33 if (sessionFactory == null) {//若是沒有建立SessionFactory對象,則首先建立 34 // rebuildSessionFactory(); 35 } 36 //若是SessionFactory對象不爲空,則調用其openSession方法建立Session對象 37 session = (sessionFactory != null) ? sessionFactory.openSession(): null; 38 threadLocal.set(session);//在ThreadLocal對象中保存該Session對象 39 } 40 return session; 41 } 42 // public static void rebuildSessionFactory() { 43 // try { 44 // configuration.configure(configFile);//讀取配置文件 45 // sessionFactory = 46 // configuration.buildSessionFactory();//根據配置文件建立sessionFactory對象 47 // } catch (Exception e) { 48 // System.err 49 // .println("%%%% Error Creating SessionFactory %%%%"); 50 // e.printStackTrace(); 51 // } 52 // } 53 public static void closeSession() throws HibernateException { 54 Session session = (Session) threadLocal.get();//從ThreadLocal對象中得到Session對象 55 threadLocal.set(null);//將當前線程Session對象從ThreadLocal對象中移除 56 if (session != null) { 57 session.close(); 58 } 59 } 60 public static org.hibernate.SessionFactory getSessionFactory() {//取得SessionFactory對象 61 return sessionFactory; 62 } 63 public static void setConfigFile(String configFile) {//設置新的配置文件 64 HibernateSessionFactory.configFile = configFile; 65 sessionFactory = null; 66 } 67 public static Configuration getConfiguration() {//得到Configuration對象 68 return configuration; 69 } 70 }
3.studentsql
1 package com.sanqing.po; 2 /* 3 * 學生表,保存學生編號,系統密碼 4 */ 5 public class Student { 6 private String studentID; 7 private String password; 8 private String studentName; 9 private Integer result; 10 private String sclass; 11 public String getStudentID() { 12 return studentID; 13 } 14 public void setStudentID(String studentID) { 15 this.studentID = studentID; 16 } 17 public String getPassword() { 18 return password; 19 } 20 public void setPassword(String password) { 21 this.password = password; 22 } 23 public String getStudentName() { 24 return studentName; 25 } 26 public void setStudentName(String studentName) { 27 this.studentName = studentName; 28 } 29 public Integer getResult() { 30 return result; 31 } 32 public void setResult(Integer result) { 33 this.result = result; 34 } 35 public String getSclass() { 36 return sclass; 37 } 38 public void setSclass(String sclass) { 39 this.sclass = sclass; 40 } 41 }
4.student.hbm.xml數據庫
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping 3 PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 5 <hibernate-mapping> 6 <class name="com.sanqing.po.Student" table="tb_student"><!-- 每一個class對應一個持久化對象 --> 7 <id name="studentID" type="string"><!-- id元素用來定義主鍵標識,並指定主鍵生成策略 --> 8 <generator class="assigned"></generator> 9 </id> 10 <property name="password" type="string"></property><!-- 映射password屬性 --> 11 <property name="studentName" type="string"></property><!-- 映射studentName屬性 --> 12 <property name="result" type="int"></property><!-- 映射result屬性 --> 13 <property name="sclass" type="string"></property><!-- 映射sclass屬性 --> 14 </class> 15 </hibernate-mapping>
5.studentDaosession
1 package com.sanqing.dao; 2 3 import java.util.List; 4 5 import com.sanqing.po.Student; 6 7 public interface StudentDAO { 8 public Student findByStudentID(String studentID);//查詢方法,根據學生ID查詢 9 public void updateStudent(Student student);//更新學生信息 10 public List<Student> findByStudentName(String studentName);//根據學生姓名查找學生 11 public List<Student> findByStudentClass(String sclass);//根據班級查找學生 12 }
6.studentDaoImplapp
1 package com.sanqing.dao; 2 3 import java.util.Iterator; 4 import java.util.List; 5 6 import org.hibernate.Query; 7 import org.hibernate.Session; 8 import org.hibernate.Transaction; 9 10 import com.sanqing.hibernate.HibernateSessionFactory; 11 import com.sanqing.po.Student; 12 import com.sanqing.po.Subject; 13 14 public class StudentDAOImpl implements StudentDAO{ 15 public Student findByStudentID(String studentID) { 16 Session session = HibernateSessionFactory.getSession();//得到Session對象 17 Student student = (Student) session.get(Student.class, studentID); 18 HibernateSessionFactory.closeSession();//關閉Session對象 19 return student; 20 } 21 22 public void updateStudent(Student student) { 23 Session session = HibernateSessionFactory.getSession();//得到Session對象 24 Transaction transaction = null;//聲明一個事務對象 25 try{ 26 transaction = session.beginTransaction();//開啓事務 27 session.update(student);//更新學生信息 28 transaction.commit();//提交事務 29 }catch(Exception ex) { 30 ex.printStackTrace(); 31 transaction.rollback();//事務回滾 32 } 33 HibernateSessionFactory.closeSession();//關閉Session對象 34 } 35 36 public List<Student> findByStudentName(String studentName) { 37 Session session = HibernateSessionFactory.getSession();//得到Session對象 38 Query query = session.createQuery("from Student as stu where stu.studentName = ?"); 39 query.setString(0, studentName); 40 List list = query.list(); //查詢結果保存到list中 41 HibernateSessionFactory.closeSession(); //關閉Session對象 42 return list; 43 } 44 45 public List<Student> findByStudentClass(String sclass) { 46 Session session = HibernateSessionFactory.getSession();//得到Session對象 47 Query query = session.createQuery("from Student as stu where stu.sclass = ?"); 48 query.setString(0, sclass); 49 List list = query.list(); //查詢結果保存到list中 50 HibernateSessionFactory.closeSession(); //關閉Session對象 51 return list; 52 } 53 }
1 package com.sanqing.dao; 2 3 import java.util.Iterator; 4 import java.util.List; 5 6 import org.hibernate.Query; 7 import org.hibernate.Session; 8 import org.hibernate.Transaction; 9 10 import com.sanqing.hibernate.HibernateSessionFactory; 11 import com.sanqing.po.Student; 12 import com.sanqing.po.Subject; 13 14 public class StudentDAOImpl implements StudentDAO{ 15 public Student findByStudentID(String studentID) { 16 Session session = HibernateSessionFactory.getSession();//得到Session對象 17 Student student = (Student) session.get(Student.class, studentID); 18 HibernateSessionFactory.closeSession();//關閉Session對象 19 return student; 20 } 21 22 public void updateStudent(Student student) { 23 Session session = HibernateSessionFactory.getSession();//得到Session對象 24 Transaction transaction = null;//聲明一個事務對象 25 try{ 26 transaction = session.beginTransaction();//開啓事務 27 session.update(student);//更新學生信息 28 transaction.commit();//提交事務 29 }catch(Exception ex) { 30 ex.printStackTrace(); 31 transaction.rollback();//事務回滾 32 } 33 HibernateSessionFactory.closeSession();//關閉Session對象 34 } 35 36 public List<Student> findByStudentName(String studentName) { 37 Session session = HibernateSessionFactory.getSession();//得到Session對象 38 Query query = session.createQuery("from Student as stu where stu.studentName = ?"); 39 query.setString(0, studentName); 40 List list = query.list(); //查詢結果保存到list中 41 HibernateSessionFactory.closeSession(); //關閉Session對象 42 return list; 43 } 44 45 public List<Student> findByStudentClass(String sclass) { 46 Session session = HibernateSessionFactory.getSession();//得到Session對象 47 Query query = session.createQuery("from Student as stu where stu.sclass = ?"); 48 query.setString(0, sclass); 49 List list = query.list(); //查詢結果保存到list中 50 HibernateSessionFactory.closeSession(); //關閉Session對象 51 return list; 52 } 53 }