Mybatis plus 实现IService方式,使用SpringbootTest引入service 测试

本文主要是介绍Mybatis plus 实现IService方式,使用SpringbootTest引入service 测试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说明:mybatis plus 不光可以继承BaseMapper 实现数据的增删改查,还可以通过继承IService方式实现,并可提供批量的增删改等方法 如:saveBatch、removeBatchByIds等,非常方便。

1、 引包

        <dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-annotation</artifactId><version>3.5.1</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-extension</artifactId><version>3.5.1</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter-test</artifactId><version>3.5.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>

2、 bean
 

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;import java.io.Serializable;
import java.util.Date;/*** @author zc* @date 2024/3/14 17:16* @desc*/
@Data
@TableName("cm_host_compare_diff")
public class HostDiffBean implements Serializable {/*** uuid, mybatis实现*/@TableId(type = IdType.ASSIGN_UUID)private String id;/*** 序列号*/private String serialNumber;/*** ip*/private String ip;/*** 设备型号*/private String deviceNo;/*** 华为端ip*/@TableField(value = "h_ip")private String hIp;/*** 华为端设备型号*/@TableField(value = "h_device_No")private String hDeviceNo;/*** 创建时间*/private Date createTime;
}

3、mapper 

说明: 需继承BaseMapper,这里虽然引用了Mapper, 但是BaseMapper里面的方法并不能使用

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neusoft.bean.HostDiffBean;
import org.apache.ibatis.annotations.Mapper;/*** @author zc* @date 2024/4/1 10:26* @desc*/
@Mapper
public interface TestMapper extends BaseMapper<HostDiffBean> {
}

4、 service

说明:继承IService

import com.baomidou.mybatisplus.extension.service.IService;
import com.neusoft.bean.HostDiffBean;
import org.springframework.stereotype.Repository;/*** @author zc* @date 2024/3/29 16:14* @desc*/
@Repository
public interface TestService extends IService<HostDiffBean> {
}

5、serviceImpl

说明:继承ServiceImpl、实现IService接口; 继承ServiceImpl则不需要重写IService的方法

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.bean.HostDiffBean;
import com.neusoft.mapper.TestMapper;
import org.springframework.stereotype.Service;/*** @author zc* @date 2024/4/2 19:22* @desc*/
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, HostDiffBean> implements TestService {
}

6、测试

说明:需要引用@SpringBootTest和RunWith,才可以在test 中引入service 使用,样例中主键id 由Mybatis plus 生成,不需要设值

mport com.neusoft.Application;
import com.neusoft.bean.HostDiffBean;
import com.neusoft.service.TestService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;/*** @author zc* @date 2024/3/19 10:29* @desc*/
@SpringBootTest(classes = Application.class)
@RunWith(SpringRunner.class)
public class ApplicationTest {@Autowiredprivate TestService testService;@Testpublic void test() {try{HostDiffBean hostDiffBean = new HostDiffBean();hostDiffBean.setIp("127.0.0.1");hostDiffBean.setHIp("127.0.0.2");hostDiffBean.setCreateTime(new Date());hostDiffBean.setSerialNumber("123");hostDiffBean.setHDeviceNo("no123");hostDiffBean.setDeviceNo("NO456");testService.save(hostDiffBean);}catch (Exception e){e.printStackTrace();}}
}

这篇关于Mybatis plus 实现IService方式,使用SpringbootTest引入service 测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/871129

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

MyBatis分页查询实战案例完整流程

《MyBatis分页查询实战案例完整流程》MyBatis是一个强大的Java持久层框架,支持自定义SQL和高级映射,本案例以员工工资信息管理为例,详细讲解如何在IDEA中使用MyBatis结合Page... 目录1. MyBATis框架简介2. 分页查询原理与应用场景2.1 分页查询的基本原理2.1.1 分

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx