1,新建实体类News.java
package com.hanqi.dao; import java.util.Date; public class News { private Integer id;
private String title;
private String contant;
private Date createdate; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContant() {
return contant;
}
public void setContant(String contant) {
this.contant = contant;
} /**
* @return the createdate
*/
public Date getCreatedate() {
return createdate;
}
/**
* @param createdate the createdate to set
*/
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
@Override
public String toString() {
return "News [id=" + id + ", title=" + title + ", contant=" + contant + ", createdate=" + createdate + "]";
}
}
2,利用hibernate生成实体类对应的配置文件,(右键-新建-其他-hbm.xml)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">
<!-- Generated 2016-1-12 23:36:52 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.hanqi.dao.News" table="NEWS">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="native" /><!-- 注意此处主键生成方法常用native -->
</id>
<property name="title" type="java.lang.String">
<column name="TITLE" />
</property>
<property name="contant" type="java.lang.String">
<column name="CONTANT" />
</property>
<property name="createdate" type="java.util.Date">
<column name="CREATEDATE" />
</property>
</class>
</hibernate-mapping>
3,将hbm.xml添加到hibernate配置文件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> <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<!-- 数据库连接 -->
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.password">test</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">test</property>
<!-- 用户方案 -->
<property name="hibernate.default_schema">TEST</property>
<!-- 数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- sql语句/调试 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- 自动建表方式 -->
<property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.validator.apply_to_ddl">false</property>
<!-- 映射文件 -->
<mapping resource="com/hanqi/dao/News.hbm.xml" /> </session-factory>
</hibernate-configuration>
4,新建JUnit Test Case类TestNews.java进行测试
package com.hanqi.dao; import static org.junit.Assert.*; import java.util.Date; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import org.hibernate.service.*; public class TestNews { @Test
public void test() {
System.out.println("测试");
//1-构建配置类
Configuration cfgrn = new Configuration().configure(); //2-构建配置工厂类
ServiceRegistry srg = new StandardServiceRegistryBuilder().
applySettings(cfgrn.getProperties()).build(); //3-构建会话工厂对象,比较耗资源
SessionFactory sf = cfgrn.buildSessionFactory(srg); //4-构建会话对象
Session se = sf.openSession(); //5-开始事务
Transaction tr = se.beginTransaction(); //6-执行操作
/* News news = new News(); news.setTitle("标题");
news.setContant("这是内容");
news.setCreatedate(new Date()); se.save(news);//插入
*/
News news2 = (News)se.get(News.class, 1); System.out.println(news2);//查询输出 //7-提交事务
tr.commit(); //8-关闭会话
se.close(); //9-关闭会话工厂
sf.close(); } }