SSH2 Project Integration Struts2.1+hibernate3.3+spring3 based on hibernate annotations and struts2 annotations

Source: Internet
Author: User

The project directory structure is as follows:

Core configuration file:

Xml

<?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 "><welcome-file-list><welcome-file>index.jsp</ welcome-file></welcome-file-list><context-param><param-name>contextconfiglocation</ Param-name><param-value>classpath:applicationcontext.xml</param-value></context-param> <listener><listener-class>org.springframework.web.context.contextloaderlistener</listener-class ></listener><filter><filter-name>openSessionInViewFilter</filter-name>< filter-class>org.springframework.orm.hibernate3.support.opensessioninviewfilter</filter-class></ Filter><filter-mapping><filter-name>opensessioninviewfilter</filter-name><url-pattern >/*</url-pattern>&Lt;/filter-mapping><filter><filter-name>struts2</filter-name><filter-class> org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilter</filter-class></filter>< filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></ Filter-mapping></web-app>

Applicationcontext.xml

<?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/beans http://www.springframework.org/schema/beans/spring- Beans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/cont Ext/spring-context-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP HTTP://WWW.SPRINGFRAMEWORK.ORG/SC Hema/aop/spring-aop-2.5.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/s Chema/tx/spring-tx-2.5.xsd "><!--inject bean--><context:annotation-config/><context with annotations: Component-scan base-package= "Com.yza"/><bean id= "Propertyconfigurer" class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" ><property name= "Locations" > <list><value>classpath:config/jdbc.properties</value></list></property></bean ><!--database connection pool--><bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= " Close "><property name=" Driverclassname "value=" ${jdbc.drive} "></property><property name=" url " Value= "${jdbc.url}" ></property><property name= "username" value= "${jdbc.user}" ></property> <property name= "Password" value= "${jdbc.password}" ></property><property name= "DefaultAutoCommit" Value= "false" ></property></bean><!--hibernate sessionfactory Create--><bean id= " Sessionfactory "class=" Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean "><               Property Name= "DataSource" ref= "DataSource"/> <!--the bean under the scan package, does not adopt mapping openness good-->  <p roperty name= "Packagestoscan" ><list><value>com.yza.model.</value> <!--note that the final "." will be converted to " Classpath*:com/xxx/entity/**/*.class "--></list></property><property name=" hibernateProperties "><props><prop key=" Hibernate.format_sql ">true</prop><prop key=" Hibernate.hbm2ddl.auto " >update</prop><prop key= "Hibernate.dialect" >org.hibernate.dialect.mysqldialect</prop></ Props></property></bean><bean id= "Hibernatetemplate" class= " Org.springframework.orm.hibernate3.HibernateTemplate "><property name=" sessionfactory "ref=" Sessionfactory " ></property></bean><!--This hard write is too dead, it is recommended to write a basic DAO, and then inherit, because if directly to hibernatedaosupport or not write will error " Sessionfactory ' or ' hibernatetemplate ' is required "--><bean id=" Sysuserinfodaoimpl "class=" Com.yza.sysuser.dao.SysUserInfoDaoImpl "><property name=" hibernatetemplate "ref=" Hibernatetemplate ">< /property></bean> <!--things configuration--><beAn id= "TransactionManager" class= "Org.springframework.orm.hibernate3.HibernateTransactionManager" >< Property Name= "Sessionfactory" ref= "sessionfactory" ></property></bean><tx:advice id= "TxAdvice" Transaction-manager= "TransactionManager" ><tx:attributes><tx:method name= "find*" read-only= "true"/ ><tx:method name= "add*" propagation= "REQUIRED"/></tx:attributes></tx:advice><aop:config ><aop:pointcut expression= "Execution (public * com.yza.service. *.*(..))" Id= "Mypointcut"/><aop:advisor advice-ref= "Txadvice" pointcut-ref= "Mypointcut"/></aop:config></ Beans>

Jdbc:

Jdbc.drive=com.mysql.jdbc.driverjdbc.url=jdbc\:mysql\://10.6.0.56\:3306/jxproject?characterencoding\=utf-8 &charactersetresults\=utf-8&profilesql\=truejdbc.user=adminsjdbc.password=root

Core Java code

Entity Bean

/** * @Project: SSH2 * @Title: Sysuserinfo.java * @Package Com.yza.model * @author Yongzhian * @date 2014-9-29 3:40:08 * @Copyright: Www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */package com.yza.model;import javax.persistence.column;import javax.persistence.entity;//Be sure to use this package, Cannot be the import Javax.persistence.generatedvalue;import under Hibernate javax.persistence.generationtype;import Javax.persistence.id;import javax.persistence.table;/** * @ClassName sysuserinfo * @Description Module description * @author Yongzhian * @Date 2014-9-29 */@Entity @table (name = "User_baseinfo", Catalog = "Commonuser") public class SysUserInfo implements Java. Io.  Serializable {/** * serialversionuid */private static final long serialversionuid = 1L; @Id @column (name = "info_id", unique = True, nullable = False) @GeneratedValue (strategy = generationtype.auto) Private Integer userId; Primary key @column (name = "Info_name") private String infoname; User name @column (name = "Sex") Private String sex; Sex/** * @return userId */puBlic Integer getUserId () {return userId;} /** * @param userid to set the UserID */public void Setuserid (Integer userId) {this.userid = userId;} /** * @return infoname */public String getinfoname () {return infoname;} /** * @param infoname to set infoname */public void Setinfoname (String infoname) {this.infoname = InfoName;} /** * @return Sex */public String getsex () {return sex;} /** * @param sex to set sex */public void Setsex (String sex) {this.sex = sex;}}
Action
/** * @Project: SSH2 * @Title: Sysuseraction.java * @Package com.yza.sysuser.action * @author Yongzhian * @date 2014-9-29 Afternoon 3:49:43 * @Copyright: Www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */package com.yza.sysuser.action;import javax.annotation.resource;import Javax.servlet.http.httpservletrequest;import Org.apache.struts2.convention.annotation.action;import Org.apache.struts2.convention.annotation.namespace;import Org.apache.struts2.convention.annotation.parentpackage;import Org.apache.struts2.convention.annotation.Result; Import Org.apache.struts2.convention.annotation.results;import Org.apache.struts2.interceptor.servletrequestaware;import Com.opensymphony.xwork2.actionsupport;import Com.yza.sysuser.dao.sysuserdao;import com.yza.sysuser.service.sysuserservice;/** * @ClassName SysuserAction * @ Description Module Description * @author Yongzhian * @Date 2014-9-29 */@ParentPackage ("Struts-default") @Namespace ("/sysuser") @ Results ({@Result (name = "Success", location = "/pages/userInfo.jsp "), public class Sysuseraction extends Actionsupport implements servletrequestaware{/** * Serialversionuid * * Private static final long serialversionuid = 1L; @Resourceprivate sysuserservice sysuserservice;protected HttpServletRequest request, @Action ("GetUserInfo") public String GetUserInfo () {String useridstr = This.request.getParameter ("UserId"), if (useridstr! = null) {Request.setattribute ("user", Sysuserservice.getsysuserinfo (Integer.parseint (USERIDSTR)));} Request.setattribute ("tt", "n"); return "Success";}  /* (non-javadoc) * <p>Title:setServletRequest</p> * <p>description: Method Description </p> * @param request * @see Org.apache.struts2.interceptor.servletrequestaware#setservletrequest (javax.servlet.http.HttpServletRequest */@Overridepublic void Setservletrequest (HttpServletRequest request) {this.request = Request;}}

Service

/** * @Project: SSH2 * @Title: Sysuserdao.java * @Package Com.yza.sysuser.dao * @author Yongzhian * @date 2014-9-29 4:19 : * @Copyright: Www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */package com.yza.sysuser.service;import com.yza.model.sysuserinfo;/** * @ClassName Sysuserdao * @ Description Module Description * @author Yongzhian * @Date 2014-9-29 */public interface Sysuserservice {public SysUserInfo Getsysuserinf O (Integer userId);}

/** * @Project: SSH2 * @Title: Sysuserserviceimpl.java * @Package com.yza.sysuser.service * @author Yongzhian * @date 2014 -9-29 PM 4:24:16 * @Copyright: Www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */package com.yza.sysuser.service;import javax.annotation.resource;import Org.springframework.stereotype.service;import Com.yza.model.sysuserinfo;import com.yza.sysuser.dao.SysuserDao;/* * * @ClassName Sysuserserviceimpl * @Description Module Description * @author Yongzhian * @Date 2014-9-29 */@Servicepublic class Sysuser Serviceimpl implements Sysuserservice {@Resourceprivate Sysuserdao sysuserdao;/* (non-javadoc) * <p>title: Getsysuserinfo</p>  * <p>description: Method Description </p>  * @param userId * @return * @see Com.yza.sysuser.service.sysuserservice#getsysuserinfo (Java.lang.Integer) */@Overridepublic SysUserInfo Getsysuserinfo (Integer userid) {return sysuserdao.getsysuserinfo (userid);}}
Dao

/** * @Project: SSH2 * @Title: Sysuserdao.java * @Package Com.yza.sysuser.dao * @author Yongzhian * @date 2014-9-29 4:19 : * @Copyright: Www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */package com.yza.sysuser.dao;import com.yza.model.sysuserinfo;/** * @ClassName Sysuserdao * @ Description Module Description * @author Yongzhian * @Date 2014-9-29 */public interface Sysuserdao {public SysUserInfo Getsysuserinfo Teger userId);}

/** * @Project: SSH2 * @Title: Sysuserinfodaoimpl.java * @Package Com.yza.sysuser.dao * @author Yongzhian * @date 2014-9-2 9 pm 4:21:16 * @Copyright: Www.yineng.com.cn Inc. All rights reserved. * @version V1.0 */package com.yza.sysuser.dao;import org.springframework.orm.hibernate3.support.HibernateDaoSupport ; Import Org.springframework.stereotype.repository;import com.yza.model.sysuserinfo;/** * @ClassName Sysuserinfodaoimpl * @Description Module Description * @author Yongzhian * @Date 2014-9-29 */@Repositorypublic class Sysuserinfodaoimpl Extends Hibernatedaosupport implements sysuserdao{/* (non-javadoc) * <p>Title:getSysUserInfo</p>  * <p>description: Method Description </p>  * @param userId * @return * @see com.yza.sysuser.dao.sysuserdao# Getsysuserinfo (Java.lang.Integer) */@Overridepublic SysUserInfo getsysuserinfo (Integer userId) {return This.gethibernatetemplate (). Get (Sysuserinfo.class, userId);} }



SSH2 Project Integration Struts2.1+hibernate3.3+spring3 based on hibernate annotations and struts2 annotations

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.