MyBatis-Plus——初识MyBatis

1 MyBatisPlus 介绍

MyBatis-Plus(简称 MP),是一个 MyBatis 的增强工具包,只做增强不做改变. 为简化开发工作、提高生产率而生。

启动加载 XML 配置时注入单表 SQL 操作 ,为简化开发工作提供生产率而生。mybatis-plus 只做增强不做改变,这里不提倡 SQL 写在代码中。

我们来看看mybatis和mybatisPlus的区别 首先,看看图标

很明显,图标中小鸟只是眼罩发生了变化。接下来,我们看看功能方面的变化

 在这里我们可以很明显的看到,mybatisPlus是在mybatis上进行了增强。

官网https://mp.baomidou.com/

功能:1、单表CURD (简单+批量)操作,自动完成。
           2、分页插件,Count 查询自动或自定义SQL 查询。
           3、Spring 根据不同环境加载不同配置支持。

使用:添加maven坐标,查看相关类,进行调用

<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>maven 官方最新版本号为准</version>
</dependency>

1、代码自动生成,查看类com.baomidou.mybatisplus.test.AutoGeneratorTest
2、使用方法,查看类com.baomidou.mybatisplus.test.UserMapperTest

2 案例 

我们先来创建表

 创建一个springboot工程 ,pom文件

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
            <scope>runtime</scope>
        </dependency>

<!--        mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

核心配置文件

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
    username: root
    password: 123456
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl  #日志信息

编写实体类

package com.liuhaiyang.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

@TableName(value = "t_student")  //指定表的名称
public class Address {
    /*
     *指定主键的方式:使用@TableId注解
     * value:指定主键字段的名称,如果主键字段的名称是id,value属性可以省略
     * type:指定主键字段的类型,IdType.AUTO表示自动增长
     */
    @TableId(value = "id",type = IdType.AUTO)
    private Integer id;
    //当属性名和字段名不一致时,指定属性和列名的对应关系(@TableField)value指列名
    @TableField(value = "name")
    private String name;
    @TableField(value = "age")
    private Integer age;

    public Address() {
    }

    public Address(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

编写mapper接口,让它继承MP框架中的BaseMapper接口。

package com.liuhaiyang.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liuhaiyang.entity.Address;

public interface AddressMapper extends BaseMapper<Address> {
}

mybatisPlus框架中的BaseMapper接口中定义了17个关于CRUD的操作方法。

 能够满足我们对表的操作,如果我们需要的操作都在这里,可以不写mapper.xml配置文件

在SpringBoot项目的启动入口类上添加 @MapperScan 注解,确保扫描mapper包下所有mybatis、mybatis-plus相关的注解。

@SpringBootApplication
@MapperScan(value = "com.liuhaiyang.mapper")  //扫描器
public class MybatisPlusApplication {

    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusApplication.class, args);
    }

}

我们在来测试一下,写一个测试类测试一下啊

insert操作

package com.liuhaiyang;

import com.liuhaiyang.entity.Address;
import com.liuhaiyang.mapper.AddressMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class AddressTest {

    @Autowired(required = false)
    private AddressMapper addressMapper;
    @Test
    public void insertaddress(){
        Address address=new Address("李四",19);
        System.out.println("insert address 的值"+addressMapper.insert(address));
    }
}

 update操作

 @Test
    public void updateaddress() {
        Address address=new Address();
        address.setId(10);
        address.setName("赵六");
        //判断字段是否会进行更新,依据字段是否为null,
        //如果非null,则加入set语句中;为null,则不加入set语句
        int rows=addressMapper.updateById(address);
        System.out.println("update的结果是:" + rows);
    }

 select操作

 @Test
    public void selectaddress() {
        Address rows=addressMapper.selectById(10);
        System.out.println("select的结果是:" + rows);
    }

 delete操作

 @Test
    public void deleteaddress() {
        int rows=addressMapper.deleteById(10);
        System.out.println("delete的结果是:" + rows);
    }

 剩下的就不操作了,感兴趣的可以自己去尝试

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值