首页 > 其他分享 >spring4+springmvc+hibernate5的maven整合

spring4+springmvc+hibernate5的maven整合

时间:2022-11-30 21:09:42浏览次数:39  
标签:hibernate5 http name springmvc springframework maven dependency version org


首先,依赖包的下载,看以下的pom.xml

 

[html] ​​view plain​​ ​​copy​​

  1. <dependencies>  
  2.     <dependency>  
  3.       <groupId>junit</groupId>  
  4.       <artifactId>junit</artifactId>  
  5.       <version>4.12</version>  
  6.       <scope>test</scope>  
  7.     </dependency>  
  8.       
  9. <!-- servlet -->
  10.     <dependency>  
  11.         <groupId>javax.servlet</groupId>  
  12.         <artifactId>servlet-api</artifactId>  
  13.         <version>2.5</version>  
  14.     </dependency>  
  15.       
  16. <!-- spring -->
  17.     <dependency>  
  18.         <groupId>org.springframework</groupId>  
  19.         <artifactId>spring-core</artifactId>  
  20.         <version>4.3.1.RELEASE</version>  
  21.     </dependency>  
  22.     <dependency>  
  23.         <groupId>org.springframework</groupId>  
  24.         <artifactId>spring-context</artifactId>  
  25.         <version>4.3.1.RELEASE</version>  
  26.     </dependency>  
  27.     <dependency>  
  28.         <groupId>org.springframework</groupId>  
  29.         <artifactId>spring-beans</artifactId>  
  30.         <version>4.3.1.RELEASE</version>  
  31.     </dependency>  
  32.     <dependency>   
  33.         <groupId>org.springframework</groupId>  
  34.         <artifactId>spring-web</artifactId>  
  35.         <version>4.3.1.RELEASE</version>  
  36.     </dependency>  
  37.     <dependency>  
  38.         <groupId>org.springframework</groupId>  
  39.         <artifactId>spring-webmvc</artifactId>  
  40.         <version>4.3.1.RELEASE</version>  
  41.     </dependency>  
  42.     <dependency>  
  43.         <groupId>org.springframework</groupId>  
  44.         <artifactId>spring-aop</artifactId>  
  45.         <version>4.3.1.RELEASE</version>  
  46.     </dependency>  
  47.     <dependency>  
  48.         <groupId>org.springframework</groupId>  
  49.         <artifactId>spring-context-support</artifactId>  
  50.         <version>4.3.1.RELEASE</version>  
  51.     </dependency>  
  52.     <dependency>  
  53.         <groupId>org.springframework</groupId>  
  54.         <artifactId>spring-tx</artifactId>  
  55.         <version>4.3.1.RELEASE</version>  
  56.     </dependency>  
  57.     <dependency>  
  58.         <groupId>org.springframework</groupId>  
  59.         <artifactId>spring-oxm</artifactId>  
  60.         <version>4.3.1.RELEASE</version>  
  61.     </dependency>  
  62.     <dependency>  
  63.         <groupId>org.springframework</groupId>  
  64.         <artifactId>spring-jdbc</artifactId>  
  65.         <version>4.3.1.RELEASE</version>  
  66.     </dependency>  
  67.     <dependency>  
  68.         <groupId>org.springframework</groupId>  
  69.         <artifactId>spring-test</artifactId>  
  70.         <version>4.3.1.RELEASE</version>  
  71.         <scope>provided</scope>  
  72.     </dependency>  
  73.       
  74. <!-- spring hibernate mapping -->
  75.     <dependency>  
  76.         <groupId>org.springframework</groupId>  
  77.         <artifactId>spring-orm</artifactId>  
  78.         <version>4.3.1.RELEASE</version>  
  79.     </dependency>  
  80.       
  81. <!-- hibernate -->
  82.     <dependency>  
  83.         <groupId>org.hibernate</groupId>  
  84.         <artifactId>hibernate-core</artifactId>  
  85.         <version>5.1.0.Final</version>  
  86.     </dependency>  
  87.       
  88. <!-- mysql -->
  89.     <dependency>  
  90.         <groupId>mysql</groupId>  
  91.         <artifactId>mysql-connector-java</artifactId>  
  92.         <version>5.1.38</version>  
  93.     </dependency>  
  94.       
  95. <!-- dbcp2 连接池-->
  96.     <dependency>  
  97.         <groupId>org.apache.commons</groupId>  
  98.         <artifactId>commons-dbcp2</artifactId>  
  99.         <version>2.1.1</version>  
  100.     </dependency>  
  101.       
  102. <!-- 上传支持 -->
  103.     <dependency>  
  104.         <groupId>commons-fileupload</groupId>  
  105.         <artifactId>commons-fileupload</artifactId>  
  106.         <version>1.3.2</version>  
  107.     </dependency>  
  108.       
  109. <!-- jstl -->
  110.     <dependency>  
  111.         <groupId>jstl</groupId>  
  112.         <artifactId>jstl</artifactId>  
  113.         <version>1.2</version>  
  114.     </dependency>  
  115.     <dependency>  
  116.         <groupId>taglibs</groupId>  
  117.         <artifactId>standard</artifactId>  
  118.         <version>1.1.2</version>  
  119.     </dependency>  
  120.       
  121. <!-- jackson json -->
  122.     <dependency>  
  123.         <groupId>com.fasterxml.jackson.core</groupId>  
  124.         <artifactId>jackson-core</artifactId>  
  125.         <version>2.7.4</version>  
  126.     </dependency>  
  127.     <dependency>  
  128.         <groupId>com.fasterxml.jackson.core</groupId>  
  129.         <artifactId>jackson-databind</artifactId>  
  130.         <version>2.7.4</version>  
  131.     </dependency>  
  132.     <dependency>  
  133.         <groupId>com.fasterxml.jackson.core</groupId>  
  134.         <artifactId>jackson-annotations</artifactId>  
  135.         <version>2.7.4</version>  
  136.     </dependency>  
  137.     <dependency>  
  138.         <groupId>org.codehaus.jackson</groupId>  
  139.         <artifactId>jackson-mapper-asl</artifactId>  
  140.         <version>1.9.13</version>  
  141.     </dependency>  
  142.       
  143. <!-- jackson xml -->
  144.     <dependency>  
  145.         <groupId>com.fasterxml.jackson.dataformat</groupId>  
  146.         <artifactId>jackson-dataformat-xml</artifactId>  
  147.         <version>2.7.4</version>  
  148.     </dependency>  
  149.     <dependency>  
  150.         <groupId>org.codehaus.woodstox</groupId>  
  151.         <artifactId>woodstox-core-asl</artifactId>  
  152.         <version>4.4.1</version>  
  153.     </dependency>  
  154.     <dependency>  
  155.         <groupId>org.json</groupId>  
  156.         <artifactId>json</artifactId>  
  157.         <version>20160212</version>  
  158.     </dependency>  
  159.   </dependencies>  

以上代码中,都有简单的组件注释,在最后面的 jackson 都是用来对 json 的支持或者用在 @Responsebody 中的json支持。。

接下来是 web.xml 的配置:

 

[html] ​​view plain​​ ​​copy​​

  1. <error-page>  
  2.     <error-code>404</error-code>  
  3.     <location>/error.jsp</location>  
  4.  </error-page>  
  5.  <error-page>  
  6.     <error-code>500</error-code>  
  7.     <location>/notfound.jsp</location>  
  8.  </error-page>  
  9.    
  10. <!-- springmvc -->
  11.  <servlet>  
  12.     <servlet-name>springmvc</servlet-name>  
  13.     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  14.     <init-param>  
  15.         <param-name>contextConfigLocation</param-name>  
  16.         <param-value>classpath:spring-mvc.xml</param-value>  
  17.     </init-param>  
  18.  </servlet>  
  19.  <servlet-mapping>  
  20.     <servlet-name>springmvc</servlet-name>  
  21.     <url-pattern>*.do</url-pattern>  
  22.  </servlet-mapping>  
  23.    
  24. <!-- 编码过滤器 -->
  25.  <filter>  
  26.     <filter-name>encodingFilter</filter-name>  
  27.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  28.     <init-param>  
  29.         <param-name>encoding</param-name>  
  30.         <param-value>UTF-8</param-value>  
  31.     </init-param>  
  32.  </filter>  
  33.  <filter-mapping>  
  34.     <filter-name>encodingFilter</filter-name>  
  35.     <url-pattern>*.do</url-pattern>  
  36.  </filter-mapping>  
  37.    
  38. <!-- spring 容器启动配置 -->
  39.  <listener>  
  40.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  41.  </listener>  
  42.  <context-param>  
  43.      <param-name>contextConfigLocation</param-name>  
  44.      <param-value>classpath:spring-context.xml</param-value>  
  45.  </context-param>  
  46.    
  47. <!-- spring beans的防止内存泄露 -->
  48.  <listener>  
  49.     <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  50.  </listener>  

简单易懂的注释,我就不多说了。。

 

 

spring-mvc.xml:

 

[html] ​​view plain​​ ​​copy​​

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:s​​chemaLocation="  
  7.             http://www.springframework.org/schema/beans  
  8.             http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  9.             http://www.springframework.org/schema/context  
  10.             http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  11.             http://www.springframework.org/schema/mvc  
  12.             http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">  
  13. ​      ​​​
  14. <!-- 扫描器 -->
  15.     <mvc:annotation-driven />  
  16.   
  17.     <context:component-scan base-package="cn.scau.controller" />  
  18. <!-- 静态资源处理 -->
  19.     <mvc:default-servlet-handler />  
  20.       
  21. <!-- 视图 -->
  22.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  23.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
  24.         <property name="prefix" value="/" />  
  25.         <property name="suffix" value=".jsp" />  
  26.     </bean>  
  27.       
  28. <!-- 上传 -->
  29.     <bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
  30. id="multipartResolver">  
  31.         <property name="resolveLazily" value="true" />  
  32.         <property name="maxUploadSize" value="20971520" />  
  33.         <property name="defaultEncoding" value="utf-8" />       
  34.     </bean>  
  35. </beans>  

 

 

spring-context.xml:

 

[html] ​​view plain​​ ​​copy​​

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:p="http://www.springframework.org/schema/p"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans   
  9.        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
  10.        http://www.springframework.org/schema/aop  
  11.        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  
  12.        http://www.springframework.org/schema/context  
  13.        http://www.springframework.org/schema/context/spring-context-4.3.xsd  
  14.        http://www.springframework.org/schema/tx  
  15.        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">  
  16. ​      ​
  17. <!-- 连接池 -->
  18.     <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
  19. destroy-method="close">  
  20.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  21.         <property name="url" value="jdbc:mysql://localhost:3306/fspc?useUnicode=true&characterEncoding=utf-8" />  
  22.         <property name="username" value="root" />  
  23.         <property name="password" value="" />  
  24.     </bean>  
  25.       
  26. <!-- hibernate 配置 -->
  27.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  28.         <property name="dataSource" ref="dataSource" />  
  29.         <property name="hibernateProperties">  
  30.             <props>  
  31.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  32.                 <prop key="hibernate.show_sql">true</prop>  
  33.                 <prop key="hibernate.format_sql">true</prop>  
  34.                 <prop key="hibernate.hbm2ddl.auto">update</prop>  
  35.             </props>  
  36.         </property>  
  37. <!-- 扫描实体类的位置 -->
  38.         <property name="packagesToScan" value="cn.scau.entity" />  
  39.     </bean>  
  40.       
  41. <!-- 配置Spring的事务管理器 -->
  42.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  43.         <property name="dataSource" ref="dataSource" />  
  44.     </bean>  
  45.       
  46. <!-- 注解事务 -->
  47.     <tx:annotation-driven transaction-manager="transactionManager" />  
  48.       
  49. <!-- 开启Annotation -->
  50.     <context:annotation-config/>  
  51.       
  52. <!-- 让Spring通过自动扫描来查询和管理Bean -->
  53.     <context:component-scan base-package="cn.scau.serviceImp,cn.scau.dao"/>  
  54.       
  55. </beans>  

以上就是我整合的配置。

 

不过在整合过程中,遇到一个很大的问题,,就是在web.xml中,如果没有加一行代码,会一直报下面这个错误

 

[html] ​​view plain​​ ​​copy​​

  1. java.lang.UnsupportedClassVersionError: com/mysql/jdbc/JDBC42CallableStatement : Unsupported major.minor version 52.0  

这个东西真的让人困扰,我装了 JDK1.8 和 JDK1.7  都没有什么用,只是在web.xml的头配置中缺少了一行代码(metadata-complete="true" )。下面就是我的 web.xml 的头代码

 

[html] ​​view plain​​ ​​copy​​

  1. <web-app   
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  6. id="WebApp_ID"
  7. metadata-complete="true"
  8. version="3.0">  
  9.  

标签:hibernate5,http,name,springmvc,springframework,maven,dependency,version,org
From: https://blog.51cto.com/u_13363083/5900500

相关文章