欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate 的介绍与编写的程序

程序员文章站 2022-06-03 21:22:03
...

1 简答题(写概念,思原理)
1.1 Hibernate框架作用
参考答案:
hibernate是一个数据访问框架(持久层框架),在项目中利用Hibernate框架可以实现对数据库的增删改查操作,为业务层构建一个持久层
1.2 Hibernate框架原理–(ORM)
参考答案:
ORM–Object Relation Mapping,对象关系映射.
主要思想:将Java对象与关系表进行自动映射,这样可以将对象直接更新到数据库;查询时,可以自动将数据表记录封装成Java对象.
1.3 Hibernate主要的体系结构
参考答案:
a.实体类(Xxx),可以有n个
与数据表对应,用于封装数据表的一行记录
b.XML映射文件(Xxx.hbm.xml),可以有n个
用于描述实体类与数据表之间的对应关系;类属性与表字段之间的对应关系。
c.主配置文件(hibernate.cfg.xml),可以有1个
用于指定连接数据库的参数,框架参数等
1.4 Hibernate开发的基本步骤
参考答案:
a.引入hibernate框架开发包
b. 添加Hibernate主配置文件
c. 根据数据表,编写实体类,映射文件
d. 利用Hibernate API操作实体对象
2 填空题(读代码、加注释)
2.1 阅读下面的代码,并在指定位置添加注释。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
  3. <!—此句代码的作用是( 将类Fee与数据库表COST做映射 )–>
  4.  <class name="com.tarena.netctoss.pojo.Fee" table="COST">
    
  5.   <!—此句代码的作用是( Fee类的id属性和数据库表的ID列做映射 )-->
    
  6.      <id name="id" column="ID" type="integer">
    
  7.       <!—此句代码的作用是( 使用序列作为主键的生产方式 )-->
    
  8.         <generator class="sequence">
    
  9.             <param name="sequence">FEE_SEQ</param>
    
  10.         </generator>
    
  11.     </id>
    
  12.     <property name="feeName" column="NAME" 
    
  13.         type="string">
    
  14.     </property>
    
  15.     <property name="baseDuration" column="BASE_DURATION" 
    
  16.         type="integer">
    
  17.     </property>
    
  18.     <property name="baseCost" column="BASE_COST" 
    
  19.         type="float">
    
  20.     </property>
    
  21.     <property name="unitCost" column="UNIT_COST" 
    
  22.         type="float">
    
  23.     </property>
    
  24.     <property name="status" column="STATUS" 
    
  25.         type="string">
    
  26.     </property>
    
  27.     <property name="descr" column="DESCR" 
    
  28.         type="string">
    
  29.     </property>
    
  30.     <property name="costType" column="COST_TYPE" 
    
  31.         type="string">
    
  32.     </property>
    
  33.     <property name="createTime" column="CREATIME" 
    
  34.         type="date">
    
  35.     </property>
    
  36.     <property name="startTime" column="STARTIME" 
    
  37.         type="date">
    
  38.     </property>
    
  39. </class>
    

3 编程题(改代码,写代码)
3.1 重构“NETCTOSS登录检查拦截器”案例
重载Struts2的Day05的“NETCTOSS登录检查拦截器”案例,使用Hibernate实现修改和删除功能。
参考答案:
1.按照Struts2的Day02的“NETCTOSS资费的分页显示”案例中,拷贝工程的方法,拷贝struts05_2创建hibernate01_3工程。如图-4,图-5所示。

图- 4

图- 5
2.创建Fee.hbm.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd”>
  3.  <class name="com.tarena.netctoss.pojo.Fee" table="COST">
    
  4.      <id name="id" column="ID" type="integer">
    
  5.          <generator class="sequence">
    
  6.              <param name="sequence">FEE_SEQ</param>
    
  7.          </generator>
    
  8.     </id>
    
  9.     <property name="feeName" column="NAME" 
    
  10.         type="string">
    
  11.     </property>
    
  12.     <property name="baseDuration" column="BASE_DURATION" 
    
  13.         type="integer">
    
  14.     </property>
    
  15.     <property name="baseCost" column="BASE_COST" 
    
  16.         type="float">
    
  17.     </property>
    
  18.     <property name="unitCost" column="UNIT_COST" 
    
  19.         type="float">
    
  20.     </property>
    
  21.     <property name="status" column="STATUS" 
    
  22.         type="string">
    
  23.     </property>
    
  24.     <property name="descr" column="DESCR" 
    
  25.         type="string">
    
  26.     </property>
    
  27.     <property name="costType" column="COST_TYPE" 
    
  28.         type="string">
    
  29.     </property>
    
  30.     <property name="createTime" column="CREATIME" 
    
  31.         type="date">
    
  32.     </property>
    
  33.     <property name="startTime" column="STARTIME" 
    
  34.         type="date">
    
  35.     </property>
    
  36. </class>
    

3.创建hibernate.cfg.xml
40. <?xml version='1.0' encoding='UTF-8'?>
41.
44.
45.
46.
47.
48.
49.
50. org.hibernate.dialect.OracleDialect
51.
52.
53. jdbc:oracle:thin:@192.168.0.20:1521:tarena
54.
55. task
56. mistarena
57.
58. oracle.jdbc.driver.OracleDriver
59.
60.
61. true
62. true
63.
64.
65.
66.
4.修改FeeDAO,修改update方法,findById方法,deleteById方法,使用Hibernate操作数据库。
67. package com.tarena.netctoss.dao;
68.
69. import java.sql.Connection;
70. import java.sql.Date;
71. import java.sql.PreparedStatement;
72. import java.sql.ResultSet;
73. import java.util.ArrayList;
74. import java.util.List;
75.
76. import org.hibernate.Session;
77. import org.hibernate.Transaction;
78.
79. import com.tarena.netctoss.pojo.Fee;
80.
81. import com.tarena.netctoss.utils.ConnectionUtils;
82. import com.tarena.netctoss.utils.HibernateUitl;
83.
84. public class FeeDAO {
85. private static final String findAll =
86. “select ID, NAME, BASE_DURATION, BASE_COST, UNIT_COST, CREATIME, STARTIME, STATUS, DESCR,COST_TYPE from (” +
87. "select rownum r, ID, NAME, BASE_DURATION, BASE_COST, UNIT_COST, CREATIME, STARTIME, STATUS, DESCR,COST_TYPE from COST where rownum < ?) a " +
88. "where a.r > ? “;
89. private static final String getTotalPages = “select count(*) from COST”;
90. private static final String findByName = “select ID, NAME, BASE_DURATION, BASE_COST, UNIT_COST, CREATIME, STARTIME, STATUS, DESCR,COST_TYPE from COST where NAME=?”;
91. // private static final String findById = “select ID, NAME, BASE_DURATION, BASE_COST, UNIT_COST, CREATIME, STARTIME, STATUS, DESCR,COST_TYPE from COST where ID=?”;
92. // private static final String deleteById = “delete from COST where ID=?”;
93. // private static final String update =“update COST set NAME=?,BASE_DURATION=?,BASE_COST=?,UNIT_COST=?,”
94. // + " DESCR=?,COST_TYPE=? where ID=?”;
95.
96. public void update(Fee fee){
97. Session session = HibernateUitl.getSession();
98. Transaction tx = session.beginTransaction();
99. fee.setCreateTime(new Date(System.currentTimeMillis()));
100. session.update(fee);
101. tx.commit();
102. session.close();
103. }
104.
105. public Fee findById(int id){
106. Session session = HibernateUitl.getSession();
107. Fee fee = (Fee)session.get(Fee.class, id);
108. session.close();
109. return fee;
110. }
111.
112. public Fee findByName(String name){
113. Connection con = null;
114. PreparedStatement stmt = null;
115. ResultSet rs = null;
116. try {
117. con = ConnectionUtils.openConnection();
118. stmt = con.prepareStatement(findByName);
119. stmt.setString(1, name);
120. rs = stmt.executeQuery();
121. Fee fee = null;
122. if (rs.next()) {
123. fee = new Fee();
124. fee.setId(rs.getInt(“ID”));
125. fee.setFeeName(rs.getString(“NAME”));
126. fee.setBaseDuration(rs.getInt(“BASE_DURATION”));
127. fee.setBaseCost(rs.getFloat(“BASE_COST”));
128. fee.setUnitCost(rs.getFloat(“UNIT_COST”));
129. fee.setCreateTime(rs.getDate(“CREATIME”));
130. fee.setStartTime(rs.getDate(“STARTIME”));
131. fee.setStatus(rs.getString(“STATUS”));
132. fee.setDescr(rs.getString(“DESCR”));
133. fee.setCostType(rs.getString(“COST_TYPE”));
134. }
135. return fee;
136. } catch (Exception e) {
137. e.printStackTrace();
138. throw new RuntimeException(e);
139. }finally{
140. ConnectionUtils.closeConnection();
141. }
142. }
143.
144. public void deleteById(int id){
145. //创建fee对象
146. Fee fee = new Fee();
147. fee.setId(id);
148. //删除
149. Session session = HibernateUitl.getSession();
150. Transaction tx = session.beginTransaction();
151. session.delete(fee);
152. tx.commit();
153. session.close();
154. }
155.
156. public int getTotalPages(int pageSize) {
157. Connection con = null;
158. PreparedStatement stmt = null;
159. ResultSet rs = null;
160. try {
161. con = ConnectionUtils.openConnection();
162. stmt = con.prepareStatement(getTotalPages);
163. rs = stmt.executeQuery();
164. rs.next();
165. int totalRows = rs.getInt(1);
166. if (totalRows % pageSize == 0) {
167. return totalRows / pageSize;
168. } else {
169. return totalRows / pageSize + 1;
170. }
171.
172. } catch (Exception e) {
173. e.printStackTrace();
174. throw new RuntimeException(e);
175. }finally{
176. ConnectionUtils.closeConnection();
177. }
178. }
179.
180.
181. public List findAll(int page,int pageSize) {
182. Connection con = null;
183. PreparedStatement stmt = null;
184. ResultSet rs = null;
185. try {
186. con = ConnectionUtils.openConnection();
187. stmt = con.prepareStatement(findAll);
188. int begin = (page-1)pageSize;
189. int end = page
pageSize+1;
190. stmt.setInt(1, end);
191. stmt.setInt(2, begin);
192. rs = stmt.executeQuery();
193. List list = new ArrayList();
194. while (rs.next()) {
195. Fee fee = new Fee();
196. fee.setId(rs.getInt(“ID”));
197. fee.setFeeName(rs.getString(“NAME”));
198. fee.setBaseDuration(rs.getInt(“BASE_DURATION”));
199. fee.setBaseCost(rs.getFloat(“BASE_COST”));
200. fee.setUnitCost(rs.getFloat(“UNIT_COST”));
201. fee.setCreateTime(rs.getDate(“CREATIME”));
202. fee.setStartTime(rs.getDate(“STARTIME”));
203. fee.setStatus(rs.getString(“STATUS”));
204. fee.setDescr(rs.getString(“DESCR”));
205. fee.setCostType(rs.getString(“COST_TYPE”));
206. list.add(fee);
207. }
208. return list;
209. } catch (Exception e) {
210. e.printStackTrace();
211. throw new RuntimeException(e);
212. }finally{
213. ConnectionUtils.closeConnection();
214. }
215.
216. }
217.
218. }
219.记记得关注哦