hibernate基本配置

来源:互联网 发布:手机淘宝个人主页标签 编辑:程序博客网 时间:2024/05/23 02:01

http://blog.csdn.net/senssic/article/details/13168861

配置第一个工程

环境:jdk1.6  eclipse   hibernate4.0.1 mysql6.0
加入hibernate的lib目录下的required目录下的所有jar包,并添加mysql的连接驱动jar包
建立项目,在src(classes)下新建hibernate配置文件hibernate.cfg.xml(或是其他名字均可),配置hibernate的属性
[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">  
  5.     <hibernate-configuration>  
  6.       <session-factory>  
  7.         
  8.      <property name="hibernate.show_sql">true</property>  
  9.         <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
  10.         <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/senssic</property>  
  11.         <property name="hibernate.connection.username">root</property>  
  12.         <property name="hibernate.connection.password">qiyu0126</property>  
  13.         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
  14.         <property name="hibernate.connection.pool_size">5</property>  
  15.         <!-- 设置外连接抓取树的最大深度 -->  
  16.         <property name="hibernate.max_fetch_depth">3</property>  
  17.         <!-- 一个非零值,会开启Hibernate使用JDBC2的批量更新功能 -->  
  18.         <property name="hibernate.jdbc.batch_size">2</property>   
  19.         <!-- 自动输出schema创建DDL语句 -->  
  20.         <property name="hibernate.hbm2ddl.auto">update</property>  
  21.       </session-factory>  
  22.     </hibernate-configuration>  
通过查看源码可知,其使用的dom4j来解析xml配置文件的,并保存在properties中,使用map来存取,其整个配置位于hibernate核心包中的org.hibernate.cfg.AvailableSettings这个接口中保存。

源码中有对应的英文作用解释。

配置数据连接池

上面的配置的是hibernate自带的数据库连接池,除此之外还有其他的数据源和容器的连接池都可以配置如下:
dbcp:
加入如下配置
[html] view plaincopyprint?
  1. <property name="dbcp.maxActive">100</property>   
  2. <property name="dbcp.whenExhaustedAction">1</property>   
  3. <property name="dbcp.maxWait">60000</property>   
  4. <property name="dbcp.maxIdle">10</property>   
  5. <property name="dbcp.ps.maxActive">100</property>   
  6. <property name="dbcp.ps.whenExhaustedAction">1</property>   
  7. <property name="dbcp.ps.maxWait">60000</property>   
  8. <property name="dbcp.ps.maxIdle">10</property>   
c3po:
加入如下配置:
[html] view plaincopyprint?
  1. <property name="c3p0.min_size">5</property>   
  2. <property name="c3p0.max_size">30</property>   
  3. <property name="c3p0.time_out">1800</property>   
  4. <property name="c3p0.max_statement">50</property>   
proxoop:
去除hibernate的连接配置加入如下配置
[html] view plaincopyprint?
  1. <property name="proxool.pool_alias">dbpool</property>   
  2. <property name="proxool.xml">conf/ProxoolConf.xml</property>   
  3. <property name="connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>  
特别注意:下面文件的路径要配置正确,否则FileNotFound 
关联文件:conf/ProxoolConf.xml配置如下: 
[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>      
  2. <something-else-entirely>   
  3. <proxool>   
  4. <alias>dbpool</alias>   
  5. <!--proxool只能管理由自己产生的连接-->   
  6. <driver-url>   
  7. jdbc:mysql://localhost:3306/senssic  
  8. </driver-url>   
  9. <driver-class>com.mysql.jdbc.Driver</driver-class>   
  10. <driver-properties>   
  11. <property name="user" value="root" />   
  12. <property name="password" value="qiyu0126" />   
  13. </driver-properties>   
  14. <!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁-->   
  15. <house-keeping-sleep-time>90000</house-keeping-sleep-time>   
  16. <!-- 指因未有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受-->   
  17. <maximum-new-connections>20</maximum-new-connections>   
  18. <!-- 最少保持的空闲连接数-->   
  19. <prototype-count>5</prototype-count>   
  20. <!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->   
  21. <maximum-connection-count>100</maximum-connection-count>   
  22. <!-- 最小连接数-->   
  23. <minimum-connection-count>10</minimum-connection-count>   
  24. </proxool>   
  25. </something-else-entirely>   

使用服务器本身的连接池(Tomcat、resin、weblogic等):
hibernate的配置:
[java] view plaincopyprint?
  1. <property name="hibernate.connection.datasource"> java:comp/env/jdbc/crm </property>   
  2. <property name="show_sql">true</property>   
  3. <property name="hibernate.generate_statistics">true</property>   
其中java:comp/env/jdbc/crm的jdbc/crm是对应的服务器中数据库连接池名字,需要在对应的环境中配置 ,tomcat5\conf\server.xml 
[java] view plaincopyprint?
  1. <Context path="/crm" docBase="E:\source\newuorss\web" >   
  2.    <Logger className="org.apache.catalina.logger.FileLogger"   
  3.    prefix="crm." suffix=".txt"  timestamp="true"/>   
  4.    <Resource name="jdbc/crm" auth="container"   
  5.                type="javax.sql.DataSource"   
  6.                maxActive="10"   
  7.                maxIdle="2"   
  8.                maxWait="10000"   
  9.                username="root"   
  10.                password="qiyu0126"   
  11.                driverClassName="com.mysql.jdbc.Driver"   
  12.                url="jdbc:mysql://localhost:3306/senssic" />   
  13.    </Context>   

hibernate自动生成表

使用xml配置生成
1.编写java实体类
[java] view plaincopyprint?
  1. package hibernate.senssic.bean;  
  2.   
  3. import javax.persistence.Column;  
  4. import javax.persistence.Entity;  
  5. import javax.persistence.GeneratedValue;  
  6. import javax.persistence.GenerationType;  
  7. import javax.persistence.Id;  
  8. import javax.persistence.Table;  
  9. public class User {  
  10.     private String name;  
  11.     private int age;  
  12.     private String id;  
  13.   
  14.     public String getId() {  
  15.         return id;  
  16.     }  
  17.   
  18.     public void setId(String id) {  
  19.         this.id = id;  
  20.     }  
  21.   
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.   
  26.     public void setName(String name) {  
  27.         this.name = name;  
  28.     }  
  29.   
  30.     public int getAge() {  
  31.         return age;  
  32.     }  
  33.   
  34.     public void setAge(int age) {  
  35.         this.age = age;  
  36.     }  
  37.   
  38. }  
2.编写映射配置文件,位于对应类文件的目录下,User.hbm.xml
[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">  
  5.     <hibernate-mapping>  
  6.       <class name="hibernate.senssic.bean.User" table="user_t">  
  7.          <id name="id">  
  8.            <generator class="uuid" ></generator>  
  9.           </id>  
  10.           <property name="name" length="35" unique="true"></property>  
  11.           <property name="age" column="userage" ></property>  
  12.       </class>  
  13.     </hibernate-mapping>  
编写测试类--使用junit
[java] view plaincopyprint?
  1. package hibernate.test;  
  2.   
  3. import org.hibernate.cfg.Configuration;  
  4. import org.hibernate.tool.hbm2ddl.SchemaExport;  
  5. import org.junit.BeforeClass;  
  6. import org.junit.Test;  
  7.   
  8. public class Mytest {  
  9.   
  10.     @BeforeClass  
  11.     public static void setUpBeforeClass() throws Exception {  
  12.         Configuration cfg = new Configuration().configure("hibernate.xml");  
  13.         SchemaExport export = new SchemaExport(cfg);  
  14.         export.create(truetrue);  
  15.     }  
  16.   
  17.     @Test  
  18.     public void test() {  
  19.   
  20.     }  
  21.   
  22. }  
在hibernate的配置xml中配置加入如下配置:
[java] view plaincopyprint?
  1. <mapping  resource="hibernate/senssic/bean/User.hbm.xml"/>  
使用hibernate的注解生成表:
1.编写有注解实例类
[java] view plaincopyprint?
  1. package hibernate.senssic.bean;  
  2.   
  3. import javax.persistence.Column;  
  4. import javax.persistence.Entity;  
  5. import javax.persistence.GeneratedValue;  
  6. import javax.persistence.GenerationType;  
  7. import javax.persistence.Id;  
  8. import javax.persistence.Table;  
  9.   
  10. @Entity  
  11. @Table(name = "sen")  
  12. public class User {  
  13.     @Column(length = 35, name = "name")  
  14.     private String name;  
  15.     @Column(length = 35, name = "age", unique = true)  
  16.     private int age;  
  17.     @Id  
  18.     @GeneratedValue(strategy = GenerationType.IDENTITY)  
  19.     private String id;  
  20.   
  21.     public String getId() {  
  22.         return id;  
  23.     }  
  24.   
  25.     public void setId(String id) {  
  26.         this.id = id;  
  27.     }  
  28.   
  29.     public String getName() {  
  30.         return name;  
  31.     }  
  32.   
  33.     public void setName(String name) {  
  34.         this.name = name;  
  35.     }  
  36.   
  37.     public int getAge() {  
  38.         return age;  
  39.     }  
  40.   
  41.     public void setAge(int age) {  
  42.         this.age = age;  
  43.     }  
  44.   
  45. }  
2.在hibernate配置文件中配置加入如下
[html] view plaincopyprint?
  1. <mapping class="hibernate.senssic.bean.User"/>  

反向生成类(eclipse)

http://blog.csdn.net/pioayang/article/details/17395525

0 0