mybatis_plus+增删改查测试项目

gitee项目地址:https://gitee.com/liangliangge520haha/programFile.git

1.导入依赖

<dependencies>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter-test</artifactId>
            <version>3.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

  2.准备application.yml配置文件

# mysql 5 驱动不同 com.mysql.jdbc.Driver
# mysql 8 驱动不同com.mysql.cj.jdbc.Driver、需要增加时区的配置serverTimezone=GMT%2B8

# DataSource Config
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启sql日志

pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  page-size-zero: true
  params: count=countSql
View Code

3.entity省略

4.mapper层

@Mapper
public interface UserMapper extends BaseMapper<User1> {//继承BaseMapper<T>
}5.测试代码
package com.liang;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liang.entity.User1;
import com.liang.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.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    UserMapper userMapper;

    @Test
    void contextLoads() {
        List<User1> user1s = userMapper.selectList(null);
        user1s.forEach(user1 -> {
            System.out.println(user1.toString());
        });
    }

    @Test
    void test1(){
        User1 user1 = userMapper.selectById(3);
        System.out.println(user1.toString());
    }

    //增
    @Test
    void test2(){
        User1 user1 = new User1();

        user1.setAge(20);
        user1.setEmail("3036346055@qq.com");
        user1.setName("啊亮");
        int insert = userMapper.insert(user1);
        System.out.println(insert);
    }

    //修改
    @Test
    public void test3(){
        User1 user1 = new User1();
        user1.setEmail("3036346055@qq.com");
        user1.setName("小亮");
        UpdateWrapper<User1> wrapper = new UpdateWrapper<>();
        wrapper.eq("age","20");
        int update = userMapper.update(user1, wrapper);
        System.out.println(update);
    }

    //删
    @Test
    public void test(){
        User1 user1 = new User1();
        user1.setId(1572581840461307905L);
        QueryWrapper<User1> wrapper = new QueryWrapper<>();
        wrapper.eq("age",20);
        int delete = userMapper.delete(wrapper);
        System.out.println(delete);
    }

    //通过id查询
    @Test
    void test4(){
        User1 user1 = userMapper.selectById(5);
        System.out.println(user1.toString());
    }

    //SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
    //多条件查询
    @Test
    void test5(){
        Map<String, Object> map = new HashMap<>();
        map.put("name","Jone");
        map.put("age",18);
        List<User1> user1s = userMapper.selectByMap(map);
        user1s.forEach(user1 -> {
            System.out.println(user1.toString());
        });
    }

    //分页查询
    @Test
    void test6(){
        PageHelper.startPage(1,10);
        List<User1> user1s = userMapper.selectList(null);
        PageInfo<User1> user1PageInfo = new PageInfo<>(user1s);





    }

}

  mybatisplus不用再写sql,极大的简化开发任务

posted @ 2022-09-21 22:57  程序员亮亮丫!!  阅读(20)  评论(0编辑  收藏  举报