MyBatisPlus_通用service篇_入门试炼_05

Mybatis-Plus 专栏收录该内容
40 篇文章 2 订阅

1. service和impl

package com.gblfy.springboot.mybatisplus.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.gblfy.springboot.mybatisplus.entity.User;

public interface UserService extends IService<User> {
}

impl

package com.gblfy.springboot.mybatisplus.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gblfy.springboot.mybatisplus.entity.User;
import com.gblfy.springboot.mybatisplus.mapper.UserMapper;
import com.gblfy.springboot.mybatisplus.service.UserService;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

//    源码分析:
//    public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {

    }

2. 实战

package com.gblfy.springboot.mybatisplus.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gblfy.springboot.mybatisplus.entity.User;
import com.gblfy.springboot.mybatisplus.service.UserService;
import lombok.extern.slf4j.Slf4j;
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.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@Slf4j
public class CommonServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void getOne() {
        User one = userService.getOne(Wrappers.<User>lambdaQuery().gt(User::getAge, 25), false);
        System.out.println(one);

    }

    /**
     * 当调用saveBatch方法会执行插入操作
     * 当调用saveOrUpdateBatch方法会不存在执行插入存在执行更新操作
     */
    @Test
    public void bath() {
        User user1 = new User();
        user1.setName("yuxin");
        user1.setAge(2);

        User user2 = new User();
        user2.setId(11L);
        user2.setName("ly");
        user2.setAge(25);

        List<User> userList = Arrays.asList(user1, user2);
//        boolean saveBatch = userService.saveBatch(userList);
        boolean saveBatch = userService.saveOrUpdateBatch(userList);
        System.out.println(saveBatch);
    }

    @Test
    public void chain() {
        List<User> userList = userService.lambdaQuery().gt(User::getAge, 25).like(User::getName, "雨").list();
        userList.forEach(System.out::println);
    }

    @Test
    public void chain2() {
        boolean update = userService.lambdaUpdate().eq(User::getAge, 25).set(User::getAge, 30).update();
        System.out.println(update);
    }

    @Test
    public void chain3() {
        boolean remove = userService.lambdaUpdate().eq(User::getAge, 25).remove();
        System.out.println(remove);
    }
}
  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 猿与汪的秘密 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值