Hibernate的增删改查

一、搭建Hibernate开发环境,这里就不说了,直接说环境搭好后的事情。

二、项目的目录结构

三、base_image.hbm.xml配置文件内容

  

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5
 6 <hibernate-mapping>
 7     <class name="cn.itheima.domain.Image" table="base_image">
 8         <id name="id" column="IMAGE_ID">
 9 <!--             <generator class="native"></generator> -->
10         </id>
11         <property name="number" column="AD_NUMBER" type="java.lang.String" />
12         <property name="ad_name" column="AD_NAME" type="java.lang.String" />
13         <property name="desc" column="AD_DESC" type="java.lang.String" />
14         <property name="image_name" column="IMAGE_NAME" type="java.lang.String" />
15         <property name="url" column="IMAGE_URL" type="java.lang.String" />
16     </class>
17
18 </hibernate-mapping>

四、Image.java中内容

  

 1 package cn.itheima.domain;
 2
 3 public class Image {
 4     public String id;
 5     public String number;
 6     public String desc;
 7     public String ad_name;
 8     public String image_name;
 9
10     public String getId() {
11         return id;
12     }
13
14     public void setId(String id) {
15         this.id = id;
16     }
17
18     public String getNumber() {
19         return number;
20     }
21
22     public void setNumber(String number) {
23         this.number = number;
24     }
25
26     public String getDesc() {
27         return desc;
28     }
29
30     public void setDesc(String desc) {
31         this.desc = desc;
32     }
33
34     public String getAd_name() {
35         return ad_name;
36     }
37
38     public void setAd_name(String ad_name) {
39         this.ad_name = ad_name;
40     }
41
42     public String getImage_name() {
43         return image_name;
44     }
45
46     public void setImage_name(String image_name) {
47         this.image_name = image_name;
48     }
49
50     public String getUrl() {
51         return url;
52     }
53
54     public void setUrl(String url) {
55         this.url = url;
56     }
57
58     public String url;
59
60 }

五、HibernateUtils.java中

  

 1 package cn.itheima.utils;
 2
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 import org.hibernate.cfg.Configuration;
 6
 7 public class HibernateUtils {
 8
 9     private static final SessionFactory factory;
10
11     //只加载一次
12     static {
13         Configuration config = new Configuration().configure();
14         factory = config.buildSessionFactory();
15     }
16
17     public static Session getSession() {
18         return factory.openSession();
19     }
20 }

六、ImageTest中的增删改查

  

  1 package cn.itheima.test;
  2
  3 import java.util.List;
  4
  5 import org.hibernate.Criteria;
  6 import org.hibernate.Query;
  7 import org.hibernate.SQLQuery;
  8 import org.hibernate.Session;
  9 import org.hibernate.SessionFactory;
 10 import org.hibernate.Transaction;
 11 import org.hibernate.cfg.Configuration;
 12 import org.junit.Test;
 13
 14 import cn.itheima.domain.Image;
 15
 16 public class ImageTest {
 17     // 添加
 18     @Test
 19     public void save() {
 20         // 1.得到session
 21         Configuration configuration = new Configuration().configure();
 22         SessionFactory sessionFactory = configuration.buildSessionFactory();
 23         Session session = sessionFactory.openSession();
 24         // 2.开启事务
 25         Transaction transaction = session.beginTransaction();
 26         // 3.操作
 27         Image book = new Image();
 28         book.setId("3");
 29         book.setAd_name("aaa");
 30         book.setDesc("desc");
 31         book.setImage_name("imagename");
 32         book.setNumber("number");
 33         book.setUrl("url");
 34         session.save(book);
 35         // 4.提交
 36         session.getTransaction().commit();
 37         session.close();
 38         sessionFactory.close();
 39     }
 40
 41     // 删除 by id
 42     @Test
 43     public void delete() {
 44         Configuration configuration = new Configuration().configure();
 45         SessionFactory sessionFactory = configuration.buildSessionFactory();
 46         Session session = sessionFactory.openSession();
 47         Transaction transaction = session.beginTransaction();
 48         Image image = new Image();
 49         image.setId("1");
 50         session.delete(image);
 51         transaction.commit();
 52         session.close();
 53         sessionFactory.close();
 54     }
 55
 56     // 修改所有  by id
 57     @Test
 58     public void update() {
 59         Configuration configuration = new Configuration().configure();
 60         SessionFactory sessionFactory = configuration.buildSessionFactory();
 61         Session session = sessionFactory.openSession();
 62         Transaction transaction = session.beginTransaction();
 63         Image image = new Image();
 64         image.setId("3");
 65         image.setAd_name("i");
 66         image.setDesc("i");
 67         image.setImage_name("i");
 68         image.setNumber("i");
 69         image.setUrl("i");
 70         session.update(image);
 71         transaction.commit();
 72         session.close();
 73         sessionFactory.close();
 74     }
 75
 76     // 查询一个,根据id
 77     @Test
 78     public void select() {
 79         Configuration configuration = new Configuration().configure();
 80         SessionFactory sessionFactory = configuration.buildSessionFactory();
 81         Session session = sessionFactory.openSession();
 82         Transaction transaction = session.beginTransaction();
 83         Image image = (Image) session.get(Image.class, "8a8ae4fe4fa54c0e014fa6f65c3d0000");
 84         transaction.commit();
 85         System.out.println(image.getAd_name());
 86         session.close();
 87         sessionFactory.close();
 88     }
 89
 90     // 查询所有by sql;
 91     @Test
 92     public void SelectAllbySql() {
 93         Configuration congConfiguration = new Configuration().configure();
 94         SessionFactory sesseionFactory = congConfiguration
 95                 .buildSessionFactory();
 96         Session session = sesseionFactory.openSession();
 97         Transaction transaction = session.beginTransaction();
 98         String sql = "select * from base_image";
 99         SQLQuery sqlQuery = session.createSQLQuery(sql);//注意SQLQuery
100         sqlQuery.addEntity(Image.class);//指定查询类型为Image
101         List<Image> list = sqlQuery.list();
102         System.out.println(list.get(0).getAd_name());
103         session.close();
104         sesseionFactory.close();
105     }
106
107     // 查询所有 hql
108     @Test
109     public void SelectAllByHql() {
110         Configuration configuration = new Configuration().configure();
111         SessionFactory sessionFactory = configuration.buildSessionFactory();
112         Session session = sessionFactory.openSession();
113         Transaction transaction = session.beginTransaction();
114         String hql = "from Image";// 根据Image类查询
115         Query query = session.createQuery(hql);
116         List<Image> list = query.list();
117         System.out.println(list.get(0).getAd_name());
118         session.close();
119         sessionFactory.close();
120     }
121
122     // 查询所有qbc
123     @Test
124     public void selectAllByQbc() {
125         Configuration configuration = new Configuration().configure();
126         SessionFactory sessionFactory = configuration.buildSessionFactory();
127         Session session = sessionFactory.openSession();
128         Transaction transaction = session.beginTransaction();
129             //QBC
130         Criteria criteria = session.createCriteria(Image.class);
131         List<Image> list = criteria.list();
132
133         session.close();
134         sessionFactory.close();
135     }
136 }

  

时间: 2024-11-05 17:23:35

Hibernate的增删改查的相关文章

hibernate+springMVC增删改查

原文:hibernate+springMVC增删改查 源代码下载地址:http://www.zuidaima.com/share/1550463770610688.htm 1,开发环境:MyEclipse 2013 字符集"UTF-8" 2,Web服务器:Tomcat 6-7都可以 3,SQL Server 2008 需要自己添加数据库db_new,也可以自己改配置文件. 4,使用技术:hibernate,springmvc     官方验证: 程序是没问题的,如楼主描述的功能,他没有

hibernate 入门增删改查demo

原文:hibernate 入门增删改查demo 源代码下载地址:http://www.zuidaima.com/share/1550463648238592.htm

Hibernate 基本增删改查操作

本文将要介绍Hibernate基本的增删改查的操作的实现,首先我们创建一个对象实例.一般情况下会创建User,本例也不例外需要创建这样的对象. [转载使用,请注明出处:http://blog.csdn.net/mahoking] User对象 public class User { private Integer id; private String userName; private String password; /*以下省略getter与setter*/ } 配置User.hbm.xml

hibernate之增删改查demo

1 package dao; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.hibernate.Query; 7 import org.hibernate.SQLQuery; 8 import org.hibernate.Transaction; 9 10 import com.sun.org.apache.bcel.internal.generic.GETSTATIC; 11 12 import

Hibernate 批量增删改查操作

上文介绍Hibernate基本的增删改查,本例将介绍更实用的批量增删改查的操作的实现.本文中增删改查的操作,仅供参考.如果读者需要应用到实际的应用场景需要的话,需要在此基础上扩展与丰富. [转载使用,请注明出处:http://blog.csdn.net/mahoking] 在学习本例时,需要扩展一下Hibernate中Session的知识.Hibernate中的Session是一级缓存,可以理解为进程级的缓存.在进程运行期间一直存在.Session可以理解为一个可以操作数据库的对象 具体如何操作

Hibernate全套增删改查+分页

1.创建一个web工程 2.导入jar包 3.创建Student表 4.创建实体类 package com.entity; public class Student { private Integer sid; private String sname; private String password; private String sex; private Integer cid; public Student() { } public Student(Integer sid, String

EasyUI + Spring MVC + hibernate实现增删改查导入导出

(这是一个故事……) 前言 作为一个JAVA开发工程师,我觉得最基本是需要懂前端.后台以及数据库. 练习的内容很基础,包括:基本增删改查.模糊查询.分页查询.树菜单.上传下载.tab页 主管发我一个已经搭建好的框架平台,在平台上进行编码,不限制技术. 虽然说不限制技术,但还是得根据已经搭建的框架平台进行编码. 所以首先第一步,分析框架平台结构组成. 入手:看目录.看配置.看jar包.看js库.看数据库... 不难发现项目是基于:Spring + Hibernate + Spring MVC +

hibernate实现增删改查的各种方法

1>接口(主要是增删改查的接口)BaseDao.java /** * * @author fly.zhou */ public interface IBaseDao { //增加对应实体的一条记录 public boolean save(Object o); //更新对应实体的一条记录 public boolean update(Object o); //增加或者更新对应实体的一条记录 public boolean saveOrUpdate(Object o); //保存一系列对象集合 publ

hibernate常用增删改查接口实现

1 /** 2 * 作者:宫长明 3 * 4 */ 5 6 7 import java.io.Serializable; 8 import java.util.List; 9 import java.util.Map; 10 11 import org.hibernate.criterion.Criterion; 12 import org.hibernate.criterion.DetachedCriteria; 13 14 @SuppressWarnings("unchecked"