首页 > 其他分享 >SpringMVC整合的 spring-mvc spring-mybatis配置文件

SpringMVC整合的 spring-mvc spring-mybatis配置文件

时间:2023-03-30 15:02:04浏览次数:41  
标签:xml jackson 配置文件 SpringMVC spring springframework version org


SpringMVC入门

spring-mvc.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    ">

	<!-- IOC注解支持 -->
	<context:component-scan base-package="controller" />

	<!-- MVC注解支持 -->
	<mvc:annotation-driven>
		<mvc:message-converters>
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>application/json;charset=UTF-8</value>
						<value>text/html;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>

	<!-- 配置内部视图解析器 -->
	<!-- 视图路径:/WEB-INF/jsp/*.jsp -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- 静态资源解析 -->
	<mvc:resources location="/static/" mapping="/static/**" />

	<!-- 实体表单处理器 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 允许上传文件的大小设置为 5000000字节 -->
		<property name="maxUploadSize" value="5000000" />
		<!-- 将请求的编码格式设置为 UTF-8 -->
		<property name="defaultEncoding" value="UTF-8" />
	</bean>

	<!-- 全局拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<!-- 拦截/admin/后面所有url -->
			<mvc:mapping path="/admin/**" />
			<bean class="interceptor.AuthInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

	<!-- 全局异常处理 -->
	<bean
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.RuntimeException">500</prop>
			</props>
		</property>
	</bean>
</beans>

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

	<!-- IOC注解支持 -->
	<context:component-scan base-package="dao,service,pojo" />

	<!-- 导入properties文件 -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:database.properties" />
	</bean>

	<context:property-placeholder location="classpath:database.properties" />

	<!-- 配置环境 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="${url}" />
		<!-- 使用${username}肯能会使用电脑的注册用户名14312 -->
		<property name="username" value="${uname}" />
		<property name="password" value="${password}" />
	</bean>


	<!-- 创建sqlSessionFactory工厂对象 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置mybatis文件 -->
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<!-- 配置环境 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 别名 -->
		<property name="typeAliasesPackage" value="pojo" />
	</bean>

	<!-- 自动实现Mapper接口 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="dao" />
	</bean>

	<!-- 事务设置 -->
	<bean id="txManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 绑定数据源 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 自动事务支持 -->
	<tx:annotation-driven transaction-manager="txManager" />

</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="autoMappingBehavior" value="FULL" />
	</settings>
</configuration>

POM.xml的相关依赖

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>myfirstMaven</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>myfirstMaven Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <!-- spring相关 版本号-->
        <spring.version>4.1.7.RELEASE</spring.version>

        <!-- mybatis相关 版本号 -->
        <mybatis.version>3.2.2</mybatis.version>

        <!-- mysql数据源驱动 版本号 -->
        <mysql-driver.version>5.1.1</mysql-driver.version>

        <!-- 日志相关 版本号 -->
        <log4j.version>1.2.17</log4j.version>
        <slf4j.version>1.7.18</slf4j.version>

        <!-- swagger相关 版本号-->
        <jackson.verson>2.8.7</jackson.verson>
    </properties>


    <dependencies>
        <!-- 单元测试 相关-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <!-- mybatis 相关-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>

        <!-- MySql数据库 相关 -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-driver.version}</version>
        </dependency>

        <!-- servlet 相关-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <!-- JSON 相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.31</version>
        </dependency>

        <!-- spring 相关 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- SSM 整合 相关-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>

        <!-- JSR303 校验 相关 -->
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.4.Final</version>
        </dependency>

        <!-- 日志 相关 -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>


        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>


        <!-- 文件上传 相关 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <!-- swagger 相关 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.1.0.Final</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.verson}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.verson}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.verson}</version>
        </dependency>

        <!-- 邮件发送 相关 -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <!-- Redis数据库 相关 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.1.0</version>
        </dependency>

        <!-- 获取终端类型 相关 -->
        <dependency>
            <groupId>nl.bitwalker</groupId>
            <artifactId>UserAgentUtils</artifactId>
            <version>1.2.4</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>untitled1</finalName>
        <pluginManagement>
            <!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
        <!-- 配置文件不在resources里 -->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

    </build>

</project>


标签:xml,jackson,配置文件,SpringMVC,spring,springframework,version,org
From: https://blog.51cto.com/loveddz/6159592

相关文章

  • Spring-Boot整合微信登陆、微信支付、邮箱发送、支付宝支付和阿里云短信
    Spring-Boot整合1.发送邮件2.支付宝支付3.阿里云短信4.微信登陆5.微信支付6.Swargger2前后端API接口信息文档7.ehcache缓存缓存8.Spring-Boot注解详解9.自定义配......
  • SpringCloud常见问题描述
    1什么是SpringCloudSpringcloud流应用程序启动器是基于SpringBoot的Spring集成应用程序,提供与外部系统的集成。SpringcloudTask,一个生命周期短暂的微服务框......
  • SpringBoot 项目使用 Sa-Token 完成登录认证
    一、设计思路对于一些登录之后才能访问的接口(例如:查询我的账号资料),我们通常的做法是增加一层接口校验:如果校验通过,则:正常返回数据。如果校验未通过,则:抛出异常,告知其需......
  • SpringBoot中集成Redis实现对redis中数据的解析和存储
    场景SpringBoot中操作springredis的工具类:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108335645上面讲的操作redis的工具类,但是对于redis的集成并没......
  • Spring
    Springhttps://start.spring.io/ Gradle相当于mavenSpring分为2.x和3.xsnapshot正在开发的快照版本,较为不稳定M2:第二个里程碑,而后会转换为snapshot,最后变为release......
  • SpringBoot——更换Tomcat服务器为 Jetty 服务器
    Jetty服务器(可能会用到)Jetty比Tomcat更轻量级,可拓展性更强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty首先要启动Jetty服务器 --> 到nexus.exe文件夹中cmd输......
  • springboot获取小程序二维码图片
    获取小程序二维码前需要获取小程序的token。这个不赘述了。获取小程序二维码官方文档上找到2个处理的Api接口。经测试,现有一个能用的 https://api.weixin.qq.com/wxa/ge......
  • Spring常见问题描述
    1渊源 原型在200210由RodJohnson发布的书中体现,后来在200403spring1.0版本发布,201709spring5.0发布,(1)springioc 控制反转,管理容器中的bean,主要使用di依赖注入实现。......
  • spring-boot-devtools 热部署造成的类加载问题
    如果你的项目在IDE中出现了像下面这些奇怪的错误objectisnotaninstanceofdeclaringclass//对象不是声明类的实例java.lang.ClassCastException:com.exam......
  • Springer出版社Applied Intelligence期刊投稿小记
      本篇文章记载自己在投稿过程面临的一些问题和解决方法,希望能对计划投稿本期刊的同学有所帮助。 1.模板    网站要求的nature模板:(注意不要尝试动模板,例如......