spring集成mybatis常用配置文件

本文介绍了SSM(Spring、SpringMVC、MyBatis)框架的配置,包括spring的applicationContext.xml、springMVC.xml、mybatis-config.xml、数据库配置db.properties以及日志配置log4j.properties。在spring的配置中,提到了将配置拆分为三个部分,并详细说明了各部分的作用,如springdao.xml负责数据库配置和MyBatis工厂设置,springservice.xml用于事务管理和包扫描。

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

 

SSM框架的配置文件

SSM框架的基本配置文件有spring的配置文件applicationContext.xml,springMVC的配置文件springMVC.xml以及mybatis的全局配置文件mybatis-config.xml和mybatis的映射文件XXXMapper.xml,此外,通常还有数据库配置文件db.properties。通常还有web.xml以及日记log4j的配置文件。

<!--more-->

spring的配置文件applicationContext.xml

<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: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-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!--读取db.properties-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--配置数据源,class属性为配置连接池-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <!--数据库驱动-->
        <property name="driverClassName" value="${jdbc.driver}"/>
        <!--连接数据库的url-->
        <property name="url" value="${jdbc.url}"/>
        <!--连接数据库的用户名-->
        <property name="username" value="${jdbc.username}"/>
        <!--连接数据库的密码-->
        <property name="password" value="${jdbc.password}"/>
        <!--最大连接数-->
        <property name="maxTotal" value="${jdbc.maxTotal}"/>
        <!--最大空闲数-->
        <property name="maxIdle" value="${jdbc.maxIdle}"/>
        <!--初始化连接数-->
        <property name="initialSize" value="${jdbc.initialSize}"/>
    </bean>
    <!-- 事务管理器,依赖于数据源-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--传播行为-->
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="add*" propagation="REQUIRED"/>
            <tx:method name="create*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!--切面-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ma.core.service.*.*(..))"/>
    </aop:config>
​
    <!--配置MyBatis工厂SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--指定mapper文件的位置-->
        <property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"/>
        <!--指定MyBatis的核心配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>
    <!-- 接口开发,扫描 com.itheima.core.dao包 ,写在此包下的接口即可被扫描到 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ma.core.dao"/>
    </bean>
​
    <!--
        批量扫描包
        让SpringIOC容器管理对象
    -->
    <context:component-scan base-package="com.xxx.xxx.xxx"/>
​
</beans>

通常,也将spring的配置文件applicationContext.xml写为三个配置文件,其他两个是springdao.xml和springservice.xml。其中applicationContext.xml文件引入其他配置文件<import resource="classpath:spring/spring-service.xml" />,而springdao.xml文件则引入数据库,获取连接池以及绑定数据源和配置MyBatis工厂SqlSessionFactory。springservice,xml文件则扫描包,将事务类注入到spring以及进行事务处理的相关配置

springMVC的配置文件springMVC.xml

<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: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-4.3.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
   
    <!--扫描Controller组件对象-->
    <context:component-scan
            base-package="com.xxx.x.x.x" />
​
    <!--处理器适配器和处理器映射器-->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!--json解析器-->
            <bean id="mappingJacksonHttpMessageConverter"
                  class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <!--class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">-->
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json</value>
                        <value>application/xml;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
​
    <!--处理静态资源文件-->
    <mvc:default-servlet-handler />
​
    <!--视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/article/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
​
    <!--文件上传解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
​
    <!--注册:登录拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--path拦截的路径,不是目录,根路径拦截失效[/项目名不需要写]-->
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/js/**" />
            <mvc:exclude-mapping path="/article/saveComments" />
            <mvc:exclude-mapping path="/login/**" />
            <mvc:exclude-mapping path="/**" />
            <mvc:exclude-mapping path="/code/**" />
            <mvc:exclude-mapping path="/js/**" />
            <mvc:exclude-mapping path="/bootstrap-3.3.7-dist/**" />
            <mvc:exclude-mapping path="/css/**" />
            <mvc:exclude-mapping path="/font-awesome-4.7.0/**" />
            <mvc:exclude-mapping path="/images/**" />
            <mvc:exclude-mapping path="/kindeditor/**" />
            <mvc:exclude-mapping path="/layui/**" />
            <mvc:exclude-mapping path="/upload/**" />
            <bean class="com.bjpowernode.blog.base.interceptor.LoginInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

mybatis的配置文件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>
    <!-- 别名定义 -->
    <typeAliases>
        <package name="com.xxx.x.x." />
    </typeAliases>
</configuration>

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_3_1.xsd"
         version="3.1">
    <!-- 配置加载Spring文件的监听器-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
​
    <!--编码过滤器-->
    <filter>
        <filter-name>EncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--强制使用该编码-->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
​
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
​
    <!-- 配置Spring MVC前端核心控制器 -->
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
​
        <!--因为服务器启动的时候会第一时间读取web.xml文件,在服务器启动的
        的时候加载springMVC.xml ServletConfig-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC/springMVC.xml</param-value>
        </init-param>
        <!--在服务器启动的时候就初始化该Servlet对象-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
        <welcome-file>xxxx</welcome-file>
    </welcome-file-list>
</web-app>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false&nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

log4j.properties

# 日志级别debug info error warn fatal
log4j.rootLogger=DEBUG, stdout
​
#如下两个配置:日志的输出方向
# Console output...
 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
​
#输出ERROR 级别以上的日志到
 #log4j.appender.R= org.apache.log4j.FileAppender 
 #log4j.appender.R.file=e:/logs/log.log 
 #log4j.appender.R.Append= true 
 #log4j.appender.R.layout= org.apache.log4j.PatternLayout 
 #log4j.appender.R.layout.ConversionPattern= %n%d%p [%l] %m%n
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值