Spring MVC框架搭建(spring4.0+hibernate4.3)

Spring下载地址: http://repo.springsource.org/libs-release-local/org/springframework/spring/

 

Hhibernate下载地址: http://sourceforge.net/projects/hibernate/files/

 

 

1. jar包引入

 

spring-framework-4.0.0.RELEASE18个):libs文件夹中除却spring-build-src-4.0.0.RELEASE.jarspring-framework-bom-4.0.0.RELEASE.jarspring-messaging-4.0.0.RELEASE.jarspring-websocket-4.0.0.RELEASE.jar四个jar包,其它全部导入,共18jar包。

 

spring依赖包aopalliance.jarcommons-logging-1.1.1.jarlog4j-1.2.15.jar

 

hibernate-release-4.3.0.Final10个):lib文件夹中required文件夹下的10jar包。

 

hibernate 缓存包(4个)ehcache-core-2.4.3.jarhibernate-ehcache-4.3.0.Final.jarslf4j-api-1.7.5.jarslf4j-log4j12-1.7.5.jar

 

其他工具包(9个)commons-fileupload-1.3.jarcommons-io-2.4.jarcommons-lang3-3.2.jarcommons-beanutils-1.9.0.jarcommons-dbcp-1.4.jarcommons-pool-1.6.jarpager-taglib.jarcommons-collections-3.2.1.jarmysql-connector-java-5.1.27.jar

 

 

  1. 配置文件

(1) web.xml配置

 

关于filter的使用,可以参考:Spring MVC过滤器-登录过滤

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
  2. <web-appversionweb-appversion="3.0"  
  3. xmlns="http://java.sun.com/xml/ns/javaee"  
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
  6. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  7. <display-name>SpringMVC</display-name>          
  8. <welcome-file-list>  
  9. <welcome-file>index.html</welcome-file>  
  10. <welcome-file>index.htm</welcome-file>  
  11. <welcome-file>index.jsp</welcome-file>  
  12. <welcome-file>default.html</welcome-file>  
  13. <welcome-file>default.htm</welcome-file>  
  14. <welcome-file>default.jsp</welcome-file>  
  15. </welcome-file-list>  
  16.    
  17. <!--加载spring的配置文件 -->  
  18. <context-param>  
  19. <param-name>contextConfigLocation</param-name>  
  20. <param-value>classpath*:config/applicationContext-*.xml</param-value>  
  21. </context-param>  
  22.    
  23. <listener>  
  24. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  25. </listener>  
  26.    
  27. <!--springMVC 框架加载服务 -->  
  28. <servlet>  
  29. <servlet-name>springMVC</servlet-name>  
  30. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  31. <init-param>  
  32. <param-name>contextConfigLocation</param-name>  
  33. <param-value>classpath*:config/applicationContext-springmvc.xml</param-value>  
  34. </init-param>  
  35. <load-on-startup>1</load-on-startup>  
  36. </servlet>  
  37. <servlet-mapping>  
  38. <servlet-name>springMVC</servlet-name>  
  39. <url-pattern>/</url-pattern>  
  40. </servlet-mapping>  
  41.    
  42. <!--filter配置,解决编码问题 -->  
  43. <filter>  
  44. <filter-name>encodingFilter</filter-name>  
  45. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  46. <init-param>  
  47. <param-name>encoding</param-name>  
  48. <param-value>UTF-8</param-value>  
  49. </init-param>  
  50. <init-param>  
  51. <param-name>forceEncoding</param-name>  
  52. <param-value>true</param-value>  
  53. </init-param>  
  54. </filter>  
  55. <!--encoding filter for jsp page -->  
  56. <filter-mapping>  
  57. <filter-name>encodingFilter</filter-name>  
  58. <url-pattern>/*</url-pattern>  
  59. </filter-mapping>  
  60.    
  61. <!--sessionFilter配置,解决session过滤问题 -->  
  62. <filter>  
  63. <filter-name>sessionFilter</filter-name>  
  64. <filter-class>com.springmvc.core.web.filter.SessionFilter</filter-class>  
  65. <init-param>  
  66. <param-name>notFilter</param-name>  
  67. <param-value>/skin/,/index.jsp, /mvc/index, /login, /logout</param-value>  
  68. </init-param>  
  69. </filter>  
  70. <filter-mapping>  
  71. <filter-name>sessionFilter</filter-name>  
  72. <url-pattern>/*</url-pattern>  
  73. </filter-mapping>  
  74.    
  75. <!--OpenSessionInViewFilter配置,解决延迟加载时Session会关闭的问题 -->  
  76. <filter>  
  77. <filter-name>openSession</filter-name>  
  78. <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>  
  79. </filter>  
  80.    
  81. <filter-mapping>  
  82. <filter-name>openSession</filter-name>  
  83. <url-pattern>/*</url-pattern>  
  84. </filter-mapping>  
  85.    
  86. <!--配置PagerFilter,用来获取分页数据 -->  
  87. <filter>  
  88. <filter-name>pagerFilter</filter-name>  
  89. <filter-class>com.springmvc.core.web.filter.PagerFilter</filter-class>  
  90. </filter>  
  91. <filter-mapping>  
  92. <filter-name>pagerFilter</filter-name>  
  93. <url-pattern>/*</url-pattern>  
  94. </filter-mapping>  
  95.    
  96. <!--session time out 23*60 -->  
  97. <session-config>   
  98.         <session-timeout>1380</session-timeout>   
  99.           </session-config>  
  100. </web-app>  

 

(2)applicationContext-springmvc.xml配置

 

此为spring核心配置文件

关于spring使用annotation注解,可以参考:1. 基于注解的 Spring MVC 简单入门  2. 基于注解的SpringMVC

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
  2. <!--  
  3. -DispatcherServlet application context for DeclareOnline's web tier.  
  4.    
  5. springmvc 的 DispatcherServlet 对应的配置文件  
  6. -->  
  7. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"   
  8.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  9.    xmlns:p="http://www.springframework.org/schema/p"   
  10.    xmlns:context="http://www.springframework.org/schema/context"   
  11.    xmlns:mvc="http://www.springframework.org/schema/mvc"   
  12.     xsi:schemaLocation="    
  13.         http://www.springframework.org/schema/beans     
  14.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  15.         http://www.springframework.org/schema/context     
  16.         http://www.springframework.org/schema/context/spring-context-3.0.xsd    
  17.         http://www.springframework.org/schema/mvc     
  18.         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">   
  19.    
  20.    
  21. <!--  
  22. -The controllers are autodetected POJOs labeled with the @Controller annotation.  
  23. Springmvc将在该路径下寻找Controller(有Controller注释的类)  
  24. -->  
  25. <context:component-scanbase-packagecontext:component-scanbase-package="com.springmvc.*"/>  
  26.    
  27. <!--开启注解 -->  
  28. <mvc:annotation-driven/>  
  29. <!--使用依赖控制器类名字的惯例,将URI映射到控制器(开启注解的另一种方式) -->  
  30. <!--  
  31. <beanclassbeanclass="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>  
  32. <beanclassbeanclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>  
  33. 启动SpringMVC的注解功能,完成请求和注解POJO的映射  
  34. <beanclassbeanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>  
  35. -->  
  36.    
  37. <!--静态资源访问 -->  
  38. <mvc:resourceslocationmvc:resourceslocation="/skin/" mapping="/skin/**"/>   
  39.    
  40. <!--定义Spring MVC 的模板文件 -->  
  41. <beanidbeanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  42. <!--支持JSTL -->  
  43. <propertynamepropertyname="viewClass"value="org.springframework.web.servlet.view.JstlView"/>  
  44. <!--模板的路径 -->  
  45. <propertynamepropertyname="prefix" value="/" />  
  46. <!--模板文件后缀 -->  
  47. <propertynamepropertyname="suffix" value=".jsp" />  
  48. </bean>  
  49.    
  50.     <!-- Spring MVC 文件上传配置 -->  
  51.     <beanidbeanid="multipartResolver"      
  52.          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"      
  53.           p:defaultEncoding="utf-8">    
  54.   <propertynamepropertyname="maxUploadSize">  
  55. <value>10485760000</value>  
  56. </property>  
  57. <propertynamepropertyname="maxInMemorySize">  
  58. <value>40960</value>  
  59. </property>          
  60.    
  61. </bean>  
  62. </beans>  


 

(3)applicationContext-hibernate.xml配置

 

A. 此文件是spring方式的hibernate连接数据库的配置

 

spring配置数据库连接方式有很多种,可以参考一下: 在Spring中配置DataSource的6种方法

此处使用了Spring利用PropertyPlaceholderConfigurer占位符,参考文章: Spring 利用PropertyPlaceholderConfigurer占位符

 

Spring事务配置的方式也有多中,可以参考: Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别

applicationContext-hibernate.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
  2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:aop="http://www.springframework.org/schema/aop"  
  4. xmlns:context="http://www.springframework.org/schema/context"  
  5. xmlns:p="http://www.springframework.org/schema/p"  
  6. xmlns:tx="http://www.springframework.org/schema/tx"  
  7.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  8.       xmlns:util="http://www.springframework.org/schema/util"  
  9.       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  10.               http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
  11.               http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd  
  12.               http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  13. http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.0.xsd"default-lazy-init="false">  
  14.    
  15. <!--======================== property configure本配置文件负责加载变量信息必须在在spring配置文件的最前面加载(第一个加载) ======================== -->  
  16. <!--载入properties配置文件,以${key}的方式引用 -->  
  17. <beanidbeanid="propertyConfigurer"  
  18. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  19. <propertynamepropertyname="locations">  
  20. <list>  
  21. <value>classpath:conf/mvcdb.properties</value>  
  22. </list>  
  23. </property>  
  24. </bean>  
  25.    
  26. <!--========================   dataconfigure   ==========================-->  
  27. <!--配置连接池(jdbc方式) -->  
  28. <!--<bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  29. <propertynamepropertyname="driverClassName" value="${mvcdb.mysql.driver}"/>  
  30. <propertynamepropertyname="url" value="${mvcdb.mysql.url}"/>  
  31. <propertynamepropertyname="username" value="${mvcdb.mysql.user}"/>  
  32. <propertynamepropertyname="password" value="${mvcdb.mysql.password}"/>  
  33. </bean>-->  
  34.    
  35. <beanidbeanid="dataSource"  
  36. class="org.apache.commons.dbcp.BasicDataSource"  
  37. destroy-method="close">  
  38. <propertynamepropertyname="driverClassName">  
  39. <value>${mvcdb.mysql.driver}</value>  
  40. </property>  
  41. <propertynamepropertyname="url">  
  42. <value>${mvcdb.mysql.url}</value>  
  43. </property>  
  44. <propertynamepropertyname="username">  
  45. <value>${mvcdb.mysql.user}</value>  
  46. </property>  
  47. <propertynamepropertyname="password">  
  48. <value>${mvcdb.mysql.password}</value>  
  49. </property>  
  50. <propertynamepropertyname="initialSize">  
  51. <value>${mvcdb.mysql.initialSize}</value>  
  52. </property>  
  53. <propertynamepropertyname="maxActive">  
  54. <value>${mvcdb.mysql.maxActive}</value>  
  55. </property>  
  56.     <property name="minIdle">  
  57. <value>${mvcdb.mysql.maxActive}</value>  
  58. </property>  
  59. <propertynamepropertyname="maxIdle">  
  60. <value>${mvcdb.mysql.maxActive}</value>  
  61. </property>  
  62. <propertynamepropertyname="maxWait">  
  63. <value>${mvcdb.mysql.maxWait}</value>  
  64. </property>  
  65. <propertynamepropertyname="testOnBorrow">  
  66. <value>${mvcdb.mysql.testOnBorrow}</value>  
  67. </property>  
  68.                  <propertynamepropertyname="testWhileIdle">  
  69. <value>${mvcdb.mysql.testWhileIdle}</value>  
  70. </property>  
  71.                  <propertynamepropertyname="validationQuery">  
  72. <value>${mvcdb.mysql.validationQuery}</value>  
  73. </property>  
  74. </bean>  
  75.    
  76.    
  77. <!--配置hibernate 上下文 -->  
  78. <beanidbeanid="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  79. <propertynamepropertyname="dataSource" ref="dataSource" />  
  80.    
  81. <!--包级扫描 hibernate实体类 -->  
  82. <propertynamepropertyname="configLocations">  
  83.   <list>  
  84. <!--<value>com.springmvc.*</value> -->  
  85.       <value>classpath*:com/springmvc/mvcdev/userInfo/hibernate/hibernate.cfg.userInfo.xml</value>  
  86.   </list>  
  87. </property>  
  88.    
  89. <propertynamepropertyname="hibernateProperties">  
  90. <props>  
  91. <propkeypropkey="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>  
  92. <propkeypropkey="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>  
  93. <propkeypropkey="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>  
  94. <propkeypropkey="hibernate.cache.use_query_cache">false</prop>  
  95. <propkeypropkey="hibernate.show_sql">true</prop>  
  96. <!--<prop key="hibernate.hbm2ddl.auto">update</prop>  -->  
  97. <propkeypropkey="hibernate.cache.use_second_level_cache">false</prop>  
  98. <propkeypropkey="hibernate.max_fetch_depth">2</prop>  
  99. <propkeypropkey="hibernate.jdbc.fetch_size">50</prop>  
  100. <propkeypropkey="hibernate.jdbc.batch_size">50</prop>  
  101. <propkeypropkey="hibernate.use_outer_join">false</prop>  
  102. <propkeypropkey="hibernate.format_sql">true</prop>  
  103. </props>  
  104. </property>  
  105. </bean>  
  106.      
  107.     <!-- 配置jdbc事务 -->  
  108. <beanidbeanid="transactionManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  109. <propertynamepropertyname="sessionFactory">  
  110. <reflocalreflocal="sessionFactory" />  
  111. </property>  
  112. </bean>  
  113.    
  114. <!--配置启用事务的动态代理 -->  
  115. <beanidbeanid="baseTransactionProxy"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"abstract="true">  
  116. <propertynamepropertyname="transactionManager">  
  117. <refbeanrefbean="transactionManager" />  
  118. </property>  
  119. <propertynamepropertyname="proxyTargetClass" value="true"/>  
  120. <propertynamepropertyname="transactionAttributes">  
  121. <props>  
  122. <propkeypropkey="insert*">PROPAGATION_REQUIRED,-Exception</prop>  
  123. <propkeypropkey="active*">PROPAGATION_REQUIRED,-Exception</prop>  
  124. <propkeypropkey="end*">PROPAGATION_REQUIRED,-Exception</prop>  
  125. <propkeypropkey="lock*">PROPAGATION_REQUIRED,-Exception</prop>  
  126. <propkeypropkey="unLock*">PROPAGATION_REQUIRED,-Exception</prop>  
  127. <propkeypropkey="update*">PROPAGATION_REQUIRED,-Exception</prop>  
  128. <propkeypropkey="modify*">PROPAGATION_REQUIRED,-Exception</prop>  
  129. <propkeypropkey="delete*">PROPAGATION_REQUIRED,-Exception</prop>  
  130. <propkeypropkey="remove*">PROPAGATION_REQUIRED,-Exception</prop>  
  131. <propkeypropkey="del*">PROPAGATION_REQUIRED,-Exception</prop>  
  132. <propkeypropkey="add*">PROPAGATION_REQUIRED,-Exception</prop>  
  133. <propkeypropkey="save*">PROPAGATION_REQUIRED,-Exception</prop>  
  134. <propkeypropkey="set*">PROPAGATION_REQUIRED,-Exception</prop>  
  135. <propkeypropkey="exchage*">PROPAGATION_REQUIRED,-Exception</prop>  
  136. <propkeypropkey="sort*">PROPAGATION_REQUIRED,-Exception</prop>  
  137. <propkeypropkey="do*">PROPAGATION_REQUIRED,-Exception</prop>  
  138. <propkeypropkey="undo*">PROPAGATION_REQUIRED,-Exception</prop>  
  139. <propkeypropkey="transact*">PROPAGATION_REQUIRED,-Exception</prop>  
  140. <propkeypropkey="enrolment*">PROPAGATION_REQUIRED,-Exception</prop>  
  141. <propkeypropkey="accept*">PROPAGATION_REQUIRED,-Exception</prop>  
  142. <propkeypropkey="init*">PROPAGATION_REQUIRED,-Exception</prop>  
  143. <propkeypropkey="edit*">PROPAGATION_REQUIRED,-Exception</prop>  
  144. <propkeypropkey="create*">PROPAGATION_REQUIRED,-Exception</prop>  
  145. <propkeypropkey="support*">PROPAGATION_REQUIRED,-Exception</prop>  
  146. <propkeypropkey="active*">PROPAGATION_REQUIRED,-Exception</prop>  
  147. <propkeypropkey="mapping*">PROPAGATION_REQUIRED,-Exception</prop>  
  148. <propkeypropkey="reset*">PROPAGATION_REQUIRED,-Exception</prop>  
  149. <propkeypropkey="change*">PROPAGATION_REQUIRED,-Exception</prop>  
  150. <propkeypropkey="submit*">PROPAGATION_REQUIRED,-Exception</prop>  
  151. <propkeypropkey="draft*">PROPAGATION_REQUIRED,-Exception</prop>  
  152. <propkeypropkey="assign*">PROPAGATION_REQUIRED,-Exception</prop>  
  153. <propkeypropkey="close*">PROPAGATION_REQUIRED,-Exception</prop>  
  154. <propkeypropkey="suspend*">PROPAGATION_REQUIRED,-Exception</prop>  
  155. <propkeypropkey="publish*">PROPAGATION_REQUIRED,-Exception</prop>  
  156. <propkeypropkey="revoke*">PROPAGATION_REQUIRED,-Exception</prop>  
  157. <propkeypropkey="print*">PROPAGATION_REQUIRED,-Exception</prop>                                  
  158. <propkeypropkey="undeploy*">PROPAGATION_REQUIRED,-Exception</prop>  
  159. <propkeypropkey="suspend*">PROPAGATION_REQUIRED,-Exception</prop>  
  160. <propkeypropkey="resume*">PROPAGATION_REQUIRED,-Exception</prop>  
  161. <propkeypropkey="rand*">PROPAGATION_REQUIRED,-Exception</prop>  
  162. <propkeypropkey="send*">PROPAGATION_REQUIRED,-Exception</prop>  
  163. <propkeypropkey="*">PROPAGATION_REQUIRED,readOnly</prop>  
  164. </props>  
  165. </property>  
  166. </bean>  
  167. </beans>  


 

B.此文件需要在mvcdb.properties文件中配置数据库的相关信息。


mvcdb.properties

[plain]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #=================================================  
  2. #mysql connection parameters  
  3. #=================================================  
  4. mvcdb.mysql.driver=com.mysql.jdbc.Driver  
  5. mvcdb.mysql.url=jdbc:mysql://localhost:3306/zszx?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true  
  6. mvcdb.mysql.user=root  
  7. mvcdb.mysql.password=123456  
  8.    
  9. mvcdb.mysql.initialSize=2  
  10. mvcdb.mysql.maxActive=10000  
  11. mvcdb.mysql.minIdle=5  
  12. mvcdb.mysql.maxIdle=2   
  13. mvcdb.mysql.maxWait=600000  
  14.    
  15. mvcdb.mysql.testOnReturn=false  
  16. mvcdb.mysql.testWhileIdle=true  
  17.    
  18. mvcdb.mysql.testOnBorrow=true  
  19. mvcdb.mysql.validationQuery=select1  


 

C.此文件扫描hibernate实体时扫描的文件,此处以hibernate.cfg.userInfo.xml为例。

 

hibernate.cfg.userInfo.xml


[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <!DOCTYPEhibernate-configuration PUBLIC  
  2. "-//Hibernate/HibernateConfiguration DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  4. <hibernate-configuration>  
  5. <session-factory>  
  6. <!--用户映射实体 -->  
  7. <mappingclassmappingclass="com.springmvc.mvcdev.userInfo.domain.Userinfo"/>  
  8. </session-factory>  
  9. </hibernate-configuration>  


D.Userinfo实体类使用注解方式关联数据库。


Hibernate 使用注解,可以参考:1. Hibernate Annotation   2.Hibernate4实战之Hibernate4注解零配置

Userinfo.java

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. packagecom.springmvc.mvcdev.userInfo.domain;  
  2.    
  3. importjava.io.Serializable;  
  4.    
  5. importjavax.persistence.Column;  
  6. importjavax.persistence.Entity;  
  7. importjavax.persistence.Id;  
  8. importjavax.persistence.Table;  
  9.    
  10. /** 
  11.  * 用户实体类 
  12.  * 
  13.  * @ClassName: Userinfo 
  14.  * @author: QuBinBin 
  15.  * @date: 2014-1-3 上午10:53:36 
  16.  * 
  17.  */  
  18.    
  19. @Entity  
  20. @Table(name= "userinfo")  
  21. publicclass Userinfo implements Serializable {  
  22. privatestatic final long serialVersionUID = -8047186992943452353L;  
  23.    
  24. /** 
  25.  * 主键ID 
  26.  */  
  27. @Id  
  28. @Column(name= "ID", unique = true, nullable = false)  
  29. privateInteger id;  
  30.    
  31. /** 
  32.  * 用户账号 
  33.  */  
  34. @Column(name= "NAME", length = 200)  
  35. privateString name;  
  36.    
  37. /** 
  38.  * 用户真实姓名 
  39.  */  
  40. @Column(name= "TRUENAME", length = 200)  
  41. privateString truename;  
  42.    
  43. /** 
  44.  * 用户密码 
  45.  */  
  46. @Column(name= "PWD", length = 100)  
  47. privateString pwd;  
  48.    
  49. /** 
  50.  * Email 
  51.  */  
  52. @Column(name= "EMAIL", length = 50)  
  53. privateString email;  
  54.    
  55. /** 
  56.  * 手机 
  57.  */  
  58. @Column(name= "MOBILE", length = 20)  
  59. privateString mobile;  
  60.    
  61. /** 
  62.  * 状态(1激活 0禁用) 
  63.  */  
  64. @Column(name= "STATUS")  
  65. privateInteger status;  
  66.    
  67. publicInteger getId() {  
  68. returnthis.id;  
  69. }  
  70.    
  71. publicvoid setUserId(Integer id) {  
  72. this.id= id;  
  73. }  
  74.    
  75. publicString getName() {  
  76. returnthis.name;  
  77. }  
  78.    
  79. publicvoid setName(String name) {  
  80. this.name= name;  
  81. }  
  82.    
  83. publicString getTruename() {  
  84. returnthis.truename;  
  85. }  
  86.    
  87. publicvoid setTruename(String truename) {  
  88. this.truename= truename;  
  89. }  
  90.    
  91. publicString getPwd() {  
  92. returnthis.pwd;  
  93. }  
  94.    
  95. publicvoid setPwd(String pwd) {  
  96. this.pwd= pwd;  
  97. }  
  98.    
  99. publicString getEmail() {  
  100. returnemail;  
  101. }  
  102.    
  103. publicvoid setEmail(String email) {  
  104. this.email= email;  
  105. }  
  106.    
  107. publicString getMobile() {  
  108. returnmobile;  
  109. }  
  110.    
  111. publicvoid setMobile(String mobile) {  
  112. this.mobile= mobile;  
  113. }  
  114.    
  115. publicInteger getStatus() {  
  116. returnthis.status;  
  117. }  
  118.    
  119. publicvoid setStatus(Integer status) {  
  120. this.status= status;  
  121. }  
  122. }  


 

(4)applicationContext-import.xml配置

 

此文件配置依赖注入的内容

applicationContext-import.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
  2.    
  3. <!--  
  4.   - Application context definition forJPetStore's business layer.  
  5.   - Contains bean references to the transactionmanager and to the DAOs in  
  6.   - dataAccessContext-local/jta.xml (seeweb.xml's "contextConfigLocation").  
  7.   -->  
  8. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"  
  9.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  10.       xmlns:context="http://www.springframework.org/schema/context"  
  11.       xmlns:aop="http://www.springframework.org/schema/aop"  
  12.       xmlns:tx="http://www.springframework.org/schema/tx"  
  13.        xsi:schemaLocation="  
  14.            http://www.springframework.org/schema/beans  
  15.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  16.            http://www.springframework.org/schema/context  
  17.            http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  18.            http://www.springframework.org/schema/tx  
  19.            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  20.            http://www.springframework.org/schema/aop  
  21.            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"  
  22.            >  
  23. <importresourceimportresource="classpath*:com/springmvc/mvcdev/userInfo/spring/applicationContext-userInfo.xml"/>  
  24.    
  25. </beans>  


 

 

具体注入内容在import的每个模块下的xml文件中。此处以applicationContext-userInfo.xml为例。

applicationContext-userInfo.xml

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xmlversionxmlversion="1.0" encoding="UTF-8"?>  
  2. <beansxmlnsbeansxmlns="http://www.springframework.org/schema/beans"  
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.       xmlns:util="http://www.springframework.org/schema/util"  
  5.       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  6. http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.0.xsd">  
  7.    
  8.    
  9. <!--******************************用户********************* -->  
  10.    
  11. <!--Hibernate 用户持久化访问接口 -->  
  12. <beanidbeanid="userInfoDAO"class="com.springmvc.mvcdev.userInfo.dao.impl.UserInfoDAOImpl">  
  13. <propertynamepropertyname="sessionFactory" ref="sessionFactory"/>  
  14. </bean>  
  15. <beanidbeanid="userInfoServiceImpl"class="com.springmvc.mvcdev.userInfo.service.impl.UserInfoServiceImpl">  
  16. <propertynamepropertyname="userInfoDAO" ref="userInfoDAO"></property>  
  17. </bean>  
  18. <!--事务控制 -->  
  19. <beanidbeanid="userInfoService" parent="baseTransactionProxy">  
  20. <propertynamepropertyname="target" ref="userInfoServiceImpl"/>  
  21. </bean>  
  22.    
  23. </beans>  

 

  1. 参考资料

 

Spring MVC 教程,快速入门,深入分析 http://elf8848.iteye.com/blog/875830/

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值