使用xml文件配置SSM整合

这篇具有很好参考价值的文章主要介绍了使用xml文件配置SSM整合。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

使用XML文件配置SSM整合。
缺点:xml解析低,降低项目响应效率。文章来源地址https://www.toymoban.com/news/detail-837841.html

配置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>
	
	<!-- 配置前端控制器:DispatcherServlet -->
	<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>

	<!-- 设置全局参数: spring配置文件的位置和名称 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml</param-value>
	</context-param>

	<!-- 配置spring监听器 -->
	<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"/>

    <!-- 配置Thymeleaf视图解析器 -->
    <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>

    <!-- 配置jsp视图解析器 -->
    <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>
        <!--<bean class="com.evan.interceptor.FirstInterceptor"/>-->
        <!--<ref bean="firstInterceptor"/>-->
        <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>
                <!--
                 key设置要处理的异常,全类名
                 value设置出现该异常时要跳转的页面所对应的逻辑视图
                 -->
                <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"/>
        <!-- 异常状态码 -->
        <!--<property name="statusCodes" value="404,500"/>-->
        <!-- 默认异常状态码 -->
        <!--<property name="defaultStatusCode" value="500"/>-->
        <!-- 排除指定异常 -->
        <!--<property name="excludedExceptions" value="java.lang.ArithmeticException"/>-->
    </bean>

    <!-- 配置视图控制器 -->
    <mvc:view-controller path="/" status-code="200" view-name="index"/>
    <!-- 配置重定向视图控制器 -->
    <mvc:redirect-view-controller path="/baidu" redirect-url="https:www.baidu.com"/>
    <!-- 配置默认的Servlet处理静态资源 -->
    <mvc:default-servlet-handler/>
    <!-- 开启mvc注解驱动 -->
    <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>

    <!--
    开启事务的注解驱动
    通过注解@Transactional所标识的方法或标识的类中所有的方法,都会被事务管理器管理事务
    transaction-manager属性的默认值是transactionManager,如果事务管理器bean的id正好就
    是这个默认值,则可以省略这个属性
    -->
    <!--<tx:annotation-driven transaction-manager="transactionManager"/>-->

    <!-- 配置事务通知-->
    <tx:advice id="txadvice">
        <!--配置事务属性参数-->
        <tx:attributes>
            <!-- 设置在哪些方法上配置相关事务 -->
            <!-- tx:method标签:配置具体的事务方法 -->
            <!-- name属性:指定方法名,可以使用星号代表多个字符 -->
            <tx:method name="get*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <!-- read-only属性:设置只读属性 -->
            <!-- rollback-for属性:设置回滚的异常 -->
            <!-- no-rollback-for属性:设置不回滚的异常 -->
            <!-- isolation属性:设置事务的隔离级别 -->
            <!-- timeout属性:设置事务的超时属性 -->
            <!-- propagation属性:设置事务的传播行为 -->
            <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>

    <!-- mybatis-spring整合 -->
    <!-- 配置SqlSessionFactoryBean -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 注入mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--<property name="mapperLocations" value="classpath:mapper/*.xml"/>
        <property name="typeAliasesPackage" value="com.evan.bean"/>-->
    </bean>

    <!--
    配置mapper接口的扫描配置
    由mybatis-spring提供,可以将指定包下所有的mapper接口创建动态代理
    并将这些动态代理作为IOC容器的bean管理
    -->
    <!--<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.evan.mapper"/>
    </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>

    <!-- 引入mybatis的映射文件 -->
    <mappers>
        <!--
            以包为单位引入映射文件
            要求:
            1、mapper接口所在的包要和映射文件所在的包一致
            2、mapper接口要和映射文件的名字一致
        -->
        <package name="com.evan.mapper"/>
    </mappers>
</configuration>

到了这里,关于使用xml文件配置SSM整合的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SSM整合(XML方式)

    软件版本: IDEA 2021.3 Maven 3.6.3 MySql (Mariadb) 10.10 JDK 1.8 1、创建Maven的web工程 2、引入pom依赖 3 设置插件 配置前端控制器 注册监听器 注册字符过滤器,处理中文乱码 创建数据库,数据表student 表字段如下: 1、业务Controller 2、页面相关Controller 1 编写 listStu.jsp 2 编写 add.jsp 3 编写

    2024年02月13日
    浏览(51)
  • SSM-XML整合

    核心配置文件 maven坐标 一、web.xml 二、spring-config.xml 二、mybaits-config.xml 三、springmvc-config.xml

    2024年02月07日
    浏览(39)
  • 3.1 基于配置文件整合SSM框架实现用户登录

    1、创建数据库与表 (1)创建数据库 数据库simonshop,采用utf8mb4编码 (2)创建用户表 创建用户表t_user (3)在用户表里插入记录 插入4条记录 2、创建Maven项目 创建Maven项目 - SSMLogin 单击【Finish】按钮 3、项目添加相关依赖 在pom.xml文件里添加相关依赖 4、创建日志属性文件 日

    2024年02月08日
    浏览(41)
  • 【业务功能篇45】SSM整合shiro项目:web.xml执行顺序

    web.xml 的加载顺序是:ServletContext - context-param - listener - filter - servlet 学习shiro时,需要配置shiro ,我们需要在filter过滤器之前,先初始化好shiro组件,不然请求认证无法走到shiro,根据web.xml的加载顺序,listener标签中会先于filter标签中的组件,所以我们配置shiro的context文件,单独

    2024年02月16日
    浏览(43)
  • idea模板的使用(配置xml文件模板)

    1. 问题的引出 我们在日常项目中可以发现,sql映射文件和mybatis主配置文件,以及application.yml文件中有很多固定不变的内容,为了方面使用,所以可以把这些xml文件设置为模板 2. 创建模板的步骤 按照图片一步一步进行即可 点击apply,点击ok就完成了;当然模板里面的内容自己

    2024年02月13日
    浏览(33)
  • 【Spring教程31】SSM框架整合实战:从零开始学习SSM整合配置,如何编写Mybatis SpringMVC JDBC Spring配置类

    欢迎大家回到《Java教程之Spring30天快速入门》,本教程所有示例均基于Maven实现,如果您对Maven还很陌生,请移步本人的博文《如何在windows11下安装Maven并配置以及 IDEA配置Maven环境》,本文的上一篇为《Rest风格简介与RESTful入门》 前面我们已经把Mybatis、Spring和SpringMVC三个框架

    2024年02月04日
    浏览(58)
  • 【Spring Boot】使用XML配置文件实现数据库操作(一)

    SQL映射文件就是我们通常说的mapper.xml配置文件,主要实现SQL语句的配置和映射,同时实现Java的POJO对象与数据库中的表和字段进行映射关联的功能。 1.1 mapper.xml的结构 下面就来详细介绍mapper.xml文件的结构。首先看一个完整的mapper.xml示例:

    2024年02月10日
    浏览(43)
  • 使用自定义XML配置文件在.NET桌面程序中保存设置

    本文将详细介绍如何在.NET桌面程序中使用自定义的XML配置文件来保存和读取设置。除了XML之外,我们还将探讨其他常见的配置文件格式,如JSON、INI和YAML,以及它们的优缺点和相关的NuGet类库。最后,我们将重点介绍我们为何选择XML作为配置文件格式,并展示一个实用的示例

    2024年02月09日
    浏览(31)
  • 【JavaWeb】关于Servlet的两种配置Web.xml文件配置或使用@WebServlet注解及urlPattern配置规则

    首先,我们需要了解到的是在Servlet2.x版本中,配置依然是通过web.xml的形式进行配置的,升级到Servlet3.x后,才可以依赖注解式方式进行配置。 代码格式: 代码展示: 访问过程: ①servlet通过浏览器地址栏输入的路径与servlet-mapping标签中的url-pattern的标签值进行匹配。 ②通过

    2024年02月06日
    浏览(28)
  • SSM 整合使用 @PropertySource 问题

    如果你想将数据库连接的相关属性移入一个 classpath 下的 “.properties” 文件中,让后再在上述配置类中结合 @PropertySource 和 @Value 来加载 .properties 配置文件,引入相关属性值,那么,你会发现一个问题,你的 @Value 读取到的属性值都是 null ! 造成这个现象的原因在在于,我们

    2024年03月17日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包