SSM— spring,springMVC,mybatis整合

前言

SSM整合主要是将mybatis交给spring管理;将spring容器交给tomcat启动;对于web的请求和访问交给springmvc处理

没啥好说的,主要是配置文件

依赖

XML 复制代码
    <dependencies>
        <!--测试依赖2个-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.3.23</version>
        </dependency>

        <!--工具依赖2个-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.36</version>
        </dependency>

        <!--web相关依赖  5个-->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.25</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>2.14.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.15.2</version>
        </dependency>

        <!--数据库依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.21</version>
        </dependency>

        <!--mybatis相关依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>
        <!--spring-ioc依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.25</version>
        </dependency>
        <!--分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.2.1</version>
        </dependency>
    </dependencies>

spring.xml-配置spring和mybatis

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: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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--我们是要通过spring配置文件,完成service层以及Mapper层对象的创建-->
    <!--service层的类会在其上添加@Service注解,直接扫包就能创建bean-->
    <context:component-scan base-package="com.wngz.ssm.service"/>
    <!--创建数据访问层对象之前,必须链接数据库,就要用到数据源,必须知道连接的数据库信息-->
    <!--要知道所连接的数据库信息,必须导入数据库配置文件db.properties-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--配置druid数据源-->
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--如何创建Mapper实例呢,需要用到SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.wngz.ssm.entity"/>

        <property name="dataSource" ref="dataSource"/>

        <property name="mapperLocations" value="classpath:mappers/*.xml"/>

        <property name="configuration">
            <bean class="org.apache.ibatis.session.Configuration">
<!--                <property name="logImpl" value="org.apache.ibatis.logging.stdout.StdOutImpl"/>-->
                <property name="mapUnderscoreToCamelCase" value="true"/>
            </bean>
        </property>

        <!--配置mybatis插件-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            <!--指定用哪个数据库-->
                            helperDialect=mysql
                            <!--是否合理化,当输入的页码大于最大页数时,返回最后一页-->
                            reasonable=true
                            <!--当页码为0时,查询全部-->
                            pageSizeZero=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    <!--扫描mapper接口所在的包,使用上面的SqlSessionFactoryBean来创建Mappers接口实例-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wngz.ssm.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!--配置事务管理器-->
    <!--务必注意,这个DataSourceTransactionManager类的bean,它的id一定要设为transactionManager-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--mysql的事务归根结底还是要使用Connection。所以这里还需要配置数据源-->
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--启动事务管理, transaction-->
    <tx:annotation-driven/>
</beans>

springmvc.xml

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.wngz.ssm.controller"></context:component-scan>
    <!--    注册springmvc相关注解-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--    让静态资源文件交给默认servlet处理-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>

db.properties

XML 复制代码
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/community?serverTimezone=GMT%2B8
jdbc.username=root
jdbc.password=123456

mappers/*.xml

XML 复制代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wngz.ssm.mapper.UserInfoMapper">
    <!-- mybatis映射文件-->
    <select id="selectByName" resultType="com.wngz.ssm.entity.UserInfo">
        select id,
               username,
               password,
               type,
               remarks,
               create_time as createTime,
               status
        from userinfo
        where username = #{username}
          and status = 1
    </select>
</mapper>
相关推荐
李少兄23 分钟前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
此木|西贝28 分钟前
【设计模式】原型模式
java·设计模式·原型模式
可乐加.糖1 小时前
一篇关于Netty相关的梳理总结
java·后端·网络协议·netty·信息与通信
s9123601011 小时前
rust 同时处理多个异步任务
java·数据库·rust
9号达人1 小时前
java9新特性详解与实践
java·后端·面试
cg50171 小时前
Spring Boot 的配置文件
java·linux·spring boot
啊喜拔牙1 小时前
1. hadoop 集群的常用命令
java·大数据·开发语言·python·scala
anlogic2 小时前
Java基础 4.3
java·开发语言
非ban必选2 小时前
spring-ai-alibaba第七章阿里dashscope集成RedisChatMemory实现对话记忆
java·后端·spring
A旧城以西2 小时前
数据结构(JAVA)单向,双向链表
java·开发语言·数据结构·学习·链表·intellij-idea·idea