码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 基于Springboot的SSM整合案例(详解)


    在这里插入图片描述

    🍁博客主页:👉不会压弯的小飞侠
    ✨欢迎关注:👉点赞👍收藏⭐留言✒
    ✨系列专栏:SSM框架整合专栏
    ✨如果觉得博主的文章还不错的话,请三连支持一下博主。
    🔥欢迎大佬指正,一起 学习!一起加油!

    在这里插入图片描述

    目录

    • 🍁前言
    • 🍁配置类
    • 🍁配置起步依赖
    • 🍁设置数据源和端口
    • 🍁dao层
    • 🍁测试
    • 🍁页面处理
    • 🍁创建主页
    • 🍁下一篇是基于Springboot的SSMP整合案例(详解)
      • 🔥分页功能
      • 🔥搜索功能
      • 🔥开启MP运行日志


    🍁前言

    • 创建SpringBoot工程的过程就不过多介绍,如需要系统的学习,可以去👉Springboot专栏 (点击直接查看) 进行学习。
    • 本案例创建使用的是👉SpringBoot阿里云版 (点击直接查看)
    • 本项目直接改进上篇👉SSM案例整合分析(详解) (点击直接查看)的代码即可。

    🍁配置类

    🔥需要把上篇SSM案例整合分析(详解)的代码导入SpringBoot工程中,直接把config包下的配置类全部干掉。SpringBoot的核心就是自动配置。

    项目结构:
    在这里插入图片描述

    🍁配置起步依赖

    🔥导入druid依赖

            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>1.2.11</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    🍁设置数据源和端口

    🔥把jdbc.properties给删掉,创建application.yml配置文件

    server:
      port: 8080
    spring:
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC
        username: root
        password: root
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    🍁dao层

    • @Mapper
    • 一般SpringBoot中注入dao的方式就是如下俩种:
      • 给每个dao接口上都加上@Mapper,它就会将mapper自动注入进spring容器。
      • 在主启动类上加入@MapperScan,指定要扫描(dao接口)包的路径。
    package com.study.dao;
    
    import com.study.domain.Book;
    import org.apache.ibatis.annotations.*;
    
    import java.util.List;
    @Mapper
    public interface BookDao {
        @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
        public int save(Book book);
        @Update("update tbl_book set type=#{type},name=#{name},description=#{description} where id=#{id}")
        public int update(Book book);
        @Delete("delete from tbl_book where id=#{id}")
        public int delete(Integer id);
        @Select("select * from tbl_book where id=#{id}")
        public Book getById(Integer id);
        @Select("select * from tbl_book")
        public List<Book> getAll();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    🍁测试

    🔥编写测试类

    package com.study;
    
    import com.study.domain.Book;
    import com.study.service.BookService;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import java.util.List;
    @SpringBootTest
    public class BookTest {
        @Autowired
        private BookService bookService;
        @Test
        public void getById(){
            Book byId = bookService.getById(7);
            System.out.println(byId);
        }
    
        @Test
        public void getAll(){
            List<Book> all = bookService.getAll();
            for (Book book : all) {
                System.out.println(book);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    运行测试:
    在这里插入图片描述

    🍁页面处理

    🔥添加静态资源
    静态资源需要放到resource下的static包中,这里就不过多描述,上篇把整个项目传入GitHub中,需要的自取。
    代码不需要修改。
    在这里插入图片描述

    🍁创建主页

    启动的时候就不用输入地址了。
    🔥index.html

    <script>
        document.location.href="pages/books.html";
    </script>
    
    • 1
    • 2
    • 3

    测试:
    🔥 新增,编辑和删除功能也是正常可以使用的。
    在这里插入图片描述

    🍁下一篇是基于Springboot的SSMP整合案例(详解)

    🔥分页功能

    🔥搜索功能

    🔥开启MP运行日志

  • 相关阅读:
    用strtok和指针数组构造一个能对字符转进行解析的函数
    一次清理全屋地面,一键清洁烘干无异味,KEEWOO启为C260 Pro洗地机上手
    Redis:新的3种数据类型Bitmaps、HyperLoglog、Geographic
    SpringCloud 微服务全栈体系(五)
    新学期,新flag | 新学期的目标
    【JS 构造|原型|原型链|继承(圣杯模式)|ES6类语法】上篇
    【感恩系列】:说点事儿 以及 我把所有的粉丝放到了中国地图上啦~
    RESTful风格学习笔记【包含示例】
    Python实战 | 使用 Python 的日志库(logging)和 pandas 库对日志数据进行分析
    [学习记录] 设计模式 1. 设计模式介绍
  • 原文地址:https://blog.csdn.net/qq_43514330/article/details/126006882
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号