首页 > 其他分享 >Mybatisplus标准数据层CRUD功能

Mybatisplus标准数据层CRUD功能

时间:2023-02-17 21:57:02浏览次数:41  
标签:功能 Mybatisplus CRUD Param user Wrapper import ew com

package com.itheima;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class Mybatisplus01QuickstartApplicationTests {

    @Autowired
    private UserDao userDao;

    //添加
    @Test
    void testSave(){
        User user = new User();
        user.setName("黑马程序员");
        user.setPassword("itheima");
        user.setAge(12);
        user.setTel("3284333232");
        userDao.insert(user);
    }

    //根据id删除
    @Test
    void testDelete(){
        int i = userDao.deleteById(1626569682363203586L);
        System.out.println(i);
    }
    
    //修改,提供那些字段就修改那些字段
    @Test
    void testUpdate(){
        User user = new User();
        user.setId(1L);
        user.setName("Tom666");

        int i = userDao.updateById(user);
    }

    //根据id查询
    @Test
    void testGetById(){
        User user = userDao.selectById(2L);
        System.out.println(user);
    }


    //查询所有
    @Test
    void testGetAll() {
        List<User> users = userDao.selectList(null);
        System.out.println(users);
    }

}

上述方法来自继承的BaseMapper

package com.itheima.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.domain.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserDao extends BaseMapper<User> {

}

点进去可以看到各种写好的方法

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.baomidou.mybatisplus.core.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;

public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);

    int deleteById(Serializable id);

    int deleteByMap(@Param("cm") Map<String, Object> columnMap);

    int delete(@Param("ew") Wrapper<T> queryWrapper);

    int deleteBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    int updateById(@Param("et") T entity);

    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);

    T selectById(Serializable id);

    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);

    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);

    T selectOne(@Param("ew") Wrapper<T> queryWrapper);

    Integer selectCount(@Param("ew") Wrapper<T> queryWrapper);

    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);

    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);

    List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);

    <E extends IPage<T>> E selectPage(E page, @Param("ew") Wrapper<T> queryWrapper);

    <E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param("ew") Wrapper<T> queryWrapper);
}

 

 

快速生成实体类方法:lombok---一个java类库,提供了一组注解,简化了POJO实体类开发

首先在pom文件中导入lombok依赖

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
在domain.User中使用如下:
package com.itheima.domain;

import lombok.*;
import org.springframework.format.annotation.DateTimeFormat;

//lombook


//@Setter
//@Getter
//@ToString
//@AllArgsConstructor

@Data
public class User {
    private Long id;
    private String name;
    private String password;
    private Integer age;
    private String tel;

}

 

标签:功能,Mybatisplus,CRUD,Param,user,Wrapper,import,ew,com
From: https://www.cnblogs.com/fxzm/p/17131572.html

相关文章