mybatis-plus入门笔记
java零基础自学点击了解:https://how2j.cn?p=81777
目录
一:数据库准备
1.创建数据库
create database mybatis_plus;
2.创建table
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));
3.导入数据
INSERT INTO user(id,name,age,email)VALUES(1,'Jone',18,'[email protected]'),(2,'Jack',20,'[email protected]'),(3,'Tom',28,'[email protected]'),(4,'Sandy',24,'[email protected]'),(5,'Billie',30,'[email protected]');
二:创建springboot项目
1:创建spring lnitializr形式的springboot项目mpdemo
2:复制下面代码到pom文件
<dependencies><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><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><!--mybatis-plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.0.5</version></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!--lombok用来简化实体类--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies>
3:点击右下方import Change下载依赖
三:安装lombok插件
1:操作步骤:file>seting...>plugins
2:搜索框搜索:lombok
3:重启idea即可
四:配置application.properties文件
1:复制以下内容到.properties文件
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8spring.datasource.username=rootspring.datasource.password=123456
2:修改自己数据量username和password
五:写代码
1:创建实体类entity.Uaer
import lombok.Data;/*** @Author: ZhangDeChen* @Date: 2020/6/4 20:28*/@Datapublic class User {private Long id;private String name;private Integer age;private String email;}
2:创建mapper.UserMapper
3:在启动类中添加:@MapperScan("xyz.dechen.mpdemo.mapper")括号中为项目mapper包的路径
4:在测试类中复制以下代码,测试.
import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import xyz.dechen.mpdemo.entity.User;import xyz.dechen.mpdemo.mapper.UserMapper;import java.util.List;@SpringBootTestclass MpdemoApplicationTests {@Autowiredprivate UserMapper userMapper;//查询user表中的所有数据@Testpublic void findAll() {List<User> users=userMapper.selectList(null);System.out.println(users);}}
5:测试成功: