hibernate框架学习之增删改查helloworld
插入数据
删除数据
修改数据
查询单条数据
查询多条数据
HelloWorldApp.java
package cn.itcast.h3.helloworld; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import cn.itcast.h3.helloworld.vo.UserModel; public class HelloWordApp {
public static void main(String[] args) { //1.加载配置文件hibernate.cfg.xml,得到一个配置对象
Configuration conf = new Configuration().configure();
//2.由配置创建一个SessionFactory
SessionFactory sf = conf.buildSessionFactory();
//3.由SessionFactory得到Session
Session s = sf.openSession();
//4.由Session对象获取事务对象Transaction
Transaction t = s.beginTransaction();
//5.完成任务
//5.1创建一个要存储的对象
UserModel um = new UserModel();
um.setUuid("1");
um.setUserName("李若亮");
um.setAge(34);
um.setAddress("传智播客");
//5.2保存
s.save(um); //6.提交事务
t.commit();
//7.关闭Session
s.close();
//8.关闭SessionFactory(略)
sf.close(); }
}
BaseOperApp.java
package cn.itcast.h3.helloworld; import java.util.List; import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; import cn.itcast.h3.helloworld.vo.UserModel; public class BaseOperApp {
//测试插入数据
void testAdd(){
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session s = sf.openSession();
Transaction t = s.beginTransaction(); //1.准备一个要添加的数据
UserModel um = new UserModel();
um.setUuid("3");
um.setUserName("李若亮");
um.setAge(34);
um.setAddress("传智播客");
//2.调用save方法 String res = (String) s.save(um);
System.out.println(res); t.commit();
s.close();
}
//测试修改数据
void testUpdate(){
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session s = sf.openSession();
Transaction t = s.beginTransaction(); //1.准备一个要添加的数据
UserModel um = new UserModel();
um.setUuid("3");
um.setUserName("Jock");
um.setAge(34);
um.setAddress("itcast");
//2.调用update方法
s.update(um); t.commit();
s.close();
}
//测试删除数据
void testDelete(){
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session s = sf.openSession();
Transaction t = s.beginTransaction(); //1.准备一个要添加的数据
UserModel um = new UserModel();
um.setUuid("1");
//2.调用delete方法
s.delete(um); //um对象只要有uuid就可以了 t.commit();
s.close();
}
//测试单个数据
void testQueryOne(){
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session s = sf.openSession();
//1.查询单个数据需要依赖主键进行查询
//2.get(模型类.Class,uuid)方法得到一个对象,这个对象被包装成了Class格式
//3.load(模型类.Class,uuid)方法得到一个对象,这个对象被包装成了Class格式
UserModel um = (UserModel) s.load(UserModel.class, "3");
System.out.println(um);
s.close();
}
//测试全部数据
void testQueryAll(){
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session s = sf.openSession();
//1.获取Query对象
//创建Query对象时,需要使用Hibernate专用的查询语句HQL
String hql = "from UserModel";
Query q = s.createQuery(hql);
//2.将结果查询出来,使用list方法,得到一个查询的结果集合
List<UserModel> queryList = q.list();
for(UserModel um:queryList){
System.out.println(um);
}
s.close();
}
//测试全部数据2
void testQueryAll2(){
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session s = sf.openSession(); //1.使用本地SQL语句查询
String sql = "select * from tbl_user";
//2.使用SQL查询,需要使用SQLQuery对象,该对象使用Session获得
SQLQuery sq = s.createSQLQuery(sql);
//3.查询结果也可以使用list将结果封装成一个集合
List<Object[]> queryList = sq.list();
for(Object[] objs: queryList){
for(Object obj:objs){
System.out.println(obj);
}
System.out.println("------------------");
} s.close();
} public static void main(String[] args) {
new BaseOperApp().testQueryAll2();
}
}
hibernate框架学习之增删改查helloworld的更多相关文章
- hibernate关联对象的增删改查------查
本篇博客是之前博客hibernate关联对象的增删改查------查 的后继,本篇代码的设定都在前文已经写好,因此读这篇之前,请先移步上一篇博客 //代码片5 SessionFactory sessi ...
- Spring JdbcTemplate框架搭建及其增删改查使用指南
Spring JdbcTemplate框架搭建及其增删改查使用指南 前言: 本文指在介绍spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转控制的使用方法和JDBC的基 ...
- Struts2+Spring+Hibernate实现员工管理增删改查功能(一)之ssh框架整合
前言 转载请标明出处:http://www.cnblogs.com/smfx1314/p/7795837.html 本项目是我写的一个练习,目的是回顾ssh框架的整合以及使用.项目介绍: ...
- Hibernate进行对象的增删改查
首先我们看看hibernate手动配置步骤 (这个了解一点就可以了,以后是不会自己全部手动配置的) 1. 创建WEB项目 2 下载hibernate-release-4.3.11.F ...
- SSHE框架整合(增删改查)
1.前期准备:jar包(c3p0.jdbc ,各个框架) web.xml文件:spring的 转码的,和Struts2的过滤器 <?xml version="1.0" e ...
- hibernate关联对象的增删改查------增
本文可作为,北京尚学堂马士兵hibernate课程的学习笔记. 这一节,我们看看hibernate关联关系的增删改查 就关联关系而已,咱们在上一节已经提了很多了,一对多,多对一,单向,双向... 其实 ...
- JDBC学习笔记——增删改查
1.数据库准备 要用JDBC操作数据库,第一步当然是建立数据表: ? 1 2 3 4 5 6 CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_I ...
- Hibernate入门案例及增删改查
一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...
- 快速入门GreenDao框架并实现增删改查案例
大家的项目中不可避免的使用到SQLite,为此我们要花费心思编写一个增删改查框架.而一个好的ORM框架则能够给我们带来极大的方便,今天给大家讲解一个非常火热的ORM-GreenDao. 基本概念 Gr ...
随机推荐
- js和jQuery中的事件绑定与普通事件
普通事件,是指直接对元素进行事件注册,然后触发 而事件绑定是将事件注册到元素上 两者区别就是在于普通事件不可以重复添加多个事件,若添加也会覆盖,只会触发其中一个事件(最后注册的那个) 而事件绑定是可以 ...
- bzoj千题计划323:bzoj1951: [Sdoi2010]古代猪文(Lucas+CRT+欧拉定理)
https://www.lydsy.com/JudgeOnline/problem.php?id=1951 先欧拉降幂 然后模数质因数分解 分别计算组合数的结果,中国剩余定理合并 #include&l ...
- MVC Repository模式
近来发现很多ASP.NET MVC的例子中都使用了Repository模式,比如Oxite,ScottGu最近发布的免费的ASP.NET MVC教程都使用了该模式.就简单看了下. 在<企业架构模 ...
- GIL 全局解释器
全局解释器锁 GIL 相当于给python解释器加了一把互斥锁 每一个进程都有一把互斥锁,所有线程必须先拿到解释器,才能执行代码, 同一进程下,所有线程并发 在 Cpython 解释器下,多个进程可以 ...
- 一个awk if 嵌套 if 的脚本
[root@makeISO sjx]# cat sex.sh #!/bin/bash # 统计如下内容,并输出结果 # (男职工未婚数量/男员工整体数量)(女员工未婚数量/女员工整体数量) echo ...
- SpringBoot入门笔记(一)、HelloWorld
本文是一篇SprintBoot学习入门笔记 1.打开Eclipse,版本为Oxygen 4.7.0 2.新建项目NewProject->MavenProject->Next->Nex ...
- C# WinForm多线程(一)----- Thread类库
Windows是一个多任务的系统,如果你使用的是windows 2000及其以上版本,你可以通过任务管理器查看当前系统运行的程序和进程.什么是进程呢?当一个程序开始运行时,它就是一个进程,进程所指包括 ...
- 关于AJAX的基础操作
AJAX开发 AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 Java ...
- clam安装
nodejs下,npm安装clam指令: npm install -g clam
- 第26月第13天 hibernate导包
1. https://yq.aliyun.com/ziliao/386827