现在的位置: 首页 > 综合 > 正文

hibernate配置相关

2018年04月20日 ⁄ 综合 ⁄ 共 3315字 ⁄ 字号 评论关闭
 

 写hibernate程序必需的:

 

hibernate.cfg.xml文件

  1. <!DOCTYPE hibernate-configuration PUBLIC
  2.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  3.     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  4. <hibernate-configuration>
  5.     <session-factory name="foo">
  6.         <property name="hibernate.connection.url">
  7.             jdbc:mysql://localhost/hibernate_fir
  8.         </property>
  9.         <property name="hibernate.connection.driver_class">
  10.             com.mysql.jdbc.Driver
  11.         </property>
  12.         <property name="hibernate.connection.username">root</property>
  13.         <property name="hibernate.connection.password">
  14.             chx/1988
  15.         </property>
  16.         <property name="hibernate.dialect">
  17.             org.hibernate.dialect.MySQLDialect
  18.         </property>
  19.         <property name="hibernate.show_sql">true</property>
  20.         <mapping resource="cn/xteam/obj/User.hbm.xml" />
  21.     </session-factory>
  22. </hibernate-configuration>

 

这个文件主要设置和数据库链接相关的东西,这里以MySQL为例。

  <mapping resource="cn/xteam/obj/User.hbm.xml" />这一行是映射实体类的

 

下面是导出数据库的一个类。

  1. package cn.xteam.obj;
  2. import org.hibernate.cfg.Configuration;
  3. import org.hibernate.tool.hbm2ddl.SchemaExport;
  4. public class ExportDB {
  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) {
  9.         Configuration cfg = new Configuration().configure();
  10.         SchemaExport export = new SchemaExport(cfg);
  11.         export.create(truetrue);
  12.         
  13.         
  14.     }
  15. }

要让它创建出表的话,还要有个实体类和一个与实体类相关的配置文件:

配置文件如下:

 

User.hbm.xml

 

  1. <?xml version="1.0"?>
  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>
  6.     
  7.     <class name="cn.xteam.obj.User">
  8.         <id name = "id">
  9.             <generator class="uuid"/>
  10.         </id>
  11.         <property name="name"/>
  12.         <property name="password" />
  13.         
  14.     </class>
  15. </hibernate-mapping>

OK了!再建个实体类,里面包含配置文件中相关属性即可:

比如这里可以用下面这个类做实体类:

  1. package cn.xteam.obj;
  2. public class User {
  3.     private String id;
  4.     private String name;
  5.     private String password;
  6.     
  7.     public String getId() {
  8.         return id;
  9.     }
  10.     public void setId(String id) {
  11.         this.id = id;
  12.     }
  13.     public String getName() {
  14.         return name;
  15.     }
  16.     public void setName(String name) {
  17.         this.name = name;
  18.     }
  19.     public String getPassword() {
  20.         return password;
  21.     }
  22.     public void setPassword(String password) {
  23.         this.password = password;
  24.     }
  25.     
  26. }

 

 

用下面这个类测试:

  1. package cn.xteam.obj;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.cfg.Configuration;
  5. public class Client {
  6.     /**
  7.      * @param args
  8.      */
  9.     public static void main(String[] args) {
  10.         
  11.         Configuration cfg = new Configuration().configure();
  12.         SessionFactory sf = cfg.buildSessionFactory();
  13.         Session session = null;
  14.         try{
  15.             session = sf.openSession();
  16.             session.beginTransaction();
  17.             User user = new User();
  18.             user.setName("neil");
  19.             user.setPassword("123456");
  20.             session.save(user);
  21.             session.getTransaction().commit();
  22.         }catch(Exception e){
  23.             session.getTransaction().rollback();
  24.         }finally {
  25.             if(session != null){
  26.                 if(session.isOpen()){
  27.                     session.close();
  28.                 }
  29.             }
  30.         }
  31.     }
  32. }

 

抱歉!评论已关闭.