zoukankan      html  css  js  c++  java
  • hibernate操作数据库总结

    这篇文章用于总结hibernate操作数据库的各种方法

    一、query方式

    1、hibernate使用原生态的sql语句执行数据库查询

    有些时候有些开发人员总觉得用hql语句不踏实,程序出现了错误,就猜测因为不是原生态的sql语句,数据库不支持,因此情愿选择回到jdbc时代。这样既耗时耗力,又破坏面向对象的编程。其实,hibernate已经考虑到这个问题,hibernate可以执行原生态的sql语句,正对每种数据库,你可以写对应的sql语句,然后用createSQLQuery(sql)即可。

    		/**
    		 * 本地sql的检索方式,使用原生态的sql语句检索
    		 * 
    		 */
    		Session session = this.getSession();
    		String sql = "select * from course where cid=:id";
    		Query query = session.createSQLQuery(sql);// 本地SQL检索方式
    		query.setInteger("id", c_id);
    		query.list();// 返回持久化的list

    2、query执行查询操作(基本方式,字符串连接方式生成hql语句)

    query执行查询操作,可以返回唯一对象或者是对象列表

    (1)query返回对象列表

    		/**
    		 * hql的检索方式,执行查询数据库操作,返回对象的列表
    		 * 采用hql语句连接方式
    		 */
    		Session session = this.getSession();
    		String hql = "select * from CourseDO where CId="+id+"";
    		Query query = session.createQuery(hql);// 本地SQL检索方式
    		query.list();// 返回持久化的list


    (2)query返回唯一对象

    		/**
    		 * hql的检索方式,执行查询数据库操作,返回唯一对象
    		 * 采用hql语句连接方式
    		 */
    		Session session = this.getSession();
    		String hql = "select * from CourseDO where CId="+id+"";
    		Query query = session.createQuery(hql);// 本地SQL检索方式
    		query.uniqueResult();// 返回持久化的list

    3、query执行查询操作

    hql采用参数方式,其中参数绑定方式分为两种:按照名字绑定,按照位置绑定

    (1)hql参数绑定采用按照名字绑定

    		/**
    		 * hql的检索方式,执行查询数据库操作,返回对象的列表
    		 * 
    		 * 参数绑定的形式分为按名字绑定,按位置绑定 此处是按照名字绑定
    		 */
    		Session session = this.getSession();
    		String hql = "select * from CourseDO where CId=:id";
    		Query query = session.createQuery(hql);// 本地SQL检索方式
    		query.setInteger("id", c_id);// 名字绑定参数
    		query.list();// 返回持久化对象的list列表

    (2)hql参数绑定采用按照位置绑定

    		/**
    		 * hql的检索方式,参数绑定的形式分为按名字绑定,按位置绑定 此处是按照位置绑定
    		 */
    		String hql = "from CourseDO where CId=?";
    		Session session = this.getSession();
    		Query query = session.createQuery(hql);
    		query.setParameter(0, c_id);// 位置绑定方式
    		query.uniqueResult();// 检索单个对象,返回唯一值

    4、query执行更新,删除操作

    利用query的executeUpdate()方法实现

    		/**
    		 * query执行更新,删除等非查询语句
    		 * 
    		 */
    		String hql = "delete from CourseDO where CId=? and Time=?";
    		Session session = this.getSession();
    		Query query = session.createQuery(hql);
    		query.setParameter(0, c_id);// 位置绑定方式
    		query.setDate(1, new Date());// 位置绑定方式,设置为Date类型
    		query.executeUpdate();// 执行delete,update和insert into 语句

    二、hibernate模版方法

    利用hibernate模版方法执行hql语句非常简单,但是有时候不是那么方便,可以自己选择使用。

    1、hibernateTemplate查询数据库

    		/**
    		 * 利用hibernate模版方法进行查询,绑定参数形式
    		 * 
    		 */
    		String hql = "from CourseDO where CId=? and Date=?";
    		List<CourseDO> courseDOs = getHibernateTemplate().find(hql, c_id,
    				new Date());

    2、hibernateTemplate更新数据

    		/**
    		 * 利用hibernate模版方法进行更新,绑定参数形式
    		 * 
    		 */
    		List<CourseDO> courseDOs = getHibernateTemplate().update(courseDO);

    3、hibernateTemplate插入数据

    		/**
    		 * 利用hibernate模版方法进行插入,绑定参数形式
    		 * 
    		 */
    		List<CourseDO> courseDOs = getHibernateTemplate().save(courseDO);

    4、hibernateTemplate删除数据

    		/**
    		 * 利用hibernate模版方法进行删除,绑定参数形式
    		 * 
    		 */
    		List<CourseDO> courseDOs = getHibernateTemplate().delete(courseDO);






  • 相关阅读:
    Mysql备份恢复
    Mysql事务学习笔记
    MongoDB进阶
    MongoDB入门
    Mysql流程解析
    Mysql Explain学习笔记
    面试题
    聚集索引和非聚集索引
    端口号占用
    classpath: 和classpath*:的区别
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3163099.html
Copyright © 2011-2022 走看看