1, 做个简单的util

public class HibernateUtils {

	private static SessionFactory sf;
static {
//加载主要的配置文件
sf = new Configuration().configure().buildSessionFactory();
} // 打开session
public static Session getSession(){
return sf.openSession();
}
}

2,做个Accounts的qrud接口

public interface AccountDaoInterface {

	//增加
void save(Account emp);
//更新
void update(Account emp);
//查找
Account findById(Serializable id);
List<Account> getAll();
List<Account> getAll(String AccountName);
List<Account> getAll(int index, int count);
//删除
void delete(Serializable id); }

  

3,实现接口

public class AccountDao implements AccountDaoInterface {

	@Override
public Account findById(Serializable id) {
Session session = null;
Transaction tx = null;
try {
//获取session
session = HibernateUtils.getSession();
//开启
tx = session.beginTransaction();
//进行查询
return (Account) session.get(Account.class, id);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
tx.commit();
session.close();
}
} @Override
public List<Account> getAll() {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
// HQL查询
Query q = session.createQuery("from Account");
return q.list();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
tx.commit();
session.close();
}
} @SuppressWarnings("unchecked")
@Override
public List<Account> getAll(String AccountName) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
Query q =session.createQuery("from Account where empName=?");
// 索引从0开始
q.setParameter(0, AccountName);
// 进行查询
return q.list();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
tx.commit();
session.close();
}
} @Override
public List<Account> getAll(int index, int count) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
Query q = session.createQuery("from Account");
// 设置分页参数
q.setFirstResult(index); // 查询开始行数位置
q.setMaxResults(count); // 查询返回的行数 List<Account> list = q.list();
return list;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
tx.commit();
session.close();
}
} @Override
public void save(Account emp) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
// 保存
session.save(emp);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
tx.commit();
session.close();
} } @Override
public void update(Account emp) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
//更新
session.update(emp); } catch (Exception e) {
throw new RuntimeException(e);
} finally {
tx.commit();
session.close();
} } @Override
public void delete(Serializable id) {
Session session = null;
Transaction tx = null;
try {
session = HibernateUtils.getSession();
tx = session.beginTransaction();
// 先查询对象,然后判断删除
Object obj = session.get(Account.class, id);
if (obj != null) {
session.delete(obj);
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
tx.commit();
session.close();
}
} }

  

4,简单使用

List<Account> accounts = new AccountDao().getAll();
for (int i = 0; i < accounts.size(); i++) {
Account account = accounts.get(i);
System.out.println(account.getName());
}

  

(29)java web的hibernate使用-crud的dao的更多相关文章

  1. java框架篇---hibernate之CRUD操作

    CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写. 下面列举实例来讲解这几个操作: 实体类: ...

  2. (28)java web的hibernate使用

    Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自 ...

  3. (31)java web的hibernate使用-一级缓存,二级缓存

    参考:https://blog.csdn.net/miachen520/article/details/52195832 hibernate自带一级缓存 和 二级缓存 一,一级缓存: 基于Sessio ...

  4. (30)java web的hibernate使用-c3p0连接池配置

    hibernate支持c3p0连接池 需要导入c3p0的jar包 <!-- 配置连接驱动管理类 --> <property name="hibernate.connecti ...

  5. [原创]java WEB学习笔记95:Hibernate 目录

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. Java Web整合开发实战:基于Struts 2+Hibernate+Spring 目录

    第1篇 Java Web开发基础第1章 Web的工作机制( 教学视频:31分钟) 1.1 理解Web的概念 1.1.1 Web的定义 1.1.2 Web的三个核心标准 1.2 C/S与B/S两种软件体 ...

  7. java web工程之Hibernate

    java web添加structs特性后再添加Hibernate特性,这可以通过右键工程->my eclipse出现工具条选中相应的条目,添加相应的属性, 添加完Hibernate后建立与数据库 ...

  8. [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  9. Intellij IDEA采用Maven+Spring MVC+Hibernate的架构搭建一个java web项目

    原文:Java web 项目搭建 Java web 项目搭建 简介 在上一节java web环境搭建中,我们配置了开发java web项目最基本的环境,现在我们将采用Spring MVC+Spring ...

随机推荐

  1. Codeforces Round #360 (Div. 2)——C. NP-Hard Problem(BFS染色判二分图)

    C. NP-Hard Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. 刷题总结——谈笑风生(主席树+dfs序的应用)

    题目: Description 设T 为一棵有根树,我们做如下的定义:• 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道高明到哪里去了”.• 设a 和 b 为 T 中的两个不同 ...

  3. Python 可变对象与不可变对象

    1. 不可变(immutable):int.字符串(string).float.(数值型number).元组(tuple) 可变(mutable):字典型(dictionary).列表型(list) ...

  4. LR(1)文法分析器 //c++ 实现

    1.先读入终结符,非终结符,和所有产生式. 2.预处理:初始化:getpp()获得每个非终结符在产生式左边时的产生式编号, 记录在 string getp[]中(可以多个). 3.获得所有的符号的fi ...

  5. Unable to locate Attribute with the the given name [] on this ManagedType

    最近在写Springboot+hibernate的项目,遇到这个问题 好坑,查了半天没发现我哪里配置错了,后来发现实体类声明字段大小写敏感,把声明字段改成小写就行了

  6. log4j配置(转载)

    Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...

  7. 51 NOD 1406 and query

    我们知道一个数S会对所有它的子集S'产生1的贡献,但是我们直接枚举子集是 3^(log2 1000000)的,会炸掉:如果直接把每个有1的位变成0往下推也会凉掉,因为这样会有很多重复的. 但是我们发现 ...

  8. java多线程异步执行

    import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.ut ...

  9. 转: 性能测试应该怎么做? (from coolshell.cn)

    转自: http://coolshell.cn/articles/17381.html 偶然间看到了阿里中间件Dubbo的性能测试报告,我觉得这份性能测试报告让人觉得做这性能测试的人根本不懂性能测试, ...

  10. [Javascript] Use a custom sort function on an Array in Javascript

    Sorting in Javascript with sort uses lexical sorting by default, which means it will sort in alphabe ...