hibernateDAO层基本的增删改查
完整的学习项目放在了我的github上,是一个半成品的在线音乐网站。
hibernate版本1.4 下面是userDAO 即对user表进行增删改查
package DAO; import java.util.List; import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction; import Bean.HibernateSessionFactory;
import Bean.User; public class UserDAO { public static void insertUser(User user) {
Transaction tx = null;
try {
Session session = HibernateSessionFactory.getSessionFactory()
.openSession();
tx = session.beginTransaction();
session.save(user); tx.commit(); } catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
}
HibernateSessionFactory.closeSession();
}//add public static void deleteUser(String userId) {
Transaction tx = null;
try {
User user = getUser(userId);
Session session = HibernateSessionFactory.getSessionFactory()
.openSession();
tx = session.beginTransaction();
session.delete(user);
tx.commit(); } catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
HibernateSessionFactory.closeSession();
}//delete public static void updateUser(User user) {
Transaction tx = null;
try {
Session session = HibernateSessionFactory.getSessionFactory()
.openSession();
tx = session.beginTransaction();
session.update(user);
tx.commit(); } catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
HibernateSessionFactory.closeSession();
}//update public static User getUser(String userId) {
Transaction tx = null;
User user = null;
try {
Session session = HibernateSessionFactory.getSessionFactory()
.openSession();
tx = session.beginTransaction();
user = (User) session.get(User.class, userId);
tx.commit(); } catch (HibernateException e) {
e.printStackTrace();
tx.rollback();
}
HibernateSessionFactory.closeSession();
return user;
}//get one public static List<User> getUsers() {
Transaction tx = null;
List<User> list = null;
try {
Session session = HibernateSessionFactory.getSessionFactory()
.openSession();
tx = session.beginTransaction();
Query query = session.createQuery("from User order by userId desc");
list = query.list();
tx.commit(); } catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
HibernateSessionFactory.closeSession();
return list;
}// get all public static List<User> checkUser(String userId,String userPwd) {
Transaction tx = null;
List<User> list = null;
try {
Session session = HibernateSessionFactory.getSessionFactory()
.openSession();
tx = session.beginTransaction();
Query query = session.createQuery("from User as u where u.userId=:userId and u.userPwd=:userPwd");
query.setString("userId", userId);
query.setString("userPwd", userPwd);
list = query.list();
tx.commit();}
catch (Exception e) {
e.printStackTrace();
tx.rollback();
}
HibernateSessionFactory.closeSession();
return list;
} //测试DAO
public static void main(String[] args){
List<User> list = UserDAO.getUsers(); System.out.println("½øÈë²âÊÔº¯Êý"); for(int i=0;i<list.size();i++){
User user= new User();
user = list.get(i);
System.out.println(user.getUserId());
}
} }
hibernateDAO层基本的增删改查的更多相关文章
- EntityFramework经典数据访问层基类——增删改查
namespace StudentSys.DAL { public class BaseService<T>:IDisposable where T:BaseEntity,new() { ...
- C# EF增删改查
1.增 //1.创建一个EF数据上下文对象 MyDBEntities context=new MyDBEntities(); //2.将要添加的数据,封装成对象 Users user = new Us ...
- Python操作MySQL数据库完成简易的增删改查功能
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目介绍 二丶效果展示 三丶数据准备 四丶代码实现 五丶完整代码 一丶项目介绍 1.叙述 博主闲暇之余花了10个小时写的 ...
- IDEA SpringBoot-Mybatis-plus 实现增删改查(CRUD)
上一篇: IDEA SpringBoot-Mybatis实现增删改查(CRUD) 下一篇:Intellij IDEA 高效使用教程 (插件,实用技巧) 最好用的idea插件大全 一.前言 Mybati ...
- SpringBoot整合MybatisPlus基本的增删改查,保姆级教程
概述 MybatisPlus是国产的第三方插件, 它封装了许多常用的CURDapi,免去了我们写mapper.xml的重复劳动,这里介绍了基本的整合SpringBoot和基础用法. 引入依赖 在项目中 ...
- GZFramwork数据库层《四》单据主从表增删改查
同GZFramwork数据库层<三>普通主从表增删改查 不同之处在于:实例 修改为: 直接上效果: 本系列项目源码下载地址:https://github.com/GarsonZhang/G ...
- GZFramwork数据库层《三》普通主从表增删改查
运行结果: 使用代码生成器(GZCodeGenerate)生成tb_Cusomer和tb_CusomerDetail的Model 生成器源代码下载地址: https://github.com/Gars ...
- GZFramwork数据库层《二》单据表增删改查(自动生成单据号码)
运行效果: 使用代码生成器(GZCodeGenerate)生成tb_EmpLeave的Model 生成器源代码下载地址: https://github.com/GarsonZhang/GZCodeGe ...
- ABP入门系列(6)——展现层实现增删改查
这一章节将通过完善Controller.View.ViewModel,来实现展现层的增删改查.最终实现效果如下图: 一.定义Controller ABP对ASP.NET MVC Controllers ...
随机推荐
- Error running Tomcat8: Address localhost:1099 is already in use 错误解决
摘要: 有时候运行web项目的时候会遇到 Error running Tomcat8: Address localhost:1099 is already in use 的错误,导致web项目无法运行 ...
- HTML5特效~3D立方体旋转
先欣赏一下该特效的最终效果 本文源码参考自http://www.cnblogs.com/ECJTUACM-873284962/进行一点点优化,下面是对此特效原理上的的剖析. 该特效是基于Css3的一些 ...
- Python入门-初始函数
今天让我们来初步认识一个在python中非常重要的组成部分:函数 首先,让我们来幻想这样一个场景: 比如说我们现在想要通过社交软件约一个妹子,步骤都有什么? print('打开手机 ') print( ...
- typeof的探讨
console.log(typeof 'abc') // "string" console.log(typeof true )// "boolean" cons ...
- hdu 3613 Best Reward (manachar算法)
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Prob ...
- EM(期望最大化)算法初步认识
不多说,直接上干货! 机器学习十大算法之一:EM算法(即期望最大化算法).能评得上十大之一,让人听起来觉得挺NB的.什么是NB啊,我们一般说某个人很NB,是因为他能解决一些别人解决不了的问题.神为什么 ...
- 使用C3的一些新属性绘制谷歌浏览器的图标
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- angular中的scope
angular.element($0).scope() 什么是scope? scope是一个refer to the application model的object.它是一个expression的执 ...
- 【Leetcode】【Medium】Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题 ...
- 【Leetcode】【Medium】Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...