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

SpringBoot整合JUnit、MyBatis、SSM


在这里插入图片描述

🐌个人主页: 🐌 叶落闲庭
💨我的专栏:💨
c语言
数据结构
javaEE
操作系统

石可破也,而不可夺坚;丹可磨也,而不可夺赤。


SpringBoot整合

  • 一、SpringBoot整合JUnit
  • 二、SpringBoot整合MyBatis
    • 2.1创建新模块
    • 2.2选择当前模块需要的技术集(MyBatis、MySQL)
    • 2.3设置数据源
    • 2.4定义数据层接口与映射配置
    • 2.5测试类中注入dao接口,测试功能
  • 三、SpringBoot整合SSM
    • 3.1配置起步依赖,必要的资源坐标(druid)
    • 3.2配置数据源、端口等
    • 3.3配置类
    • 3.4设置@Mapper
    • 3.5测试类
    • 3.6页面
    • 3.7测试运行结果

一、SpringBoot整合JUnit

  • 名称:@SpringBootTest
  • 类型:测试类注解
  • 位置:测试类定义上方
  • 作用:设置JUnit加载的SpringBoot启动类
  • 范例:
@SpringBootTes(classes = SpringboottestApplication.class)
class SpringboottestApplicationTests{}
  • 相关属性
    • classes:设置SpringBoot启动类
  • 注意:
  • 如果测试类在SpringBoot启动类的包或子包中,可以省略启动类的设置,也就是省略classes的设定

二、SpringBoot整合MyBatis

2.1创建新模块

在这里插入图片描述


在这里插入图片描述


2.2选择当前模块需要的技术集(MyBatis、MySQL)


在这里插入图片描述


2.3设置数据源

spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatisusername: rootpassword: 123456
  • SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区
url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
  • 或在Mysql数据库端配置时区解决此问题

2.4定义数据层接口与映射配置

@Mapper
public interface UserDao {@Select("select * from tb_user where id=#{id}")public User selectById(@Param("id") int id);
}

2.5测试类中注入dao接口,测试功能

@SpringBootTest
class SpringbootMybatisApplicationTests {@Autowiredprivate UserDao userDao;@Testvoid testGetById() {User user = userDao.selectById(1);System.out.println(user);}}

三、SpringBoot整合SSM

3.1配置起步依赖,必要的资源坐标(druid)

<!--TODO 添加必要的依赖坐标--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.16</version></dependency>

3.2配置数据源、端口等

server:port: 80
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTCusername: rootpassword: 123456

3.3配置类

  • 不需要

3.4设置@Mapper

//TODO 添加@Mapper
@Mapper
public interface UserDao {@Select("select * from tb_user where id=#{id}")public User selectById(@Param("id") int id);
}

3.5测试类

@SpringBootTest
class UserServiceImplTest {@Autowiredprivate UserService userService;@Testvoid selectById() {User user = userService.selectById(2);System.out.println(user);}
}

3.6页面

  • 前端页面的所有文件放置在resources目录下的static目录中

3.7测试运行结果


在这里插入图片描述


http://www.lryc.cn/news/148896.html

相关文章:

  • virtuoso61x中集成calibre
  • com.google.guava:guava 组件安全漏洞及健康分析
  • Hadoop服务脚本
  • [QT]设置程序仅打开一个,再打开就唤醒已打开程序的窗口
  • 数据库(二) Oracle篇
  • TDengine函数大全-目录
  • 代理模式之静态代理
  • LeetCode——栈的压入、弹出序列
  • Flutter 逆向安全
  • 【微服务部署】01-Kubernetes部署流程
  • SPI3+DMA外设驱动-TFTLCD初始化
  • 通过chatgpt 学习React的useEffect
  • rabbitMq介绍及使用
  • rabbitmq载在.net中批量消费的问题记录
  • 【RPC 协议】序列化与反序列化 | lua-cjson | lua-protobuf
  • Flutter的Timer类
  • Chrome小恐龙快跑小游戏——Python实现
  • Web网站服务器
  • Docker consul 容器服务自动发现和更新
  • CentOS 8 执行yum命令报错:Failed to set locale, defaulting to C.UTF-8
  • 8. 损失函数与反向传播
  • Angular安全专辑之四 —— 避免服务端可能的资源耗尽(NodeJS)
  • Servlet学习总结(Request请求与转发,Response响应,Servlet生命周期、体系结构、执行流程等...)
  • 雅思写作 三小时浓缩学习顾家北 笔记总结(二)
  • Element Plus 日期选择器的使用和属性
  • 中国五百强企业用泛微为合同加速,提升数字化办公水平
  • Vue3 QRCode生成
  • 2023年8月随笔之有顾忌了
  • 正中优配:红筹股是啥意思?
  • 《Linux从练气到飞升》No.19 进程等待