SSM整合

SSM整合(重点)

1. 注意点

注意尽量不要用最新版,可能没来得及匹配

例如: Spring6 解决了Tomcat10 的jakarta包的问题

但Spring6升级了,mybatis和mabytis-spring没有升级,甚至无法创建sqlSessionFactory实例

注意事项:

  • spring管理放在一个里面,applicationContext,spring-dao,spring-service, spring-mvc放在一个spring管理中,注意观察是否在一个(project structure中的facets)

请添加图片描述

  • 注意lib目录,经常更新

请添加图片描述

  • mybatis:mapper不能用com.cao.dao.*
  • 这些都是血和泪的教训

2. 版本选择

jdk : 17

Mysql : 8.0.29

Mybatis: 3.5.9

mybatis-spring:2.0.7(注意看官方文档的推荐)

Spring: 5.3.23

Tomcat: 9 ===>xml:4.0 not 5.0

这样就使用javax not Jakarta(servlet)

3. SSM整合

spring整合的文件都放在resource目录下

请添加图片描述

3.1 Maven依赖(jar包) pom.xml

<dependencies>
    <!--工具-->
    <!--使用junit实现单元测试-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <!--使用Lombok实现pojo-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
    </dependency>
    <!--使用Jackson实现前后端分离-->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.14.1</version>
    </dependency>



    <!--数据库 mybatis,mysql,c3p0或者spring-jdbc-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.9</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.28</version>
    </dependency>
    <!--数据源要一个即可c3p0或者spring-jdbc-->
    <dependency>
        <groupId>com.mchange</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.5.5</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.3.24</version>
    </dependency>




    <!--spring: spring-core, mybatis-spring(整合Mybatis),aop(aspectjweaver)-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.23</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.7</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.9.1</version>
    </dependency>



    <!--mvc: servlet jsp jstl-->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.2.1-b03</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp.jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

</dependencies>

除此之外还需要防止资源文件导不出进行配置 pom.xml

<build>
    <finalName>test</finalName>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

3.2 applicationContext.xml

这是总的spring配置文件,主要就是import别的spring配置文件,比较简单

<?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"
       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/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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <import resource="spring-dao.xml"/>
    <import resource="spring-service.xml"/>
    <import resource="spring-mvc.xml"/>
    
</beans>

3.3 dao层配置

新建db.properties, mybatis-config.xml, spring-dao.xml

db.properties:

配置数据库连接

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
jdbc.user=root
jdbc.password=123456
mybatis-config.xml:

Mybatis核心配置文件(我的想法是尽量在这里面配置,因为比较熟悉,包括mapper什么的)

<?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="logImpl" value="STDOUT_LOGGING"/>
    </settings>

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

    <!--    设置映射:这是唯一要改变的地方,每写一个dao层接口记得配置-->
    <mappers>
        <mapper class="com.cao.dao.BooksMapper"/>
    </mappers>

</configuration>
spring-dao.xml:

Spring整合Mybatis的文件

最重要的就是注册sqlSessionFactory的bean(3),由此我们需要dataSource(2). 接着就是要通过sqlSessionFactory手写获得sqlSession实现dao层的方法,要么自动(4)

<?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"
       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/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
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--3.可以导入数据库配置文件(mybatis中导也行,在哪都行)-->
    <context:property-placeholder location="classpath:db.properties"/>

    <!--1.数据源(Spring数据源, 或者其他数据源)
            dbcp c3p0 druid hikari
            这里是c3p0-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <!--其他特点配置-->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <!-- 关闭连接后不自动commit -->
        <property name="autoCommitOnClose" value="false"/>
        <!-- 获取连接超时时间 -->
        <property name="checkoutTimeout" value="10000"/>
        <!-- 当获取连接失败重试次数 -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>
    <!--    spring默认数据源配置-->
    <!--    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">-->
    <!--        <property name="driverClassName" value="${jdbc.driver}"/>-->
    <!--        <property name="url" value="${jdbc.url}"/>-->
    <!--        <property name="username" value="${jdbc.user}"/>-->
    <!--        <property name="password" value="${jdbc.password}"/>-->
    <!--    </bean>-->

    
    
    <!--2.SqlSessionFactory(核心配置=mybatis-config.xml):需要数据源-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--绑定mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>


    <!--4*.新方式实现注入,之前:要手写impl类并手动注入Impl实现dao功能,而且在实现类中还要注入SqlSession
        现在:通过反射自动去做这件事,配置dao接口扫描包,这样可以直接从spring中拿到dao-->
    <!--解释 : https://www.cnblogs.com/jpfss/p/7799806.html-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--注入sqlSessionFactory,注意这里是value而不是ref-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <!--扫描dao包-->
        <property name="basePackage" value="com.cao.dao"/>
    </bean>


    <!--    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
    <!--        <property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />-->
    <!--        <property name="sqlSessionFactory" ref="sqlSessionFactory" />-->
    <!--    </bean>-->

</beans>

3.4 service层配置

spring-service.xml

这一块比较简单,第一个就是要注册所有的Bean(扫描包,配置),第二个就是事务管理

<?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"
       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/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
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--    1.扫描注解-->
    <context:component-scan base-package="com.cao.service"/>

    <!--    2.将所有的业务层注入到spring:注解/配置-->
    <!--    这是xml配置方式举例-->
    <!--    <bean id="booksServiceImpl" class="com.cao.service.BooksServiceImpl">-->
    <!--        <property name="bm" ref="booksMapper"/>-->
    <!--    </bean>-->

    <!--    3.事务(最重要的部分)-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--    4.aop事务支持!!!-->
        <!--    声明式事务-->
<!--        <tx:advice id="txAdvice" transaction-manager="transactionManager">-->
<!--            <tx:attributes>-->
<!--                &lt;!&ndash;所有方法&ndash;&gt;-->
<!--                <tx:method name="*" propagation="REQUIRED"/>-->
<!--            </tx:attributes>-->
<!--        </tx:advice>-->
<!--        <aop:config>-->
<!--            &lt;!&ndash;这里是可以改变的,切点&ndash;&gt;-->
<!--            <aop:pointcut id="txPointcut" expression="execution(* com.cao.dao.*.*(..))"/>-->
<!--            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>-->
<!--        </aop:config>-->

</beans>

3.5 controller层配置

这层负责接收前端的部分,所以还需配置web.xml

web.xml==>4.0

1.注册DispatcherServlet,和2.乱码过滤(非常强大)

<?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">
    
	<!--springMVC的核心-->
    <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:applicationContext.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乱码过滤-->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <!--/*非常重要-->
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>
spring-mvc.xml

1.注解驱动 2.扫描包 3.过滤静态资源 4.视图过滤器

<?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"
       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/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
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--1.注解驱动:不用mapper和adapter-->
    <mvc:annotation-driven>
        <!--StringHttpMessageConverter转换配置,防止Json乱码-->
        <!--        <mvc:message-converters register-defaults="true">-->
        <!--            <bean class="org.springframework.http.converter.StringHttpMessageConverter">-->
        <!--                <constructor-arg value="UTF-8"/>-->
        <!--            </bean>-->
        <!--            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">-->
        <!--                <property name="objectMapper">-->
        <!--                    <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">-->
        <!--                        <property name="failOnEmptyBeans" value="false"/>-->
        <!--                    </bean>-->
        <!--                </property>-->
        <!--            </bean>-->
        <!--        </mvc:message-converters>-->
    </mvc:annotation-driven>

    <!--2*.静态资源过滤,让静态资源(jsp,img,mp4...)不走servlet映射-->
    <mvc:default-servlet-handler/>


    <!--3.扫描包:扫描注解-->
    <context:component-scan base-package="com.cao.controller"/>


    <!--4.视图过滤器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

4. 总结

合抱之木,生于毫末;九层之台,起于垒土;千里之行,始于足下。

大家一定要仔细配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值