Mybatisplus中BaseMapper下方法演示

一、BaseMapper下方法展示

在这里插入图片描述

二、SpringBoot项目结构

1.项目结构

其他的不重要
在这里插入图片描述

2.sql文件

DROP TABLE IF EXISTS user;

CREATE TABLE user
(
    id BIGINT(20) NOT NULL COMMENT '主键ID',
    name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
    age INT(11) NULL DEFAULT NULL COMMENT '年龄',
    email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
    PRIMARY KEY (id)
);



DELETE FROM user;

INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');

3.application.yml

# DataSource Config
spring:
  datasource:
    # 配置数据源信息
    # type: com.zaxxer.hikari.HikariDataSource
    type: com.alibaba.druid.pool.DruidDataSource
    # 配置连接数据库的各个信息
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/mytest?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root

mybatis-plus:
  configuration:
    #默认不显示SQL日志
    #    log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

4.User

package com.cyl.entity;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @Author cyl
 * @create 2022/3/18
 */
@Data
@Accessors(chain = true)
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

5.UserMapper

package com.cyl.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cyl.entity.User;
import org.apache.ibatis.annotations.Mapper;

/**
 * @Author cyl
 * @create 2022/3/18
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

}

三、增

package com.cyl;

import com.cyl.entity.User;
import com.cyl.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @Author cyl
 * @create 2022/3/18
 */
@SpringBootTest
public class TestInsert {

    @Autowired
    UserMapper userMapper;

    @Test
    public void testInsert(){
        System.out.println("----- insert method test ------");
        User user = new User();
        user.setName("cyl")
                .setAge(23)
                .setEmail("aaa@123.com");
        // INSERT INTO user ( id, name, age, email ) VALUES ( 1504726979303559169, 'cyl', 23, 'aaa@123.com' )
        int result = userMapper.insert(user);
        System.out.println("result="+result);
        System.out.println("id="+user.getId());
    }
}

四、删

package com.cyl;

import com.cyl.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

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

/**
 * @Author cyl
 * @create 2022/3/18
 */
@SpringBootTest
public class TestDelete {

    @Autowired
    UserMapper userMapper;

    /**
     * 通过 id 删除
     */
    @Test
    public void testDeleteById(){
        System.out.println("----- deleteById method test ------");
        // DELETE FROM user WHERE id=1504726979303559169
        int result = userMapper.deleteById(1504726979303559169L);
        System.out.println("result="+result);
    }

    /**
     * 通过 map 设置条件删除
     * DELETE FROM user WHERE name = 'cyl' AND age = 22
     */
    @Test
    public void testDeleteByMap(){
        System.out.println("----- deleteByMap method test ------");
        Map<String,Object> map = new HashMap<>();
        map.put("name","cyl");
        map.put("age",22);
        int result = userMapper.deleteByMap(map);
        System.out.println("result="+result);
    }

    /**
     * 通过 in( ) 批量删除
     */
    @Test
    public void testDeleteBatchIds(){
        System.out.println("----- deleteBatchIds method test ------");
        List<Long> idList = Arrays.asList(1L, 2L);
        // DELETE FROM user WHERE id IN ( 1 , 2 )
        int result = userMapper.deleteBatchIds(idList);
        System.out.println("result="+result);
    }
}

五、改

package com.cyl;

import com.cyl.entity.User;
import com.cyl.mapper.UserMapper;
import com.cyl.util.FunTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * @Author cyl
 * @create 2022/3/18
 */
@SpringBootTest
public class TestUpdate {

    @Autowired
    UserMapper userMapper;

    @Test
    public void testUpdateById(){
        System.out.println("----- updateById method test ------");
        User user = new User();
        user.setId(3L)
                .setName("cyl")
                .setEmail("aaa@123.com");
        // UPDATE user SET name='cyl', email='aaa@123.com' WHERE id=3
        int result = userMapper.updateById(user);
        System.out.println(result);
    }

}

六、查

package com.cyl;

import com.cyl.entity.User;
import com.cyl.mapper.UserMapper;
import com.cyl.util.FunTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

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

/**
 * @Author cyl
 * @create 2022/3/18
 */
@SpringBootTest
public class TestSelect {

    @Autowired
    UserMapper userMapper;

    @Test
    public void testSelectAll(){
        System.out.println("----- selectAll method test ------");
        //  SELECT id,name,age,email FROM user
        List<User> userList = userMapper.selectList(null);
        System.out.println("----- 测试函数式接口使用 :: 引用方法输出 ------");
        userList.forEach(System.out::println);
    }

    @Test
    public void testSelectById(){
        System.out.println("----- selectById method test ------");
        //  SELECT id,name,age,email FROM user WHERE id=1
        User user = userMapper.selectById(1L);
        System.out.println(user);
    }

    @Test
    public void testSelectBatchIds(){
        System.out.println("----- selectBatchIds method test ------");
        //   SELECT id,name,age,email FROM user WHERE id IN ( 4 , 5 )
        List<Long> idList = Arrays.asList(4L, 5L);
        List<User> userList = userMapper.selectBatchIds(idList);
        userList.forEach(System.out::println);
    }

    @Test
    public void testSelectByMap(){
        System.out.println("----- selectByMap method test ------");
        //   SELECT id,name,age,email FROM user WHERE id IN ( 4 , 5 )
        Map<String,Object> map = new HashMap<>();
        map.put("name","cyl");
        map.put("age",23);
        //  SELECT id,name,age,email FROM user WHERE name = 'cyl' AND age = 23
        List<User> userList = userMapper.selectByMap(map);
        userList.forEach(System.out::println);
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值