参考资料:MybatisPlus官网
CREATE DATABASE `mybatisplus`
USE `mybatisplus`
CREATE TABLE `user`(
id BIGINT(20) NOT NULL COMMENT '主键ID',
NAME VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
);
DELETE FROM USER;
INSERT INTO USER (id, NAME, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
数据库名为 mybatisplus,表名为 user
mysql
Lombok(不是必须品,只是可以偷点懒)
mybatis-plus
# mysql
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
# 东8时区,serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 配置日志,方便查看SQL语句
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
User
package com.zy.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
实现 BaseMapper 接口
UserMapper
package com.zy.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zy.pojo.User;
import org.springframework.stereotype.Repository;
@Repository //代表持久层
//在对应的mapper上面 继承 基本的接口BaseMapper
public interface UserMapper extends BaseMapper
//继承之后,所有的CRUD就完成了
}
package com.zy;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//扫描mapper文件夹
@MapperScan("com.zy.mapper")
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@Test
public void select(){
//查询全部用户
List
users.forEach(System.out::println);
}
结果
手机扫一扫
移动阅读更方便
你可能感兴趣的文章