hibernate实践之一
来源:互联网 发布:开淘宝店怎么寻找货源 编辑:程序博客网 时间:2024/04/29 21:30
1.加载jar包
从这个网站中下载下来hibernate-search-4.5.0.Final-dist将其解压,然后把hibernate-search-4.5.0.Final-dist\hibernate-search-4.5.0.Final\dist\lib\required里的jar包全部加入lib里,再加一个hibernate-search-4.5.0.Final-dist\hibernate-search-4.5.0.Final\dist\lib\provided里的hibernate-jpa-2.1-api-1.0.0.Final.jar包,再加一个数据库驱动包ojdbc6.jar。
连接数据库,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="hbm2ddl.auto">update</property> <!-- 在properties中存在4中,即create-drop/create/update/validate -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="connection.username">chen</property>
<property name="connection.password">123</property>
<property name="myeclipse.connection.profile">oracle</property>
<property name="show_sql">true</property> <!-- 显示SQL语句 -->
<mapping resource="cn/itcast/hibernate/domain/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
2建立类与表的关系,user类
package cn.itcast.hibernate.domain;
import java.util.Date;
public class User {
private int id;
private String name;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
user.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="cn.itcast.hibernate.domain">
<class name="User" table="t_user">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="name" column="name" type="java.lang.String"/>
<property name="birthday" column="birthday" type="java.util.Date"/>
</class>
</hibernate-mapping>
3得到session对象
package cn.itcast.hibernate.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public final class HibernateUtil { // 此类不可以被继承
private HibernateUtil() {
} // 此类不可以在其他地方被new
private static SessionFactory sessionFactory;
static { // static代码块中的代码只被运行一次,全局
Configuration cfg = new Configuration();
cfg.configure(); // 如果名字不是hibernate.cfg.xml,需要在方法中说明
sessionFactory = cfg.buildSessionFactory();
}
// 获得SessionFactory,类似于DriverManager
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
// 获得Session,类似于Connection
public static Session getSession() {
return sessionFactory.openSession();
}
}
4测试
package cn.itcast.hibernate.test;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.itcast.hibernate.domain.User;
import cn.itcast.hibernate.util.HibernateUtil;
public class Base {
public static void main(String[] args) {
User user = new User();
user.setBirthday(new Date());
user.setName("刘鹏");
addUser(user); // 增加
// user.setId(3);
// deleteUserById(user); // 根据ID删除
User u = getUserById(27); // 根据ID查询
System.out.println("名称为:" + u.getName());
u.setName("小飞");
updateUser(u); // 根据更新
}
// 插入
static void addUser(User user) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.save(user);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
throw e; // 一定要将异常抛出,否则什么错误也看不见
}
} finally {
if (s != null) {
s.close();
}
}
}
// 根据主键ID删除
static void deleteUserById(User user) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.delete(user);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
throw e;
}
} finally {
if (s != null) {
s.close();
}
}
}
// 更新
static void updateUser(User user) {
Session s = null;
Transaction tx = null;
try {
s = HibernateUtil.getSession();
tx = s.beginTransaction();
s.update(user);
tx.commit();
} catch (HibernateException e) {
if (tx != null) {
tx.rollback();
throw e;
}
} finally {
if (s != null) {
s.close();
}
}
}
// 根据主键ID查询
static User getUserById(int id) {
Session s = null;
User user = null;
try {
s = HibernateUtil.getSession();
// User.class会反射出包.类名称,对应查找映射文件,找到数据库中的表
user = (User) s.get(User.class, id);
} catch (HibernateException e) {
System.out.println("查询出错");
} finally {
if (s != null) {
s.close();
}
}
return user;
}
}
- hibernate实践之一
- hibernate实践之一 .
- Hibernate Validator实践之一
- Hibernate Validator实践之一 入门篇
- Hibernate级联实践之一(one-to-one)
- Hibernate实践
- Hibernate实践
- Hibernate实践
- Hibernate实践
- Hibernate实践
- Hibernate实践
- Hibernate实践
- Hibernate实践
- ADO开发实践之一
- Remove实践之一
- Android UiAutomator 实践之一
- docker实践入门之一
- BootStrap实践之一
- poj 1321
- java 2013-1
- oracle中的函数
- AMR音频编码器概述及文件格式分析
- 如何连续的测量蓝牙的RSSI
- hibernate实践之一
- 怎么去掉图片作为超链接时出现的框框
- 黑马程序员-面向对象01
- 免费智能建站系统大全
- ZOJ 3734 LIKE vs CANDLE
- Calibre-免费开源的“一站式”的电子书管理阅读格式转换软件
- Dell D630安装ubuntu
- 音标tr与dr
- 《雪国列车》逼韦恩斯坦妥协 英美将播未删减版