HelloWorld
HelloWorld
----------------------------------------------------
1. 新建数据库:hibernate5
2. 新建Java工程: hibernate-1
3. 项目下新建目录lib并导入jar包
antlr-2.7.7.jar
c3p0-0.9.2.1.jar
dom4j-1.6.1.jar
hibernate-c3p0-4.2.4.Final.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.4.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
mchange-commons-java-0.2.3.4.jar
mysql-connector-java-5.1.7-bin.jar
4. 创建实体类: com.atguigu.hibernate.helloworld.News.java
private Integer id;
private String title;
private String authod;
private Date date;
+ getter/setter(), 带参构造方法, 无参构造方法, toString()
5. 创建实体类对应的对象-关系映射文件News.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>
<class name="com.atguigu.hibernate.helloworld.News" table="NEWS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 使用数据库主键本地方式, 比如: mysql是自增, oracle是序列 -->
<generator class="native" />
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="authod" type="java.lang.String">
<column name="AUTHOD" />
</property>
<property name="date" type="java.sql.Date">
<column name="DATE" />
</property>
</class>
</hibernate-mapping>
6. 创建hibernate.cfg.xml配置文件[默认找src下的hibernate.cfg.xml, 如果不是]
作用: 连接数据库, 创建数据表, 管理对象-关系映射文件News.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 连接数据库 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate5</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<!-- 数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 是否打印sql -->
<property name="hibernate.show_sql">true</property>
<!-- sql是否格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- 生成数据表的策略 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 导入*.hbm.xml对象关系映射文件 -->
<mapping resource="com/atguigu/hibernate/helloworld/News.hbm.xml"/>
</session-factory>
</hibernate-configuration>
7. 创建单元测试: com.atguigu.hibernate.helloworld.HibernateTest.java
package com.atguigu.hibernate.helloworld;
import java.sql.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;
public class HibernateTest {
@Test
public void test() {
// 1. 创建SessionFactory
SessionFactory sessionFactory = null;
Configuration configuration = new Configuration().configure();
// 4.0之前这样创建
//sessionFactory = configuration.buildSessionFactory();
// 4.x之后这样创建[需要注册]
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
// 2. 创建Session
Session session = sessionFactory.openSession();
// 3. 开启事务
Transaction transaction = session.beginTransaction();
// 4. 执行事务[保存对象]
News news = new News("Java", "Peter", new Date(new java.util.Date().getTime()));
session.save(news);
// 5. 提交事务
transaction.commit();
// 6. 关闭Session
session.close();
// 7. 关闭SessionFactory
sessionFactory.close();
}
}
8. 测试: 通过! 数据库中生成对应的数据表
-----------------------------------------------------------------
--------------------------------------------------------------------------------
范例1: 获取数据表的一条记录[反射机制]
News news = (News) session.get(News.class, 1);
System.out.println(news);
后台打印:
Hibernate:
select
news0_.ID as ID1_0_0_,
news0_.TITLE as TITLE2_0_0_,
news0_.AUTHOD as AUTHOD3_0_0_,
news0_.DATE as DATE4_0_0_
from
NEWS news0_
where
news0_.ID=?
News [id=1, title=Java, authod=Peter, date=2017-03-14]