使用IDEA整合spring4+spring mvc+hibernate

时间:2022-02-14 23:16:53

配置文件 
spring-mvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:contenxt="http://www.springframework.org/schema/context"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/mvc
10         http://www.springframework.org/schema/mvc/spring-mvc.xsd
11         http://www.springframework.org/schema/context
12         http://www.springframework.org/schema/context/spring-context.xsd">
13 
14     <!-- 扫描Controller包 -->
15     <contenxt:component-scan base-package="com.sm1234.controller"/>
16 
17     <!-- 开启注映射器和适配器 -->
18     <mvc:annotation-driven></mvc:annotation-driven>
19 
20     <!-- 视图解析器 -->
21     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
22         <property name="prefix" value="/WEB-INF/jsp/"></property>
23         <property name="suffix" value=".jsp"></property>
24     </bean>
25 
26 </beans>

spring.xml配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:contenxt="http://www.springframework.org/schema/context"
 6     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans
 9         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://www.springframework.org/schema/mvc
11         http://www.springframework.org/schema/mvc/spring-mvc.xsd
12         http://www.springframework.org/schema/context
13         http://www.springframework.org/schema/context/spring-context.xsd
14          http://www.springframework.org/schema/tx
15         http://www.springframework.org/schema/tx/spring-tx.xsd">
16 
17     <!-- 加载jdbc.properties -->
18     <contenxt:property-placeholder location="classpath:jdbc.properties"/>
19 
20     <!-- 创建c3p0连接池 -->
21     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
22         <property name="jdbcUrl" value="${jdbc.url}"/>
23         <property name="driverClass" value="${jdbc.driver_class}"/>
24         <property name="user" value="${jdbc.user}"/>
25         <property name="password" value="${jdbc.password}"/>
26     </bean>
27 
28     <!-- Spring整合Hibernate -->
29     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
30         <property name="dataSource" ref="dataSource"/>
31         <property name="hibernateProperties">
32             <props>
33                 <!-- 是否输出sql语句 -->
34                 <prop key="hibernate.show_sql">true</prop>
35                 <!-- 自动维护表 -->
36                 <prop key="hibernate.hbm2ddl.auto">update</prop>
37             </props>
38         </property>
39         <property name="packagesToScan">
40             <!-- 扫描实体所在的包 -->
41             <list>
42                 <value>com.sm1234.domain</value>
43             </list>
44         </property>
45     </bean>
46 
47     <!-- 开启Spring的事务管理 -->
48     <tx:annotation-driven transaction-manager="transactionManager"/>
49     <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
50         <property name="sessionFactory" ref="sessionFactory"/>
51     </bean>
52 
53     <!-- 开启Spring注解扫描 -->
54     <contenxt:component-scan base-package="com.sm1234.dao,com.sm1234.service"/>
55 
56 </beans>

jdbc.properties配置文件

1 jdbc.url=jdbc:mysql://localhost:3306/test
2 jdbc.driver_class=com.mysql.jdbc.Driver
3 jdbc.user=root
4 jdbc.password=6527293

web.xml配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>ch01.spring-mvc</display-name>
 4 
 5 
 6   <filter>
 7     <filter-name>CharacterEncodingFilter</filter-name>
 8     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
 9     <init-param>
10       <param-name>encoding</param-name>
11       <param-value>UTF-8</param-value>
12     </init-param>
13   </filter>
14   <filter-mapping>
15     <filter-name>CharacterEncodingFilter</filter-name>
16     <url-pattern>/*</url-pattern>
17   </filter-mapping>
18 
19   <!-- 请求方法的过滤器 -->
20   <filter>
21     <filter-name>HiddenHttpMethodFilter</filter-name>
22     <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
23   </filter>
24   <filter-mapping>
25     <filter-name>HiddenHttpMethodFilter</filter-name>
26     <url-pattern>/*</url-pattern>
27   </filter-mapping>
28 
29 
30   <!-- SpringMVC的核心控制器 -->
31   <servlet>
32     <servlet-name>DispatcherServlet</servlet-name>
33     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
34     <init-param>
35       <param-name>contextConfigLocation</param-name>
36       <param-value>classpath:spring-mvc.xml</param-value>
37     </init-param>
38   </servlet>
39   <servlet-mapping>
40     <servlet-name>DispatcherServlet</servlet-name>
41     <url-pattern>*.action</url-pattern>
42   </servlet-mapping>
43 
44   <!-- Spring启动监听器 -->
45   <listener>
46     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
47   </listener>
48   <context-param>
49     <param-name>contextConfigLocation</param-name>
50     <param-value>classpath:spring.xml</param-value>
51   </context-param>
52 
53 
54   <welcome-file-list>
55     <welcome-file>index.html</welcome-file>
56     <welcome-file>index.htm</welcome-file>
57     <welcome-file>index.jsp</welcome-file>
58     <welcome-file>default.html</welcome-file>
59     <welcome-file>default.htm</welcome-file>
60     <welcome-file>default.jsp</welcome-file>
61   </welcome-file-list>
62 </web-app>

pom.xml依赖文件

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5   <modelVersion>4.0.0</modelVersion>
  6 
  7   <groupId>com.sm1234</groupId>
  8   <artifactId>ch01_spring_mvc</artifactId>
  9   <version>1.0-SNAPSHOT</version>
 10   <packaging>war</packaging>
 11 
 12   <name>ch01_spring_mvc Maven Webapp</name>
 13   <!-- FIXME change it to the project's website -->
 14   <url>http://www.example.com</url>
 15 
 16   <properties>
 17     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 18     <maven.compiler.source>1.7</maven.compiler.source>
 19     <maven.compiler.target>1.7</maven.compiler.target>
 20   </properties>
 21 
 22   <dependencies>
 23     <dependency>
 24       <groupId>org.springframework</groupId>
 25       <artifactId>spring-core</artifactId>
 26       <version>4.3.16.RELEASE</version>
 27     </dependency>
 28 
 29     <dependency>
 30       <groupId>org.springframework</groupId>
 31       <artifactId>spring-web</artifactId>
 32       <version>4.3.16.RELEASE</version>
 33     </dependency>
 34 
 35     <dependency>
 36       <groupId>org.springframework</groupId>
 37       <artifactId>spring-aop</artifactId>
 38       <version>4.3.16.RELEASE</version>
 39     </dependency>
 40 
 41     <dependency>
 42       <groupId>org.springframework</groupId>
 43       <artifactId>spring-context-support</artifactId>
 44       <version>4.3.16.RELEASE</version>
 45     </dependency>
 46 
 47     <dependency>
 48       <groupId>org.springframework</groupId>
 49       <artifactId>spring-tx</artifactId>
 50       <version>4.3.16.RELEASE</version>
 51     </dependency>
 52 
 53     <dependency>
 54       <groupId>org.springframework</groupId>
 55       <artifactId>spring-orm</artifactId>
 56       <version>4.3.16.RELEASE</version>
 57     </dependency>
 58 
 59     <dependency>
 60       <groupId>org.springframework</groupId>
 61       <artifactId>spring-jdbc</artifactId>
 62       <version>4.3.16.RELEASE</version>
 63     </dependency>
 64 
 65     <dependency>
 66       <groupId>org.hibernate</groupId>
 67       <artifactId>hibernate-core</artifactId>
 68       <version>5.2.2.Final</version>
 69     </dependency>
 70 
 71     <dependency>
 72       <groupId>com.mchange</groupId>
 73       <artifactId>c3p0</artifactId>
 74       <version>0.9.5</version>
 75     </dependency>
 76 
 77     <dependency>
 78       <groupId>javax.persistence</groupId>
 79       <artifactId>persistence-api</artifactId>
 80       <version>1.0</version>
 81     </dependency>
 82 
 83     <dependency>
 84       <groupId>mysql</groupId>
 85       <artifactId>mysql-connector-java</artifactId>
 86       <version>5.1.46</version>
 87     </dependency>
 88 
 89     <dependency>
 90       <groupId>javax.servlet</groupId>
 91       <artifactId>jstl</artifactId>
 92       <version>1.2</version>
 93     </dependency>
 94 
 95     <dependency>
 96       <groupId>commons-fileupload</groupId>
 97       <artifactId>commons-fileupload</artifactId>
 98       <version>1.3.3</version>
 99     </dependency>
100 
101     <dependency>
102       <groupId>org.springframework</groupId>
103       <artifactId>spring-webmvc</artifactId>
104       <version>4.3.16.RELEASE</version>
105     </dependency>
106 
107     <dependency>
108       <groupId>javax.servlet</groupId>
109       <artifactId>javax.servlet-api</artifactId>
110       <version>3.1.0</version>
111     </dependency>
112 
113     <dependency>
114       <groupId>commons-httpclient</groupId>
115       <artifactId>commons-httpclient</artifactId>
116       <version>3.1</version>
117     </dependency>
118 
119     <dependency>
120       <groupId>com.fasterxml.jackson.core</groupId>
121       <artifactId>jackson-databind</artifactId>
122       <version>2.9.2</version>
123     </dependency>
124     <dependency>
125       <groupId>com.fasterxml.jackson.core</groupId>
126       <artifactId>jackson-annotations</artifactId>
127       <version>2.9.2</version>
128     </dependency>
129     <dependency>
130       <groupId>com.fasterxml.jackson.core</groupId>
131       <artifactId>jackson-core</artifactId>
132       <version>2.9.2</version>
133     </dependency>
134   </dependencies>
135 
136   <build>
137     <finalName>ch01_spring_mvc</finalName>
138     <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
139       <plugins>
140         <plugin>
141           <artifactId>maven-clean-plugin</artifactId>
142           <version>3.0.0</version>
143         </plugin>
144         <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
145         <plugin>
146           <artifactId>maven-resources-plugin</artifactId>
147           <version>3.0.2</version>
148         </plugin>
149         <plugin>
150           <artifactId>maven-compiler-plugin</artifactId>
151           <version>3.7.0</version>
152         </plugin>
153         <plugin>
154           <artifactId>maven-surefire-plugin</artifactId>
155           <version>2.20.1</version>
156         </plugin>
157         <plugin>
158           <artifactId>maven-war-plugin</artifactId>
159           <version>3.2.0</version>
160         </plugin>
161         <plugin>
162           <artifactId>maven-install-plugin</artifactId>
163           <version>2.5.2</version>
164         </plugin>
165         <plugin>
166           <artifactId>maven-deploy-plugin</artifactId>
167           <version>2.8.2</version>
168         </plugin>
169       </plugins>
170     </pluginManagement>
171   </build>
172 </project>

注意:tomcat要选择tomcat7,否则将请求方法进行转换的过滤器就没法生效,其次在配置扫描注解的时候,spring mvc的配置文件中必须配置controller包的自动扫描,否则会出现no mapping found错误

项目代码结构截图 

 

使用IDEA整合spring4+spring mvc+hibernate

 

前端视图 :

使用IDEA整合spring4+spring mvc+hibernate