Struts2 + hibernate + spring environment setup [SSH setup ]!!

Source: Internet
Author: User

Struts2 + hibernate + spring Environment Construction

I have been learning SSH over the past few days. Some people say there is no need to learn frameworks for the sake of learning frameworks !! I think I am learning the framework for the purpose of learning the framework, but it is also the situation! For the time being, no matter what others say, learning this kind of thing is your own as long as you learn it. I believe that knowledge will not be learned in vain. Do not "hate less when books are used", haha... it's a little Rainy. Learn how to use it first, and then gradually improve it !!

Record the specific setup process: (Project runtime environment: myeclipse + MySQL + jdk1.6)

Step 1:Create three user liberary in myeclipse: struts2, hibernate, and jar packages required for spring running. After creating a project, introduce the three liberary files just created.

Step 2:ModifyWeb. xmlThe configuration is as follows:




<? XML version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <! -- Configure and load the spring configuration file --> <listener-class> Org. springframework. web. context. contextloaderlistener </listener-class> </listener> <context-param> <param-Name> contextconfiglocation </param-Name> <param-value> classpath: applicationcontext. XML </param-value> </context-param> <! -- Lazy initial session is automatically closed, but the page request is not completed! --> <! -- Solution 2: opensessioninviewinterceptor --> <filter-Name> opensessioninview </filter-Name> <filter-class> Org. springframework. orm. hibernate3.support. opensessioninviewfilter </filter-class> </filter> <filter-mapping> <filter-Name> opensessioninview </filter-Name> <URL-pattern>/* </url-Pattern> </filter-mapping> <! -- Struts2 --> <filter-Name> struts2 </filter-Name> <filter-class> Org. apache. struts2.dispatcher. filterdispatcher </filter-class> </filter> <filter-mapping> <filter-Name> struts2 </filter-Name> <URL-pattern>/* </url-Pattern> </filter-mapping> <welcome-file-List> <welcome-File> index. JSP </welcome-File> </welcome-file-List> </Web-app>

Step 3:Modify the spring configuration file.CodeAs follows:






<? XML version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: context = "http://www.springframework.org/schema/context" xmlns: AOP = "http://www.springframework.org/schema/aop" xmlns: Tx = "http://www.springframework.org/schema/tx" xsi: schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0. Xsdhttp: // your http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <context: annotation-config/> <! -- Inject according to annotation --> <context: component-scan base-package = "com. wk"/> <! -- Create datasource --> <bean id = "datasource" class = "org. apache. commons. DBCP. basicdatasource "Destroy-method =" close "> <property name =" driverclassname "value =" com. mySQL. JDBC. driver "/> <property name =" url "value =" JDBC: mysql: // localhost: 3306/Spring "/> <property name =" username "value =" root "/> <property name =" password "value =" wangkang "/> </bean> <! -- Integrate hibernate --> <bean id = "sessionfactory" class = "org. springframework. orm. hibernate3.annotation. annotationsessionfactorybean "> <property name =" datasource "ref =" datasource "/> <property name =" packagestoscan "> <list> <value> COM. wk. model </value> </List> </property> <property name = "hibernateproperties"> <props> <prop key = "hibernate. dialect "> Org. hibernate. dialect. mysqldialect </prop> <prop key = "hibernate. show_ SQL"> True </prop> </props> </property> </bean> <! -- Spring_xml Transaction Management --> <bean id = "txmanager" class = "org. springframework. orm. hibernate3.hibernatetransactionmanager "> <property name =" sessionfactory "ref =" sessionfactory "/> </bean> <AOP: config> <AOP: pointcut id = "servicebusiness" expression = "execution (Public * COM. web .. *. add (..)) "/> <AOP: Advisor advice-ref =" txadvice "pointcut-ref =" servicebusiness "Order =" 2 "/> </AOP: config> <TX: advice id = "txadvice" transaction- Manager = "txmanager"> <TX: Attributes> <TX: method name = "get *" Read-Only = "true"/> <TX: method Name = "add *" propagation = "required"/> </TX: Attributes> </TX: Advice> <! -- Hibernatetemplate --> <bean id = "hibernatetemplate" class = "org. springframework. orm. hibernate3.hibernatetemplate "> <property name =" sessionfactory "> <ref bean =" sessionfactory "/> </property> </bean> </beans>

Which of the following statements is used for spring injection?Annotation
Hibernate ing is also used.AnnotationMethod

.






Paste other code, mainly because the configuration file is difficult to write, and other things are better handled:






1. Model --- person






Package COM. wk. model; import javax. persistence. entity; import javax. persistence. generatedvalue; import javax. persistence. ID; import Org. springframework. stereotype. component; @ entity @ component ("person") public class person {private int ID; private string name; private string address; private string permission Sion; Public Person () {super () ;}@ ID @ generatedvaluepublic int GETID () {return ID;} public void setid (int id) {This. id = ID;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} Public String getaddress () {return address;} public void setaddress (string address) {This. address = address;} Public String get1_sion () {return 1_sion;} public void set1_sion (string 1_sion) {This. sion = Sion ;}}

2. Dao --- persondao






 
Package COM. wk. dao; import COM. wk. model. person; public interface persondao {public int save (person); Public int Delete (int id); Public int Update (person); Public Person getperson (int id );}

3. impl --- persondaoimpl






Package COM. wk. dao. impl; import Org. hibernate. hibernateexception; import Org. hibernate. session; import Org. springframework. stereotype. component; import COM. wk. dao. persondao; import COM. wk. model. person; import COM. wk. util. hibernateutil; @ component ("persondao") public class persondaoimpl implements persondao {public int Delete (int id) {session = hibernateutil. getsessionfactory (). getcurrentsession (); person = new person (); try {session. begintransaction (); person. setid (ID); Session. delete (person); Session. begintransaction (). commit (); return 1;} catch (hibernateexception e) {e. printstacktrace ();} return 0;} public person getperson (int id) {session = hibernateutil. getsessionfactory (). getcurrentsession (); person = new person (); try {session. begintransaction (); person = (person) session. get (person. class, ID); Session. begintransaction (). commit (); Return person;} catch (hibernateexception e) {e. printstacktrace ();} return NULL;} public int save (person) {session = hibernateutil. getsessionfactory (). getcurrentsession (); try {session. begintransaction (); Session. save (person); Session. begintransaction (). commit (); return 1;} catch (hibernateexception e) {e. printstacktrace ();} return 0;} public int Update (person personvo) {session = hibernateutil. getsessionfactory (). getcurrentsession (); person = new person (); try {session. begintransaction (); person = (person) session. load (person. class, personvo. GETID (); person. setname (personvo. getname (); person. setaddress (personvo. getaddress (); person. setsion Sion (personvo. getextension Sion (); Session. update (person); Session. flush (); // improves the efficiency of the session. begintransaction (). commit (); return 1;} catch (hibernateexception e) {e. printstacktrace () ;}return 0 ;}}

4. strutsaction --- personaction






Package COM. wk. action; import javax. annotation. resource; import Org. springframework. stereotype. component; import COM. opensymphony. xwork2.actionsupport; import COM. wk. dao. persondao; import COM. wk. dao. impl. persondaoimpl; import COM. wk. model. person; @ component ("personaction") public class personaction extends actionsupport {Private Static final long serialversionuid =-415819837781302739l; persondao = New persondaoimpl (); person = new person (); int id = 0; Public String addperson () {int flag = persondao. save (person); If (flag = 1) {return "success" ;}else {return "fail" ;}} Public String delperson () {int flag = persondao. delete (ID); If (flag = 1) {return "success" ;}else {return "fail" ;}} Public String updateperson () {int flag = persondao. update (person); If (flag = 1) {return "success";} el Se {return "fail" ;}} Public String searchperson () {person = persondao. getperson (ID); If (person! = NULL) {return "success";} else {return "fail" ;}} public person getperson () {return person ;}public void setperson (person) {This. person = person;} public int GETID () {return ID;} public void setid (int id) {This. id = ID;} public persondao getpersondao () {return persondao;} @ Resource (name = "persondao") Public void setpersondao (persondao) {This. persondao = persondao ;}}

This is almost the case. It encapsulates the code of future generations and is not displayed on the front-end. However, all JUnit tests have passed.







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.