การรวมกรอบ SSH
คำนำ: บางคนบอกว่าเฟรมเวิร์กกระแสหลักยังคงเป็นที่นิยมในขณะนี้และ SSM ออกมาเป็นเวลานาน ฉันไม่คิดอย่างนั้น ทุกวันนี้หลาย บริษัท ยังคงใช้ SSH และหากพวกเขาเปลี่ยนเป็นกรอบหลักจะมีค่าใช้จ่าย ตัวอย่างเช่นในฟิลด์ไอทีทางการเงินขอแนะนำให้ใช้ไฮเบอร์เนตในเลเยอร์ฐานข้อมูลเพราะสามารถพัฒนาได้อย่างรวดเร็วเว้นแต่จะเป็นอินเทอร์เน็ต เนื่องจากเกี่ยวข้องกับการเกิดขึ้นพร้อมกันสูงเลเยอร์ฐานข้อมูลจึงใช้ mybatis ซึ่งมีประสิทธิภาพการโต้ตอบข้อมูลที่เร็วขึ้น ดังนั้น SSH จึงไม่สามารถเพิกเฉยได้
1. SSH คืออะไร
SSH เป็นเฟรมเวิร์กแบบบูรณาการของ Struts+Spring+Hibernate และปัจจุบันเป็นเฟรมเวิร์กโอเพ่นซอร์สยอดนิยมสำหรับเว็บแอปพลิเคชัน
ระบบที่รวมกรอบ SSH แบ่งออกเป็นสี่เลเยอร์ในแง่ของความรับผิดชอบ: เลเยอร์การนำเสนอเลเยอร์ลอจิกทางธุรกิจเลเยอร์การคงอยู่ของข้อมูลและเลเยอร์โมดูลโดเมนเพื่อช่วยให้นักพัฒนาสร้างเว็บแอปพลิเคชันที่มีโครงสร้างที่ชัดเจนการใช้ซ้ำที่ดีและการบำรุงรักษาที่สะดวกสบายในระยะสั้น ในหมู่พวกเขา struts ถูกใช้เป็นโครงสร้างพื้นฐานโดยรวมของระบบรับผิดชอบในการแยก MVC และในส่วนโมเดลของกรอบ Struts, การควบคุมการกระโดดทางธุรกิจโดยใช้กรอบการทำงานจำเพาะเพื่อให้การสนับสนุนเลเยอร์การคงอยู่ วิธีการเฉพาะคือ: ใช้วิธีการวิเคราะห์เชิงวัตถุเพื่อเสนอบางรุ่นตามความต้องการใช้โมเดลเหล่านี้เป็นวัตถุ Java พื้นฐานจากนั้นเขียนอินเทอร์เฟซ DAO (วัตถุการเข้าถึงข้อมูล) พื้นฐานและให้การใช้งาน DAO ของไฮเบอร์เนต คลาส DAO ที่ดำเนินการโดยสถาปัตยกรรมไฮเบอร์เนตใช้เพื่อตระหนักถึงการแปลงและการเข้าถึงระหว่างคลาส Java และฐานข้อมูล ในที่สุดฤดูใบไม้ผลิจัดการและจัดการ struts และไฮเบอร์เนต
---------- สารานุกรม Baidu
2. ชิ้นส่วนที่เกี่ยวข้องใน SSH
3. การปรับใช้อย่างรวดเร็วของสภาพแวดล้อม
ที่นี่เราใช้ตัวอย่างเล็ก ๆ เพื่อช่วยลูกค้าเพื่อแสดงให้เห็นถึงการรวมกันของ SSH
1. นำเข้าแพ็คเกจ JAR ที่จำเป็น
1). เฟรมเวิร์ก struts2
* struts-2.3.24/apps/struts2-blank/web-inf/lib/* jar-แพ็คเกจขวดทั้งหมดที่จำเป็นโดย struts2
* struts2-spring-plugin-2.3.24.jar --- struts2 รวมแพ็คเกจปลั๊กอินของสปริง
2). เฟรมเวิร์กไฮเบอร์เนต
* hibernate-release-5.0.7.final/lib/required/*.jar-แพ็คเกจ Jar ที่ต้องการโดย Hibernate Framework
* SLF4J-API-1.6.1.JAR-อินเตอร์เฟสบันทึก
* slf4j-log4j12-1.7.2.jar-การใช้งานบันทึก
* mysql-connector-java-5.1.7-bin.jar-แพ็คเกจไดรเวอร์ mysql
3). กรอบฤดูใบไม้ผลิ
* แพ็คเกจ IOC Core
* แพ็คเกจ AOP Core
* เทมเพลต JDBC และแพ็คเกจหลักธุรกรรม
* สปริงรวมแพ็คเกจทดสอบ Junit
* สปริงรวมแพ็คเกจแกนไฮเบอร์เนต
* Spring Integrates Struts2 Core Package
2. กำหนดค่ารหัสสปริงและ struts ใน web.xml
1) กำหนดค่าตัวกรอง Core Struts2
สิ่งนี้หมายถึงการสกัดกั้นทั้งหมด
<!-กำหนดค่าตัวกรองหลัก-> <filter> <filter-name> struts2 </filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter </filter-class> <url-pattern>/*</url-pattern> </filter-mapping>
2) กำหนดค่าสปริงฟัง
เมื่อบริการเริ่มต้นไฟล์การกำหนดค่าสปริงจะถูกโหลดก่อน
<!-กำหนดค่าฟังสำหรับ Framework Spring Framework Web-> <Listener> <Sistener-Lass> org.springFramework.web.context.context.contextloaderListener </listener-class> </listener>
3) กำหนดค่าเส้นทางการโหลดเริ่มต้น
<!-ผู้ฟังโหลดไฟล์ web-inf ตามค่าเริ่มต้น คุณต้องกำหนดค่าพารามิเตอร์เพื่อโหลดไฟล์ที่ระบุ-> <context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: ApplicationContext.xml </param-value> </context-param>
สรุป: รหัสทั้งหมดสำหรับ web.xml คือ
<!-กำหนดค่าผู้ฟังที่รวมเว็บใน Framework Spring-> <Listener> <Sistener-Lass> org.springFramework.web.context.context.contextloaderListener </listener-class> </listener> <! <param-Name> contextConfigLocation </param-name> <param-value> classpath: ApplicationContext.xml </param-value> </context-param> <!-กำหนดค่าตัวกรองหลัก-> <filter> <Tilter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter </filter-class> </filter> <filter-mapping>
2. เขียนไฟล์กำหนดค่าที่เกี่ยวข้องภายใต้ SRC
1) ฤดูใบไม้ผลิ: ApplicationContext.xml
นำเข้าข้อ จำกัด ที่เกี่ยวข้อง
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> </epeans>
2) ไฮเบอร์เนต: hibernate.cfg.xml
นำเข้าข้อ จำกัด ที่เกี่ยวข้องและกำหนดค่าฐานข้อมูล
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! DOCTYPE HIBERNATE-Configuration สาธารณะ "-// hibernate/hibernate การกำหนดค่า dtd 3.0 // en" "http://www.hibernate.org/dtd/hibernate <session-factory> <!-ต้องกำหนดค่า-> <property name = "hibernate.connection.driver_class"> com.mysql.jdbc.driver </คุณสมบัติ> <property name = "hibernate.connection.url"> jdbc: mysql: //192.168.174.174.174. name = "hibernate.connection.username"> root </property> <property name = "hibernate.connection.password"> root </คุณสมบัติ> <property name = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </คุณสมบัติ> <! name = "hibernate.format_sql"> true </property> <property name = "hibernate.hbm2ddl.auto"> อัปเดต </property> <!-กำหนดค่าพูลการเชื่อมต่อสำหรับ c3p0-> <property name = "การเชื่อมต่อ -> <!-ไฟล์การกำหนดค่าการแม็พ-> <การแม็พทรัพยากร = "com/clj/domain/customer.hbm.xml"/> </session-factory> </hibernate-configuration>
3) กำหนดค่า log4j.properties
### ข้อความบันทึกโดยตรงไปยัง stdout ### log4j.appender.stdout = org.apache.log4j.consoleAppenderlog4j.appender.stdout.target = System.errlog4j.appender.stdout.layout = org.apache.log4j.patternlayoutlog4j.aptdout %c {1}: %l - %m %n ### ข้อความโดยตรงไปยังไฟล์ mylog.log ### log4j.appender.file = org.apache.log4j.FileAppenderLog4j.appender.file.file = c/: mylog.loglog4j.appender.file.layout = org.aPache.log4j.patternlayoutlog4j.aphent.lay.lay %c {1}: %l - %m %n ### ตั้งค่าระดับบันทึก - สำหรับการบันทึกการบันทึก verbose เพิ่มเติม 'ข้อมูล' เป็น 'debug' ### log4j.rootlogger = ข้อมูล stdout4) struts2: struts.xml
นำเข้าข้อ จำกัด ที่เกี่ยวข้อง
<? xml version = "1.0" การเข้ารหัส = "UTF-8"?> <! Doctype struts สาธารณะ "-// Apache Software Foundation // dtd struts การกำหนดค่า 2.1 // en" "http://struts.apache.org/dtds/struts-2.3.dtd"
สรุป: ไฟล์กำหนดค่าที่ SRC ต้องการแสดงไว้ในรูป
3. กำหนดค่าเลเยอร์ DAO
กำหนดอินเทอร์เฟซและคลาสการใช้งาน
อินเทอร์เฟซสาธารณะ customerdao {โมฆะสาธารณะบันทึก (ลูกค้าลูกค้า);} Public Class CustomerDaoimpl ใช้ CustomerDao {โมฆะสาธารณะบันทึก (ลูกค้าลูกค้า) {}}4. กำหนดอินเทอร์เฟซเลเยอร์ธุรกิจและคลาสการใช้งาน
แพ็คเกจ com.clj.service; นำเข้า com.clj.domain.customer; อินเทอร์เฟซ Public CustomerService {โมฆะสาธารณะบันทึก (ลูกค้าลูกค้า);}แพ็คเกจ com.clj.service; นำเข้า org.springframework.transaction.annotation.transactional; นำเข้า com.clj.dao.customerdao; นำเข้า com.clj.domain.customer;/** * ผู้ดูแลระบบธุรกิจของลูกค้า -
5. กำหนดคลาส pojo
Hibernate ดำเนินการตารางฐานข้อมูลโดยการใช้งานคลาส POJO เพื่อให้ได้การทำแผนที่ความสัมพันธ์เชิงวัตถุ
แพ็คเกจ com.clj.domain; ลูกค้าระดับสาธารณะ {ส่วนตัว Long Cust_id; สตริงส่วนตัว cust_name; Private Long Cust_user_id; Private Long Cust_create_id; สตริงส่วนตัว cust_source; สตริงส่วนตัว cust_industry; สตริงส่วนตัว cust_level; สตริงส่วนตัว cust_linkman; สตริงส่วนตัว cust_phone; สตริงส่วนตัว cust_mobile; Public Long getCust_id () {return cust_id; } โมฆะสาธารณะ setcust_id (Long Cust_id) {this.cust_id = cust_id; } สตริงสาธารณะ getCust_Name () {return cust_name; } โมฆะสาธารณะ setCust_Name (สตริง cust_name) {this.cust_name = cust_name; } สาธารณะยาว getCust_USER_ID () {return cust_user_id; } โมฆะสาธารณะ setcust_user_id (Long Cust_user_id) {this.cust_user_id = cust_user_id; } สาธารณะยาว getCust_Create_id () {return cust_create_id; } โมฆะสาธารณะ setcust_create_id (Long cust_create_id) {this.cust_create_id = cust_create_id; } สตริงสาธารณะ getCust_Source () {return cust_source; } โมฆะสาธารณะ setCust_Source (สตริง cust_source) {this.cust_source = cust_source; } สตริงสาธารณะ getCust_Industry () {return cust_industry; } โมฆะสาธารณะ setcust_industry (สตริง cust_industry) {this.cust_industry = cust_industry; } สตริงสาธารณะ getCust_level () {return cust_level; } โมฆะสาธารณะ setCust_level (สตริง cust_level) {this.cust_level = cust_level; } สตริงสาธารณะ getCust_Linkman () {return cust_linkman; } โมฆะสาธารณะ setCust_linkman (สตริง cust_linkman) {this.cust_linkman = cust_linkman; } สตริงสาธารณะ getCust_phone () {return cust_phone; } โมฆะสาธารณะ setcust_phone (สตริง cust_phone) {this.cust_phone = cust_phone; } สตริงสาธารณะ getCust_Mobile () {return cust_mobile; } โมฆะสาธารณะ setcust_mobile (สตริง cust_mobile) {this.cust_mobile = cust_mobile; } @Override สตริงสาธารณะ toString () {return "ลูกค้า [cust_id =" + cust_id + ", cust_name =" + cust_name + ", cust_user_id =" + cust_user_id + ", cust_create_id =" cust_create_id + " cust_level = " + cust_level +", cust_linkman = " + cust_linkman +", cust_phone = " + cust_phone +", cust_mobile = " + cust_mobile +"] "; -6. กำหนด customer.hbm.xml
ไฟล์การกำหนดค่านี้เกี่ยวข้องกับคลาส POJO ของลูกค้า ไฟล์นี้จะต้องอยู่ภายใต้แพ็คเกจเดียวกับคลาส Pojo ของลูกค้า
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! Doctype Hibernate-Mapping สาธารณะ "-// hibernate/hibernate mapping dtd 3.0 // en" "http://www.hibernate.org/dtd/hibernate-mapping-3 name = "com.clj.domain.customer" table = "cst_customer"> <id name = "cust_id" คอลัมน์ = "cust_id"> <generator/> </id> <property name = "cust_name" คอลัมน์ = "cust_name"/> คอลัมน์ = "cust_create_id"/> <ชื่อทรัพย์สิน = "cust_source" คอลัมน์ = "cust_source"/> <ชื่อทรัพย์สิน = "cust_industry" คอลัมน์ = "cust_industry"/> <ชื่อทรัพย์สิน = "cust_level" คอลัมน์ = "cust_level"/> <property name = "cust_linkman name = "cust_phone" คอลัมน์ = "cust_phone"/> <property name = "cust_mobile" คอลัมน์ = "cust_mobile"/> </class> </hibernate-mapping>
แผนภาพการก่อสร้างโครงการ
4. การสาธิตการสาธิตเบื้องต้นเพื่อการอนุรักษ์ลูกค้า
ที่นี่ก่อนอื่นเรากำหนดเลเยอร์การคงอยู่ให้กับ Heibernate ชั้นธุรกิจไปยัง struts2 และอินสแตนซ์การสร้างถึงฤดูใบไม้ผลิ
1. กำหนดอินเทอร์เฟซเพื่อประหยัดลูกค้าและใช้แบบฟอร์มแบบฟอร์มเพื่อส่งข้อมูล
ตามชื่อโดเมนเราจะเห็นได้ว่าวิธีการของ Wildcard ของ struts2 ใช้สำหรับการเข้าถึง
<form id = form1 name = form1 action = "$ {pageContext.request.contextpath}/customer_add.action" วิธีการ = post> <!-ส่วนตารางที่ถูกละไว้-> </form>2. กำหนดค่าคำขอยอมรับใน struts.xml ข้ามไปยังการดำเนินการที่ระบุตามชื่อและวิธีการดำเนินการและดำเนินการวิธีการที่ระบุ
Spring Integrates Struts2 Method One: การดำเนินการได้รับการจัดการโดย Framework Struts2
* เนื่องจากแพ็คเกจ struts2-spring-plugin-2.3.24.jar มาพร้อมกับไฟล์การกำหนดค่า struts-plugin.xml รหัสต่อไปนี้รวมอยู่ในไฟล์กำหนดค่า
* <ค่าคงที่ = "structs.objectFactory" value = "Spring" /> เปิดค่าคงที่ หากเปิดค่าคงที่สามารถใช้ค่าคงที่ต่อไปนี้ได้
* struts.objectFactory.spring.autowire = ชื่อค่าคงที่นี้เป็นคลาสที่อนุญาตให้ดำเนินการในการประกอบวัตถุถั่วโดยอัตโนมัติ!
<? XML เวอร์ชัน = "1.0" การเข้ารหัส = "UTF-8"?> <! Doctype struts สาธารณะ "-// Apache Software Foundation // dtd struts การกำหนดค่า 2.1 // en" "http://struts.apache.org/dtds/struts-2.3.dtd" Extends = "struts-default" namespace = "/"> <!-กำหนดค่าการกระทำของลูกค้า-> <!-วิธีที่ 1: aciton ได้รับการจัดการโดย Framework Struts2-> <action name = "customer_*" method = "{1}"/> </pack> </struts>3. กำหนดค่าถั่วและธุรกรรมที่เกี่ยวข้องใน Spring ApplicationContext.xml
ที่นี่การใช้คุณสมบัติ IOC (การผกผัน) ในฤดูใบไม้ผลิงานในการสร้างอินสแตนซ์จะถูกส่งมอบให้กับการจัดการกรอบฤดูใบไม้ผลิ
<bean id = "customerervice"> <property name = "customerdao" ref = "customerdao"> </property> </ebean> <bean id = "customerdao"> <property name = "hibernatetemplate" ref = "hibernatetemplate"/> </ebean> </ebeans>
4. เขียนคลาสการใช้งาน Layer Layer Class ที่เกี่ยวข้อง
ที่นี่คลาสเทมเพลตที่จัดทำโดย hibernate ใช้เพื่อล้อมรอบเซสชันภายในเพื่อให้สามารถเรียกใช้วิธีการในเซสชั่น
/** * เลเยอร์การคงอยู่ * * @author Administrator * */Class Public CustomerDaoImpl ใช้ CustomerDao {// บันทึกข้อมูลลงในฐานข้อมูล (คลาสแม่แบบโทร (จัดทำโดย Hibernate, เซสชันที่ห่อหุ้มภายใน) โมฆะสาธารณะ sethibernatetemplate (hibernatetemplate hibernatetemplate) {this.hibernatetemplate = hibernatetemplate; } / *** บันทึกลูกค้า* / โมฆะสาธารณะบันทึก (ลูกค้าลูกค้า) {system.out.println ("เลเยอร์ถาวร: บันทึกลูกค้า"); hibernatetemplate (). บันทึก (ลูกค้า); -5. เขียนคลาสการใช้งานชั้นธุรกิจ
แพ็คเกจ com.clj.service; นำเข้า org.springframework.transaction.annotation.transactional; นำเข้า com.clj.dao.customerdao; นำเข้า com.clj.domain.customer;/** * ผู้ดูแลระบบธุรกิจของลูกค้า โมฆะสาธารณะ SetCustomerDao (CustomerDao CustomerDao) {this.customerdao = customerdao; } // ใช้เพื่อบันทึกโมฆะสาธารณะของลูกค้าบันทึก (ลูกค้าลูกค้า) {system.out.println ("เลเยอร์ธุรกิจ, บันทึกลูกค้า"); customerdao.save (ลูกค้า); -6. เขียนรหัสที่เกี่ยวข้องกับการกระทำ
นี่คือคลาสเทมเพลตของ struts2
แพ็คเกจ com.clj.web.action; นำเข้า org.apache.struts2.servletactionContext; นำเข้า org.springframework.web.context.webapplicationContext; นำเข้า org.springframework.web.context.support.webapplicationContext com.clj.service.customerservice; นำเข้า com.opensymphony.xwork2.actionsupport; นำเข้า com.opensymphony.xwork2.modeldriven;/** * ชั้นควบคุมของลูกค้า * @author ผู้ดูแลระบบลูกค้าใหม่ ลูกค้าสาธารณะ getModel () {ส่งคืนลูกค้า; } // ให้คุณลักษณะของสมาชิกบริการและจัดเตรียมวิธีการที่กำหนดลูกค้าบริการลูกค้าบริการลูกค้า; โมฆะสาธารณะ setCustomerservice (ลูกค้าบริการลูกค้า) {this.customerservice = customerservice; } / *** บันทึกลูกค้า* @return* / สตริงสาธารณะเพิ่ม () {system.out.println ("เว็บเลเยอร์, บันทึกลูกค้า"); // วิธีการที่ 1: สร้าง Web Factory (การดำเนินการถูกสร้างขึ้นโดย struts2) WebApplicationContext Context = WebApplicationContextUts.getWebapplicationContext (servletActionContext.getServletContext ()); CustomerService CS = (ลูกค้าบริการ) บริบท GetBean ("ลูกค้าบริการ"); // วิธีการโทร cs.save (ลูกค้า); ไม่กลับมา; -5. การรวมโครงการเพิ่มประสิทธิภาพโครงการ
1. การรวม Spring Struts2 วิธี 2: การดำเนินการได้รับการจัดการโดย Framework Spring
ใส่ไฟล์การกำหนดค่าการกำหนดค่าคลาสแอ็คชั่นที่เฉพาะ
<struts> <!-กำหนดค่าโครงสร้างแพ็คเกจ-> <package name = "crm" ขยาย = "struts-default" namespace = "/"> <!-กำหนดค่าการกระทำของลูกค้า-> <! จำเป็นต้องใช้ถั่วการกำหนดค่า srping บนแท็กคลาส-> <action name = "customer_*" method = "{1}"> </action> </package> </struts>2. กำหนดค่าคลาสแอ็คชั่นใน ApplicationContext.xml
หมายเหตุ: 1) Framework Spring สร้างการสร้างลูกค้าตามค่าเริ่มต้นในขณะที่ Framework Struts2 มีหลายรายการ ดังนั้นคุณต้องกำหนดค่า prope = "Prototype"
2) ไม่มีการประกอบอัตโนมัติของ struts2 ในเวลานี้ ในการดำเนินการคุณจะต้องกำหนดค่าคุณสมบัติลูกค้าบริการด้วยตนเองและสร้างวิธีการตั้งค่าในคลาสแอ็คชั่น
<!-กำหนดค่าโมดูลลูกค้า-> <!-เน้น: aciton ที่กำหนดค่าจะต้องเป็นหลายคอลัมน์-> <bean id = "customerAction" ขอบเขต = "ต้นแบบ"> <!-หมายเหตุ: เมื่อ struts จัดการการกระทำตามแพ็คเกจ struts-plugin คุณจะต้องจัดเตรียมวิธีการ อย่างไรก็ตามการดำเนินการได้รับการจัดการโดยฤดูใบไม้ผลิและแอสเซมบลีอัตโนมัติไม่ถูกต้องดังนั้นคุณต้องทำการฉีดการกำหนดค่าด้วยตนเอง-> <property name = "CustomerService" ref = "CustomerService"> </property> </ebean>
3. กำหนดค่าธุรกรรม
สปริงรวมวิธีการไฮเบอร์เนตหนึ่ง: (ไฟล์กำหนดค่ากับ hibernate.cfg.xml เน้น: การกำหนดค่าที่เชื่อมโยงเธรดปัจจุบันไม่สามารถเพิ่มได้)
ในอดีตเมื่อเล่น Hibernate, hibernate.cfg.xml ได้รับการจัดการโดยกรอบ Hibernate ไฟล์การกำหนดค่าสามารถสร้าง SessionFactory เลเยอร์การคงอยู่โหลดไฟล์การกำหนดค่านี้เพื่อรับ SessionFactory ดังนั้นการสร้างเซสชันที่สร้างจากโรงงานเพิ่มการลบและการเปลี่ยนข้อมูลเป็น ในเวลานี้ไฟล์การกำหนดค่าควรส่งมอบให้กับการจัดการฤดูใบไม้ผลิใช้ประโยชน์จากคุณสมบัติ IOC ของฤดูใบไม้ผลิอย่างเต็มที่
Framework Spring ให้บริการคลาสเครื่องมือ Hibernatedaosupport ซึ่งสามารถสืบทอดได้ในอนาคตโดย DAO! - ก่อนที่จะแนะนำไฟล์การกำหนดค่าหลักของไฮเบอร์เนตเลเยอร์ DAO จะต้องสืบทอดคลาสแม่แบบไฮเบอร์เนตไคโอชูปอตซึ่งห่อหุ้มเทมเพลตธุรกรรมภายใน
ดูซอร์สโค้ด:
1) ปรับเปลี่ยนคลาสการใช้งานเลเยอร์การคงอยู่ที่สอดคล้องกันและปล่อยให้มันสืบทอด Hibernatedaosupport
แพ็คเกจ com.clj.dao; นำเข้า org.springframework.orm.hibernate5.hibernatetemplate; นำเข้า org.springframework.orm.hibernate5.support.hibernatedaosupport; นำเข้า com.clj.domain. @Author Administrator * * /Class Public CustomerDaoImpl ขยาย HiberNatedAoSupport ดำเนินการลูกค้า {// บันทึกข้อมูลลงในฐานข้อมูล (โทรไปที่คลาสเทมเพลต (จัดทำโดย Hibernate เซสชันที่ห่อหุ้ม) / * โมฆะสาธารณะ sethibernatetemplate (hibernatetemplate hibernatetemplate) {this.hibernatetemplate = hibernatetemplate; }*// ***บันทึกลูกค้า*/ โมฆะสาธารณะบันทึก (ลูกค้าลูกค้า) {System.out.println ("เลเยอร์ถาวร: บันทึกลูกค้า"); this.getHiberNateTemplate (). บันทึก (ลูกค้า); -2) ปรับเปลี่ยนเลเยอร์ธุรกิจและเปิดใช้งานคำอธิบายประกอบการทำธุรกรรม
แพ็คเกจ com.clj.service; นำเข้า org.springframework.transaction.annotation.transactional; นำเข้า com.clj.dao.customerdao; นำเข้า com.clj.domain.customer;/** * ผู้ดูแลระบบธุรกิจของลูกค้า โมฆะสาธารณะ SetCustomerDao (CustomerDao CustomerDao) {this.customerdao = customerdao; } // ใช้เพื่อบันทึกโมฆะสาธารณะของลูกค้าบันทึก (ลูกค้าลูกค้า) {system.out.println ("เลเยอร์ธุรกิจ, บันทึกลูกค้า"); customerdao.save (ลูกค้า); -3) แก้ไขไฟล์ ApplicationContext.xml
แนะนำไฟล์กำหนดค่าไฮเบอร์เนตก่อน
<!-ถั่วเขียนชื่อได้รับการแก้ไขโดยฤดูใบไม้ผลิใช้เพื่อโหลดไฟล์การกำหนดค่าของ hibernate.cfg.xml-> <bean id = "sessionfactory"> <!-เส้นทางการกำหนดค่า: เมื่อเซิร์ฟเวอร์เริ่มต้นวัตถุจะถูกสร้างขึ้น value = "classpath: hibernate.cfg.xml"/> </ebean>
กำหนดค่าการจัดการธุรกรรมแพลตฟอร์ม: ใช้ในการจัดการธุรกรรม โปรดทราบว่าตอนนี้ใช้กรอบการทำงานแบบไฮเบอร์เนตดังนั้นจึงจำเป็นต้องใช้ผู้จัดการธุรกรรมของกรอบการทำงาน Hibernate
<!-กำหนดค่าตัวจัดการธุรกรรมแพลตฟอร์มก่อน-> <bean id = "transactionManager"> <!-การทำธุรกรรมการฉีดเซสชันสามารถจัดการธุรกรรมและโรงงานสามารถสร้างเซสชัน-> <property name = "SessionFactory" ref = "SessionFactory"/> </bean>
คำอธิบายประกอบการทำธุรกรรมแบบเปิด
<!-คำอธิบายประกอบเพื่อเปิดใช้งานการทำธุรกรรม-> <tx: Transaction-Driven Transaction-Manager = "TransactionManager"/>
ลบการกำหนดค่าคลาสเทมเพลตและกำหนดค่า sessionFactory สำหรับเลเยอร์การคงอยู่
<!-ในอนาคต Dao จำเป็นต้องสืบทอด Hibernatedaosupport และ Inject SessionFactory-> <bean id = "customerdao"> <!-<property name = "hibernatetemplate" ref = "hibernatetemplate"/> <! ref = "SessionFactory"/> </ebean>
รหัสทั้งหมดมีดังนี้
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <! <!-เส้นทางการกำหนดค่า: เมื่อเซิร์ฟเวอร์เริ่มต้นวัตถุจะถูกสร้างขึ้นดังนั้นจึงโหลดไฟล์ hibernate.cfg.xml ดังนั้นจึงสร้างวัตถุเซสชัน-> <property name = "การกำหนดค่า" value = "classpath: hibernate.cfg.xml"/> เซสชันสามารถจัดการธุรกรรมและโรงงานสามารถสร้างเซสชัน-> <property name = "SessionFactory" ref = "SessionFactory"/> </ebean> <!-คำอธิบายประกอบเพื่อเปิดใช้งานการทำธุรกรรม-> <tx: การทำธุรกรรมที่ขับเคลื่อนด้วยคำอธิบาย prope = "prototype"> <!-หมายเหตุ: เมื่อ struts จัดการการกระทำขึ้นอยู่กับแพ็คเกจ Jar ที่มี struts-plugin, struts คงที่ rost.objectFactory.spring.autowire = ชื่อถูกเปลี่ยนเพื่อเปิดและสามารถประกอบโดยอัตโนมัติ คุณจะต้องจัดเตรียมวิธีการ อย่างไรก็ตามการดำเนินการได้รับการจัดการโดยฤดูใบไม้ผลิและแอสเซมบลีอัตโนมัติไม่ถูกต้องดังนั้นคุณต้องทำการฉีดการกำหนดค่าด้วยตนเอง-> <ชื่อทรัพย์สิน = "ลูกค้าบริการ" ref = "ลูกค้าบริการ"> </คุณสมบัติ> </epean> <bean id = "customerservice"> name = "hibernatetemplate" ref = "hibernatetemplate"/>-> <!-คลาสเทมเพลตไม่ได้ถูกฉีดที่นี่ แต่ sessionFactory เพราะเทมเพลตต้องการเซสชัน (เซสชันที่ห่อหุ้ม)-> <property name = "sessionfactory" ref = "sessionfactory" HibernatedaoSupport ไม่จำเป็นต้องกำหนดค่า-> <!-<bean id = "hibernatetemplate"> inject sessionfactory <property name = "SessionFactory"/> </ebean>-> </epeans>
4) ปรับเปลี่ยนคลาสการกระทำ
เนื่องจากคลาสการใช้งานเลเยอร์ธุรกิจได้รับการฉีดวิธีการเลเยอร์ธุรกิจสามารถเรียกได้โดยตรงในเวลานี้โดยไม่ต้องโหลดถั่ว
แพ็คเกจ com.clj.web.action; นำเข้า org.apache.struts2.servletactionContext; นำเข้า org.springframework.web.context.webapplicationContext; นำเข้า org.springframework.web.context.support.webapplicationContext com.clj.service.customerservice; นำเข้า com.opensymphony.xwork2.actionsupport; นำเข้า com.opensymphony.xwork2.modeldriven;/** * ชั้นควบคุมของลูกค้า * @author ผู้ดูแลระบบลูกค้าใหม่ ลูกค้าสาธารณะ getModel () {ส่งคืนลูกค้า; } // ให้คุณลักษณะของสมาชิกบริการและจัดเตรียมวิธีการที่กำหนดลูกค้าบริการลูกค้าบริการลูกค้า; โมฆะสาธารณะ setCustomerservice (ลูกค้าบริการลูกค้า) {this.customerservice = customerservice; } / *** บันทึกลูกค้า* @return* / สตริงสาธารณะเพิ่ม () {system.out.println ("เว็บเลเยอร์, บันทึกลูกค้า"); // วิธีการที่ 1: สร้าง Web Factory (การดำเนินการถูกสร้างขึ้นโดย struts2) /*webapplicationContext context = webApplicationContextUtils.getWebapplicationContext (servletActionContext.getServletContext ()); CustomerService CS = (ลูกค้าบริการ) บริบท GetBean ("ลูกค้าบริการ"); // วิธีการโทร cs.save (ลูกค้า); */ customerService.save (ลูกค้า); ไม่กลับมา; -การรวมสปริงวิธีไฮเบอร์เนตสอง: (ไฟล์การกำหนดค่าที่ไม่มี hibernate.cfg.xml)
ที่นี่เราจะลบไฟล์การกำหนดค่าหลักของไฮเบอร์เนต ก่อนลบคุณจะต้องกำหนดค่าเนื้อหาที่เกี่ยวข้องในไฟล์การกำหนดค่าไปยังไฟล์ ApplicationInconText.xml ของ Spring เพื่อรับ
1. ตรวจสอบเนื้อหาที่เกี่ยวข้องในไฟล์ hibernate.cfg.xml
* พารามิเตอร์พื้นฐานของการเชื่อมต่อฐานข้อมูล (4 พารามิเตอร์หลัก)
* คุณสมบัติที่เกี่ยวข้องกับไฮเบอร์เนต
* กลุ่มการเชื่อมต่อ
* แผนที่ไฟล์
2. แนะนำการกำหนดค่า
แนะนำกลุ่มการเชื่อมต่อ
<!-กำหนดค่าพูลการเชื่อมต่อสำหรับ c3p0 first-> <bean id = "dataSource"> <property name = "driverclass" value = "com.mysql.jdbc.driver"/> <property name = "jdbcurl" value = "JDBC: MySQL: //192.168.174.174.174.174. value = "root"/> <property name = "password" value = "root"/> </ebean>
แก้ไขเซสชันที่เกี่ยวข้อง: เนื่องจากไม่มีไฟล์การกำหนดค่าสำหรับ hibernate.cfg.xml คุณต้องแก้ไขการกำหนดค่าและฉีดพูลการเชื่อมต่อ
แนะนำไฟล์การแมปวัตถุ: เนื่องจากไฟล์การกำหนดค่าของ hibernate.cfg.xml ไม่ได้สแกนอีกต่อไปและไฟล์การกำหนดค่าจะต้องถูกฉีด
<!-ถั่วเขียนชื่อได้รับการแก้ไขและมีให้ในฤดูใบไม้ผลิเพื่อโหลดไฟล์การกำหนดค่าของ hibernate.cfg.xml-> <bean id = "sessionfactory"> <!-โหลดพูลการเชื่อมต่อแรก-> <property name = "DataSource" ref = "DataSource"/> <! key = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </prop> <prop key = "hibernate.show_sql"> true </prop> <prop key = "hibernate.format_sql"> true </prop> <!-แนะนำไฟล์การกำหนดค่าการแม็พ-> <property name = "MappingResources"> <list> <dalue> com/clj/domain/customer.hbm.xml </alue> </list> </property> </ebean>
ตอนนี้: รหัส ApplicationContext.xml มีดังนี้
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml xmlns: context = "http://www.springframework.org/schema/context" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http:/ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://ww.springframework.org/schema http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <! value = "com.mysql.jdbc.driver"/> <ชื่อคุณสมบัติ = "jdbcurl" value = "jdbc: mysql: //192.168.174.130: 3306/ssh"/> <ชื่อทรัพย์สิน = "ผู้ใช้" value = "root"/> < โดยฤดูใบไม้ผลิเพื่อโหลดไฟล์การกำหนดค่าของ hibernate.cfg.xml-> <bean id = "sessionfactory"> <!-โหลดพูลการเชื่อมต่อครั้งแรก-> <ชื่อคุณสมบัติ = "dataSource" ref = "DataSource"/> <! key = "hibernate.dialect"> org.hibernate.dialect.mysqldialect </prop> <prop key = "hibernate.show_sql"> true </prop> <prop key = "hibernate.format_sql"> true </prop> <!-การฉีดไฟล์การกำหนดค่าการแมป-> <property name = "MappingResources"> <slist> <dalue> com/clj/domain/customer.hbm.xml </value> </list> </property> </ebean> <! -> <property name = "SessionFactory" ref = "SessionFactory"/> </ebean> <!-คำอธิบายประกอบสำหรับการเปิดธุรกรรม-manager = "transactionManager"/> <!-กำหนดค่าโมดูลลูกค้า-> <! struts-plugin, strut.ObjectFactory.spring.autowire = ชื่อได้เปิดและสามารถประกอบโดยอัตโนมัติ คุณจะต้องให้วิธีการที่กำหนดเท่านั้น However, the action is managed by spring and the automatic assembly is invalid, so you need to manually perform configuration injection--> <property name="customerService" ref="customerService"></property> </bean> <bean id="customerService"> <property name="customerDao" ref="customerDao"></property> </bean> <!-- In the future, Dao needs to inherit HibernateDaoSupport and inject sessionFactory --> <bean id="customerDao"> <!--<property name="hibernateTemplate" ref="hibernateTemplate"/> -> <!-- The template class is not injected here, but sessionFactory, because the template needs session (encapsulated session)--> <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- Configure the template class (provided by the hibernate framework, encapsulated session) and is handed over to spring management at this time. If the persistence layer inherits HibernateDaoSupport, there is no need to configure --> <!-- <bean id="hibernateTemplate"> Inject sessionFactory <property name="sessionFactory"/> </bean>--></beans>
此时可以安心的删除hibernate.cfg.xml文件了
这样SSH整合完毕
六、Hibernate模板常用方法
注意:以下代码省略了接口中的演示(偷了个懒,相信初学者不会看不懂)
1)插入:
持久层
package com.clj.dao;import java.util.List;import org.hibernate.criterion.DetachedCriteria;import org.springframework.orm.hibernate5.HibernateTemplate;import org.springframework.orm.hibernate5.support.HibernateDaoSupport;import com.clj.domain.Customer;/** * Persistence layer* Inherits HibernateDaoSupport and encapsulates HibernateTemplate internally * @author Administrator * */public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao { @Override public void update(Customer customer) { // TODO Auto-generated method stub this.getHibernateTemplate().update(customer); -业务层
package com.clj.service;import java.util.List;import org.springframework.transaction.annotation.Transactional;import com.clj.dao.CustomerDao;import com.clj.domain.Customer;/** * Customer's business layer* @author Administrator * */@Transactionalpublic class CustomerServiceImpl implements CustomerService{ private CustomerDao customerDao; public void setCustomerDao(CustomerDao customerDao) { this.customerDao = customerDao; } @Override public void update(Customer customer) { // TODO Auto-generated method stub customerDao.update(customer); -测试类
package com.clj.test;import java.util.List;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.clj.domain.Customer;import com.clj.service.CustomerService;/** * Simple way to test Hiberante template class* @author Administrator * */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class Demo1 { @Resource(name="customerService") private CustomerService customerService; /** * Test insert*/ @Test public void run1(){ Customer customer=new Customer(); customer.setCust_id(1L); customer.setCust_name("test"); customerService.update(customer); -2)以下为指定查询、查询所有、离线查询代码
持久层
package com.clj.dao;import java.util.List;import org.hibernate.criterion.DetachedCriteria;import org.springframework.orm.hibernate5.HibernateTemplate;import org.springframework.orm.hibernate5.support.HibernateDaoSupport;import com.clj.domain.Customer;/** * Persistence layer* Inherits HibernateDaoSupport and encapsulates HibernateTemplate internally * @author Administrator * */public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao { //Save data to the database (call template class (provided by hibernate, encapsulated session)) /*private HibernateTemplate hibernateTemplate; public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; }*/ /** * Save customer*/ public void save(Customer customer) { System.out.println("Permanent layer: save customer"); this.getHibernateTemplate().save(customer); } @Override public void update(Customer customer) { // TODO Auto-generated method stub this.getHibernateTemplate().update(customer); } /** * Query by primary key*/ public Customer getById(Long id) { return this.getHibernateTemplate().get(Customer.class, id); } /** * Query all*/ @Override public List<Customer> findAll() { String sql="from Customer"; List<Customer> list=(List<Customer>) this.getHibernateTemplate().find(sql); return list; } /** * QBC offline query*/ @Override public List<Customer> findAllByQBC() { DetachedCriteria criteria=DetachedCriteria.forClass(Customer.class); List<Customer> list=(List<Customer>) this.getHibernateTemplate().findByCriteria(criteria); return list; -业务层
package com.clj.service;import java.util.List;import org.springframework.transaction.annotation.Transactional;import com.clj.dao.CustomerDao;import com.clj.domain.Customer;/** * Customer's business layer* @author Administrator * */@Transactionalpublic class CustomerServiceImpl implements CustomerService{ private CustomerDao customerDao; public void setCustomerDao(CustomerDao customerDao) { this.customerDao = customerDao; } //Use to save customers public void save(Customer customer) { System.out.println("Business layer, save customer"); customerDao.save(customer); } @Override public void update(Customer customer) { // TODO Auto-generated method stub customerDao.update(customer); } @Override public Customer getById(Long id) { // TODO Auto-generated method stub return customerDao.getById(id); } @Override public List<Customer> findAll() { return customerDao.findAll(); } @Override public List<Customer> findAllByQBC() { // TODO Auto-generated method stub return customerDao.findAllByQBC(); -测试类
package com.clj.test;import java.util.List;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.clj.domain.Customer;import com.clj.service.CustomerService;/** * Simple way to test Hiberante template class* @author Administrator * */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class Demo1 { @Resource(name="customerService") private CustomerService customerService; /** * Test insert*/ @Test public void run1(){ Customer customer=new Customer(); customer.setCust_id(1L); customer.setCust_name("test"); customerService.update(customer); } /** * Test query the specified customer*/ @Test public void run2(){ Customer customer=customerService.getById(2L); System.out.println(customer); } /** * Query all customers*/ @Test public void run3(){ List<Customer> list=customerService.findAll(); System.out.println(list); } /** * QBC(offline query) */ @Test public void run4(){ List<Customer> list=customerService.findAllByQBC(); System.out.println(list); -七、关于SSH延迟加载问题
1. 使用延迟加载的时候,再WEB层查询对象的时候程序会抛出异常!
* 原因是延迟加载还没有发生SQL语句,在业务层session对象就已经销毁了,所以查询到的JavaBean对象已经变成了托管态对象!
* 注意:一定要先删除javassist-3.11.0.GA.jar包(jar包冲突了)
2. 解决办法
Spring框架提供了一个过滤器,让session对象在WEB层就创建,在WEB层销毁。只需要配置该过滤器即可
* 但是:要注意需要在struts2的核心过滤器之前进行,spring监听器之后配置
<!-- 解决延迟加载的问题--> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
3、演示延迟加载
持久层:调用load方法,此方法时延迟加载的
/** * Lazy load*/ @Override public Customer loadById(long id) { // TODO Auto-generated method stub return this.getHibernateTemplate().load(Customer.class, id); -业务层
@Override public Customer loadById(long id) { // TODO Auto-generated method stub return customerDao.loadById(id); -测试类
@Test public void run5(){ Customer customer=customerService.loadById(2L); System.out.println(customer); -ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น