使用XML文件配置SSM整合。
缺点:xml解析低,降低项目响应效率。
配置web.xml
| <?xml version="1.0" encoding="UTF-8"?> |
| <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" |
| version="4.0"> |
| |
| |
| <filter> |
| <filter-name>characterEncodingFilter</filter-name> |
| <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> |
| <init-param> |
| <param-name>encoding</param-name> |
| <param-value>true</param-value> |
| </init-param> |
| <init-param> |
| <param-name>forceEncoding</param-name> |
| <param-value>true</param-value> |
| </init-param> |
| </filter> |
| <filter-mapping> |
| <filter-name>characterEncodingFilter</filter-name> |
| <url-pattern>/*</url-pattern> |
| </filter-mapping> |
| |
| |
| <filter> |
| <filter-name>hiddenHttpMethodFilter</filter-name> |
| <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> |
| </filter> |
| <filter-mapping> |
| <filter-name>hiddenHttpMethodFilter</filter-name> |
| <url-pattern>/*</url-pattern> |
| </filter-mapping> |
| |
| |
| <servlet> |
| <servlet-name>dispatcherServlet</servlet-name> |
| <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> |
| <init-param> |
| <param-name>contextConfigLocation</param-name> |
| <param-value>classpath:springmvc.xml</param-value> |
| </init-param> |
| <load-on-startup>1</load-on-startup> |
| </servlet> |
| <servlet-mapping> |
| <servlet-name>dispatcherServlet</servlet-name> |
| <url-pattern>/</url-pattern> |
| </servlet-mapping> |
| |
| |
| <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> |
| |
| </web-app> |
配置控制层(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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> |
| |
| |
| <context:component-scan base-package="com.evan.controller"/> |
| |
| |
| <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> |
| <property name="order" value="1"/> |
| <property name="characterEncoding" value="UTF-8"/> |
| <property name="templateEngine"> |
| <bean class="org.thymeleaf.spring5.SpringTemplateEngine"> |
| <property name="templateResolver"> |
| <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> |
| <property name="characterEncoding" value="UTF-8"/> |
| <property name="order" value="2"/> |
| <property name="templateMode" value="HTML5"/> |
| |
| <property name="prefix" value="/WEB-INF/views"/> |
| |
| <property name="suffix" value=".html"/> |
| </bean> |
| </property> |
| </bean> |
| </property> |
| </bean> |
| |
| |
| <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="resourceViewResolver"> |
| <property name="order" value="1"/> |
| <property name="prefix" value="/WEB-INF/views"/> |
| <property name="suffix" value=".jsp"/> |
| </bean> |
| |
| |
| <mvc:interceptors> |
| |
| |
| <mvc:interceptor> |
| |
| <mvc:mapping path="/**"/> |
| |
| <mvc:exclude-mapping path="/test/hello"/> |
| |
| <ref bean="firstInterceptor"/> |
| </mvc:interceptor> |
| </mvc:interceptors> |
| |
| |
| <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> |
| |
| <property name="order" value="1"/> |
| |
| <property name="exceptionMappings"> |
| <props> |
| |
| |
| |
| |
| <prop key="java.lang.Exception">error</prop> |
| <prop key="java.lang.NullPointerException">error</prop> |
| <prop key="java.lang.ArithmeticException">error</prop> |
| </props> |
| </property> |
| |
| <property name="defaultErrorView" value="error"/> |
| |
| <property name="exceptionAttribute" value="ex"/> |
| |
| |
| |
| |
| |
| |
| </bean> |
| |
| |
| <mvc:view-controller path="/" status-code="200" view-name="index"/> |
| |
| <mvc:redirect-view-controller path="/baidu" redirect-url="https:www.baidu.com"/> |
| |
| <mvc:default-servlet-handler/> |
| |
| <mvc:annotation-driven/> |
| |
| </beans> |
配置业务层组件(spring.xml)
jdbc.properties
| jdbc.username=root |
| jdbc.password=****** |
| jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8&characterEncoding=utf8 |
| jdbc.driver=com.mysql.cj.jdbc.Driver |
| <?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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> |
| |
| |
| <context:component-scan base-package="com.evan"> |
| |
| <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> |
| </context:component-scan> |
| |
| |
| <context:property-placeholder location="classpath:jdbc.properties"/> |
| |
| |
| <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> |
| <property name="driverClassName" value="${jdbc.driver}"/> |
| <property name="url" value="${jdbc.url}"/> |
| <property name="username" value="${jdbc.username}"/> |
| <property name="password" value="${jdbc.password}"/> |
| </bean> |
| |
| |
| <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> |
| <property name="dataSource" ref="dataSource"/> |
| </bean> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| <tx:advice id="txadvice"> |
| |
| <tx:attributes> |
| |
| |
| |
| <tx:method name="get*" read-only="true"/> |
| <tx:method name="query*" read-only="true"/> |
| <tx:method name="find*" read-only="true"/> |
| |
| |
| |
| |
| |
| |
| <tx:method name="save*" read-only="false" rollbackfor="java.lang.Exception" propagation="REQUIRES_NEW"/> |
| <tx:method name="update*" read-only="false" rollbackfor="java.lang.Exception" propagation="REQUIRES_NEW"/> |
| <tx:method name="delete*" read-only="false" rollbackfor="java.lang.Exception" propagation="REQUIRES_NEW"/> |
| </tx:attributes> |
| </tx:advice> |
| |
| |
| <aop:config> |
| |
| <aop:pointcut id="pt" expression="execution(* com.evan.spring5.service.UserService.*(..))"/> |
| |
| <aop:advisor advice-ref="txadvice" pointcut-ref="pt"/> |
| </aop:config> |
| |
| |
| |
| <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> |
| |
| <property name="dataSource" ref="dataSource"/> |
| |
| <property name="configLocation" value="classpath:mybatis-config.xml"/> |
| |
| |
| </bean> |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| </beans> |
配置持久层(mybatis-config.xml)
| <?xml version="1.0" encoding="UTF-8" ?> |
| <!DOCTYPE configuration |
| PUBLIC "-//mybatis.org//DTD Config 3.0//EN" |
| "http://mybatis.org/dtd/mybatis-3-config.dtd"> |
| <configuration> |
| |
| |
| <settings> |
| |
| <setting name="mapUnderscoreToCamelCase" value="true"/> |
| |
| <setting name="logImpl" value="SLF4J"/> |
| </settings> |
| |
| |
| <typeAliases> |
| |
| <package name="com.evan.bean"/> |
| </typeAliases> |
| |
| <plugins> |
| |
| <plugin interceptor="com.github.pagehelper.PageInterceptor"/> |
| </plugins> |
| |
| |
| <mappers> |
| |
| |
| |
| |
| |
| |
| <package name="com.evan.mapper"/> |
| </mappers> |
| </configuration> |
· [翻译] 为什么 Tracebit 用 C# 开发
· 腾讯ima接入deepseek-r1,借用别人脑子用用成真了~
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· RFID实践——.NET IoT程序读取高频RFID卡/标签