hibernate 配置

1.配置 hibernate.cfg.xml   在src裏面

Java代碼
  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.         <!-- Database connection settings -->  
  8.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  9.         <property name="connection.url">jdbc:mysql://localhost:3306/test</property>  
  10.         <property name="connection.username">root</property>  
  11.         <property name="connection.password">mysql</property>  
  12.   
  13.         <!-- JDBC connection pool (use the built-in) -->  
  14.         <property name="connection.pool_size">2</property>  
  15.   
  16.         <!-- SQL dialect -->  
  17.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  18.         <!-- Echo all executed SQL to stdout -->  
  19.         <property name="show_sql">true</property>  
  20.   
  21.         <!-- Drop and re-create the database schema on startup -->  
  22.         <property name="hbm2ddl.auto">update</property>  
  23.         <mapping resource="ls/student/pojo/Student.hbm.xml" />  
  24.         <mapping resource="ls/teacher/pojo/Teacher.hbm.xml" />  
  25.     </session-factory>  
  26. </hibernate-configuration>  

 2.配置pojo裏面Teacher類的 Teacher.hbm.xml

Java代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC    
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >  
  5. <hibernate-mapping package="ls.teacher.pojo">  
  6.     <class name="ls.teacher.pojo.Teacher" table="Teacher">  
  7.         <id name="userid" column="userid" type="string" >  
  8.             <generator class="assigned"/>  
  9.         </id>  
  10.         <property name="name" column="name" type="string"></property>  
  11.         <property name="password" column="password" type="string"></property>  
  12.         <property name="age" column="age" type="string"></property>  
  13.     </class>  
  14. </hibernate-mapping>  

 

3.數據操作 增刪改查

Java代碼
  1. package ls.teacher.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import ls.teacher.pojo.Teacher;  
  6.   
  7. import org.hibernate.Query;  
  8. import org.hibernate.Session;  
  9. import org.hibernate.SessionFactory;  
  10. import org.hibernate.Transaction;  
  11. import org.hibernate.cfg.Configuration;  
  12.   
  13. public class TeacherDao {  
  14.     private static  Teacher teacher = null;  
  15.     /** 
  16.      * 添加老師 
  17.      * @param password 
  18.      * @return 
  19.      */  
  20.     public Teacher addTeacher(String userid,String name,String password,String age){  
  21.         Configuration cfg =  new  Configuration().addResource("hibernate.cfg.xml");//初始化configuration配置文件  
  22.         SessionFactory sf = cfg.configure().buildSessionFactory();//建一個SessionFactory  
  23.         Session session =  sf.openSession();//打開Session  
  24.         Transaction transaction = session.beginTransaction();//開始一個事物  
  25.         try {  
  26.             teacher = new Teacher();  
  27.             teacher.setUserid(userid);  
  28.             teacher.setName(name);  
  29.             teacher.setPassword(password);  
  30.             teacher.setAge(age);  
  31.             session.save(teacher);  
  32.             transaction.commit();  
  33.         } catch (Exception e) {  
  34.             // TODO: handle exception  
  35.             transaction.rollback();  
  36.         }  
  37.         return teacher;  
  38.     }  
  39.   
  40.     /** 
  41.      * 通過id查詢老師信息 
  42.      * @param userid 
  43.      * @return Teacher 
  44.      */  
  45.     public Teacher selectTeacher(String userid){  
  46.         Configuration cfg =  new  Configuration().addResource("hibernate.cfg.xml");//初始化configuration配置文件  
  47.         SessionFactory sf = cfg.configure().buildSessionFactory();//建一個SessionFactory  
  48.         Session session =  sf.openSession();//打開Session  
  49.         Transaction transaction = session.beginTransaction();//開始一個事物  
  50.         String hql = "from Teacher teacher where teacher.userid=?";  
  51.         List<Teacher> list = null;  
  52.             try {  
  53.                 list = session.createQuery(hql).setString(0, userid).list();  
  54.                 System.out.println("pp:"+list.get(0).getPassword());  
  55.                 transaction.commit();  
  56.                 teacher = list.get(0);  
  57.             } catch (Exception e) {  
  58.                 // TODO: handle exception  
  59.                 transaction.rollback();  
  60.             }finally{  
  61.                 session.close();  
  62.             }  
  63.         return teacher;  
  64.     }  
  65.   
  66.     /** 
  67.      * 更新老師密碼    
  68.      * @return 
  69.      */  
  70.     public boolean updateTeacher(String userid,String password){  
  71.         boolean flag = false;  
  72.         Configuration cfg = new Configuration().addResource("hibernate.cfg.xml");  
  73.         SessionFactory sf = cfg.configure().buildSessionFactory();  
  74.         Session session = sf.openSession();  
  75.         Transaction transaction = session.beginTransaction();  
  76.         String hql = "update Teacher teacher set teacher.password=? where teacher.userid=?";  
  77.         try {  
  78.             Query queryupdate = session.createQuery(hql).setString(0, password).setString(1, userid);  
  79.             queryupdate.executeUpdate();  
  80.             transaction.commit();  
  81.             flag = true;  
  82.         } catch (Exception e) {  
  83.             // TODO: handle exception  
  84.             transaction.rollback();  
  85.         }finally{  
  86.             session.close();  
  87.         }  
  88.         return flag;  
  89.     }  
  90.   
  91.     /** 
  92.      * 刪除老師信息 
  93.      * @return 
  94.      */  
  95.     public boolean deleteTeacher(String userid) {  
  96.         boolean flag = false;  
  97.         Configuration cfg =  new  Configuration().addResource("hibernate.cfg.xml");  
  98.         SessionFactory sf = cfg.configure().buildSessionFactory();  
  99.         Session session =  sf.openSession();  
  100.         Transaction transaction = session.beginTransaction();  
  101.         String hql = "delete from Teacher teacher where teacher=?";  
  102.         try {  
  103.             Query querydelete = session.createQuery(hql).setString(0, userid);  
  104.             querydelete.executeUpdate();  
  105.             transaction.commit();  
  106.             flag = true;  
  107.         } catch (Exception e) {  
  108.             // TODO: handle exception  
  109.             session.close();  
  110.         }  
  111.         return  flag;  
  112.     }  
  113.   

發佈了53 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章