mybatis-plus快速上手秘籍

MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

废话不多说,直接开干!!!!!!!!!!

创建数据库

所有的增删改查都是围绕数据库在做的,所以建表是第一步。

DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `name` varchar(256) NOT NULL COMMENT '名字',
  `age` int(5) NOT NULL COMMENT '年龄',
  `sex` int(1) NOT NULL COMMENT '性别',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8mb4;

创建项目,引入依赖

没有依赖怎么使用技术呢?pom文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <version>2.2.2.RELEASE</version>
        <artifactId>spring-boot-starter-parent</artifactId>
    </parent>
    <groupId>net.zyy</groupId>
    <artifactId>mybatis-test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>
</project>

配置文件

配置文件肯定就配置一下数据库之类的了:

类的编写 

pojo类:

@TableName("test")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Test {
    @TableId(type = IdType.AUTO)
    private Integer id;
    private String name;
    private Integer age;
    private Integer sex;
}

启动类: 一定要扫描到mapper类所在的包

@SpringBootApplication
@MapperScan("net.zyy.mapper")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

TestMapper类:直接继承BaseMapper类即可

public interface TestMapper extends BaseMapper<Test> {
}

数据插入:

当前数据库是空空的,所以就先插入点数据吧:

@RestController
public class TestController {

    @Autowired
    TestMapper testMapper;

    @RequestMapping("/save")
    public void save(){
        String source = "abcdefghijklmnopqrstuvwxyz";
        Random random = new Random();
        Test test = new Test();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 100; i++) {
            test.setAge(i);
            if (i % 2 == 0){
                test.setSex(0);
            }else {
                test.setSex(1);
            }
            for (int j = 0; j < 3; j++) {
                int index = random.nextInt(source.length());
                sb.append(source.charAt(index));
            }
            test.setName(sb.toString());
            sb = new StringBuilder();
            testMapper.insert(test);
        }
    }
}

直接@Autowire注入TestMapper,调用insert()方法即可插入数据,无需再编写SQL语句,xml文件,偷懒之必备神器,访问http://localhost:8000/save即可向数据库里插入一百条记录。

数据查找

查找所有数据:

@GetMapping("getList")
    public List<Test> getList(){
        QueryWrapper<Test> queryWrapper = new QueryWrapper<>();
        List<Test> tests = testMapper.selectList(queryWrapper);
        return tests;
    }

只需要调用selectList()方法,并传入QueryWrapper对象,因为是查找所有数据,所以无需添加查询条件,这样访问http://localhost:8000/getList即可查询所有数据。

根据Id查找数据:

@GetMapping("getOne")
    public Test getOne(){
        Test test = testMapper.selectById(1);
        return test;
    }

直接调用selectById,传入Id即可,简单又好用。

批量Id查询:

@GetMapping("getBatchIds")
    public List<Test> getBatchIds(){
        List<Test> list = testMapper.selectBatchIds(Arrays.asList(1,2,3));
        return list;
    }

根据条件批量查询:

查询sex为1的所有数据:

@GetMapping("selectList")
    public List<Test> selectList(){
        QueryWrapper<Test> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sex",1);
        List<Test> list = testMapper.selectList(queryWrapper);
        return list;
    }

数据修改

@GetMapping("update")
    public Integer update(){
        //根据Id修改
        testMapper.updateById();
        //根据条件修改,都返回int类型,表示影响的行数
        testMapper.update();
    }

数据删除

 @RequestMapping("delete")
    public Integer delete(){
        //根据wrapper条件删除
        testMapper.delete();
        //根据Id删除,都返回int类型,表示影响的行数
        testMapper.deleteById();
        //批量根据Id删除
        testMapper.deleteBatchIds();
    }

我们发现大多数的操作都与Wrapper建立着联系,下一章将透过源码仔细讲解Wrapper类的作用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有个金丝熊叫老许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值