Hibernate搭建步骤:
1、下载所需包:antlr-2.7.7.jar,dom4j-1.6.1.jar,hibernate-commons-annotations-4.0.2.Final.jar,hibernate-core-4.2.21.Final.jar,hibernate-jpa-2.0-api-1.0.1.Final.jar,javassist-3.18.1-GA.jar,jboss-logging-3.1.0.GA.jar,jboss-transaction-api_1.1_spec-1.0.1.Final.jar,junit4_4.3.1.jar,mysql-connector-java-5.1.21.jar
2、新建一个Java Project 工程,新建一个lib文件,将jar包粘贴进lib文件,全选jar包,鼠标右键——Build Path——Add to Build Path
3、在src文件下新建Hibernate配置文件:hibernate.cfg.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="connection.username">root</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql:///test</property>
<!-- 配置hibernate的基本信息 -->
<!-- hibernate所使用的数据库方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLInnoDBDialect
</property>
<!-- 执行操作时是否在控制台打印SQL -->
<property name="show_sql">true</property>
<!-- 是否对SQL进行格式化 -->
<property name="format_sql">true</property>
<!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">create</property>
<!-- 设置Hibernate的食物隔离级别1,2,4,8 -->
<property name="connection.isolation">2</property>
<!-- 删除对象后,使其OID置为null -->
<!-- <property name="user_identifier_rollback">true</property> -->
<!-- 配置C3P0数据源 -->
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.timeout">2000</property>
<property name="hibernate.c3p0.idle_test_period">2000</property>
<property name="hibernate.c3p0.max_statements">10</property>
<!-- 设定jdbc的statement读取数据的时候每次从数据库中取出的数据条数 -->
<property name="hibernate.jdbc.fetch_size">100</property>
<!-- 设定对数据库进行批量删除,批量更新和批量插入的时候批次大小 -->
<property name="jdbc.batch.size">30</property>
<!-- 指定关联的.hbm.xml -->
<mapping resource="xiaolong/hibernate/news.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4、在src文件下建立Package如:xiaolong.hibernate,再在package下新建一个class如:NEWS.java,内容如下:
package xiaolong.hibernate;
import java.sql.Date;
public class NEWS {
private Integer id;
private String title;
private String author;
private Date date;
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 getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public NEWS(String title, String author, Date date) {
super();
this.title = title;
this.author = author;
this.date = date;
}
public NEWS(){
}
@Override
public String toString() {
return "NEWS [id=" + id + ", title=" + title + ", author="
+ author + ", date=" + date + "]";
}
}
5、在xiaolong.hibernate的package下新建news的p配置文件: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="xiaolong.hibernate.NEWS" table="NEWS" dynamic-update="true">
<id name="id" type="java.lang.Integer">
<column name="ID"></column>
<!-- 指定主键的生成方式,native使用数据库本地方式 -->
<generator class="native"></generator>
</id>
<property name="title" type="java.lang.String">
<column name="TITLE"></column>
</property>
<property name="author" type="java.lang.String">
<column name="AUTHOR"></column>
</property>
<property name="date" type="java.sql.Date">
<column name="DATE"></column>
</property>
</class>
</hibernate-mapping>
6、在MySQL里新建数据库NEWS,运行sql语句如下:
CREATE TABLE `NEWS` (
`ID` int(11) NOT NULL AUTO_INCREMENT ,
`TITLE` varchar(255) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL ,
`AUTHOR` varchar(255) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL ,
`DATE` date NULL DEFAULT NULL ,
PRIMARY KEY (`ID`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=gbk COLLATE=gbk_chinese_ci
AUTO_INCREMENT=3
ROW_FORMAT=COMPACT
;
7、在xiaolong.hibernate下建立class也就是测试类hibernatetest.java,内容如下:
package xiaolong.hibernate;
import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class Hibernatetest {
//
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
System.out.println("init");
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(
configuration.getProperties()).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
@After
public void destroy(){
System.out.println("destroy");
transaction.commit();
session.close();
sessionFactory.close();
}
@Test
public void testGet(){
//1表示ID
NEWS hw = (NEWS)session.get(NEWS.class, 1);
System.out.println(hw);
}
@Test
public void testUpdate(){
NEWS hw = (NEWS)session.get(NEWS.class, 1);
hw.setAuthor("cc");
//可省略
session.update(hw);
}
@Test
public void testSave(){
NEWS hw = new NEWS();
hw.setAuthor("qq");
hw.setTitle("QQ");
hw.setDate(new Date(new java.util.Date().getTime()));
session.save(hw);
}
}
8、说明:以上就是全部步骤,直接选中testSave鼠标右键——RUN AS ——1 JUnit Test
所需jar包的下载地址:http://down.51cto.com/data/2149802