SSM整合--笔记总结

news2024/9/20 17:33:22

1.概述

ssm(springmvc spring mybatis)这三个框架的整合。 spring和springmvc他们隶属于一家公司,他们无需整合。 spring和mybatis框架的整合。 spring把mybatis中的配置内容放到自己的配置文件中。因为我们可以让tomcat加载spring配置文件。

思考:mybatis配置文件中究竟有哪些?

<1>数据源

<2>引入映射文件

<3>别名

<4>插件

<5>引入属性文件


spring就是把上面12345放入spring配置文件中。

2.步骤

2.1创建一个Maven的web工程

2.2引入所有依赖

<properties>

<spring.version>5.2.10.RELEASE</spring.version>

<lombok.version>1.18.24</lombok.version>

</properties>

<dependencies>

<!--spring和mybatis整合的jar-->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis-spring</artifactId>

<version>2.0.7</version>

</dependency>

<!--spring-jdbc-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

<version>${spring.version}</version>

</dependency>

<!--spring和springmvc的核心依赖-->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>

<!--lombok-->

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>${lombok.version}</version>

</dependency>

<!--mysql-->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.28</version>

</dependency>

<!--servlet-->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.1.0</version>

</dependency>

<!--druid数据源-->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.2.8</version>

</dependency>

<!--mybatis-->

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.5.10</version>

</dependency>

<!--junit单元测试-->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.13.2</version>

<scope>test</scope>

</dependency>

<!--jackson转化json-->

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.13.3</version>

</dependency>

<!--pagehelper 分页-->

<dependency>

<groupId>com.github.pagehelper</groupId>

<artifactId>pagehelper</artifactId>

<version>6.1.0</version>

</dependency>

</dependencies>

2.3spring的配置文件

1.springmvc的配置

①包扫描

②开启注解

③放行静态资源

④视图解析器

2.spring的配置

①加载数据库的属性文件

②Druid数据源

3.mybtais的配置

封装一个SqlSessionFactoryBean类

表示mapper映射文件的路径

指定数据源

为实体类起别名

4.为mybatis的dao接口生成代理实现类

<?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">
<!--1.springmvc的配置-->
   <!--包扫描-->
    <context:component-scan base-package="com.wjy"/>
   <!--开启注解-->
    <mvc:annotation-driven/>
    <!--放行静态资源-->
    <mvc:default-servlet-handler/>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
<!--2.spring的配置-->
    <!--加载数据库的属性文件-->
    <context:property-placeholder location="classpath:db.properties"/>
    <!--Druid数据源-->
    <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
<!--3.加载mybtais配置  封装一个类SqlSessionFactoryBean-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
    <!--表示mapper映射文件的路径-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    <!--指定数据源-->
        <property name="dataSource" ref="ds"/>
    <!--为实体类起别名-->
        <property name="typeAliasesPackage" value="com.wjy.entity"/>
    </bean>
<!--4.为mybatis的dao接口生成代理实现类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.wjy.dao"/>
    </bean>
</beans>

2.4web.xml加载spring配置文件

<?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">
<!--过滤器-->
    <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>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>EncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
<!--    -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml</param-value>
        </init-param>
        <!--tomcat启动时加载-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

2.5生成实体类和dao接口以及映射文件

利用快速创建的方式

2.6EmpService业务层接口和实现类

package com.wjy.service;

import com.wjy.entity.Emp;

public interface EmpService {
    /**
     * 业务层接口
     * @param id
     * @return 根据id查询员工信息
     */
    public Emp getById(Integer id);

    /**
     * 根据id删除员工信息
     * @param id
     * @return
     */
    public Integer deleteById(Integer id);

    /**
     * 添加员工信息
     * @param emp
     * @return
     */
    public Integer add(Emp emp);

    /**
     * 根据id修改
     * @param emp
     * @return
     */
    public Integer update(Emp emp);
}
@Service//交给容器解决EmpServiceImp对象
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    public Emp getById(Integer id) {
        Emp emp = empMapper.selectByPrimaryKey(id);
        return emp;
    }

    public Integer deleteById(Integer id) {
        int i = empMapper.deleteByPrimaryKey(id);
        return i;
    }

    public Integer add(Emp emp) {
        int i = empMapper.insert(emp);
        return i;
    }

    public Integer update(Emp emp) {
        int i = empMapper.updateByPrimaryKey(emp);
        return i;
    }
}

2.7控制层controller

调用service业务层

@RestController//作用:Controller+ResponseBody--自动转换为json类型
@RequestMapping("/emp")
public class EmpController {
    @Autowired//按照类型匹配,找不到按照名字匹配
    private EmpService empService;

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("byId")//API接口(功能接口)
    public Emp byId(Integer id){
        Emp emp = empService.getById(id);
        return emp;
    }

    @DeleteMapping("deleteById")
    public Integer deleteById(Integer id){
        Integer row = empService.deleteById(id);
        return row;
    }
    @PostMapping("add")
    public Integer insert(@RequestBody Emp emp){
        Integer add = empService.add(emp);
        return add;
    }
    @PutMapping("update")
    public Integer update(@RequestBody Emp emp){
        Integer update = empService.update(emp);
        return update;
    }
}

2.8启动tomcat并测试

浏览器只能测试get请求的接口

测试工具---Postman---apifox--apipost等

查询(get请求)

删除(delete请求)

增加(post请求)、修改(put请求),都是json传输

3.返回统一的json格式数据

controller层接口中他们的返回类型各式各样的,未来前后端交互时,要求接口返回的类型必须统一。

{

code:"状态码";--前端可以用按照你的状态码判断操作成功和失败

msg:"消息";--前端根据后端返回的消息弹出不同的内容

data:"数据";--前端可以拿出数据并进行展示

}

@RestController//作用:Controller+ResponseBody--自动转换为json类型
@RequestMapping("/emp")
public class EmpController {
    @Autowired//按照类型匹配,找不到按照名字匹配
    private EmpService empService;
   
    @GetMapping("byId")//API接口(功能接口)
    public R byId(Integer id){
        Emp emp = empService.getById(id);
        return new R(200,"查询员工信息成功",emp);
    }

    @DeleteMapping("deleteById")
    public R deleteById(Integer id){
        Integer row = empService.deleteById(id);
        if (row>0){
            return new R(200,"删除成功",null);
        }
        return new R(500,"删除失败",null);
    }
    @PostMapping("add")
    public R insert(@RequestBody Emp emp){
        Integer add = empService.add(emp);
        if (add >0) {
            return new R(200,"添加员工成功",null);
        }
        return new R(200,"添加员工失败",null);
    }
    @PutMapping("update")
    public R update(@RequestBody Emp emp){
        Integer update = empService.update(emp);
        if (update >0) {
            return new R(200,"修改成功",null);
        }
        return new R(200,"修改成功",null);
    }
}

4.设置一个统一页面跳转的controller层

5.前端搜索框

 <%--搜索表单的开始--%>
    <el-form :inline="true" :model="empSearchForm" class="demo-form-inline">
        <el-form-item label="姓名">
            <el-input v-model="empSearchForm.name" placeholder="员工名称"></el-input>
        </el-form-item>
        <el-form-item label="入职时间">
            <el-date-picker
                    v-model="empSearchForm.daterange"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
            </el-date-picker>
        </el-form-item>
        <el-form-item label="所在部门">
            <el-select v-model="empSearchForm.deptId" placeholder="所在部门">
                <el-option label="区域一" value="shanghai"></el-option>
                <el-option label="区域二" value="beijing"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" >查询</el-button>
        </el-form-item>
    </el-form>
    <%--搜索表单的结束--%>

5.1搜索框下拉所有的部门信息

DeptController

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;
    @GetMapping("/list")
    public R list(){
      List<Dept> depts = deptService.selectAll();
      return new R(200,"查询部门成功",depts);
    }
}

DeptServiceImpl

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    public List<Dept> selectAll() {
       return deptMapper.selectAll();
    }
}

DeptMapper

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1925879.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SD card知识学习

一、基础知识 1、简介 SD Card 全称(Secure Digital Memory Card)&#xff0c;日本电子公司松下&#xff08;Panasonic&#xff09;、瑞典公司爱立信&#xff08;Ericsson&#xff09;、德国公司西门子&#xff08;Siemens&#xff09;共同开发的&#xff0c;于1999年发布根…

Java中如何发送短信?(荣耀典藏版)

大家好&#xff0c;我是月夜枫~~ 本来是没计划写这方面的文章&#xff0c;奈何粉丝经常私信要求整理一篇发短信的文章&#xff0c;今天他来了。 很多业务场景里&#xff0c;我们都需要发送短信&#xff0c;比如登陆验证码、告警、营销通知、节日祝福等等。 这篇文章&#xf…

JavaSE——集合框架二(4/6)-Map集合的遍历方式(键找值,键值对,Lambda)、Map集合案例(需求与分析,问题解决)

目录 Map集合的遍历方式 键找值 键值对 Lambda Map集合案例 需求与分析 问题解决 Map集合的遍历方式 键找值 先获取Map集合全部的键&#xff0c;再通过遍历键来找值。 键值对 把“键值对”看成一个整体进行遍历&#xff08;较为复杂&#xff09; Lambda JDK 1.8 开…

C嘎嘎:函数模版和类模版

目录 泛型编程 函数模版 函数模版概念 函数模版的格式 函数模版的原理 函数模版的实例化 函数参数的匹配原则 类模版 类模版的定义格式 类模版的实例化 泛型编程 如何实现一个通用的交换函数呢 void Swap(int& left, int& right) {int temp left;left rig…

【每日一练】python之sum()求和函数实例讲解

在Python中&#xff0c; sum()是一个内置函数&#xff0c;用于计算可迭代对象&#xff08;如列表、元组等&#xff09;中所有元素的总和。如下实例&#xff1a; """ 收入支出统计小程序 知识点:用户输入获取列表元素添加sum()函数&#xff0c;统计作用 "&…

快捷:通过胶水语言实现工作中测试流程并行、加速

通过胶水语言实现工作中测试流程并行、加速 通过胶水语言实现工作中测试流程并行、加速工作场景&#xff08;背景&#xff09;问题抽象&#xff08;挑战&#xff09;如何做&#xff08;行动&#xff09;获得了什么&#xff08;结果&#xff09;后记相关资源 通过胶水语言实现工…

Oracle 性能诊断包收费依据

Which Data Dictionary or Dynamic Performance Views Require Purchase of the Diagnostics and / or Tuning Pack? (Doc ID 2082355.1)​编辑To Bottom In this Document Goal Solution References APPLIES TO: Oracle Database - Enterprise Edition - Version 10.2.0.5 …

AI口语练习APP主要功能

AI口语练习APP主要功能可以分为以下几个方面&#xff0c;AI口语练习APP可以帮助用户克服练习口语的场地、时间、语言环境等限制&#xff0c;更方便、高效地练习口语&#xff0c;提高英语口语水平。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎…

Profibus协议转Profinet协议网关模块连接智能电表通讯案例

一、背景 在工业自动化领域&#xff0c;Profibus协议和Profinet协议是两种常见的工业通讯协议&#xff0c;而连接智能电表需要用到这两种协议之间的网关模块。本文将通过一个实际案例&#xff0c;详细介绍如何使用Profibus转Profinet模块&#xff08;XD-PNPBM20&#xff09;实…

电脑案件冲突问题

一.故障展示 有一天我打开了电脑,发现3这个数字按键一直在输入,拔了外界的键盘,他这个按键还是会冲突 ,就如同上面的图一样 ,可能是电脑内部的键位进了灰卡住了什么东西导致的,于是我果断就电脑上的按键给扣下来了,扣的时候不知道里面的结构非常的谨慎,所以没导致里面的结构被损…

Amazon EC2 部署Ollama + webUI

最近和同事闲聊&#xff0c;我们能不能内网自己部署一个LLM&#xff0c;于是便有了Ollama webUI的尝试 对于Linux&#xff0c;使用一行命令即可 curl -fsSL https://ollama.com/install.sh | shollama --help Large language model runnerUsage:ollam…

C语言 ——— const关键字

目录 const修饰变量 const修饰指针变量 const放在指针类型之前 const放在指针类型之后 小结 const修饰变量 当 const 修饰 int类型 的 变量a 后&#xff0c;此时的 变量a 就具有长属性&#xff0c;就不能被赋值为其他的值 将 变量a的地址 存储到 指针变量pa 中&#xff…

【JavaScript】解决 JavaScript 语言报错:Uncaught TypeError: XYZ is not iterable

文章目录 一、背景介绍常见场景 二、报错信息解析三、常见原因分析1. 对非数组类型使用 for...of 循环2. 对非可迭代对象使用扩展运算符3. 在 Promise.all 中传递非可迭代对象4. 使用解构赋值时&#xff0c;右侧值非可迭代 四、解决方案与预防措施1. 确保使用可迭代对象2. 使用…

开源项目的浪潮:机遇、挑战与未来展望

&#x1f308;所属专栏&#xff1a;【其它】✨作者主页&#xff1a; Mr.Zwq✔️个人简介&#xff1a;一个正在努力学技术的Python领域创作者&#xff0c;擅长爬虫&#xff0c;逆向&#xff0c;全栈方向&#xff0c;专注基础和实战分享&#xff0c;欢迎咨询&#xff01; 您的点…

二分查找和斐波那契查找

这里写自定义目录标题 二分查找斐波那契查找二分查找改进B二分查找改进C 二分查找 int binSearch(int* arr, int lo, int hi,int target) {while (lo < hi){int mid lo ((hi - lo) >> 1);if (arr[mid] > target) hi mid;else if (arr[mid] < target) lo mi…

【postgresql】锁

PostgreSQL 提供了多种锁模式来控制对表和行的并发访问&#xff0c;以确保数据的一致性和完整性。这些锁模式包括表级锁和行级锁&#xff0c;它们可以由应用程序显式控制&#xff0c;也可以在执行大多数 PostgreSQL 命令时自动获取。 锁类型 PostgreSQL类型的锁包括&#xff…

【JavaEE】网络编程——TCP

&#x1f921;&#x1f921;&#x1f921;个人主页&#x1f921;&#x1f921;&#x1f921; &#x1f921;&#x1f921;&#x1f921;JavaEE专栏&#x1f921;&#x1f921;&#x1f921; 文章目录 前言1.网络编程套接字1.1流式套接字(TCP)1.1.1特点1.1.2编码1.1.2.1ServerSo…

开发个人Ollama-Chat--10 绑定域名

开发个人Ollama-Chat–10 绑定域名 域名购买最好找正规的渠道购买&#xff0c;不要因贪图小便宜而多走很多的弯路。我就是第一次购买域名&#xff0c;到了一个坑壁的平台"西部数码"&#xff0c;SSL证书申请了2个月&#xff0c;没下来&#xff0c;客服也贼不专业&…

SAP 消息输出 - Adobe Form

目录 1 安装链接 2 前台配置 - Fiori app 2.1 维护表单模板 (maintain form templates) 2.2 管理微标 (manage logos) 2.3 管理文本 (manage texts) 3 后台配置 3.1 定义表单输出规则 3.2 分配表单模板 SAP 消息输出&#xff0c;不仅是企业内部用来记录关键业务操作也是…

GAN 如何打造人造名人身份?

GAN 如何打造人造名人身份&#xff1f; 文章目录 一、介绍二、生成对抗网络&#xff08;GAN&#xff09;三、什么是发电机&#xff1f;四、什么是鉴别器&#xff1f;五、对抗性训练六、实现七、数据7.1 初始配置和设置7.2 数据加载器7.3 噪声产生7.4 发电机7.5 鉴别器 八、训练…