当前位置: 首页 > article >正文

SSM— spring,springMVC,mybatis整合

前言

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

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

依赖

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

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


http://www.kler.cn/a/383931.html

相关文章:

  • SpringMVC学习记录(三)之响应数据
  • sql数据库-DQL-条件查询
  • 【Linux】冯诺依曼体系、再谈操作系统
  • hive切换表底层文件类型以及分隔符
  • Java:二维数组
  • 论文阅读笔记-Covariate Shift: A Review and Analysis on Classifiers
  • Flutter鸿蒙next 实现一个计算器应用
  • SpringBoot中使用SpringTask实现定时任务
  • OpenDroneMap Webodm
  • java-web-苍穹外卖-day1:软件开发步骤简化版+后端环境搭建
  • OCR、语音识别与信息抽取:免费开源的AI平台在医疗领域的创新应用
  • Rust智能指针和生命周期
  • 栈虚拟机和寄存器虚拟机,有什么不同?
  • 【运动的&足球】足球场景目标检测系统源码&数据集全套:改进yolo11-ASF-P2
  • 如何基于pdf2image实现pdf批量转换为图片
  • leetcode动态规划(二十六)-最长重复子数组
  • JS数据结构之“栈”、“队列”、“链表”
  • 【数学】通用三阶矩阵特征向量的快速求法 超简单!!!
  • 重构代码之参数化方法
  • 这款神器,运维绝杀 !!!
  • Docker 配置镜像加速
  • ECMAScript 6
  • 台式电脑如何改ip地址:全面解析与实操指南
  • AJAX学习笔记总结
  • 【LeetCode】【算法】283. 移动零
  • 数据结构之线段树