Hibernate配置步骤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate配置步骤相关的知识,希望对你有一定的参考价值。
1、创建WEB项目
2、从http://www.hibernate.org/下载hibernate-release-4.3.11.Final.zip,并解压。
3、将hibernate必须的包加入lib
4、打开hibernate-release-4.3.11.Final\\lib\\required文件夹,导入jar文件:
5、打开hibernate-release-4.3.11.Final\\lib\\optional\\ehcache文件夹,导入jar文件:
6、打开hibernate-release-4.3.11.Final\\lib\\optional\\c3p0文件夹,导入jar文件:
7、配置hibernate.cfg.xml。打开hibernate-release-4.3.11.Final\\project\\etc文件夹,选择hibernate.cfg.xml文件并复制到src下。
8、打开hibernate.cfg.xml文件,并设置数据库连接
1 1 <!DOCTYPE hibernate-configuration PUBLIC 2 2 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 3 3 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 4 4 5 5 <hibernate-configuration> 6 6 7 7 <session-factory> 8 8 <!-- 定义方言,即告诉框架你用的是什么数据库 --> 9 9 <property name="dialect"> 10 10 org.hibernate.dialect.mysqlDialect 11 11 </property> 12 12 13 13 <!-- 定义连接路径 --> 14 14 <property name="connection.url"> 15 15 jdbc:mysql://localhost:3306/hibernate?characterEncoding=UTF-8 16 16 </property> 17 17 <!-- 定义连接名与密码 --> 18 18 <property name="connection.username">root</property> 19 19 <property name="connection.password"></property> 20 20 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 21 21 22 22 <!-- 指定使用c3p0连接池 --> 23 23 <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property> 24 24 <!-- 指定连接池最大连接数 --> 25 25 <property name="hibernate.c3p0.max_size">20</property> 26 26 <!-- 指定连接池最小连接数 --> 27 27 <property name="hibernate.c3p0.min_size">1</property> 28 28 <!-- 指定连接池里连接超时时长 --> 29 29 <property name="hibernate.c3p0.timeout">5000</property> 30 30 31 31 <!-- 指定连接池里最大缓存多少个PreparedStatement对象 --> 32 32 <property name="hibernate.c3p0.max_statements">100</property> 33 33 <property name="hibernate.c3p0.idle_test_period">3000</property> 34 34 <property name="hibernate.c3p0.acquire_increment">2</property> 35 35 36 36 <!--以格式良好的方式显示SQL语句--> 37 37 <property name="format_sql">true</property> 38 38 <!--显示SQL语句--> 39 39 <property name="show_sql">true</property> 40 40 41 41 <!-- 添加hbm文件配置 --> 42 42 <mapping resource="com/lovo/xmls/UserInfoT.hbm.xml" /> 43 43 <mapping class="com.lovo.beans.UserInfoT"/> 44 44 </session-factory> 45 45 46 46 </hibernate-configuration>
9、创建数据库表,并封装实体Bean与XXX.hbm.xml文件,例如:UserInfoT.java:
1 public class UserInfoT implements java.io.Serializable { 2 3 // Fields 4 5 /** 6 * 7 */ 8 private static final long serialVersionUID = -8360690722407472061L; 9 private Long id; 10 private String userName; 11 private String password; 12 private Integer age; 13 14 // Constructors 15 16 /** default constructor */ 17 public UserInfoT() { 18 } 19 20 /** full constructor */ 21 public UserInfoT(String userName, String password, Integer age) { 22 this.userName = userName; 23 this.password = password; 24 this.age = age; 25 } 26 27 // Property accessors 28 29 public Long getId() { 30 return this.id; 31 } 32 33 public void setId(Long id) { 34 this.id = id; 35 } 36 37 public String getUserName() { 38 return this.userName; 39 } 40 41 public void setUserName(String userName) { 42 this.userName = userName; 43 } 44 45 public String getPassword() { 46 return this.password; 47 } 48 49 public void setPassword(String password) { 50 this.password = password; 51 } 52 53 public Integer getAge() { 54 return this.age; 55 } 56 57 public void setAge(Integer age) { 58 this.age = age; 59 } 60 61 }
然后打开 hibernate-release-4.3.11.Final\\project\\hibernate-core\\src\\main\\resources\\org\\hibernate文件夹下的hibernate-mapping-3.0.dtd文件。,复制head部分。
1 <?xml version="1.0" encoding="utf-8"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- 5 Mapping file autogenerated by MyEclipse Persistence Tools 6 --> 7 <hibernate-mapping> 8 <class name="com.lovo.beans.UserInfoT" table="user_info_t" catalog="hibernate"> 9 <id name="id" type="java.lang.Long"> 10 <column name="id" /> 11 <!-- 指定ID的生成方式 --> 12 <generator class="identity" /> 13 </id> 14 <property name="userName" type="java.lang.String"> 15 <column name="user_name" length="32"> 16 <comment>用户名</comment> 17 </column> 18 </property> 19 <property name="password" type="java.lang.String"> 20 <column name="password" length="32"> 21 <comment>密码</comment> 22 </column> 23 </property> 24 <property name="age" type="java.lang.Integer"> 25 <column name="age"> 26 <comment>年龄</comment> 27 </column> 28 </property> 29 </class> 30 </hibernate-mapping>
OK,配置完成!
以上是关于Hibernate配置步骤的主要内容,如果未能解决你的问题,请参考以下文章