当前位置: 代码迷 >> Web前端 >> hibernate java project范例
  详细解决方案

hibernate java project范例

热度:663   发布时间:2012-09-21 15:47:26.0
hibernate java project实例
1 User.java

   1.  package px.malijun;  
   2.   
   3. public class User {  
   4.     private int id;  
   5.     private String name;  
   6.     private String password;  
   7.     private String email;  
   8.     public String getEmail() {  
   9.         return email;  
  10.     }  
  11.     public void setEmail(String email) {  
  12.         this.email = email;  
  13.     }  
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.     public void setName(String name) {  
  18.         this.name = name;  
  19.     }  
  20.     public String getPassword() {  
  21.         return password;  
  22.     }  
  23.     public void setPassword(String password) {  
  24.         this.password = password;  
  25.     }  
  26.     public int getId() {  
  27.         return id;  
  28.     }  
  29.     public void setId(int id) {  
  30.         this.id = id;  
  31.     }  
  32.  
  33. }  

建立相应的xml映射文件:

User.hbm.xml
   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.   
   6. <hibernate-mapping>  
   7.     <class name="px.malijun.User" table="users">  
   8.     <id name="id">  
   9.      <generator class="identity"/>  
  10.      </id>  
  11.         <property name="name"/>  
  12.         <property name="password"/>  
  13.         <property name="email"/>  
  14.     </class>  
  15. </hibernate-mapping>  


在数据库中建立一个myproject数据库,在其中建立一个users表
在src的根目录下(以上的user.hbm.xml映射文件与User.java在同一目录下)建立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">  
#   
# <hibernate-configuration>  
#   
#     <session-factory>  
#       
#         <!-- 显示实际操作数据库时的SQL -->  
#           
#         <property name="show_sql">true</property>  
#           
#         <!-- SQL 方言,这边设定的是MySQL -->  
#           
#         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
#           
#         <!-- JDBC 驱动程序 -->  
#           
#         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
#           
#         <!-- JDBC URL  注意这边我设定的所使用的编码 -->  
#           
#         <property name="connection.url">jdbc:mysql://localhost:3306/myproject?useUnicode=true&amp;characterEncoding=utf8</property>  
#           
#         <!-- 数据库使用者 -->  
#           
#         <property name="connection.username">root</property>  
#           
#         <!-- 数据库密码 -->  
#           
#         <property name="connection.password"></property>  
#           
#         <!-- 以下设置对象与数据库表格映像文件 -->  
#         <mapping resource="px/malijun/User.hbm.xml"/>  
#       
#     </session-factory>  
#   
# </hibernate-configuration>

由于常规操作都类似,我们建立了一个抽象类:

抽象类中的方法,不一定都是抽象的
但含有抽象方法的类一定是抽象类
可以 absctract 的方法不能实现
abstract class里可以有非abstract 方法 这样的方法可以实现

HibernateBase.java
   1.  package px.malijun;  
   2.   
   3. import org.hibernate.*;  
   4. import org.hibernate.cfg.*;  
   5. import java.util.*;  
   6. import java.io.IOException;  
   7. import java.io.PrintWriter;  
   8.        
   9.      public abstract class HibernateBase   
  10.      {  
  11.      protected SessionFactory sessionFactory;//会话工厂,用于创建会话  
  12.      protected Session session;//hibernate会话  
  13.      protected Transaction transaction; //hiberante事务  
  14.        
  15.      public HibernateBase()throws HibernateException  
  16.      {  
  17.      this.initHibernate();  
  18.      }  
  19.      // 帮助方法  
  20.      protected void initHibernate()  
  21.      throws HibernateException {  
  22.        
  23.      // 装载配置,构造 SessionFactory对象  
  24.      sessionFactory = new Configuration().configure().buildSessionFactory();  
  25.      }  
  26.        
  27.      /** 
  28.      *开始一个hibernate事务 
  29.      */  
  30.      protected void beginTransaction()  
  31.      throws HibernateException {  
  32.        
  33.      session = sessionFactory.openSession();  
  34.      transaction = session.beginTransaction();  
  35.      }  
  36.        
  37.      /** 
  38.      *结束一个hibernate事务。 
  39.      */  
  40.      protected void endTransaction(boolean commit)  
  41.      throws HibernateException {  
  42.        
  43.      if (commit) {  
  44.      transaction.commit();  
  45.      } else {  
  46.      //如果是只读的操作,不需要commit这个事务。  
  47.      transaction.rollback();  
  48.      }  
  49.      session.close();  
  50.      }  
  51. }  

然后建立一个具体针对User操作的一个类:
UserBean.java
   1.  package px.malijun;  
   2.   
   3. import org.hibernate.*;  
   4. import org.hibernate.cfg.*;  
   5. import java.util.*;  
   6.   
   7. /** 
   8.  * 和course相关的业务逻辑 
   9.  */  
  10. public class UserBean extends HibernateBase {  
  11.     public UserBean() throws HibernateException {  
  12.         super();  
  13.     }  
  14.   
  15.     /** 
  16.      * 增加一个Course 
  17.      */  
  18.     public void addUser(User user) throws HibernateException {  
  19.         beginTransaction();  
  20.         session.save(user);  
  21.         endTransaction(true);  
  22.     }  
  23.   
  24.     /** 
  25.      * 查询系统中所有的Course,返回的是包含有Course持久对象的Iterator。 
  26.      */  
  27.     public Iterator getAllUsers() throws HibernateException {  
  28.         String queryString = "select users from User as user";  
  29.         beginTransaction();  
  30.         Query query = session.createQuery(queryString);  
  31.         Iterator it = query.iterate();  
  32.         return it;  
  33.     }  
  34.   
  35.     /** 
  36.      * 删除给定ID的course 
  37.      */  
  38.     public void deleteUser(String id) throws HibernateException {  
  39.         beginTransaction();  
  40.         User user = (User) session.load(User.class, id);  
  41.         session.delete(user);  
  42.         endTransaction(true);  
  43.     }  
  44.   
  45.     /** 
  46.      * 按course的名字进行模糊查找,返回的是包含有Course持久对象的Iterator。 
  47.      */  
  48.     public Iterator getSomeUser(String name) throws HibernateException {  
  49.         String queryString = "select u from User as u where u.name like :name";  
  50.         beginTransaction();  
  51.         Query query = session.createQuery(queryString);  
  52.         query.setString("name", "%" + name + "%");  
  53.         Iterator it = query.iterate();  
  54.         return it;  
  55.     }  
  56. }  

测试:

Test.java
   1.  package px.malijun;  
   2.   
   3. public class Test {  
   4.   
   5.     /** 
   6.      * @param args 
   7.      */  
   8.     public static void main(String[] args) {  
   9.         // TODO Auto-generated method stub  
  10.         UserBean ub=new UserBean();  
  11.         User user = new User();  
  12.         user.setName("你好,世界!");  
  13.         user.setPassword("123456");  
  14.         user.setEmail("ok@ujs.edu.cn");  
  15.         ub.addUser(user);  
  16.   
  17.     }  
  18.   
  19. }  

  相关解决方案