本文主要是介绍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 测试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!