Démonstration de la méthode sous BaseMapper dans Mybatisplus

1. Affichage de la méthode sous BaseMapper

insérez la description de l'image ici

Deux, la structure du projet SpringBoot

1. Structuration du projet

Les autres ne sont pas importants
insérez la description de l'image ici

fichier 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, '[email protected]'),
(2, 'Jack', 20, '[email protected]'),
(3, 'Tom', 28, '[email protected]'),
(4, 'Sandy', 21, '[email protected]'),
(5, 'Billie', 24, '[email protected]');

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.Utilisateur

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> {
    
    

}

Trois, augmenter

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("[email protected]");
        // INSERT INTO user ( id, name, age, email ) VALUES ( 1504726979303559169, 'cyl', 23, '[email protected]' )
        int result = userMapper.insert(user);
        System.out.println("result="+result);
        System.out.println("id="+user.getId());
    }
}

Quatre, supprimer

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);
    }
}

Cinq, change

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("[email protected]");
        // UPDATE user SET name='cyl', email='[email protected]' WHERE id=3
        int result = userMapper.updateById(user);
        System.out.println(result);
    }

}

Six, vérifiez

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);
    }
}

Je suppose que tu aimes

Origine blog.csdn.net/qq_43010602/article/details/123578040
conseillé
Classement