mybitsplus的idworker应java什么类型_MyBatisPlus学习整理(一)

本文是MyBatisPlus的学习笔记,涵盖了入门、数据库建表、SpringBoot集成、注解使用、CRUD操作、条件构造器查询、分页查询、更新和删除等基础知识。通过示例代码展示了如何使用MyBatisPlus进行数据库操作。
摘要由CSDN通过智能技术生成

本文是通过慕课网相关课程学习MyBatisPlus整理的笔记。

MyBatisPlus入门 : - ) 老师讲的挺好的,还不会MyBatisPlus的小伙伴门可以听一下。

MyBatisPlus官网

MyBatisPlus源码地址

MyBatisPlus架构图(盗用官网的,侵,删。)

mybatis-plus.png

SpringBoot第一个简单应用

数据库建表

#创建用户表

CREATE TABLE user (

id BIGINT(20) PRIMARY KEY NOT NULL COMMENT '主键',

name VARCHAR(30) DEFAULT NULL COMMENT '姓名',

age INT(11) DEFAULT NULL COMMENT '年龄',

email VARCHAR(50) DEFAULT NULL COMMENT '邮箱',

manager_id BIGINT(20) DEFAULT NULL COMMENT '直属上级id',

create_time DATETIME DEFAULT NULL COMMENT '创建时间',

CONSTRAINT manager_fk FOREIGN KEY (manager_id)

REFERENCES user (id)

) ENGINE=INNODB CHARSET=UTF8;

#初始化数据:

INSERT INTO user (id, name, age, email, manager_id

, create_time)

VALUES (1087982257332887553, '大boss', 40, 'boss@baomidou.com', NULL

, '2019-01-11 14:20:20'),

(1088248166370832385, '王天风', 25, 'wtf@baomidou.com', 1087982257332887553

, '2019-02-05 11:12:22'),

(1088250446457389058, '李艺伟', 28, 'lyw@baomidou.com', 1088248166370832385

, '2019-02-14 08:31:16'),

(1094590409767661570, '张雨琪', 31, 'zjq@baomidou.com', 1088248166370832385

, '2019-01-14 09:15:15'),

(1094592041087729666, '刘红雨', 32, 'lhm@baomidou.com', 1088248166370832385

, '2019-01-14 09:48:16');

依赖

org.springframework.boot

spring-boot-starter-jdbc

org.springframework.boot

spring-boot-starter-web

mysql

mysql-connector-java

runtime

org.springframework.boot

spring-boot-configuration-processor

true

org.projectlombok

lombok

true

org.springframework.boot

spring-boot-starter-test

test

com.baomidou

mybatis-plus-boot-starter

3.1.2

springboot配置文件

spring:

datasource:

driver-class-name: com.mysql.cj.jdbc.Driver

username: root

password: root

url: jdbc:mysql://localhost:3306/test?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true

logging:

level:

root: warn

org.ywb.demo.dao: trace

pattern:

console: '%p%m%n'

创建相关包,如图:

image.png

在pojo包中新建和数据库user表映射的类

@Data

public class User {

private Long id;

private String name;

private Integer age;

private String email;

private String managerId;

private LocalDateTime createTime;

}

在dao包中创建mapper接口,并集成mybatisPlus的BaseMapper

public interface UserMapper extends BaseMapper {

}

在springboot启动类添加@MapperScan扫描dao层接口

@MapperScan("org.ywb.demo.dao")

@SpringBootApplication

public class MybatisPlusDemoApplication {

public static void main(String[] args) {

SpringApplication.run(MybatisPlusDemoApplication.class, args);

}

}

8.编写测试类

@RunWith(SpringRunner.class)

@SpringBootTest

public class MybatisPlusDemoApplicationTests {

@Resource

private UserMapper userMapper;

@Test

public void select(){

List users = userMapper.selectList(null);

users.forEach(System.out::println);

}

}

运行结果:

image.png

常用注解

MyBatisPlus提供了一些注解供我们在实体类和表信息出现不对应的时候使用。通过使用注解完成逻辑上匹配。

注解名称

说明

@TableName

实体类的类名和数据库表名不一致

@TableId

实体类的主键名称和表中主键名称不一致

@TableField

实体类中的成员名称和表中字段名称不一致

@Data

@TableName("t_user")

public class User {

@TableId("user_id")

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值