1.倒入相应的jar包
2.在SRC下建立一个hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库方言 -->
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<!-- 连接字符串 -->
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521:orcl
</property>
<!-- 用户名 -->
<property name="connection.username">bbs</property>
<!-- 密码 -->
<property name="connection.password">123</property>
<!-- 数据库的连接驱动 -->
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<!-- 是否根据实体对象生成新的表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 是否打印显示sql语句 -->
<property name="show_sql">true</property>
<!-- C3P0连接池的配置 -->
<property name="connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<!-- 最小连接数 -->
<property name="c3p0.min_size">5</property>
<!-- 最大连接数 -->
<property name="c3p0.max_size">20</property>
<!-- 连接的最大空闲时间,时间为秒 -->
<property name="c3p0.timeout">3600</property>
<!-- 最大的statement数 -->
<property name="c3p0.max_statements">50</property>
<!-- 每隔多少时间检查连接池的空闲连接,时间为秒 -->
<property name="c3p0.idle_test_period">120</property>
<!-- 连接池用完之后新建的连接数量 -->
<property name="c3p0.acquire_increment">2</property>
<mapping resource="com/gxa/bj/model/UserInfo.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
3.建立一个model层
1)编写一个用户类
package com.gxa.bj.model;
public class UserInfo {
private Integer userId;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
private String userName;
private String userPwd;
private String userEmail;
}
2)建立一个UserInfo.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.gxa.bj.model" >
<class name="UserInfo" table="UserInfo">
<!-- 主键生成策略 -->
<id name="userId" type="java.lang.Integer">
<!-- 数据库列的名称 -->
<column name="userId"></column>
<!-- 如何生成ID号 -->
<generator class="sequence">
<param name="sequence">usernext</param>
</generator>
</id>
<property name="userPwd" column="userPwd"></property>
<property name="userName" column="userName"></property>
<property name="userEmail" column="userEmail"></property>
</class>
</hibernate-mapping>
4.编写一个测试类TestUI
package com.gxa.bj.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import com.gxa.bj.model.UserInfo;
public class TestUI {
public static void main(String[] args) {
// TODO Auto-generated method stub
Configuration config = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();
SessionFactory factory = config.buildSessionFactory(serviceRegistry);
//提供每次操作的session对象
Session session = factory.openSession();
//直接操作的实体对象(游离状态的数据)
// UserInfo u= new UserInfo();
// u.setUserEmail("test@11.com");
// u.setUserName("admin123");
// u.setUserPwd("123");
// //开启事务
// session.beginTransaction();
// session.save(u);//数据变为持久状态
// session.getTransaction().commit();
// session.close();
// System.out.println("数据提交成功");
//删除操作
//首先需要从数据库里去获取数据(持久状态,对象是从数据库获取的)
/*session.beginTransaction();
UserInfo u2 = (UserInfo) session.get(UserInfo.class, 122);
session.delete(u2);
session.getTransaction().commit();//提交事务
System.out.println("数据删除成功");
session.close();
*/
//更新操作,也需要从数据库里获取数据
// UserInfo u3 =(UserInfo) session.get(UserInfo.class,65);
// session.beginTransaction();
// u3.setUserName("李四疯");
// session.update(u3);
// session.getTransaction().commit();
// System.out.println("数据更新成功");
//查询所有的数据
List<UserInfo> list = session.createQuery("from UserInfo").list();
for(UserInfo tmpuser :list){
System.out.println("姓名:"+tmpuser.getUserName()+",密码:"+tmpuser.getUserPwd());
}
session.close();
}
}