Hibernate(一)文件配置、表结构的生成、默认与c3p0连接池的配置

来源:互联网 发布:养老金认证软件下载 编辑:程序博客网 时间:2024/05/17 21:56

隔了这么久,终于有时间来学学Hibernate了,话说配置还真繁杂,不过是后面开发的基础。

在开发过程中,经常看到分层现象,主要目的是为了解耦。

B/S最少分三层:

view    表示层
   action/sevlet/xx  数据
   jsp 模板
service 业务层
dao     数据访问层


下面是真正的Hibernate入门。


首先是导入一些必须的jar包;数据库采用MySQL,连接数据库当然要数据库驱动包。

工程目录:


调试建的工程是Java Project

实体类User.java

package test.hibernate.domain;public class User {private int id;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {// TODO Auto-generated method stubreturn "[User:id="+id+",name:"+name+"]";}}
配置文件hibernate.cfg.xml,其中文件名是hibernate.cfg

<!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 name="foo"><!-- 配置数据库信息 -->    <property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/hibernate_2015</property><!-- 可简写为<property name="connection.url">jdbc:mysql:///hibernate_2015</property> --><property name="connection.username">root</property><property name="connection.password">686175</property><!-- 显示生成的sql语句,不写的话默认是false --><property name="show_sql">true</property><mapping resource="test/hibernate/domain/User.hbm.xml"/></session-factory></hibernate-configuration>
对象与表的映射文件User.hbm.xml

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="test.hibernate.domain"><class name="User" table="t_user"><id name="id" type="integer" column="id">            <generator class="native"/></id><property name="name" type="string" column="name"/></class></hibernate-mapping>
id的class属性值取为native表示采用数据库中的配置属性

主要测试类App.java

package test.hibernate.domain;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.classic.Session;import org.junit.Test;public class App {private static SessionFactory sessionFactory;static{Configuration cfg=new Configuration();cfg.configure("hibernate.cfg.xml");sessionFactory=cfg.buildSessionFactory();}@Testpublic void testSave() throws Exception {User user=new User();user.setName("张三");Session session=sessionFactory.openSession();Transaction transaction = session.beginTransaction();session.save(user);transaction.commit();session.close();//关闭session,释放资源}@Testpublic void testGet() throws Exception {Session session=sessionFactory.openSession();Transaction transaction=session.beginTransaction();User user=(User) session.get(User.class, 1);System.out.println(user);transaction.commit();session.close();}}

Hibernate主配置文件
1,配置的key前面的hibernate.前缀 可以有,也可以没有。如hibernate.dialect或dialect都可以。
2,按作用可分为三类:
1,数据库信息
<property ...>
方言、JdbcUrl、驱动、用户名、密码
2,导入映射文件
<mapping ...>
3,其他配置
<property ...>
show_sql 显示生成的SQL语句
format_sql 格式化生成的SQL语句
hbm2ddl.auto        自动生成表结构
hibernate.hbm2ddl.auto
生成表结构的两种方式:
1,hbm2ddl.auto  
               create      没表的时候自动创建,先删除,在创建
               update      如果表不存在就创建,不一样就更新,一样就什么都不做
               create-drop 初始化时创建表,SessionFactory执行close()时删除表
               validate    验证表结构与HBM中的是否一致,如果不一致就抛异常
2,使用SchemaExport工具类
注意:只能建表,不能建库

package test.hibernate.domain;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;import org.junit.Test;public class CreateSchema {@Testpublic void test() throws Exception {Configuration cfg = new Configuration().configure();SchemaExport schemaExport = new SchemaExport(cfg);/* * 第一个参数:打印ddl到控制台  * 第二个参数:导出script到数据库 */schemaExport.create(true, true);}}


为了提高性能,在生产中会选择比较专业的c3p0连接池

<!-- c3p0连接池 --><!-- 使用c3p0连接池,配置连接池提供的供应商 --><property name="connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property><!-- 在连接池中可用的数据库连接的最小数目 --><property name="c3p0.min_size">5</property><!-- 在连接池中可用的数据库连接的最大数目 --><property name="c3p0.max_size">10</property><!-- 设定数据库连接的过期时间,以秒为单位,如果连接池中的某个数据库连接处于空闲状态的时间超过timeout时间,就会从连接池中清除 --><property name="c3p0.timeout">100</property><!-- 以秒为单位,每idle_test_period秒检查所有连接池中的空闲连接 --><property name="c3p0.idle_test_period">2000</property>


原文出处:http://blog.csdn.net/lindonglian/article/details/46850623

0 0