Spring +SpringMVC + Mybatis的整合配置文件(方便以后复习方便)

    技术2025-09-11  59

    1、web.xml配置

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SSM40</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>SSM40</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SSM40</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 引入Spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-*.xml</param-value> </context-param> <!-- 启动Spring的监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- log4j配置start --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <!-- 指定系统根目录路径 --> <param-name>webAppRootKey</param-name> <param-value>SSM40.root</param-value> </context-param> <!-- Spring 加载 Log4j 的监听 --> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> <!-- log4j配置end --> <!--编码 --> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

    2、Mybatis的配置

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" > <configuration> <typeAliases> <!--扫描实体包,当查询的Mapper.xml文件 中,参数或者返回值是实体类型时,直接写实体名,不需要写全限定名 --> <package name="cn.bdqn.pojo"/> </typeAliases> </configuration>

    3、Spring的配置

    <?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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- 开启自动扫描,这样才能使得对应包下的注解生效 --> <context:component-scan base-package="cn.bdqn.service"></context:component-scan> <!-- 读取数据库的配置文件 --> <context:property-placeholder location="classpath:database.properties" /> <!-- 数据源采用dbcp连接池 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton"> <property name="driverClassName" value="${driver}"></property> <property name="url" value="${url}"></property> <property name="username" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <!-- 配置Spring事务的管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务管理类 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!--mybatis --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- 引入mybatis配置文件 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!-- 配置mybatis的自动注册 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.bdqn.dao"></property> </bean> </beans>

    4、SpringMVC 的配置

    <?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置映射处理器 --> <!-- <bean name="/index.html" class="cn.bdqn.controller.IndexController"></bean>--> <context:component-scan base-package="cn.bdqn.controller"></context:component-scan> <!-- 配置视图解析器 --> <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> --> <!-- 配置多视图解析器,允许同样的数据内容呈现不同的view --> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="favorParameter" value="true"></property> <property name="defaultContentType" value="text/html"></property> <property name="mediaTypes"> <map> <entry key="html" value="text/html;charset=UTF-8"></entry> <entry key="json" value="application/json;charset=UTF-8"></entry> <entry key="xml" value="application/xml;charset=UTF-8"></entry> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </list> </property> </bean> <!-- 配置MultipartResolver解析器 ,用于文件上传,注意id一定要有,并且名字不能改变--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="5000000"></property> <property name="defaultEncoding" value="UTF-8"></property> </bean> <!--配置拦截器 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/user/**"/> <mvc:mapping path="/provider/**"/> <bean class="cn.bdqn.intercepter.LoginIntercepter"></bean> </mvc:interceptor> </mvc:interceptors> <!--引入静态资源 --> <!-- <mvc:resources mapping="/statics/**" location="/statics/" /> --> <mvc:resources mapping="/statics/**" location="/statics/" /> <!-- 配置全局处理 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.RuntimeException">error</prop> </props> </property> </bean> <mvc:annotation-driven> <mvc:message-converters> <!--StringHttpMessageConverter解决json乱码问题全局配置 --> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!-- 配置FastJsonHttpMessageConverter消息转换器,规定json传输时间类型的格式为默认的时间格式 --> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json</value> </list> </property> <property name="features"> <list> <!-- 输出date的日期转换器--> <value>WriteDateUseDateFormat</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> </beans>

    5、表结构:

    我就不信我还会忘记,我真的真的很生气了

    Processed: 0.009, SQL: 9