插入数据
删除数据
修改数据
查询单条数据
查询多条数据

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的更多相关文章

  1. hibernate关联对象的增删改查------查

    本篇博客是之前博客hibernate关联对象的增删改查------查 的后继,本篇代码的设定都在前文已经写好,因此读这篇之前,请先移步上一篇博客 //代码片5 SessionFactory sessi ...

  2. Spring JdbcTemplate框架搭建及其增删改查使用指南

    Spring JdbcTemplate框架搭建及其增删改查使用指南 前言: 本文指在介绍spring框架中的JdbcTemplate类的使用方法,涉及基本的Spring反转控制的使用方法和JDBC的基 ...

  3. Struts2+Spring+Hibernate实现员工管理增删改查功能(一)之ssh框架整合

    前言        转载请标明出处:http://www.cnblogs.com/smfx1314/p/7795837.html 本项目是我写的一个练习,目的是回顾ssh框架的整合以及使用.项目介绍: ...

  4. Hibernate进行对象的增删改查

    首先我们看看hibernate手动配置步骤 (这个了解一点就可以了,以后是不会自己全部手动配置的) 1.    创建WEB项目 2       下载hibernate-release-4.3.11.F ...

  5. SSHE框架整合(增删改查)

    1.前期准备:jar包(c3p0.jdbc ,各个框架) web.xml文件:spring的   转码的,和Struts2的过滤器 <?xml version="1.0" e ...

  6. hibernate关联对象的增删改查------增

    本文可作为,北京尚学堂马士兵hibernate课程的学习笔记. 这一节,我们看看hibernate关联关系的增删改查 就关联关系而已,咱们在上一节已经提了很多了,一对多,多对一,单向,双向... 其实 ...

  7. JDBC学习笔记——增删改查

    1.数据库准备  要用JDBC操作数据库,第一步当然是建立数据表: ? 1 2 3 4 5 6 CREATE TABLE `user` (   `id` int(11) NOT NULL AUTO_I ...

  8. Hibernate入门案例及增删改查

    一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...

  9. 快速入门GreenDao框架并实现增删改查案例

    大家的项目中不可避免的使用到SQLite,为此我们要花费心思编写一个增删改查框架.而一个好的ORM框架则能够给我们带来极大的方便,今天给大家讲解一个非常火热的ORM-GreenDao. 基本概念 Gr ...

随机推荐

  1. java动态获取上传文件的编码类型

    package com.sjfl.main; import java.io.BufferedReader; import java.io.File; import java.io.FileInputS ...

  2. nginx的rewrite指令

    需求:我们在接收到一个/api/upload 开头的url的时候 要转发到另外一个地址.但是又不想追加/api/upload  只想追加/upload 地址. Nginx提供了rewrite指令,用于 ...

  3. LeetCode(192. Word Frequency)

    192. Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...

  4. 混合app开发--js和webview之间的交互总结

    使用场景:原生APP内嵌套H5页面,app使用的是webview框架进行嵌套 这样就存在两种情况 1.原生app调用H5的方法 2.H5调用app的方法 分别讲解下,其实app与H5之间的交互式非常简 ...

  5. Java SSM框架之MyBatis3(十)MyBatis批量插入数据(MySql)

    插入成功后返回自增主键 <insert id="insertRole" parameterType="role" useGeneratedKeys=&qu ...

  6. 今天终于想明白为什么java包要倒着写

    比如 com.baidu.video,因为java内部实际上是以文件夹形式存在的,是按com,baidu,video依次生成文件夹的具体功能的是子文件夹,所以要倒着写.

  7. Tomcat下载,及环境变量配置

    首先,介绍一下Tomcat: Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun 和其他一些公 ...

  8. ubantu中安装TensorFlow遇到的问题

    因为Ubuntu中装有python3.python2两种python环境,在装TensorFlow时需要根据版本进行适配 1.安装pip3 #在python2下安装pip sudo apt-get i ...

  9. vue父组件如何调用子组件的属性或方法

    常常我们需要组件的拆分,就涉及到父子调用的关系,那么父组件如何调用子组件的属性和方法呢? 子组件child <template> <div> {{msg}} </div& ...

  10. 前端向服务器请求数据并渲染的方式(ajax/jQuery/axios/vue)

    原理: jQuery的ajax请求:complete函数一般无论服务器有无数据返回都会显示(成功或者失败都显示数据): return result