sringMVC和mybatis的相关配置

本文介绍了一个结合SpringMVC与MyBatis技术的Web应用实例,详细展示了web.xml配置、MyBatis配置文件及SpringMVC配置文件的内容。通过配置文件,实现了项目的初始化设置、请求映射、视图解析、文件上传限制等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Web.xml

 

<?xml version="1.0"encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

         xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

         xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

         id="WebApp_ID"version="3.0">

         <display-name>springMVCmyBatis</display-name>

         <servlet>

                   <servlet-name>springMVCmyBatis</servlet-name>

                   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

                  

                   <!--联系springmvc -->

                   <init-param>

                            <param-name>contextConfigLocation</param-name>

                            <param-value>classpath*:config/springMVC-servlet.xml</param-value>

                   </init-param>

                   <!--设置DispatcherServlet启动时创建 -->

                   <load-on-startup>1</load-on-startup>

         </servlet>

         <servlet-mapping>

                   <servlet-name>springMVCmyBatis</servlet-name>

                   <url-pattern>/</url-pattern>

         </servlet-mapping>

 

         <!--设置字符编码 -->

         <filter>

                   <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>

                   <url-pattern>/*</url-pattern>

         </filter-mapping>

 

         <!--默认首页 -->

         <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-list>

</web-app>

 

 

Mybatis

<?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>

         <!--引入data.properties文件 -->

         <propertiesresource="config/data.properties" />

          <!-- 映射封装好的对象 -->

         <typeAliases>     

                   <packagename="com.etc.entity"/>

         </typeAliases>

          <!-- 配置环境 jdbc -->

         <environmentsdefault="development">

                   <environmentid="development">

                            <transactionManagertype="JDBC" />

                            <!--是否使用连接池技术 -->

                            <dataSourcetype="POOLED">

                                     <propertyname="driver" value="${driver}" />

                                     <propertyname="url" value="${url}" />

                                     <propertyname="username" value="${username}" />

                                     <propertyname="password" value="${password}" />

                            </dataSource>

                   </environment>

                  

         </environments>

         <mappers>

             <!-- 把此包中的映射文件加载,要求映射文件名与数据访问接口名要一致。 -->

                   <packagename="com.etc.dao" />

         </mappers>

</configuration>

 

 

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:tx="http://www.springframework.org/schema/tx"

         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-3.0.xsd

                   http://www.springframework.org/schema/tx

                   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

                   http://www.springframework.org/schema/context

                   http://www.springframework.org/schema/context/spring-context-3.0.xsd

                   http://www.springframework.org/schema/mvc

                   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

                  

         <!--拼接  -->

         <beanname="viewResolver"

                   class="org.springframework.web.servlet.view.InternalResourceViewResolver">

                   <propertyname="prefix" value="/"></property>

                   <propertyname="suffix" value=".jsp"></property>

         </bean>

         <!--设置这三个目录下的文件不采用拼接  -->

         <mvc:resourceslocation="/img/" mapping="/img/**" />

         <mvc:resourceslocation="/css/" mapping="/css/**" />

         <mvc:resourceslocation="/js/" mapping="/js/**" />

        

         <!--设置上传文件要求  -->

         <beanid="multipartResolver"

                   class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

                   <propertyname="maxUploadSize" value="1048576"></property>

                   <propertyname="maxInMemorySize" value="4096"></property>

                   <propertyname="defaultEncoding" value="UTF-8"></property>

         </bean>

 

 

         <!--继承 <bean id="parameterMethodNameResolver"class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">

                   <propertyname="paramName" value="action"></property></bean> 参数方法解析器 -->

        

         <!--映射 controller -->

         <context:component-scanbase-package="com.etc.controller"></context:component-scan>

         <!--开启注解配置 -->

         <mvc:annotation-driven/>

 

</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值