一、项目结构

二、hibernate核心配置文件:   hibernate.cfg.xm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- 配置hibernate核心配置文件 -->
<hibernate-configuration>
<!-- 配置hibernate数据源连接 -->
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="hibernate.connection.username">wzf</property>
<property name="hibernate.connection.password">1234</property> <!-- 配置数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 配置sql打印、格式化 -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property> <!-- 配置hibernate映射文件位置 -->
<mapping resource="com/gomai/pojo/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

三、hrbernate的映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 配置hibernate映射文件 -->
<hibernate-mapping>
<class name="com.gomai.pojo.Student" table="student">
<!-- 配置主键生成策略 -->
<id name="student_id" column="STUID">
<generator class="sequence">
<param name="sequence">SQ_STUDENT</param>
</generator>
</id>
<!-- 配置表与属性 -->
<property name="student_name" column="stuname"></property>
<property name="student_age" column="stuage"></property>
<property name="student_sex" column="stusex"></property>
<property name="student_no" column="stuno"></property>
</class>
</hibernate-mapping>

四、测试类(该类包括增删改查四个方法的实现,下面依次介绍)

新增:

	/**
* 添加:通过序列生成主键自增
         */
@Test
public void insertStu(){
Configuration configure = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
//开启事务
Transaction tr = session.beginTransaction(); int i = session.createSQLQuery("insert into student values(SQ_STUDENT.nextval,?,?,?,?)")
.setParameter(0, 2)
.setParameter(1, "露娜")
.setParameter(2, 23)
.setParameter(3, "女")
.setParameter(4, 1003)
.executeUpdate(); System.out.println(i);
try {
tr.commit();
} catch (HibernateException e) {
tr.rollback();
e.printStackTrace();
}finally{
session.close();
}
}

 删除:

/**
* 删除
*/
@Test
public void deleteStu(){
Configuration configure = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
//开启事务
Transaction tr = session.beginTransaction(); int i = session.createSQLQuery("delete from student where stuid = ?")
.setParameter(0, 100)
.executeUpdate();
System.out.println("TestSQL.deleteStu()" + i); //事务回滚、关流
try {
tr.commit();
} catch (Exception e) {
tr.rollback();
e.printStackTrace();
}finally{
session.close();
}
}

更新:

	/**
* 更新
*/
@Test
public void updateStu(){
Configuration configure = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tr = session.beginTransaction();
int i = session.createSQLQuery("update student set stuname = ?,stusex = ? where stuid = ? ")
.setParameter(0, "公孙离")
.setParameter(1, "女")
.setParameter(2, 9)
.executeUpdate(); System.out.println(i); //事务回滚、关流
try {
tr.commit();
} catch (Exception e) {
tr.rollback();
e.printStackTrace();
}finally{
session.close();
}
}

查询:

/**
* 查询
*/
@Test
public void searchStu(){
Configuration configure = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configure.buildSessionFactory();
Session session = sessionFactory.openSession(); List<Student> list = session.createSQLQuery("select * from student")
.addEntity(Student.class)
.list(); for (Student student : list) {
System.out.println(student);
}
session.close(); }

原文出处:

小白农, Hibernate通过createSQLQuery( )方法实现增删改查, https://blog.csdn.net/w18706334163/article/details/79923466

Hibernate通过createSQLQuery( )方法实现增删改查的更多相关文章

  1. HibernateTemplate、HibernateDaoSupport两种方法实现增删改查Good(转)

    Spring+Hibernate两种方法实现增删改查 首先,定义一个Customer的bean类,设置好Customer.hbm.xml文件.再定义好一个Dao接口.准备好一个jdbc.propert ...

  2. get,post,put,delete四种基础方法对应增删改查

    PUT,DELETE,POST,GET四种基础方法对应增删改查 1.GET请求会向数据库发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改.增加数 ...

  3. hibernate对单表的增删改查

    ORM: 对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping) 实现对单表的增删改查 向区域表中增加数据: 第一步: 新建一个Da ...

  4. Hibernate3回顾-5-简单介绍Hibernate session对数据的增删改查

    5. Hibernate对数据的增删改查 5.1Hibernate加载数据 两种:get().load() 一. Session.get(Class arg0, Serializable arg1)方 ...

  5. 2、hibernate七步走完成增删改查

    一.hibernate框架介绍如下 1.框架=模板 2.Hibernate是对象模型与关系数据库模型之间的桥梁 3.hibernate持久化概念 什么是ORM ORM是对象关系映射,是一种数据持久化操 ...

  6. Hibernate之API初识及增删改查实现

    声明:关于hibernate的学习.非常大一部分东西都是概念性的. 大家最好手里都有一份学习资料,在我的博文中.我不会把书本上的概念一类的东西搬过来.那没有不论什么意义.关于hibernate的学习, ...

  7. hibernate基本配置与简单增删改查

    ORM(Object Relation Mapping)是对象关系映射,是一个思想,它的作用是在关系数据库与对象之间做一个自动映射,将数据库中的表格映射到一个类,也就是持久化类,数据表中每行映射为对象 ...

  8. VS连接Access数据库--连接字符串及执行查询语句的方法(增删改查,用户名查重,根据用户获取密码查询)

    ACCESS数据的连接及语句执行操作,不难,久不用会生疏,每次都要找资料,干脆自己整理下,记录下来,需要的时候,直接查看,提高效率.也供初学者参考 1.连接字符串 public static stri ...

  9. hibernate与struts框架实现增删改查

    这里配置hibernate与struts不再过多赘述,配置搭建前文已经详细讲解,配置如下: hibernate.hbm.xml配置: <?xml version="1.0" ...

随机推荐

  1. 一、hexo+github搭建个人博客的过程记录

    前提: 1.新建一个github仓库 2.安装配置Node.js 3.安装配置Git 前提 步骤1.新建一个github仓库 打开github网站,(注册)登录账号,新建一个仓库; 注:==仓库名称要 ...

  2. 'while' statement cannot complete without throwing an exception

    You are probably using Android Studio or IntelliJ. If so, you can add this above your method contain ...

  3. 适合新手入门Spring Security With JWT的Demo

    Demo 地址:https://github.com/Snailclimb/spring-security-jwt-guide .欢迎 star! Spring Security 是Spring 全家 ...

  4. 如何才能通俗易懂的解释js里面的‘闭包’?

    1. "闭包就是跨作用域访问变量." [示例一] var name = 'wangxi' function user () { // var name = 'wangxi' fun ...

  5. oracle OCCI编程

    1. 创建OCCI环境变量 Environment *env = Environment::createEnvironment(); Environment对象的建立必须放在第一位,而且也必须是最后一 ...

  6. 简单几招提速 Kotlin Kapt编译

    https://droidyue.com/blog/2019/08/18/faster-kapt/?hmsr=toutiao.io&utm_medium=toutiao.io&utm_ ...

  7. Python的变量命名规则

    1.只能用大小写字母与“_”(下划线) 2.数字不能用在开头,如:12tea 3.不能使用空格 4.C语言的变量命名规则如上相同

  8. MySQL修炼之路二

    1. 表字段的操作 1. 语法: alter table 表名 执行动作: 2. 添加字段(add) alter table 表名 add 字段名 数据类型: alter table 表名 add 字 ...

  9. linux部署django项目流程(全)

    1.python3和python2共存配置 流程在下面网址中 https://www.cnblogs.com/vinic-xxm/p/11358894.html 2.安装依赖包 yum install ...

  10. JMETER 生成测试报告

    JMETER测试报告样例 JMETER 提供的生成测试报告功能,能够生成漂亮的HTML测试报告. 上图是测试统计图 20个用户并发,测试时长一分钟,发起流程320次,没有出错,TPS为6.5,平均发起 ...