初级,最笨重的方法:
package com.svse.dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.svse.entity.TDept;
//部门数据处理
public class DeptDao_1 {
//全查询 ,
public List getAll(){
List ar = new ArrayList();
//1、加载配置文件,连接数据库
Configuration config = new Configuration().configure();
//2、得到数据映射的工具:SessionFactory;
SessionFactory sessionFactory = config.buildSessionFactory();
//3、得到动作处理工具Session,执行相应的动作;
Session session = sessionFactory.openSession();
//4、使用Session工具执行动作;
ar = session.createQuery("FROM TDept").list();
//5、关闭工具
session.close();
return ar;
}
//查询一个
public TDept getOne(int p_id){
TDept dept = new TDept();
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
// 通过ID 获取到一个对象;TDept.class 使用到java的反射机制;
dept = (TDept) session.get(TDept.class, new Integer(p_id));
session.close();
return dept;
}
/*************************************************************************/
/********************************
* 增、删、改 :三种操作设计到数据的变化,则影响数据的安全性,所以需要使用事务以保证数据的
* 安全性;
*
*
*
*
********************************/
//增加
public void addDept(TDept dept){
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction ctx = session.beginTransaction();
//增加session.save(dept);
session.save(dept);
ctx.commit();
session.close();
}
//修改
public void update(TDept dept){
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
//开启事务
Transaction ctx = session.beginTransaction();
//修改:update();方法
session.update(dept);
//提交事务
ctx.commit();
session.close();
}
//删除
public void delete(TDept dept){
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
// 开启事务以保证数据的安全性;
Transaction ctx = session.beginTransaction();
//动作:delete(); 操作
session.delete(dept);
//提交事务
ctx.commit();
session.close();
}
public static void main(String[] args) {
DeptDao_1 dao = new DeptDao_1();
System.out.println(dao.getAll().size());
}
}
中级:较为简化的连接方法:
package com.svse.dao;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.svse.entity.TUser;
//使用封装类简化代码进行操作
public class UserDao1 {
//定义基本属性字段
private static Configuration config = null;
private static SessionFactory sessionFactory = null;
private static Session session = null;
private static Transaction ctx = null;
//该内部类的特点:UserDao1这个实例化一次的时候自动的调用一次;
static{
try{
config = new Configuration().configure();
sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
}catch(Exception ex){
ex.printStackTrace();
}
}
//增加
public void addUser(TUser user){
ctx = session.beginTransaction();
session.save(user);
ctx.commit();
session.close();
}
//修改
public void updateUser(TUser user){
ctx = session.beginTransaction();
session.update(user);
ctx.commit();
session.close();
}
//删除
public void delUser(TUser user){
ctx = session.beginTransaction();
session.delete(user);
ctx.commit();
session.close();
}
//全查询
public List getAll(){
List ar = new ArrayList();
ar = session.createQuery("FROM TUser").list();
session.close();
return ar;
}
//查询一个
public TUser getOne(int u_id){
TUser user = new TUser();
user = (TUser) session.get(TUser.class, new Integer(u_id));
session.close();
return user;
}
}
高级:最简单的方法,使用接口封装:
辅助类:
package com.svse.util;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
定义接口规则:
package com.svse.util;
import org.hibernate.Session;
//结果定义规则
public interface IHibernateSessionFactorySupport {
//得到Session
public Session getSession();
//开启事务
public void beginTransaction();
//提交事务
public void commitTransaction();
//关闭所有
public void closeAll();
}
接口实现,编写规则:
package com.svse.util;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class HibernateSessionFactorySupportImpl implements
IHibernateSessionFactorySupport {
//定义事务的对象:
private Transaction ctx = null;
//开启事务
public void beginTransaction() {
ctx = this.getSession().beginTransaction();
}
//提交事务
public void commitTransaction() {
try{
ctx.commit();
}catch(Exception ex){
ex.printStackTrace();
//事务回滚
if(ctx!=null){
ctx.rollback();
}
}finally{
HibernateSessionFactory.closeSession();
}
}
//关闭所有:调用自动生类中定义方法
public void closeAll() {
HibernateSessionFactory.closeSession();
}
//得到Session
public Session getSession() {
//返回一个Session的对象
return HibernateSessionFactory.getSession();
}
}
具体数据获取,访问数据库得到数据:
package com.svse.dao;
import java.util.List;
import com.svse.entity.TUser;
import com.svse.util.HibernateSessionFactorySupportImpl;
public class UserDao2 extends HibernateSessionFactorySupportImpl {
//增加--涉及到数据的变化,需要使用事务
public void addUser(TUser user){
//第一步:开启事务
this.beginTransaction();
//第二步:执行动作
this.getSession().save(user);
//第三步:提交事务,并关闭相关的工具
this.commitTransaction();
}
//修改
public void updateUser(TUser user){
//第一步:涉及到数据的安全性,先开启事务;
this.beginTransaction();
//第二步:获得Session对象并执行修改的动作;
this.getSession().update(user);
//第三步:提交事务,并关闭相关的工具;
this.commitTransaction();
}
//删除
public void delUser(int u_id){
//1、开启事务
this.beginTransaction();
//2、执行动作
this.getSession().delete(this.getOne(u_id));
//3、提交事务:并关闭相关的工具
this.commitTransaction();
}
//查询一个
public TUser getOne(int u_id){
//第一步:获取Session对象执行动作,得到集合对象获取数据;
TUser user= (TUser) this.getSession().get(TUser.class, new Integer(u_id));
//第二步:关闭Session工具;
this.closeAll();
return user;
}
//全查询
public List getAll(){
List ar = this.getSession().createQuery("FROM TUser").list();
this.closeAll();
return ar;
}
public static void main(String[] args) {
UserDao2 dao = new UserDao2();
TUser user = new TUser();
user.setUId(1);
user.setUName("小红aa");
user.setUPwd("123");
dao.updateUser(user);
}
}
分享到:
相关推荐
Hibernate连接数据库的注册的项目
java利用hibernate连接数据库,hibernate应用范例
hibernate连接数据库的连接配置。包括oracle等一系列配置。
hibernate连接金仓数据库所需jar包集合。
Hibernate连接数据库.pdf
此文件主要有Hibernate连接数据库的方法...
hibernate配置数据库连接池的三种方法(数据库连接池的概念)
自己收藏 适合初学者 hibernate 连接数据库基础源码
hibernate 连接数据库基础源码 适合初学
hibernate 连接数据库基础源码 适合初学
文档中列出了hibernate连接各种数据库的配置,同时也提供了使用c3p0连接各种数据库的配置,非常方便您的开发。
这是一个使用hibernate连接各种数据库的方法的一个概述!
用eclicpse实现的hibernate连接oracle数据库,并提取中文的小程序
hibernate同时配置多个数据库连接 可以同时连接不同IP的不同数据库
Hibernate配置数据库连接;操作技巧;减少写代码
hibernate连接Oracle和mySql数据库驱动下载,mysql-connector-java-5.1.7-bin.jar,ojdbc6.jar,ojdbc7.jar,xdb6.jar,ucp.jar,simplefan.jar,orai18n.jar,ons.jar,antlr-2.7.7.jar,c3p0-0.9.2.1.jar等等全部...
Hibernate不同数据库的连接及SQL方言
本项目主要用JavaDom解析xml文件模拟Hiberntate连接数据库。 注:本项目只做学习之用。有错误希望指出
Hibernate连接MySql数据库生成表结构,本例演示了一种比较少见的方式