SpringBoot整合JUnit、MyBatis、SSM


🐌个人主页: 🐌 叶落闲庭

💨我的专栏:💨
c语言
数据结构
javaEE
操作系统

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


SpringBoot整合

一、SpringBoot整合JUnit

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

二、SpringBoot整合MyBatis

2.1创建新模块



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



2.3设置数据源

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

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

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

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

java 复制代码
@SpringBootTest
class SpringbootMybatisApplicationTests {

    @Autowired
    private UserDao userDao;

    @Test
    void testGetById() {
        User user = userDao.selectById(1);
        System.out.println(user);
    }

}

三、SpringBoot整合SSM

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

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

3.2配置数据源、端口等

yaml 复制代码
server:
  port: 80
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
    username: root
    password: 123456

3.3配置类

  • 不需要

3.4设置@Mapper

java 复制代码
//TODO 添加@Mapper
@Mapper
public interface UserDao {

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

3.5测试类

java 复制代码
@SpringBootTest
class UserServiceImplTest {

    @Autowired
    private UserService userService;
    @Test
    void selectById() {
        User user = userService.selectById(2);
        System.out.println(user);
    }
}

3.6页面

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

3.7测试运行结果



相关推荐
我登哥MVP3 小时前
Spring Boot 从“会用”到“精通”:参数解析原理
java·spring boot·后端·spring·servlet·maven·intellij-idea
小江的记录本5 小时前
【Spring全家桶】Spring Cloud 2023.0.x:微服务核心理论、CAP/BASE定理(附《思维导图》+《面试高频考点清单》)
java·spring boot·后端·spring·spring cloud·微服务·面试
我登哥MVP5 小时前
Spring Boot 从“会用”到“精通”:Model-Map原理
java·spring boot·后端·spring·servlet·maven·mybatis
Full Stack Developme6 小时前
MyBatis-Plus 分页使用详解
mybatis
鱼鳞_6 小时前
苍穹外卖-Day12(数据统计)
java·spring boot
心之伊始6 小时前
Java 后端 AI 应用网关实战:多模型路由、Fallback、超时和可观测性设计
java·spring boot·大模型·架构设计·ai网关
落木萧萧8257 小时前
MyBatisGX 批量操作:比 MyBatis-Plus 和 MyBatis-Flex 更好用、更快
mybatis·orm
Jabes.yang7 小时前
Java电商订单系统面试全流程解析:接口设计、数据库、微服务与分布式事务实战
java·微服务·mybatis·分布式事务·电商·订单系统·接口设计
RuoyiOffice7 小时前
2026 企业定制开发选型:从零开发、低代码、SaaS 与 RuoYi Office 怎么选?
spring boot·uni-app·开源·saas·oa·定制化·ruoyioffice
闪电悠米7 小时前
黑马点评-秒杀优化-04_lua_and_db_fallback
服务器·开发语言·网络·数据库·缓存·junit·lua