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中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会