springmvc+spring+mybatis整合之配置文件总结

目录

一、mybatis框架

​ sqlMapConfig.xml

二、springmvc框架

springmvc.xml

三、spring框架

aplicationContext.xml

aplicationContext-dao.xml

aplicationContext-service.xml

aplicationContext-trans.xml

四、公共的配置文件

db.properties

log4j.properteis

web.xml


一、mybatis框架

​ sqlMapConfig.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.weicai.ssm.po"/>
    </typeAliases>

</configuration>

二、springmvc框架

springmvc.xml

  • ​ 配置包扫描contoller
  • ​ 配置处理器映射器和处理器适配器
  • ​ 配置视图解析器
<?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
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--配置包扫描controller-->
    <context:component-scan base-package="com.weicai.ssm.controller"></context:component-scan>

    <!--注解驱动方式配置处理器映射器和处理器适配器-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀:页面的公共目录路径-->
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <!--配置后缀:页面的扩展名称-->
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

三、spring框架

aplicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--导入其他模块的配置文件-->
    <import resource="classpath:spring/applicationContext-dao.xml"></import>
    <import resource="classpath:spring/applicationContext-service.xml"></import>
    <import resource="classpath:spring/applicationContext-trans.xml"></import>

</beans>

aplicationContext-dao.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"
       xsi:schemaLocation="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.xsd">

    <!--配置持久层相关对象-->
    <!--加载属性资源文件-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

    <!--配置数据源对象-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--注入连接数据库的四个基本要素-->
        <property name="driverClassName" value="${db.driver}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>

    <!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源对象-->
        <property name="dataSource" ref="dataSource"></property>

        <!--加载mybatis主配置文件-->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
    </bean>

    <!--配置管理mapper代理对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--配置要扫描的包即可-->
        <property name="basePackage" value="com.weicai.ssm.dao"></property>
    </bean>

</beans>

aplicationContext-service.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"
       xsi:schemaLocation="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.xsd">

    <!--配置业务层对象-->
    <context:component-scan base-package="com.weicai.ssm.service"></context:component-scan>

</beans>

aplicationContext-trans.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: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.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--配置声明式事务-->
    <!--第一步:配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源对象-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--第二步:配置aop和切入点表达式-->
    <aop:config>
        <aop:pointcut id="pt1" expression="execution(* com.weicai.ssm.service..*.*(..))"></aop:pointcut>

        <!--第三步:建立通知(事务管理器)和切入点表达式关系-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
    </aop:config>

    <!--第四步:配置通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--第五步:配置事务属性-->
        <tx:attributes>
            <!--配置增删改规则-->
            <tx:method name="save*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="add*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="update*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="del*" propagation="REQUIRED" read-only="false"/>

            <!--配置查询规则-->
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>

        </tx:attributes>
    </tx:advice>

</beans>

四、公共的配置文件

db.properties

  • 连接数据库的属性
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/db_ssm?characterEncoding=utf-8
db.username=root
db.password=root

log4j.properteis

  • 日志配置
# Global logging configuration
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

web.xml

  • ​ 配置一个监听器ContextLoderListener
  • ​ 配置前端控制器DispatcherServlet
  •  配置字符集编码的过滤器
<?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"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

  <display-name>ssm</display-name>


  <!--指定spring配置文件位置  -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
  </context-param>

<!--
      配置ContextLoaderListener监听器,说明:
      1.ContextLoaderListener监听器,监听ServletContext对象的创建。一旦ServletContext对象创建,
      它立即帮助我们创建spring容器,并且放入ServletContext域中。
      2.该监听器,默认只能加载WEB-INF目录下,名称为applicationContext.xml的配置文件
      3.通过context-param标签,配置指定spring的配置文件位置,改变默认行为。
-->

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

    <!--指定使用的字符集编码:UTF-8-->
    <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>

  <!--配置前端控制器:DispatcherServlet-->
  <servlet>
    <servlet-name>ssm</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <!--加载springmvc.xml文件-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>

    <!--配置什么时候加载前端控制器,说明:
      1.配置大于等于0的整数,表示在web容器启动的时候加载
      2.配置小于0的整数,表示在第一次请求到达的时候加载
    -->
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>ssm</servlet-name>
    <!--配置拦截的url规则,说明:
    1.*.do,表示以.do结尾的请求进入前端控制器
    2./,表示所有请求都进入前端控制器
    -->
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

</web-app>

发布了18 篇原创文章 · 获赞 19 · 访问量 5357

猜你喜欢

转载自blog.csdn.net/weixin_44586883/article/details/87388436