配置第一个工程
环境:jdk1.6 eclipse hibernate4.0.1 mysql6.0
加入hibernate的lib目录下的required目录下的所有jar包,并添加mysql的连接驱动jar包
建立项目,在src(classes)下新建hibernate配置文件hibernate.cfg.xml(或是其他名字均可),配置hibernate的属性
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/senssic</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">qiyu0126</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.pool_size">5</property>
<!-- 设置外连接抓取树的最大深度 -->
<property name="hibernate.max_fetch_depth">3</property>
<!-- 一个非零值,会开启Hibernate使用JDBC2的批量更新功能 -->
<property name="hibernate.jdbc.batch_size">2</property>
<!-- 自动输出schema创建DDL语句 -->
<property name="hibernate.hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
通过查看源码可知,其使用的dom4j来解析xml配置文件的,并保存在properties中,使用map来存取,其整个配置位于hibernate核心包中的org.hibernate.cfg.AvailableSettings这个接口中保存。
源码中有对应的英文作用解释。
配置数据连接池
上面的配置的是hibernate自带的数据库连接池,除此之外还有其他的数据源和容器的连接池都可以配置如下:
dbcp:
加入如下配置
<property name="dbcp.maxActive">100</property>
<property name="dbcp.whenExhaustedAction">1</property>
<property name="dbcp.maxWait">60000</property>
<property name="dbcp.maxIdle">10</property>
<property name="dbcp.ps.maxActive">100</property>
<property name="dbcp.ps.whenExhaustedAction">1</property>
<property name="dbcp.ps.maxWait">60000</property>
<property name="dbcp.ps.maxIdle">10</property>
c3po:
加入如下配置:
<property name="c3p0.min_size">5</property>
<property name="c3p0.max_size">30</property>
<property name="c3p0.time_out">1800</property>
<property name="c3p0.max_statement">50</property>
proxoop:
去除hibernate的连接配置加入如下配置
<property name="proxool.pool_alias">dbpool</property>
<property name="proxool.xml">conf/ProxoolConf.xml</property>
<property name="connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</property>
特别注意:下面文件的路径要配置正确,否则FileNotFound
关联文件:conf/ProxoolConf.xml配置如下:
<?xml version="1.0" encoding="utf-8"?>
<something-else-entirely>
<proxool>
<alias>dbpool</alias>
<!--proxool只能管理由自己产生的连接-->
<driver-url>
jdbc:mysql://localhost:3306/senssic
</driver-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver-properties>
<property name="user" value="root" />
<property name="password" value="qiyu0126" />
</driver-properties>
<!-- proxool自动侦察各个连接状态的时间间隔(毫秒),侦察到空闲的连接就马上回收,超时的销毁-->
<house-keeping-sleep-time>90000</house-keeping-sleep-time>
<!-- 指因未有空闲连接可以分配而在队列中等候的最大请求数,超过这个请求数的用户连接就不会被接受-->
<maximum-new-connections>20</maximum-new-connections>
<!-- 最少保持的空闲连接数-->
<prototype-count>5</prototype-count>
<!-- 允许最大连接数,超过了这个连接,再有请求时,就排在队列中等候,最大的等待请求数由maximum-new-connections决定-->
<maximum-connection-count>100</maximum-connection-count>
<!-- 最小连接数-->
<minimum-connection-count>10</minimum-connection-count>
</proxool>
</something-else-entirely>
使用服务器本身的连接池(Tomcat、resin、weblogic等):
hibernate的配置:
<property name="hibernate.connection.datasource"> java:comp/env/jdbc/crm </property>
<property name="show_sql">true</property>
<property name="hibernate.generate_statistics">true</property>
其中java:comp/env/jdbc/crm的jdbc/crm是对应的服务器中数据库连接池名字,需要在对应的环境中配置 ,tomcat5\conf\server.xml
<Context path="/crm" docBase="E:\source\newuorss\web" >
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="crm." suffix=".txt" timestamp="true"/>
<Resource name="jdbc/crm" auth="container"
type="javax.sql.DataSource"
maxActive="10"
maxIdle="2"
maxWait="10000"
username="root"
password="qiyu0126"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/senssic" />
</Context>
hibernate自动生成表
使用xml配置生成
1.编写java实体类
package hibernate.senssic.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
public class User {
private String name;
private int age;
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.编写映射配置文件,位于对应类文件的目录下,User.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="hibernate.senssic.bean.User" table="user_t">
<id name="id">
<generator class="uuid" ></generator>
</id>
<property name="name" length="35" unique="true"></property>
<property name="age" column="userage" ></property>
</class>
</hibernate-mapping>
编写测试类--使用junit
package hibernate.test;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.BeforeClass;
import org.junit.Test;
public class Mytest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
Configuration cfg = new Configuration().configure("hibernate.xml");
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
@Test
public void test() {
}
}
在hibernate的配置xml中配置加入如下配置:
<mapping resource="hibernate/senssic/bean/User.hbm.xml"/>
1.编写有注解实例类
package hibernate.senssic.bean;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "sen")
public class User {
@Column(length = 35, name = "name")
private String name;
@Column(length = 35, name = "age", unique = true)
private int age;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.在hibernate配置文件中配置加入如下
<mapping class="hibernate.senssic.bean.User"/>