码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate的增删改查

时间:2016-01-14 16:06:08      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

一、搭建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 }

 

  

Hibernate的增删改查

标签:

原文地址:http://www.cnblogs.com/liyuchen/p/5130209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!