Struts框架和Hibernate框架的整合完整版介紹_第1頁
Struts框架和Hibernate框架的整合完整版介紹_第2頁
Struts框架和Hibernate框架的整合完整版介紹_第3頁
Struts框架和Hibernate框架的整合完整版介紹_第4頁
Struts框架和Hibernate框架的整合完整版介紹_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、Struts框架和Hibernate框架的整合1、首先寫一個student的實體類,命名為:StudentEntity.javapackage com.liu.student.entity;/* * 數(shù)據(jù)庫表的映射實體類文件 * author Calasin */public class StudentEntity private String s_id;/ 學號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實體類StudentEntity的配置文件:StudentEntity.hbm.xml<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-/Hibernate/Hibernate Mapping DTD 3.0/EN""<hibernate-mapping><!- name屬性:對應(yīng)實體類的地址,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ù)庫,但是一般兩個都寫成一樣,這樣可以避免出錯 name屬性:主鍵id,對應(yīng)的是java中的類中的屬性, column屬性:對應(yīng)數(shù)據(jù)庫中的主鍵id -><id name="s_id" column="s_id"><!- 主鍵生

5、成策略:uuid 32位隨機的字符串 -><generator class="uuid"></generator></id><!- name屬性:對應(yīng)java中的實體類屬性,column屬性:對應(yīng)的數(shù)據(jù)庫中的字段名稱,type屬性:表示java中的數(shù)據(jù)類型,默認可以不添加 -><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、接下來寫實體類的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)建一個List類型的學生列表private StudentService studentService = new StudentServiceImpl();private StudentEntity s

8、tudentEntity;/* * 查詢學生表的列表信息 * * 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è)計:設(shè)計Student對象及相關(guān)實體配置文件,工具類(得到一個Session對象), * StudentDao接口(實現(xiàn)此接口即以操作數(shù)據(jù)庫),下面代碼用"Dao"代替,編寫主配置文件,編寫測試類。 *2StudentDao的設(shè)計,最初打算設(shè)計成通用Object的操作, * 后來發(fā)現(xiàn)它的Session對象操作

11、都要傳遞一個對象,就設(shè)計成如下形式。內(nèi)容如下: */public interface Dao public List getList(String hql);/查看學生的列表信息public void add(Object obj);/增加public void update(Object obj);/修改public void delete(Object obj);/刪除public Object getObj(Class cls, String id);/獲取單一實體類對象5、接下來寫接口實現(xiàn)類的具體操作方法DaoImpl,實現(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接口實現(xiàn)類,具體的操作,主要工作由Hibernate來完成 * 以下幾點需要注意:導包:Hibernate包,數(shù)據(jù)庫包;改寫配置文件;查詢方法的設(shè)計;注意事務(wù),特別是“增刪改”要注意事務(wù)。 * author Calasin */public class DaoImpl implements

13、 Dao private Session session;public DaoImpl() / 提供一個空參構(gòu)造/ 首先構(gòu)建一個Configuration對象,可以調(diào)用configure()方法Configuration cfg = new Configuration().configure();/ 再用這個對象構(gòu)建一個SessionFactory對象,才是真正意義上可操控的實例對象SessionFactory sessionFactory = cfg.buildSessionFactory();/ 然后在用這個對象來構(gòu)建一個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();/* * 獲取單一實體類對象 */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();/* * 修改單一實體類對象 */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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論