分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

对象序列化和反序列--Hibernate的查询和新增极其相似

发布时间:2023-09-06 01:12责任编辑:彭小芳关键词:Hibernate
Hibernate几个关键字
持久化,ORM(关系对象映射)(数据库中关系称作是一张表)


应用在项目中,刘一从写的查询代码,每次都挂掉,想要弄出测试数据,自己想着把查询出来的复杂数据弄到文件里
自己要是去造那些复杂数据很麻烦
public class Object1 { ???public static void main(String args[]){ ?????????HashMap<String, Object> obj=new HashMap<String,Object>(); ?????????obj.put("hello","String"); ?????????ArrayList<String> array = new ArrayList<String>(); ?????????array.add("abcdefg"); ?????????writeObjectToFile(array); ?????//将这个瞬时对象写到了磁盘里面了 ????????????ArrayList<String> obj1= ?(ArrayList<String>) readObjectFromFile(); ???????//和hibernate的查询代码极其相似 ???} ???public static ?void writeObjectToFile(Object obj) { ???????File file = new File("D:\\java\\test.dat"); ???????FileOutputStream out; ???????try { ???????????out = new FileOutputStream(file); ???????????ObjectOutputStream objOut = new ObjectOutputStream(out); ???????????objOut.writeObject(obj); ???????????objOut.flush(); ???????????objOut.close(); ???????????System.out.println("write object success!"); ???????} catch (IOException e) { ???????????System.out.println("write object failed"); ???????????e.printStackTrace(); ???????} ???} ???????public static Object readObjectFromFile() { ???????Object temp = null; ???????File file = new File("D:\\java\\test.dat"); ???????FileInputStream in; ???????try { ???????????in = new FileInputStream(file); ???????????ObjectInputStream objIn = new ObjectInputStream(in); ???????????temp = objIn.readObject(); ???????????objIn.close(); ???????????System.out.println("read object success!"); ???????} catch (IOException e) { ???????????System.out.println("read object failed"); ???????????e.printStackTrace(); ???????} catch (ClassNotFoundException e) { ???????????e.printStackTrace(); ???????} ???????return temp; ???}
public static void main(String[] args) { ???????SessionFactory sf = new Configuration().configure().buildSessionFactory(); ???????Session s = sf.openSession(); ???????s.beginTransaction(); ????????????????????Product p = new Product(); ???????p.setName("iphone7"); ???????p.setPrice(1000); ???????s.save(p); ??//将一个内存中的对象转到物理数据库中的一条记录 ???????s.getTransaction().commit(); ???????????????????//根据id查询 ???????Product p4 = (Product)s.get(Product.class,1);

      String hql="from Product";
      List<Product> list2=(List<Product>) s.createQuery(hql).list();

 
所有用户

最后一个是条件查询

以后继续,没成功,不要在这里浪费时间了

String hql2="select * from Product where price<? ";

@SuppressWarnings("unchecked")
List<Product> list2=(List<Product>) s.createQuery(hql2).setParameter(0, 2000l).list();

对象序列化和反序列--Hibernate的查询和新增极其相似

原文地址:http://www.cnblogs.com/cs-lcy/p/7554571.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved