栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis-plus的Service

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis-plus的Service

Service 1、通用Service接口

可以看官网的CRUD接口

https://baomidou.com/pages/49cc81/

在example包下创建接口service.UserService

package com.example.service.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.pojo.User;

public interface UserService extends IService {
}

在service包下创建文件impl.UserServiceImpl

@Service
package com.example.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.UserMapper;
import com.example.pojo.User;
import com.example.service.service.UserService;

public class UserServiceImpl extends ServiceImpl implements UserService {

}
ServiceImpl 
UserMapper:自己创建的接口类
User:自己创建的实体类
2、测试通用Service之查询总记录数

创建测试文件

package com.example.mybatisplus;

import com.example.service.service.UserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class MybatisPlusServiceTest {
    @Autowired
    private UserService userService;

    @Test
    public void test1(){
    	//查询总记录数
        //SELECt COUNT( * ) FROM user
        long count = userService.count();
        System.out.println("总结记录数:"+count);
    }
}

3、测试通用Service之批量添加功能

如果用baseMapper的基本insert添加数据,如果以后有几千行甚至几万行数据需要添加,需要多少代码?这时候需要这个通用接口来实现

public void testInsertMore(){
        //INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? )
        List list=new ArrayList<>();
        for (int i = 0; i <=10; i++) {
            User user=new User();
            user.setName("lisi"+i);
            user.setAge(20+i);
            list.add(user);
        }
        boolean b = userService.saveBatch(list);
        System.out.println(b);
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/764058.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号