Spring+SpringMvc+Hibernate+Maven整合各大配置文件记录
1.Idea新建的Maven架构

2.Maven的对象模型的内容
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> |
| <modelVersion>4.0.0</modelVersion> |
| <groupId>com.chen</groupId> |
| <artifactId>sshm</artifactId> |
| <packaging>war</packaging> |
| <version>1.0-SNAPSHOT</version> |
| <name>sshm Maven Webapp</name> |
| <url>http://maven.apache.org</url> |
| <dependencies> |
| <dependency> |
| <groupId>junit</groupId> |
| <artifactId>junit</artifactId> |
| <version>3.8.1</version> |
| <scope>test</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-context</artifactId> |
| <version>4.3.9.RELEASE</version> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-core</artifactId> |
| <version>4.3.9.RELEASE</version> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-beans</artifactId> |
| <version>4.3.9.RELEASE</version> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-web</artifactId> |
| <version>4.3.9.RELEASE</version> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-aspects</artifactId> |
| <version>4.3.9.RELEASE</version> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-orm</artifactId> |
| <version>4.3.9.RELEASE</version> |
| </dependency> |
| |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-webmvc</artifactId> |
| <version>4.3.9.RELEASE</version> |
| </dependency> |
| |
| |
| <dependency> |
| <groupId>org.slf4j</groupId> |
| <artifactId>slf4j-api</artifactId> |
| <version>1.7.6</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>aspectj</groupId> |
| <artifactId>aspectjrt</artifactId> |
| <version>1.5.3</version> |
| </dependency> |
| <dependency> |
| <groupId>org.aspectj</groupId> |
| <artifactId>aspectjtools</artifactId> |
| <version>1.8.7</version> |
| </dependency> |
| <dependency> |
| <groupId>javax.servlet</groupId> |
| <artifactId>jstl</artifactId> |
| <version>1.2</version> |
| <scope>runtime</scope> |
| </dependency> |
| <dependency> |
| <groupId>javax.annotation</groupId> |
| <artifactId>javax.annotation-api</artifactId> |
| <version>1.2</version> |
| </dependency> |
| <dependency> |
| <groupId>javax.servlet</groupId> |
| <artifactId>servlet-api</artifactId> |
| <version>2.5</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.hibernate</groupId> |
| <artifactId>hibernate-core</artifactId> |
| <version>4.2.3.Final</version> |
| </dependency> |
| |
| |
| |
| <dependency> |
| <groupId>c3p0</groupId> |
| <artifactId>c3p0</artifactId> |
| <version>0.9.1.2</version> |
| </dependency> |
| |
| |
| |
| <dependency> |
| <groupId>mysql</groupId> |
| <artifactId>mysql-connector-java</artifactId> |
| <version>5.1.35</version> |
| </dependency> |
| |
| </dependencies> |
| <build> |
| <finalName>sshm</finalName> |
| </build> |
| </project> |
3、web.xml配置
| <web-app 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_3_0.xsd" |
| version="3.0"> |
| |
| <display-name>Archetype Created Web Application</display-name> |
| <welcome-file-list> |
| <welcome-file>/index.jsp</welcome-file> |
| </welcome-file-list> |
| |
| |
| <context-param> |
| <param-name>contextConfigLocation</param-name> |
| <param-value>classpath:spring.xml</param-value> |
| </context-param> |
| |
| <listener> |
| <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> |
| </listener> |
| |
| |
| <filter> |
| <filter-name>encodingFilter</filter-name> |
| <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> |
| <init-param> |
| <param-name>encoding</param-name> |
| <param-value>UTF-8</param-value> |
| </init-param> |
| </filter> |
| <filter-mapping> |
| <filter-name>encodingFilter</filter-name> |
| <url-pattern>/*</url-pattern> |
| </filter-mapping> |
| |
| |
| <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:spring-mvc.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> |
| |
| |
| |
| <filter> |
| <filter-name>SpringOpenSessionInViewFilter</filter-name> |
| <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> |
| </filter> |
| <filter-mapping> |
| <filter-name>SpringOpenSessionInViewFilter</filter-name> |
| <url-pattern>/*</url-pattern> |
| </filter-mapping> |
| </web-app> |
4.jdbc.properties
| |
| jdbc.driver = com.mysql.jdbc.Driver |
| jdbc.url = jdbc:mysql://localhost:3306/db_demo?useUnicode=true&characterEncoding=utf-8 |
| jdbc.username = root |
| jdbc.password = password |
| |
| |
| hibernate.dialect = org.hibernate.dialect.MySQLDialect |
| hibernate.show_sql = true |
| hibernate.format_sql = true |
| hibernate.hbm2ddl.auto = update |
5.springmvc.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:mvc="http://www.springframework.org/schema/mvc" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans-4.1.xsd |
| http://www.springframework.org/schema/context |
| http://www.springframework.org/schema/context/spring-context-4.1.xsd |
| http://www.springframework.org/schema/mvc |
| http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> |
| |
| <mvc:annotation-driven/> |
| |
| <context:component-scan base-package="com.chen.web"/> |
| |
| |
| <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
| <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> |
| <property name="prefix" value="/page/"/> |
| <property name="suffix" value=".jsp"/> |
| </bean> |
| |
| |
| |
| |
| |
| |
| </beans> |
6.spring-hibernate.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:aop="http://www.springframework.org/schema/aop" |
| xmlns:tx="http://www.springframework.org/schema/tx" |
| xmlns:context="http://www.springframework.org/schema/context" |
| xsi:schemaLocation=" |
| http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd |
| http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans-3.2.xsd |
| http://www.springframework.org/schema/tx |
| http://www.springframework.org/schema/tx/spring-tx-3.2.xsd |
| http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd |
| "> |
| |
| |
| <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> |
| <property name="driverClass" value="${jdbc.driver}"/> |
| <property name="jdbcUrl" value="${jdbc.url}"/> |
| <property name="user" value="${jdbc.username}"/> |
| <property name="password" value="${jdbc.password}"/> |
| <property name="maxPoolSize" value="40"/> |
| <property name="minPoolSize" value="1"/> |
| <property name="initialPoolSize" value="10"/> |
| <property name="maxIdleTime" value="20"/> |
| </bean> |
| |
| |
| <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> |
| <property name="dataSource" ref="dataSource"/> |
| <property name="packagesToScan" value="com.chen"/> |
| <property name="hibernateProperties"> |
| <props> |
| <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> |
| <prop key="hibernate.dialect">${hibernate.dialect}</prop> |
| <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> |
| </props> |
| </property> |
| </bean> |
| |
| |
| <bean id="transactionManager" |
| class="org.springframework.orm.hibernate4.HibernateTransactionManager"> |
| <property name="sessionFactory" ref="sessionFactory"/> |
| </bean> |
| |
| |
| <tx:advice id="txAdvice" transaction-manager="transactionManager"> |
| |
| <tx:attributes> |
| <tx:method name="insert*" propagation="REQUIRED"/> |
| <tx:method name="update*" propagation="REQUIRED"/> |
| <tx:method name="edit*" propagation="REQUIRED"/> |
| <tx:method name="save*" propagation="REQUIRED"/> |
| <tx:method name="add*" propagation="REQUIRED"/> |
| <tx:method name="new*" propagation="REQUIRED"/> |
| <tx:method name="set*" propagation="REQUIRED"/> |
| <tx:method name="remove*" propagation="REQUIRED"/> |
| <tx:method name="delete*" propagation="REQUIRED"/> |
| <tx:method name="change*" propagation="REQUIRED"/> |
| <tx:method name="get*" propagation="REQUIRED" read-only="true"/> |
| <tx:method name="find*" propagation="REQUIRED" read-only="true"/> |
| <tx:method name="load*" propagation="REQUIRED" read-only="true"/> |
| <tx:method name="*" propagation="REQUIRED" read-only="true"/> |
| </tx:attributes> |
| </tx:advice> |
| |
| |
| <aop:config> |
| <aop:pointcut id="serviceOperation" expression="execution(* com.chen.service.*.*(..))"/> |
| <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/> |
| </aop:config> |
| |
| |
| <context:component-scan base-package="com.chen.*"/> |
| </beans> |
7.spring.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" |
| xsi:schemaLocation=" |
| http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
| http://www.springframework.org/schema/context |
| http://www.springframework.org/schema/context/spring-context-3.0.xsd"> |
| |
| |
| <context:component-scan base-package="com.chen.*" use-default-filters="true"> |
| <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> |
| </context:component-scan> |
| |
| <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> |
| <property name="locations"> |
| <list> |
| |
| <value>classpath:jdbc.properties</value> |
| </list> |
| </property> |
| </bean> |
| |
| <import resource="classpath:spring-hibernate.xml"/> |
| </beans> |
8.index.jsp
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <html> |
| <head> |
| <title>首页</title> |
| </head> |
| <body> |
| <form method="post" action="${pageContext.request.contextPath}/demo/test" > |
| <input type="submit" name="测试" value="测试跳转"> |
| </form> |
| </body> |
| </html> |
9.test.jsp
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> |
| <html> |
| <head> |
| <title>测试界面</title> |
| </head> |
| <body> |
| <h1>测试界面</h1> |
| </body> |
| </html> |
10.web文件夹下面的DemoController.java

11.启动项目,得到如下的图所示,并成功跳转到测试界面,则此次的配置成功,可以进入代码编辑流程


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理