SSM整合中的配置详情

目录

所需配置文件:

        1、web.xml

        2、springmvc-config.xml        springmvc核心配置文件

        3、applicationContext.xml        spring核心配置文件

        4、mybatis-config.xml        mybatis的核心配置文件

        5、db.properties        数据库连接信息

        6、log4j.properties        日志文件

具体案例:

        1、web.xml

        2、springmvc-config.xml        springmvc核心配置文件

        3、applicationContext.xml        spring核心配置文件

        4、mybatis-config.xml        mybatis的核心配置文件

        5、db.properties        数据库连接信息

        6、log4j.properties        日志文件        


所需配置文件:

        1、web.xml

                (1)配置前端控制器dispatcherServlet

                  (2)配置spring监听器(必须配置)

                (3)配置编码过滤器(为了避免中文乱码问题)

        2、springmvc-config.xml        springmvc核心配置文件

                (1)配置视图解析器

                (2)配置注解驱动(必须配置)

                (3)配置拦截器

                (4)开启controller注解

                (5)放行静态资源文件

        3、applicationContext.xml        spring核心配置文件

                (1)读取db.properties

                (2)配置数据源

                (3)配置事务管理器

                (4)开启事务注解

                (5)配置mybatis工厂

                (6)配置mapper基于接口开发

                (7)开启service层的注解

        4、mybatis-config.xml        mybatis的核心配置文件

                (1)设置别名

        5、db.properties        数据库连接信息

        6、log4j.properties        日志文件

具体案例:

        1、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">

    <!--配置前端控制器-->
    <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-config.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监听器--><!--必须配置-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--配置编码过滤器-->
    <filter>
        <filter-name>encoding</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>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

</web-app>

        2、springmvc-config.xml        springmvc核心配置文件

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

  
    <!--配置视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

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

    <!--开启controller注解-->
    <context:component-scan base-package="com.core.controller"/>

    <!--配置登录拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/><!--/**配置,表示拦截所有路径-->
            <mvc:exclude-mapping path="/login"/><!--配置不需要拦截的路径-->
            <bean class="com.core.interceptor.LoginInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

    <!--放行静态资源文件-->
    <mvc:default-servlet-handler />


</beans>

        3、applicationContext.xml        spring核心配置文件

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

    <!--配置数据源-->
    <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"></property>
    </bean>

    <!--开启事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

    <!--配置mybatis工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

    <!--基于MapperScannerConfigurer接口开发-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.core.dao"></property>
    </bean>

    <!--开启注解-->
    <context:component-scan base-package="com.core.service"/>

</beans>

        4、mybatis-config.xml        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>

    <!-- 设置别名 -->
    <typeAliases>
        <package name="com.core.po" />
    </typeAliases>

    <!--<mappers>
        <package name="com.core.dao"/>
    </mappers>-->
</configuration>

        5、db.properties        数据库连接信息

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/boot_crm?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=root
jdbc.maxTotal=30
jdbc.maxIdle=10
jdbc.initialSize=5

        6、log4j.properties        日志文件        

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.core=DEBUG
# 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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM整合是指将SpringSpringMvc和Mybatis这三个框架进行整合,以提高项目的开发效率和代码的复用性。下面是SSM整合的思路与配置详解: 1. 配置准备: 在开始整合之前,需要确保你已经配置好了Java开发环境和相应的IDE工具。此外,还要保证你已经下载安装了SpringSpringMvc和Mybatis的相关依赖库。 2. 配置过程: 2.1 引入依赖: 首先,在你的项目引入SpringSpringMvc和Mybatis的相关依赖库。你可以通过在pom.xml文件添加相应的依赖来完成这一步骤。这些依赖库包括spring-core、spring-webmvc、mybatis等。 2.2 配置Web.xml文件: 在Web.xml文件,你需要配置SpringSpringMvc的相关配置。这包括配置DispatcherServlet、ContextLoaderListener和字符编码过滤器等。可以通过在Web.xml文件添加相应的配置来完成这一步骤。 2.3 配置SpringMvc.xml文件: 在SpringMvc.xml文件,你需要配置SpringMvc的相关配置,包括扫描控制器、视图解析器、静态资源路径等。可以通过在SpringMvc.xml文件添加相应的配置来完成这一步骤。 2.4 配置mybatis.xml文件: 在mybatis.xml文件,你需要配置Mybatis的相关配置,包括数据库连接信息、Mapper扫描路径等。可以通过在mybatis.xml文件添加相应的配置来完成这一步骤。 2.5 配置applicationContext.xml文件: 在applicationContext.xml文件,你需要配置Spring的相关配置,包括扫描注解、声明事务管理器等。可以通过在applicationContext.xml文件添加相应的配置来完成这一步骤。 3. mybatis逆向工程: 如果你想要通过数据库表生成相应的实体类、Mapper接口和Mapper XML文件,可以使用mybatis逆向工程来完成。你需要配置相应的代码生成器,包括数据库连接信息、表名、生成路径等。 总结起来,SSM整合的思路是将SpringSpringMvc和Mybatis三个框架整合在一起,通过配置文件和相关依赖库来实现。具体的配置过程包括引入依赖、配置Web.xml文件、配置SpringMvc.xml文件、配置mybatis.xml文件和配置applicationContext.xml文件。如果需要生成实体类和Mapper文件,可以使用mybatis逆向工程来完成。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SSM整合思路与配置详解](https://blog.csdn.net/slysxy/article/details/107710775)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SSM整合,手把手教程,详解思路讲解](https://blog.csdn.net/liyingjie2001/article/details/124809314)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值