- 配置环境
MyEclipse/MySql5.7/Hibernate5.2.10/jdk_1.8 - jar包导入
antlr-2.7.7.jar
classmate-1.3.0.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-5.0.1.Final.jar
hibernate-core-5.2.10.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-2.0.3.Final.jar
javassist-3.20.0-GA.jar
jboss-logging-3.3.0.Final.jar
jboss-transaction-api_1.2_spec-1.0.1.Final.jar
这些jar包在hibernate-release-5.2.10.Final\hibernate-release-5.2.10.Final\lib\required
路径下的required目录下
当然这里还需要数据库驱动包
mysql-connector-java-5.1.43-bin.jar
- 在src目录下编写hibernate.cfg.xml
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!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.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="connection.url">jdbc:mysql:///hibernate</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">1</property>
<!-- 其他配置 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<!--映射文件 -->
<mapping resource="model/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这里介绍下其他配置
<property name="hibernate.show_sql">true</property>
是否在控制台打印sql语句
<property name="hibernate.format_sql">false</property>
是否格式话sql语句
<property name="hibernate.hbm2ddl.auto">create</property>
将映射转换为数据定义语言的方式
注意:<property name="hibernate.dialect"></property>
这里mysql5.7要使用org.hibernate.dialect.MySQL5InnoDBDialect
但是在hibernate。properties文件中提供的是这样的,要改下。
MySQL
hibernate.dialect org.hibernate.dialect.MySQLDialect
hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
hibernate.connection.driver_class com.mysql.jdbc.Driver
hibernate.connection.url jdbc:mysql:///test
hibernate.connection.username gavin
hibernate.connection.password
- 示例User类和映射文件
User.java
package model;
import javax.persistence.criteria.CriteriaBuilder.In;
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;
}
}
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="model">
<class name="User" table="t_user">
<id name="id" type="int" column="id" >
<generator class="native"/>
</id>
<property name="name" type="string" column="name" length="20"/>
</class>
</hibernate-mapping>
网友评论