20151215 Hibernate学习笔记——基本的配置和使用方法
Hibernate 基本配置
1. 下载 Hibernate
2. 导入jar包
将hibernate根目录\lib\required文件夹下的包全部导入,同时还需要导入数据库的驱动包.要注意的是因为要使用JPA标准的标签(需要import javax.persistence),所以还要导入jpa.jar包(百度一下下载即可).(看一些介绍说不用导此包也行,但我不导却报错了,待研究)
3. 配置xml文档
在中文html文档中有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>
<!-- Database connection settings 数据库的设置 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hj_legend</property>
<property name="connection.username">root</property>
<property name="connection.password"></property>
<!-- JDBC connection pool 连接池 (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect 数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache 缓存 -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout 输出sql语句 -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup 更新数据库(无则创建,有则不创建) -->
<property name="hbm2ddl.auto">update</property>
<!-- <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
<!-- 配置持久化类的类名 -->
<mapping class="com.hj.hibernate.bean.User"/>
</session-factory>
</hibernate-configuration>
4. 用注解配置javaBean
package com.hj.hibernate.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity//说明此类是一个持久化类
@Table(name = "tb_user")//说明对应的数据库表名
public class User
{
@Id//说明此字段是id
@GeneratedValue(strategy=GenerationType.IDENTITY)//id自动增长
private int userId;//以下三个字段为了简便命名与数据库中的命名相同,这样就不用单独配置了
private String userName;
private String password;
public User()
{
super();
}
public User(String userName, String password)
{
super();
this.userName = userName;
this.password = password;
}
public int getId()
{
return userId;
}
public void setId(int id)
{
this.userId = id;
}
public String getUserName()
{
return userName;
}
public void setUserName(String userName)
{
this.userName = userName;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
用Hibernate操作数据库
基本步骤:
- 获取Configuraction
- 获取SessionFactory
- 获取session并开始事务
- 用面向对象的方法操作数据库
- 关闭资源
java代码如下(向tb_user表中添加一条记录):
package com.hj.hibernate.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.hj.hibernate.bean.User;
/**
* 12/15/2015 11:06:15 AM
* @author HangJian
*
*/
public class Main
{
/**
* @param args
*/
public static void main(String[] args)
{
//获得配置
Configuration cfg = new Configuration().configure();
//获取sessionFactory
SessionFactory sf = cfg.buildSessionFactory();
//创建session
Session sess = sf.openSession();
//开始事务
Transaction ts = sess.beginTransaction();
//创建实体类的对象
User user = new User("hjhjhjhjhjhj", "123");
//保存
sess.save(user);
//提交事务
ts.commit();
//关闭session
sess.close();
sf.close();/**/
}
}