springmvc+spring+hibernate配置过程
开发工具:eclipse
项目目录:
jar包:
web.xml
<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.5"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"metadata-complete="true"><display-name>SMSH</display-name><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:hanxuanyuan/config/spring-*.xml</param-value></context-param><!--配置Spring的用于初始化容器对象的监听器--><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!--中央控制器--><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath*:hanxuanyuan/config/springmvc-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!--配置Session--><filter><filter-name>openSession</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>openSession</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
springmvc-servlet.xml
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"><!--启用注解--><mvc:annotation-driven/><!--静态资源访问--><!--<mvc:resourceslocation="/js/"mapping="/js/**"/>--><!--扫描包--><context:component-scanbase-package="hanxuanyuan.controller"/><!--视图配置--><beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver"><propertyname="prefix"value="/"></property><propertyname="suffix"value=".jsp"></property></bean></beans>
spring-hibernate.xml
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!--自动扫描与装配bean--><context:component-scanbase-package="hanxuanyuan"/><beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><propertyname="driverClassName"value="oracle.jdbc.driver.OracleDriver"></property><propertyname="url"value="jdbc:oracle:thin:@localhost:1521:demo"></property><propertyname="username"value="test"></property><propertyname="password"value="test"></property></bean><beanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="hibernateProperties"><props><propkey="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop><propkey="hibernate.show_sql">true</prop><propkey="hibernate.format_sql">true</prop></props></property><propertyname="configLocations"><list><value>classpath*:hanxuanyuan/config/hibernate.cfg.xml</value></list></property></bean><!--配置声明式事务管理(采用注解的方式)--><beanid="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><propertyname="sessionFactory"ref="sessionFactory"></property></bean><beanid="txBase"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"lazy-init="true"abstract="true"><propertyname="transactionManager"ref="transactionManager"></property><propertyname="transactionAttributes"><props><propkey="add*">PROPAGATION_REQUIRED,-Exception</prop><propkey="update*">PROPAGATION_REQUIRED,-Exception</prop><propkey="insert*">PROPAGATION_REQUIRED,-Exception</prop><propkey="modify*">PROPAGATION_REQUIRED,-Exception</prop><propkey="get*">PROPAGATION_NEVER</prop></props></property></bean></beans>
spring-bean.xml
<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-2.5.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><beanid="userDao"class="hanxuanyuan.Dao.UserDaoImpl"><propertyname="sessionFactory"ref="sessionFactory"></property></bean><beanid="userServiceBase"class="hanxuanyuan.service.UserServiceImpl"><propertyname="userDao"ref="userDao"></property></bean><beanid="userService"parent="txBase"><propertyname="target"ref="userServiceBase"></property></bean></beans>
hibernate.cfg.xml
<!DOCTYPEhibernate-configurationPUBLIC"-//Hibernate/HibernateConfigurationDTD3.0//EN""http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><mappingclass="hanxuanyuan.entity.GYh"/></session-factory></hibernate-configuration>
UserController.java
packagehanxuanyuan.controller;importjava.util.List;importjavax.annotation.Resource;importjavax.servlet.http.HttpServletRequest;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.RequestMapping;importhanxuanyuan.entity.GYh;importhanxuanyuan.service.UserService;@Controller@RequestMapping("/user")publicclassUserController{@Resource(name="userService")privateUserServiceuserService;@RequestMapping("/list")publicStringlist(){return"userlist";}@RequestMapping("/add")publicStringadd(GYhgyh,Modelmodel,HttpServletRequestrequest){userService.addUser(gyh);List<GYh>list=userService.queryList();model.addAttribute("list",list);return"userlist";}}
后台向前台传值方式:放入model.addAtribute()中
entity: GYh.java
packagehanxuanyuan.entity;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.SequenceGenerator;importjavax.persistence.Table;/***GYhentity.@authorMyEclipsePersistenceTools*/@Entity@Table(name="G_YH")publicclassGYhimplementsjava.io.Serializable{//Fields@Id@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="payablemoney_seq")@SequenceGenerator(name="payablemoney_seq",sequenceName="SEQ_ID")privateLongid;privateStringyhlx01;privateStringyhlx02;privateStringxm;privateStringdm;privateStringdw;privateStringsfz;privateStringjgz;privateStringyjgz;privateStringgrdh;privateStringmm;privateStringsm;privateStringcjsj;privateStringzt;privateLongryid;privateStringdwid;privateStringzzmc;//Constructors/**defaultconstructor*/publicGYh(){}/**fullconstructor*/publicGYh(Stringyhlx01,Stringyhlx02,Stringxm,Stringdm,Stringdw,Stringsfz,Stringjgz,Stringyjgz,Stringgrdh,Stringmm,Stringsm,Stringcjsj,Stringzt,Longryid,Stringdwid,Stringzzmc){this.yhlx01=yhlx01;this.yhlx02=yhlx02;this.xm=xm;this.dm=dm;this.dw=dw;this.sfz=sfz;this.jgz=jgz;this.yjgz=yjgz;this.grdh=grdh;this.mm=mm;this.sm=sm;this.cjsj=cjsj;this.zt=zt;this.ryid=ryid;this.dwid=dwid;this.zzmc=zzmc;}//PropertyaccessorspublicLonggetId(){returnthis.id;}publicvoidsetId(Longid){this.id=id;}publicStringgetYhlx01(){returnthis.yhlx01;}publicvoidsetYhlx01(Stringyhlx01){this.yhlx01=yhlx01;}publicStringgetYhlx02(){returnthis.yhlx02;}publicvoidsetYhlx02(Stringyhlx02){this.yhlx02=yhlx02;}publicStringgetXm(){returnthis.xm;}publicvoidsetXm(Stringxm){this.xm=xm;}publicStringgetDm(){returnthis.dm;}publicvoidsetDm(Stringdm){this.dm=dm;}publicStringgetDw(){returnthis.dw;}publicvoidsetDw(Stringdw){this.dw=dw;}publicStringgetSfz(){returnthis.sfz;}publicvoidsetSfz(Stringsfz){this.sfz=sfz;}publicStringgetJgz(){returnthis.jgz;}publicvoidsetJgz(Stringjgz){this.jgz=jgz;}publicStringgetYjgz(){returnthis.yjgz;}publicvoidsetYjgz(Stringyjgz){this.yjgz=yjgz;}publicStringgetGrdh(){returnthis.grdh;}publicvoidsetGrdh(Stringgrdh){this.grdh=grdh;}publicStringgetMm(){returnthis.mm;}publicvoidsetMm(Stringmm){this.mm=mm;}publicStringgetSm(){returnthis.sm;}publicvoidsetSm(Stringsm){this.sm=sm;}publicStringgetCjsj(){returnthis.cjsj;}publicvoidsetCjsj(Stringcjsj){this.cjsj=cjsj;}publicStringgetZt(){returnthis.zt;}publicvoidsetZt(Stringzt){this.zt=zt;}publicLonggetRyid(){returnryid;}publicvoidsetRyid(Longryid){this.ryid=ryid;}@OverridepublicStringtoString(){return"GYh[id="+id+",yhlx01="+yhlx01+",yhlx02="+yhlx02+",xm="+xm+",dm="+dm+",dw="+dw+",sfz="+sfz+",jgz="+jgz+",yjgz="+yjgz+",grdh="+grdh+",mm="+mm+",sm="+sm+",cjsj="+cjsj+",zt="+zt+",ryid="+ryid+"]";}publicStringgetDwid(){returndwid;}publicvoidsetDwid(Stringdwid){this.dwid=dwid;}publicStringgetZzmc(){returnzzmc;}publicvoidsetZzmc(Stringzzmc){this.zzmc=zzmc;}}
项目源码链接:http://down.51cto.com/data/2302295
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。