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的属性
- <?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>
源码中有对应的英文作用解释。
配置数据连接池
上面的配置的是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>
去除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>
关联文件: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>
- <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;
- }
- }
- <?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>
- 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;
- }
- }
- <mapping class="hibernate.senssic.bean.User"/>
反向生成类(eclipse)
http://blog.csdn.net/pioayang/article/details/17395525
0 0
- Hibernate 基本配置
- hibernate的基本配置
- Hibernate 的基本配置
- hibernate基本配置
- Spring+hibernate基本配置
- SpringMVC+Hibernate基本配置
- hibernate的基本配置
- Hibernate基本配置
- Hibernate基本配置
- hibernate 基本配置
- hibernate-1-基本配置
- hibernate基本配置
- Hibernate基本配置
- hibernate基本配置
- Hibernate基本配置
- hibernate 基本配置和使用
- hibernate.cfg.xml基本配置
- Servlet, Spring, hibernate基本配置
- Oracle undo 表空间管理
- java开发中如何获取远程文件的大小
- php CGI SAPI
- Java 多线程编程之一 进程与线程,并发和并行的区别
- rails paperclip
- hibernate基本配置
- 数字加字母 验证码
- Spring的bean、dao、service、action的生命周期
- Android usb camera设备添加
- FastCGI
- 酷站欣赏:分享13个五彩缤纷的国外网站作品
- java.lang.IllegalArgumentException: node to traverse cannot be null! 处理方法
- C# 异常"关于对象不能从DBNull转换为其他类型"
- linux下如何安装.bunld文件