1、内容: hibernate 也是一个经典的[数据访问中间件] 开源框架。
2、hibernate核心组件
SessionFactory[整个数据的操作]重量级组件
Session[对数据库的一次业务操作] 轻量级
3、ORM(对象关系映射): 是一种数据访问层 解决方案、用它开源很好的移植到不同数据库平台。
通过 对象模型 操作 数据库关系模型
4、hibernate配置
配置SessionFactory
开发大致步骤:
(参考文档路径)\hibernate-distribution-3.6.8.Final-dist\hibernate-distribution-3.6.8.Final\documentation\manual\zh-CN\pdf
(配置文件路径)
hibernate3.jar\org\hibernate - ZIP 压缩文件, 解包大小为 9,174,527 字节
1、配置 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>
<!--Database connection settings -->
<!--连接数据库-->
<property name="connection.driver_class"
>oracle.jdbc.driver.OracleDriver</property>
<!--连接URL-->
<property name="connection.url"
>jdbc:oracle:thin:@localhost:1521:orcl</property>
<!--帐号-->
<property name="connection.username"
>scott</property>
<!--密码-->
<property name="connection.password"
>tiger</property>
<!--SQL dialect 方言(hiernate可以将对对象模型的操作转为对oracle实际的SQL)-->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!--Enable Hibernate's automatic session context management -->
<!--将通过当前sessionFactory得到的session会被绑定到当前的线程-提高性能-->
<property name="current_session_context_class">thread</property>
<!--Echo all executed SQL to stdout -->
<!--将hibernate转成的SQL语句-显示到控制台->
<property name="show_sql">true</property>
<mapping resource="com/it/bean/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
com/it/bean/UserInfo.hbm.xml
(文件路径)hibernate3.jar\org\hibernate - ZIP 压缩文件, 解包大小为 9,174,527 字节
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.it.bean">
<class name="UserInfo" table="userInfo">
<id name="user_id" column="user_id">
<!-- 主键生成策略 -->
<generator class="assigned"></generator>
</id>
<property name="user_pwd" column="user_pwd"></property>
<property name="user_sex" column="user_sex"></property>
2、创建SessionFactory
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
}
3、创建Session
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
}catch(Exception e){
e.printStackTrace();
}
}
}
4、创建Transaction(事务处理对象)
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
//事务
Transaction tx = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
}catch(Exception e){
e.printStackTrace();
}
}
}
5、开启事务
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
//事务
Transaction tx = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
//开启事务
tx = sesison.beginTransaction();
}catch(Exception e){
e.printStackTrace();
}
}
}
6、提交事务
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
//事务
Transaction tx = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
//开启事务
tx = sesison.beginTransaction();
//提交
tx.commit();
}catch(Exception e){
e.printStackTrace();
}
}
}
7、执行操作
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
//事务
Transaction tx = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
//开启事务
tx = sesison.beginTransaction();
UserInfo u = new UserInfo("9989","sd","23");
//执行U对象 操作 保存
Session.save(u); -------添加
//session.delete(u); ------删除
//UserInfo u1 = (UserInfo)session.get(UserInfo.class,"1001"); ------查询
//System.out.println(u1.getUser_pwd());
//u1.setUser_sex("weinihaom?-sidashu"); ----修改
// Query query = session.createQuery("from UserInfo");
//query.list(); -------查询2
//提交
tx.commit();
}catch(Exception e){
e.printStackTrace();
}
}
}
8、异常处理块,事务回滚
public class Text1{
public static void main(String[] args){
//创建SessionFactory
SessionFavtory sessionFactory = null;
//创建Session
Session session = null;
//事务
Transaction tx = null;
try{
sessionFactory = new Configuration.configure().buildSessionFactory();
session = sessionFactory.getCurrentSession();
//开启事务
tx = sesison.beginTransaction();
UserInfo u = new UserInfo("9989","sd","23");
//执行U对象 操作 保存
Session.save(u);
//提交
tx.commit();
}catch(Exception e){
e.printStackTrace();
//事务回滚
tx.rollback();
}
}
}
封装---工具包
package com.it.dao.util;
/**
*单例模式
*/
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class SessionFactoryUtils {
private static SessionFactory sessionFactory;
static{
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}
基础包(BaseDAO)
package com.it.dao;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import com.it.dao.util.SessionFactoryUtils;
public class BaseDAO<T>{
/**
*查询2
*/
public List<T> find(String hql,String...params){
Session session = null;
Transaction tx = null;
List<T> list = null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
Query query =session.createQuery(hql);
for (int i = 0; i < params.length; i++) {
query.setString(i, params[i]);
}
list=query.list();
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
return list;
}
public List<T> find(String hql){
Session session = null;
Transaction tx = null;
List<T> list = null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
Query query =session.createQuery(hql);
list=query.list();
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
return list;
}
public void add(Object o){
Session session = null;
Transaction tx = null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
session.save(o);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
}
/**
*查询
*/
public T get(Class<T> clz,String OID){
Session session = null;
Transaction tx = null;
T t=null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
t=(T) session.get(clz, OID);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
return t;
}
/**
*删除
*/
public void delete(Class<T> clz,String OID){
Session session = null;
Transaction tx = null;
Object o =get(clz,OID);
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
session.delete(o);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
}
/**
*修改
*/
public void update(Object o){
Session session = null;
Transaction tx = null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
session.update(o);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
}
public void delete(Object o){
Session session = null;
Transaction tx = null;
try {
session = SessionFactoryUtils.getSessionFactory().getCurrentSession();
tx = session.beginTransaction();
session.delete(o);
tx.commit();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
tx.rollback();
}
}
}
userDAO
package com.it.dao;
import java.util.List;
import com.it.bean.UserInfo;
public class UserDAO extends BaseDAO<UserInfo>{
/**
*查询
*/
public List<UserInfo> findUsers(UserInfo user){
String hql = "from UserInfo user where user.user_id like ? and user.user_pwd like ? and user.user_sex like ? ";
String []params={"%"+user.getUser_id()+"%","%"+user.getUser_pwd()+"%","%"+user.getUser_sex()+"%"};
return super.find(hql,params);
}
/**
*查询2
*/
public List<UserInfo> findAllUsers(){
String hql = "from UserInfo";
return super.find(hql);
}
public void add(UserInfo user){
super.add(user);
}
public void delete(String id){
super.delete(UserInfo.class, id);
}
public void delete(UserInfo user){
super.delete(user);
}
public UserInfo get(String user_id){
return super.get(UserInfo.class,user_id);
}
}
package com.it.test;
import java.util.List;
import com.it.bean.UserInfo;
import com.it.dao.BaseDAO;
import com.it.dao.UserDAO;
public class Test {
public static void main(String[] args) {
UserDAO dao = new UserDAO();
//UserInfo u = new UserInfo("1","1","1");
//UserInfo u = new UserInfo("1","1","");
List<UserInfo> users = dao.findUsers(u);
for (UserInfo user : users) {
System.out.println(user.getUser_pwd());
}
}
}