




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Struts框架和Hibernate框架的整合1、首先寫一個(gè)student的實(shí)體類,命名為:StudentEntity.javapackage com.liu.student.entity;/* * 數(shù)據(jù)庫表的映射實(shí)體類文件 * author Calasin */public class StudentEntity private String s_id;/ 學(xué)號private String s_name;/ 姓名private int s_age;/ 年齡public String getS_id() return s_id;public void setS_id(String s_id)
2、this.s_id = s_id;public String getS_name() return s_name;public void setS_name(String s_name) this.s_name = s_name;public int getS_age() return s_age;public void setS_age(int s_age) this.s_age = s_age;public String toString() return "StudentEntity s_id=" + s_id + ", s_name=" + s_
3、name+ ", s_age=" + s_age + ""2、接著寫student實(shí)體類StudentEntity的配置文件:StudentEntity.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-/Hibernate/Hibernate Mapping DTD 3.0/EN""<hibernate-mapping><!- name屬性:對應(yīng)實(shí)體類的地址,table屬性:對應(yīng)數(shù)據(jù)庫的表
4、名稱(對應(yīng)數(shù)據(jù)庫表是什么名字,這里就是寫什么名字) -><class name="com.liu.student.entity.StudentEntity" table="student"><!- name對應(yīng)的是java,column對應(yīng)的是數(shù)據(jù)庫,但是一般兩個(gè)都寫成一樣,這樣可以避免出錯(cuò) name屬性:主鍵id,對應(yīng)的是java中的類中的屬性, column屬性:對應(yīng)數(shù)據(jù)庫中的主鍵id -><id name="s_id" column="s_id"><!- 主鍵生
5、成策略:uuid 32位隨機(jī)的字符串 -><generator class="uuid"></generator></id><!- name屬性:對應(yīng)java中的實(shí)體類屬性,column屬性:對應(yīng)的數(shù)據(jù)庫中的字段名稱,type屬性:表示java中的數(shù)據(jù)類型,默認(rèn)可以不添加 -><property name="s_name" column="s_name" type="java.lang.String"></property><pro
6、perty name="s_age" column="s_age"></property></class></hibernate-mapping>3、接下來寫實(shí)體類的Action:StudentActionpackage com.liu.student.action;import java.util.List;import com.liu.student.entity.StudentEntity;import com.liu.student.service.StudentService;import com.li
7、u.student.service.StudentServiceImpl;import com.opensymphony.xwork2.ActionSupport;/* * 與jsp頁面交互,完成數(shù)據(jù)傳遞 * author Calasin */public class StudentAction extends ActionSupport private List studentList;/創(chuàng)建一個(gè)List類型的學(xué)生列表private StudentService studentService = new StudentServiceImpl();private StudentEntity s
8、tudentEntity;/* * 查詢學(xué)生表的列表信息 * * return */public String studentList() studentList = studentService.getStudentList();return "studentList"public String updPage() studentEntity = studentService.getStudentEntity(studentEntity.getS_id();return "updPage"public String upd() studentServi
9、ce.update(studentEntity);return "upd"public List getStudentList() return studentList;public void setStudentList(List studentList) this.studentList = studentList;public StudentEntity getStudentEntity() return studentEntity;public void setStudentEntity(StudentEntity studentEntity) this.stude
10、ntEntity = studentEntity;4、接下來寫提供數(shù)據(jù)庫的接口interface: Dao.javapackage com.liu.dao;import java.util.List;/* * 提供數(shù)據(jù)庫接口 * author Calasin *1總體設(shè)計(jì):設(shè)計(jì)Student對象及相關(guān)實(shí)體配置文件,工具類(得到一個(gè)Session對象), * StudentDao接口(實(shí)現(xiàn)此接口即以操作數(shù)據(jù)庫),下面代碼用"Dao"代替,編寫主配置文件,編寫測試類。 *2StudentDao的設(shè)計(jì),最初打算設(shè)計(jì)成通用Object的操作, * 后來發(fā)現(xiàn)它的Session對象操作
11、都要傳遞一個(gè)對象,就設(shè)計(jì)成如下形式。內(nèi)容如下: */public interface Dao public List getList(String hql);/查看學(xué)生的列表信息public void add(Object obj);/增加public void update(Object obj);/修改public void delete(Object obj);/刪除public Object getObj(Class cls, String id);/獲取單一實(shí)體類對象5、接下來寫接口實(shí)現(xiàn)類的具體操作方法DaoImpl,實(shí)現(xiàn)Dao接口package com.liu.dao;import
12、 java.util.List;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;/* * Dao接口實(shí)現(xiàn)類,具體的操作,主要工作由Hibernate來完成 * 以下幾點(diǎn)需要注意:導(dǎo)包:Hibernate包,數(shù)據(jù)庫包;改寫配置文件;查詢方法的設(shè)計(jì);注意事務(wù),特別是“增刪改”要注意事務(wù)。 * author Calasin */public class DaoImpl implements
13、 Dao private Session session;public DaoImpl() / 提供一個(gè)空參構(gòu)造/ 首先構(gòu)建一個(gè)Configuration對象,可以調(diào)用configure()方法Configuration cfg = new Configuration().configure();/ 再用這個(gè)對象構(gòu)建一個(gè)SessionFactory對象,才是真正意義上可操控的實(shí)例對象SessionFactory sessionFactory = cfg.buildSessionFactory();/ 然后在用這個(gè)對象來構(gòu)建一個(gè)SessionFactory對象session = sessionF
14、actory.openSession();/ 增加public void add(Object obj) / TODO Auto-generated method stub/ 刪除public void delete(Object obj) / TODO Auto-generated method stub/* * 查詢列表方法(通用查詢) */public List getList(String hql) / TODO Auto-generated method stubtry session.beginTransaction();/ 開啟事務(wù)Query query = session.cr
15、eateQuery(hql);List list = query.list();session.getTransaction().commit();/ 提交return list; catch (Exception e) / TODO: handle exceptionreturn null; finally session.close();/* * 獲取單一實(shí)體類對象 */public Object getObj(Class cls, String id) try session.beginTransaction();Object object = session.get(cls, id);
16、session.getTransaction().commit();return object; catch (Exception e) / TODO: handle exceptionreturn null; finally session.close();/* * 修改單一實(shí)體類對象 */public void update(Object obj) / TODO Auto-generated method stubtry session.beginTransaction();session.update(obj);session.getTransaction().commit(); cat
17、ch (Exception e) / TODO: handle exception finally session.close();6、接下來寫service層:StudentServicepackage com.liu.student.service;import java.util.List;import com.liu.student.entity.StudentEntity;public interface StudentService public List getStudentList();/查public void add (StudentEntity studentEntity
18、);/增public void update(StudentEntity studentEntity);/改public void delete(StudentEntity studentEntity);/刪public StudentEntity getStudentEntity(String s_id);7、接下來寫service層:StudentServiceImplpackage com.liu.student.service;import java.util.List;import com.liu.dao.Dao;import com.liu.dao.DaoImpl;import c
19、om.liu.student.entity.StudentEntity;/* * 針對數(shù)據(jù)庫中student表的操作 * * author Calasin * */public class StudentServiceImpl implements StudentService private Dao dao = new DaoImpl();public void add(StudentEntity studentEntity) / TODO Auto-generated method stubpublic void delete(StudentEntity studentEntity) /
20、TODO Auto-generated method stubpublic StudentEntity getStudentEntity(String s_id) / TODO Auto-generated method stubStudentEntity studentEntity = (StudentEntity) dao.getObj(StudentEntity.class, s_id);return studentEntity;public List getStudentList() / TODO Auto-generated method stubString hql = "
21、;from StudentEntity"List list = dao.getList(hql);return list;public void update(StudentEntity studentEntity) / TODO Auto-generated method stubdao.update(studentEntity);8、配置文件config介紹(hibernate.cfg.xml和struts.xml)Hibernate.cfg.xml介紹如下:<!DOCTYPE hibernate-configuration PUBLIC"-/Hibernate/
22、Hibernate Configuration DTD 3.0/EN""<hibernate-configuration><session-factory><property name="hibernate.connection.url">jdbc:mysql:/localhost:3306/test?characterEncoding=utf-8</property><property name="hibernate.connection.driver_class">com
23、.mysql.jdbc.Driver</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123456</property><!- 數(shù)據(jù)庫方言 -><property name="hibernate.dialect">org.hibernate.dialect.MySQ
24、LDialect</property><property name="hibernate.show_sql">true</property><mapping resource="com/liu/student/entity/StudentEntity.hbm.xml"/></session-factory></hibernate-configuration>Struts.xml介紹如下:<?xml version="1.0" encoding="UTF-8" ?&
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 軟件無償借用合同協(xié)議
- 道路黑化工程合同協(xié)議
- 農(nóng)村生態(tài)環(huán)境保護(hù)與農(nóng)業(yè)發(fā)展協(xié)同協(xié)議
- 車隊(duì)用工協(xié)議書范本
- 瀝青環(huán)保協(xié)議書
- 無權(quán)處分協(xié)議書
- 實(shí)驗(yàn)儀器設(shè)備維修維護(hù)協(xié)議
- 車輛裝潢招標(biāo)合同協(xié)議
- 沙場干股協(xié)議書
- 遠(yuǎn)程影像會診協(xié)議合同
- 預(yù)防老年人癡呆
- 三年級信息科技第23課《分解描述問題》教學(xué)設(shè)計(jì)、學(xué)習(xí)任務(wù)單及課后練習(xí)
- 數(shù)據(jù)庫應(yīng)用技術(shù)-第三次形考作業(yè)(第10章~第11章)-國開-參考資料
- 設(shè)備調(diào)試工作流程
- 養(yǎng)老護(hù)理員的禮儀培訓(xùn)課件
- 農(nóng)業(yè)水利工程基礎(chǔ)知識單選題100道及答案
- 2024江蘇南通醋酸纖維有限公司第二批次招聘33人筆試參考題庫附帶答案詳解
- 四川樂山歷年中考語文現(xiàn)代文閱讀真題37篇(截至2024年)
- 護(hù)理一病一品匯報(bào)
- 機(jī)器學(xué)習(xí)與非線性方程-深度研究
- 2023年小學(xué)科學(xué)實(shí)驗(yàn)知識競賽試題庫含答案
評論
0/150
提交評論