-
Hibernate(2)-- 第一个Hibernate法度
添加时间:2013-5-23 点击量:一、筹办工作
1.1 开辟景象
开辟对象:Eclipse
数据库: sql server 2005
1.2 测试景象
二、开辟流程
2.1 创建测试表
2.2 编写实体类及映射文件
2.3 设备Hibernate.cfg.xml 文件
2.4 编写Dao文件
2.5 编写Hibernate 的操纵对象类。
2.5 应用Junit 进行单位测试
三、法度开辟
3.1 创建数据库测试表 Person
在SqlServer数据中,对于Hibernate和数据库的映射关系中,只有datetime和二进制斗劲特别。所以在表平分别创建了响应的字段
CREATE TABLE person(
id [int] IDENTITY(1,1) NOT NULL,
pname varchar(20) ,
pwd varchar(20) ,
registdate datetime, --用于测试Datetime类型
pfile image --用于测试二进制文件
)
View Code
3.2 创建实体类 和 映射文件 Person.java
实体类
1 package msj.system.model;
2
3 import java.sql.Date;
4
5 public class Person {
6 private int id;
7 private String name;
8 private String pwd;
9 private Date registDate;
10 private byte pfile;
11
12 public int getId() {
13 return id;
14 }
15 public void setId(int id) {
16 this.id = id;
17 }
18 public String getName() {
19 return name;
20 }
21 public void setName(String name) {
22 this.name = name;
23 }
24 public String getPwd() {
25 return pwd;
26 }
27 public void setPwd(String pwd) {
28 this.pwd = pwd;
29 }
30 public Date getRegistDate() {
31 return registDate;
32 }
33 public void setRegistDate(Date registDate) {
34 this.registDate = registDate;
35 }
36 public byte getPfile()
37 {
38 return pfile;
39 }
40 public void setPfile(byte pfile)
41 {
42 this.pfile = pfile;
43 }
44
45
46 }
View Code
映射文件(person.hbm.xml)
<?xml version=1.0 encoding=UTF-8?>
<!DOCTYPE hibernate-mapping PUBLIC
-//Hibernate/Hibernate Mapping DTD 3.0//EN
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd >
<hibernate-mapping>
<class name=msj.system.model.Person table=person>
<id name=id column=id type=int>
<generator class=identity></generator>
</id>
<property name=name column=pname type=string/>
<property name=pwd column=pwd type=string/>
<property name=registDate column=registDate type=date/>
<property name=pfile column=pfile type=binary></property>
</class>
</hibernate-mapping>
View Code
注释:
3.2.1: <class name=msj.system.model.Person table=person>
本文件把实体类 msj.system.model.Person 和数据库中的表person 接洽关系。
3.2.1.1: Name=”msj.system.model.Person”
问:本xml文件映射的是哪个实体对象?
答:此处必须是包名+类名(msj.system.model.Person)
3.2.1.2: Table=”person”
问:映射的实体对象对应数据库中哪个表?
答:数据库中的person表
3.2.2: <id name=id column=id type=int>
实体类中的id属性 对应数据库主键字段id,其类型为int
3.2.2.1 : name=id
问:主键对应实体类中的那个属性?
答:对应实体类Person中的id属性
3.2.2.2:column=id type=int
问:实体类中属性对应数据库那个字段?
答:对应数据库字段 id 其类型为int
3.2.3 : <generator class=identity>
主键的生陈规矩为自增长类型
可以参考mapping 文件具体映射关系表
3.2.4 <property name=name column=pname type=string/>
实体对象Person中的name属性 对应数据库中的pname属性,类型为字符串。
3.3 设备hibernate.cfg.mxl 中增长数据连接
把方才建树的person.hbm.xml 参加到hibernate文件中
<?xml version=1.0 encoding=UTF-8?>
<!DOCTYPE hibernate-configuration PUBLIC
-//Hibernate/Hibernate Configuration DTD 3.0//EN
http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd>
<hibernate-configuration>
<session-factory>
<property name=hibernate.connection.driver_class>com.microsoft.sqlserver.jdbc.SQLServerDriver </property>
<property name=hibernate.connection.url>jdbc:sqlserver://localhost:1433;databaseName=hibernate</property>
<property name=hibernate.connection.username>sa </property>
<property name=hibernate.connection.password>zz</property>
<property name=hibernate.dialect>org.hibernate.dialect.SQLServerDialect</property>
<property name=hibernate.show_sql>true </property>
<mapping resource=Person.hbm.xml/>
</session-factory>
</hibernate-configuration>
View Code
3.4 建树Dao文件
注:
1 遵守分层原则,Dao文件处于数据库操纵层,其首要完成数据库的增删改操纵。
2 遵守架构设计原则,我们这里采取接口设计原则。
(若是仅仅为了进修,你可以直接建树dao操纵类,而无需进行接口设计。)
dao接口定义 PersonDao
1 package msj.system.dao;
2
3 import java.util.List;
4 import msj.system.model.Person;
5
6 public interface PersonDao {
7 public Person GetSingle(int id); //获取单个文件
8 public List<Person> GetAll(); //获取全部文件
9 public void Save(Person person);
10 public void Update(Person person);
11 public void Delete(Person person);
12 }
View Code
Dao 实现类 PersonDaoImpl
1 package msj.system.dao.impl;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.hibernate.Query;
7 import org.hibernate.Session;
8 import org.hibernate.Transaction;
9
10 import msj.system.dao.PersonDao;
11 import msj.system.model.Person;
12 import msj.util.HibernateUtil;
13
14 public class PersonDaoImpl implements PersonDao {
15
16 @Override
17 public Person GetSingle(int id) {
18 Person t = null;
19 Session session = HibernateUtil.OpenSession();
20 try {
21 t = (Person) session.get(Person.class, id);
22 } catch (Exception Ex) {
23 Ex.printStackTrace();
24 } finally {
25 HibernateUtil.CloseSession(session);
26 }
27
28 return t;
29 }
30
31 @Override
32 public List<Person> GetAll() {
33
34 List<Person> list = new ArrayList<Person>();
35 Session session = HibernateUtil.OpenSession();
36 try {
37 list = (List<Person>) session.createQuery( Person).list();
38 } catch (Exception Ex) {
39 Ex.printStackTrace();
40 } finally {
41 HibernateUtil.CloseSession(session);
42 }
43
44 return list;
45 }
46
47 @Override
48 public void Save(Person person) {
49
50 Session session = HibernateUtil.OpenSession();
51 Transaction tx = session.beginTransaction();
52 try {
53 session.save(person);
54 tx.commit();
55 } catch (Exception Ex) {
56 tx.rollback();
57 Ex.printStackTrace();
58 } finally {
59 HibernateUtil.CloseSession(session);
60 }
61 }
62
63 @Override
64 public void Update(Person person) {
65
66 Session session = HibernateUtil.OpenSession();
67 Transaction tx = session.beginTransaction();
68 try {
69 session.(person);
70 tx.commit();
71 } catch (Exception Ex) {
72 tx.rollback();
73 Ex.printStackTrace();
74 } finally {
75 HibernateUtil.CloseSession(session);
76 }
77 }
78
79 @Override
80 public void Delete(Person person) {
81
82 Session session = HibernateUtil.OpenSession();
83 Transaction tx = session.beginTransaction();
84 try {
85 session.(person);
86 tx.commit();
87 } catch (Exception Ex) {
88 tx.rollback();
89 Ex.printStackTrace();
90 } finally {
91 HibernateUtil.CloseSession(session);
92 }
93 }
94
95 }
View Code
3.5 编写Hibernate 的操纵对象 HibernateUtil
1 package msj.util;
2
3 import org.hibernate.Session;
4 import org.hibernate.SessionFactory;
5 import org.hibernate.cfg.Configuration;
6
7 public class HibernateUtil {
8 private static SessionFactory sessionfactory = null ;
9
10 static{
11 try{
12 sessionfactory = new Configuration().configure().buildSessionFactory();
13 }catch(Exception Ex)
14 {
15 Ex.printStackTrace();
16 }
17 }
18
19 public static Session OpenSession()
20 {
21 return sessionfactory.openSession();
22 }
23
24 public static void CloseSession(Session session)
25 {
26 session.close();
27 }
28 }
View Code
3.6 编写测试类
1 package msj.system.dao;
2
3 import java.io.FileInputStream;
4 import java.io.InputStream;
5 import java.util.List;
6
7 import msj.system.dao.impl.PersonDaoImpl;
8 import msj.system.model.Person;
9
10 import org.junit.AfterClass;
11 import org.junit.Assert;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14
15 /
16 PersonDao 测试类
17
18 /
19 public class PersonDaoTest {
20
21 private static PersonDao dao = null;
22 private Person t = null;
23
24 @BeforeClass
25 public static void setUpBeforeClass() throws Exception {
26 try {
27 dao = new PersonDaoImpl();
28
29 } catch (Exception Ex) {
30 Ex.printStackTrace();
31 }
32 }
33
34 /
35 @throws java.lang.Exception
36 /
37 @AfterClass
38 public static void tearDownAfterClass() throws Exception {
39 try {
40 dao = null;
41 } catch (Exception Ex) {
42 Ex.printStackTrace();
43 }
44 }
45
46 /
47 获取单个对象测试
48 /
49 @Test
50 public void testGetSingle() {
51 try {
52 List<Person> tList = dao.GetAll();
53 PersonDao dao = new PersonDaoImpl();
54 Person tOld = null;
55 if (tList.size() <= 0)
56 {
57 Assert.fail(数据库中没有记录);
58 }
59 else
60 {
61 tOld = tList.get(0);
62 t = dao.GetSingle(tOld.getId());
63
64 //测出仅供打印出文件的存取是否正确,真正测试中可以不存在
65 byte[] b = new byte[t.getPfile().length];
66 b = t.getPfile();
67
68 String str = new String(b,gbk);
69 System.out.print(str);
70 }
71 } catch (Exception Ex) {
72 Ex.printStackTrace();
73 Assert.fail(Ex.getMessage());
74 }
75
76 Assert.assertNotNull(t);
77 }
78
79 /
80 获取全部测试
81 /
82 @Test
83 public void testGetAll() {
84 List<Person> list = null;
85 try {
86 list = dao.GetAll();
87 } catch (Exception Ex) {
88 Ex.printStackTrace();
89 Assert.fail(Ex.getMessage());
90 }
91
92 Assert.assertNotNull(list);
93 }
94
95 /
96 新增测试
97 /
98 @Test
99 public void testSave() {
100
101 try {
102 Person p = new Person();
103 p.setName(001);
104 p.setPwd(001);
105 p.setRegistDate(new java.sql.Date(new java.util.Date().getTime()));
106
107 InputStream is = new FileInputStream(c:/test.txt);
108 int count = is.available();
109 byte[] b = new byte[count];
110 is.read(b);
111 p.setPfile(b);
112 is.close();
113
114 dao.Save(p);
115 } catch (Exception Ex) {
116 Ex.printStackTrace();
117 Assert.fail(Ex.getMessage());
118 }
119 }
120
121 /
122 批改测试
123 /
124 @Test
125 public void testUpdate() {
126 try {
127 List<Person> tList = dao.GetAll();
128 PersonDao dao = new PersonDaoImpl();
129 Person pOld = null;
130 String test = ;
131 if (tList.size() <= 0)
132 {
133 Assert.fail(数据库中没有记录);
134 }
135 else
136 {
137 pOld = tList.get(0);
138 Person p = dao.GetSingle(pOld.getId());
139
140 test = p.getName() + 批改;
141 p.setName(test);
142 dao.Update(p);
143
144 p = dao.GetSingle(pOld.getId());
145
146 Assert.assertNotSame(没有批改成功, test, p.getName());
147 }
148
149 } catch (Exception Ex) {
150 Ex.printStackTrace();
151 Assert.fail(Ex.getMessage());
152 }
153
154 }
155
156 /
157 删除测试
158 /
159 @Test
160 public void testDelete() {
161
162 try {
163 List<Person> tList = dao.GetAll();
164 PersonDao dao = new PersonDaoImpl();
165 Person pOld = null;
166 String test = ;
167 if (tList.size() <= 0)
168 {
169 Assert.fail(数据库中没有记录);
170 }
171 else
172 {
173 pOld = tList.get(0);
174 Person p = dao.GetSingle(pOld.getId());
175 dao.Delete(p);
176 p = dao.GetSingle(pOld.getId());
177 Assert.assertTrue(数据没有删除对象, p == null);
178 }
179
180 } catch (Exception Ex) {
181 Ex.printStackTrace();
182 Assert.fail(Ex.getMessage());
183 }
184
185 }
186
187 }
View Code
至此、本实例停止。
第一个hibernate法度 源码
参考:景象设备篇 http://www.cnblogs.com/msjqd/articles/3085704.html
