完整的学习项目放在了我的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层基本的增删改查的更多相关文章

  1. EntityFramework经典数据访问层基类——增删改查

    namespace StudentSys.DAL { public class BaseService<T>:IDisposable where T:BaseEntity,new() { ...

  2. C# EF增删改查

    1.增 //1.创建一个EF数据上下文对象 MyDBEntities context=new MyDBEntities(); //2.将要添加的数据,封装成对象 Users user = new Us ...

  3. Python操作MySQL数据库完成简易的增删改查功能

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目介绍 二丶效果展示 三丶数据准备 四丶代码实现 五丶完整代码 一丶项目介绍 1.叙述 博主闲暇之余花了10个小时写的 ...

  4. IDEA SpringBoot-Mybatis-plus 实现增删改查(CRUD)

    上一篇: IDEA SpringBoot-Mybatis实现增删改查(CRUD) 下一篇:Intellij IDEA 高效使用教程 (插件,实用技巧) 最好用的idea插件大全 一.前言 Mybati ...

  5. SpringBoot整合MybatisPlus基本的增删改查,保姆级教程

    概述 MybatisPlus是国产的第三方插件, 它封装了许多常用的CURDapi,免去了我们写mapper.xml的重复劳动,这里介绍了基本的整合SpringBoot和基础用法. 引入依赖 在项目中 ...

  6. GZFramwork数据库层《四》单据主从表增删改查

    同GZFramwork数据库层<三>普通主从表增删改查 不同之处在于:实例 修改为: 直接上效果: 本系列项目源码下载地址:https://github.com/GarsonZhang/G ...

  7. GZFramwork数据库层《三》普通主从表增删改查

    运行结果: 使用代码生成器(GZCodeGenerate)生成tb_Cusomer和tb_CusomerDetail的Model 生成器源代码下载地址: https://github.com/Gars ...

  8. GZFramwork数据库层《二》单据表增删改查(自动生成单据号码)

    运行效果: 使用代码生成器(GZCodeGenerate)生成tb_EmpLeave的Model 生成器源代码下载地址: https://github.com/GarsonZhang/GZCodeGe ...

  9. ABP入门系列(6)——展现层实现增删改查

    这一章节将通过完善Controller.View.ViewModel,来实现展现层的增删改查.最终实现效果如下图: 一.定义Controller ABP对ASP.NET MVC Controllers ...

随机推荐

  1. 使用SSH连接LINUX的命令

    查看端口号是否被占用 netstat -tunlp|grep 端口号 杀掉 kill-9 pid 后台运行 nohup 应用程序名 & disown -a && exit 屏幕 ...

  2. 接口调用 读取图片报错 Access to the path '' is denied.解决方案

    调用接口 读取服务器上 图片 报错: Server was unable to process request. ---> Access to the path '图片路径' is denied ...

  3. BBS登录与注册功能

    登录功能 视图函数 def my_login(request): if request.method == 'GET': return render(request, 'login.html') el ...

  4. 将ReportingService 2008配置为匿名访问 - 转载

    众所周知在ReportingService 2008中微软取消了匿名访问,我们再也不能像2005时那样靠修改IIS来实现匿名访问了.但在微软MSDN BLOG中还是提供了以下这种设置匿名访问的方式. ...

  5. 《火星救援》NASA惊现lisp

    duang-跳出个界面上面一个lisp程序.

  6. 一张图看懂CSS cascade, specific, importance, inheritance

    http://www.w3.org/TR/CSS21/cascade.html#inheritance http://www.w3.org/TR/CSS21/cascade.html#cascade ...

  7. C可变参数

    https://www.cnblogs.com/pengdonglin137/p/3345911.html#_label0

  8. Struts2学习-ssh框架

    SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架. http://www.cnblogs.com/laibin/p/5847111.h ...

  9. February 9 2017 Week 6 Thursday

    Happy life lies in a peaceful mind. 幸福的生活存在于心绪的宁静当中. What can be seen as happy life? Maybe the answe ...

  10. BIND简易教程(1):安装及基本配置

    首先,为什么说是简易教程呢?因为BIND的功能实在太多,全写出来的话要连载好久,我觉得我没有那么多精力去写:而我了解的仅仅是有限的一点点,不敢造次.百度上的文章也是一抓一大把呐!所以,教点基本使用方法 ...