博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
20160419__第1课_第6课
阅读量:4499 次
发布时间:2019-06-08

本文共 5958 字,大约阅读时间需要 19 分钟。

1、

我封装的 操作Hibernate的类:

1 package hibernateOper; 2  3 import org.hibernate.SessionFactory; 4 import org.hibernate.cfg.Configuration; 5  6 @SuppressWarnings("deprecation") 7 final public class ThibernateOper 8 {
// ZC: 上面的"final",让别人无法再继承该类 9 public ThibernateOper()10 {}11 12 // 一般连接一个数据库,只用一个SessionFactory。13 private static SessionFactory FsessionFactory = null;14 static // 静态块,只会执行一次15 {16 // Configuration.configure() 中可以参入参数[hibernate.cfg.xml的文件名和路径]17 FsessionFactory = (new Configuration()).configure().buildSessionFactory();18 }19 20 public static SessionFactory GetSessionFactory()21 {22 return FsessionFactory;23 }24 25 public static void main(String[] args)26 {27 }28 }

 

2、

对Oracle的简单操作:

1 package test; 2  3 import java.util.Date; 4  5 import hibernateOper.ThibernateOper; 6  7 import org.hibernate.Session; 8 import org.hibernate.SessionFactory; 9 import org.hibernate.Transaction;10 11 import domain.Employee;12 13 public class TTest14 {15     public static void SimpleInsert()16     {17         SessionFactory sf = ThibernateOper.GetSessionFactory();18         Session session = sf.openSession();19         Transaction ts = session.beginTransaction();20         21         try22         {23             Employee e = new Employee();24             e.setName("zz");25             e.setEmail("zzz@163.com");26             e.setHiredate(new Date());27             28             session.save(e);29             ts.commit();30             session.close();31         }32         catch(Exception ex)33         {34             ts.rollback();35             if ( (session != null)&&(session.isOpen()) )36                 session.close();37             ex.printStackTrace();38         }39     }40     41     public static void SimpleUpdate()42     {43         SessionFactory sf = ThibernateOper.GetSessionFactory();44         Session session = sf.openSession();45         Transaction ts = session.beginTransaction();46         47         try48         {49             // 强转50             Employee e = (Employee)session.load(Employee.class, 1);51             e.setName("zxcvb");52             53             ts.commit();54             session.close();55         }56         catch(Exception ex)57         {58             ts.rollback();59             if ( (session != null)&&(session.isOpen()) )60                 session.close();61             ex.printStackTrace();62         }63     }64     65     public static void SimpleDelete()66     {67         SessionFactory sf = ThibernateOper.GetSessionFactory();68         Session session = sf.openSession();69         Transaction ts = session.beginTransaction();70         71         try72         {73             // 强转74             Employee e = (Employee)session.load(Employee.class, 2);75             e.setName("zxcvb");76             77             session.delete(e);78             ts.commit();79             session.close();80         }81         catch(Exception ex)82         {83             ts.rollback();84             if ( (session != null)&&(session.isOpen()) )85                 session.close();86             ex.printStackTrace();87         }88     }89     90     public static void main(String[] args)91     {92         //SimpleInsert();93         //SimpleUpdate();94         SimpleDelete();95     }96 97 }

 

 

 

 

 

3、

hibernate.cfg.xml

1 
2 5
6
7 8
9
10 org.hibernate.dialect.Oracle10gDialect11
12
13 jdbc:oracle:thin:@localhost:1521:orcl14
15
scott
16
tiger
17
18 oracle.jdbc.driver.OracleDriver19
20
21 OracleLocal22
23
true
24
true
25
26 27
28 29

 

4、

Employee.hbm.xml

1 
2 4
7
8
9
10
11
14
15
emp_seq16
17
18
19
20
21
22
23
24
25
26
27
28

 

5、

Employee.java

1 package domain; 2  3 import java.math.BigDecimal; 4 import java.util.Date; 5  6 /** 7  * Employee entity. @author MyEclipse Persistence Tools 8  */ 9 10 public class Employee implements java.io.Serializable {11 12     // Fields13 14     private static final long serialVersionUID = 1L;15     16     //private BigDecimal id;17     private Integer id;18     private String name;19     private String email;20     private Date hiredate;21 22     // Constructors23 24     /** default constructor */25     public Employee() {26     }27 28     /** full constructor */29     public Employee(Integer id, String name, String email, Date hiredate) {30         this.id = id;31         this.name = name;32         this.email = email;33         this.hiredate = hiredate;34     }35 36     // Property accessors37 38     public Integer getId() {39         return this.id;40     }41 42     public void setId(Integer id) {43         this.id = id;44     }45 46     public String getName() {47         return this.name;48     }49 50     public void setName(String name) {51         this.name = name;52     }53 54     public String getEmail() {55         return this.email;56     }57 58     public void setEmail(String email) {59         this.email = email;60     }61 62     public Date getHiredate() {63         return this.hiredate;64     }65 66     public void setHiredate(Date hiredate) {67         this.hiredate = hiredate;68     }69 70 }

 

6、

 

转载于:https://www.cnblogs.com/javaskill/p/5408208.html

你可能感兴趣的文章
(转)ApplicationDomain
查看>>
form注册表单圆角 demo
查看>>
python 学习之 函数参数和内置函数
查看>>
leetcode 1108
查看>>
PHP底层的运行机制与原理
查看>>
ERP笔记3-数据库的版本化
查看>>
编译原理的思维导图
查看>>
关于Spring boot 中application.yml配置文件没有小绿叶图标的问题
查看>>
JAVA vs C++之速度二
查看>>
main函数中如何等待协程运行完毕
查看>>
JS小问题之——缓冲运动
查看>>
C#导出EXCEL方法总结
查看>>
【poj3342】 Party at Hali-Bula
查看>>
SCM基础之组织结构设计
查看>>
「OC」@property @synthesize和id
查看>>
(爱加密系列教程六)Android代码注入大揭秘
查看>>
ERP程序开发中遇到的六种错误
查看>>
Hibernate_拦截器与日志文件
查看>>
2-Sixteenth Scrum Meeting-20151216
查看>>
Visual Studio 2015、2013、2012、2010、2008、2005各版本下载+有效密钥激活
查看>>