插入数据
删除数据
修改数据
查询单条数据
查询多条数据
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
原文地址:https://www.cnblogs.com/xyhero/p/9348848.html