SSM学习10:整合MyBatis、MyBatisPlus

SpringBoot整合MyBatis

与创建spring web项目类型,添加上相应依赖
在这里插入图片描述
实体类

public class Account {
    private int id;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    private String name;
    private double money;
}

接口

@Mapper
public interface AccountDao {
    @Select("select * from account where id = #{id}")
    public Account getAccount(int id);
}

@Mapper 注解主要用于标记数据访问层(DAO 层或称为 Mapper 层)的接口。这个注解是 MyBatis 框架的一部分,用于与 Spring 框架集成时自动创建这些接口的实现类,并将其作为 Bean 注入到其他需要的地方。

数据库配置
resources/application.yml

# 数据源配置
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123abc!@#

测试类中运行

@SpringBootTest
class BootMpApplicationTests {

    @Autowired
    private AccountDao accountDao;

    @Test
    void contextLoads() {
        Account account = accountDao.getAccount(1);
        System.out.println(account.getName());
    }
}

在这里插入图片描述

MyBatisPlus

简介

MyBatisPlus是基于Mybatis框架基础上开发的增强型工具,用于简化开发、提高效率。

项目搭建

在上面项目的基础上添加对应的依赖,在dependencies中添加

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

AccountDao2.class

// 需要使用mybatis-plus提供的的BaseMapper
@Mapper
public interface AccountDao2 extends BaseMapper<Account> {
}

测试类中运行

@SpringBootTest
class BootMpApplicationTests {

    @Autowired
    private AccountDao accountDao;

    @Autowired
    private AccountDao2 accountDao2;

    @Test
    void contextLoads() {
        Account account = accountDao.getAccount(1);
        System.out.println(account.getName());

        // 使用mybatis-plus提供的方法
        Account account1 = accountDao2.selectById(1);
        System.out.println("使用mybatis-plus获取:" + account1.getName());
    }

}

在这里插入图片描述

补充
使用lombok简化实体类,在pom.xml中添加

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

简化前:

public class Account {
    private int id;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    private String name;
    private double money;
}

简化后

@Getter
@Setter
@ToString
public class Account {
    private int id;
    private String name;
    private double money;
}

或者

@Data
public class Account {
    private int id;
    private String name;
    private double money;
}

@Data 为当前实体类在编译期设置对应的get/set方法,无惨/有惨 构造方法,toString方法等

分页功能

配置
config/MpConfig .class

@Configuration
public class MpConfig {
    // 设置mybatis-plus的分页功能
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        // 1、创建拦截器
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 2、添加分页拦截器
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

使用

// 设置当前页码和每页显示的条数
 IPage page = new Page(1, 2);
 accountDao2.selectPage(page, null);
 System.out.println("总页:" + page.getPages());
 System.out.println("总条数:" + page.getTotal());

在这里插入图片描述
在控制台开始执行日志

application.yml

# 开启日志输出(输出到控制台)
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

在这里插入图片描述

条件查询

mybatis-plus将书写复杂的sql查询条件进行了封装,使用编程的形式完成查询条件的组合

QueryWrapper

QueryWrapper qw = new QueryWrapper();
// 查询金钱大于1000的
qw.gt("money", 1000);
List<Account> list = accountDao2.selectList(qw);

System.out.println("总条数是:" + list.size());

在这里插入图片描述

lambda表达式方式

QueryWrapper<Account> qw = new QueryWrapper();
// 查询金钱大于1000的
qw.lambda().gt(Account::getMoney, 1000);
List<Account> list = accountDao2.selectList(qw);

System.out.println("总条数是:" + list.size());

或者

LambdaQueryWrapper<Account> qw = new LambdaQueryWrapper<>();
qw.gt(Account::getMoney, 1000);
List<Account> list = accountDao2.selectList(qw);

System.out.println("总条数是:" + list.size());
LambdaQueryWrapper<Account> qw = new LambdaQueryWrapper<>();
// 多条件查询,查询金额大于1000并且姓名为张三
qw.gt(Account::getMoney, 1000)
        .eq(Account::getName, "张三");
List<Account> list = accountDao2.selectList(qw);

System.out.println("总条数是:" + list.size());

在这里插入图片描述
查询投影

用于指定查询时返回哪些字段

LambdaQueryWrapper<Account> qw = new LambdaQueryWrapper<>();
// 查询投影,只查询name和money
qw.select(Account::getName, Account::getMoney);
List<Account> list = accountDao2.selectList(qw);

System.out.println("总条数是:" + list.size());

或者

QueryWrapper<Account> qw = new QueryWrapper<>();
 // 查询投影,只查询name和money
 qw.select("id", "money");
 List<Account> list = accountDao2.selectList(qw);

 System.out.println("总条数是:" + list.size());

在这里插入图片描述

映射匹配兼容

表字段与编码属性设计不同步
正常情况下实体类中的属性名与数据库中的字段名称时一致的。但也会出现不一致的情况,这时候可以使用TableField去进行关联

public class User{
    @TableField(value = "username")
    private String name;
    
    private int age;
}

编码中添加了数据库中未定义的属性
数据库中不需要改字段,但是实体类中需要该属性

public class User{
    @TableField(value = "username")
    private String name;
    
    private int age;
    
    // 该字段,表中不存在
    @TableField(exist = false)
    private int isOnline;
}

设置属性是否参与查询

// 查询数据时不返回密码
@TableField(select = false)
private String password;

数据库表名与实体类类名不一致

@TableName("t_user")
public class User{}

id生成策略

public class Account {
    // 使用雪花算法生成id
    @TableId(type = IdType.ASSIGN_ID)
    private int id;
}
  • AUTO:使用数据库id自增控制id的生成
  • NONE:不设置id生成策略
  • INPUT:用户手工输入
  • ASSIGN_ID:雪花算法生成id
  • ASSIGN_UUID:以UUID生成算法作为id生成策略
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无知的小菜鸡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值