
参考文档:
http://docs.jboss.org/hibernate/orm/4.2/quickstart/en-US/html_single/#d5e57
JPA与Hibernate、ibatis等的关系有点类似于JDBC和mysqlDriver,oracleDriver的关系
JPA定义了一套标准的接口规范,Hibernate等框架去按照这套规范实现,Hibernate的作者也参与了JPA规范的定义。
mysql中创建表student
hibernate.cfg.xml
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate?serverTimezone=UTC</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) 一般用jndi-->
<!--<property name="connection.pool_size">1</property>-->
<!-- SQL dialect 方言-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management 暂时注释掉 3.2以后-->
<!--<property name="current_session_context_class">thread</property>-->
<!-- Disable the second-level cache 禁用二级缓存-->
<property name="cache.provider_class">org.hibernate.cache.NoCacheRegionFactoryAvailableException</property>
<!-- Echo all executed SQL to stdout 要不要把生成的sql打印出来-->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup hbm:hibernatemapping ddl建表语句 要不要用hibernate自动生成建表语句 暂时注释-->
<!--<property name="hbm2ddl.auto">update</property>-->
<mapping resource="model/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
student.java
package model;
/**
* Created by dongyu.liu on 2017-5-22.
*/
public class Student {
private int id;
private String name;
private int age;
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;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Student.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="model">
<!--table 属性不配就是默认-->
<class name="Student" >
<!-- id指属性 name 代表类中的属性 column可省略代表name和数据库名字一致-->
<id name="id" >
<generator class="increment"/>
</id>
<property name="name" />
<property name="age"/>
</class>
</hibernate-mapping>
测试
package model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Created by dongyu.liu on 2017-5-22.
*/
public class TestStudent {
public static void main(String[] args) {
Student s = new Student();
s.setId();
s.setName("s1");
s.setAge();
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session sessions = sf.openSession();
sessions.beginTransaction();
sessions.save(s);
sessions.getTransaction().commit();
sf.close();
}
}
遇到的问题
1 .xml文件无法编译到class文件夹中,在pom.xml中增加配置
<build>
<finalName>hibernatetest</finalName>
<!-- xml文件正常编译到class目录中-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
- The server time zone value ‘XXXXXX’ is unrecognized or represents more than one time zone.这个问题是时区不同步造成的,可以在hibernate.cfg.xml文件中在url后面添加参数:serverTimezone=UTC