菜鸟的mybatisplus学习总结菜鸟的mybatis plus学习总结
说明
更新时间:2020/11/3 17:51,更新了注解集合
更新时间:2020/8/31 15:47,更新了代码⽣成器
更新时间:2020/8/22 16:34,更新了mybatis plus基本内容
本⽂主要对mybatis plus学习总结,本⽂会持续更新,不断地扩充
本⽂仅为记录学习轨迹,如有侵权,联系删除
⼀、项⽬配置
(1)sql⽂件
/*
Navicat Premium Data Transfer
Source Server : test1
Source Server Type : MySQL
Source Server Version : 80015
Source Host : localhost:3306
Source Schema : test1
Target Server Type : MySQL
Target Server Version : 80015
File Encoding : 65001
Date: 22/08/2020 16:35:37
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS =0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS`user`;
CREATE TABLE`user`(
`id`int(11)NOT NULL AUTO_INCREMENT COMMENT'主键',
`name`varchar(30)CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT'姓名',
`age`int(11)NULL DEFAULT NULL COMMENT'年龄',
`email`varchar(50)CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT'邮箱',
`version`int(10)NULL DEFAULT NULL COMMENT'乐观锁',
`update_time`datetime(0)NULL DEFAULT NULL COMMENT'更新时间',
`create_time`datetime(0)NULL DEFAULT NULL COMMENT'创建时间',
`deleted`int(1)NULL DEFAULT NULL COMMENT'逻辑删除',
`password`varchar(30)CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT'密码',
`phone`varchar(11)CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT'电话号码',
PRIMARY KEY(`id`)USING BTREE
)ENGINE=InnoDB AUTO_INCREMENT=1297022245490749443CHARACTER SET= utf8 COLLATE= utf8_general_ci COMMENT='user' ROW_FOR MAT = Dynamic;
-- ----------------------------
-
- Records of user
-- ----------------------------
INSERT INTO`user`VALUES(1,'灰太狼',12,'test1@baomidou',3,'2020-07-03 12:51:25','2020-07-03 12:51:25',0,'123','156********'); INSERT INTO`user`VALUES(2,'喜⽺⽺',20,'test2@baomidou',1,'2020-07-03 11:47:47','2020-07-03 11:47:47',0,'123','156********'); INSERT INTO`user`VALUES(3,'Tonny',28,'test3@baomidou',1,'2020-07-03 11:47:49','2020-07-03 11:47:49',0,'123','156********'); INSERT INTO`user`VALUES(4,'Sandy',21,'test4@baomidou',1,'2020-07-03 11:47:51','2020-07-03 11:47:51',0,'123','156********'); INSERT INTO`user`VALUES(5,'Tonny',24,'test5@baomidou',1,'2020-07-03 11:47:53','2020-07-03 11:47:53',0,'123','156********'); INSERT INTO`user`VALUES(6,'Tonny',2,'123456@qq',1,'2020-07-03 11:59:01','2020-07-03 11:59:01',0,'123','156********');
INSERT INTO`user`VALUES(7,'李四2',2,'123456@qq',10,'2020-08-16 11:46:14','2020-08-15 17:49:33',0,'123','156********');
INSERT INTO`user`VALUES(8,'张三',19,'123@qq',NULL,'2020-08-22 15:38:04','2020-08-22 15:
38:04',0,NULL,NULL);
INSERT INTO`user`VALUES(9,'张三',19,'123@qq',NULL,'2020-08-22 15:41:44','2020-08-22 15:41:44',0,NULL,NULL);
INSERT INTO`user`VALUES(10,'张三',19,'123@qq',NULL,'2020-08-22 15:42:48','2020-08-22 15:42:48',0,NULL,NULL);
INSERT INTO`user`VALUES(11,'张三',19,'123@qq',1,'2020-08-22 15:43:23','2020-08-22 15:43:23',1,NULL,NULL);
SET FOREIGN_KEY_CHECKS =1;
(2)pom
下⾯⽤到的pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<version>2.2.6.RELEASE</version>
<relativePath/><!-- lookup parent from repository --> </parent>
<groupId>com.zsc</groupId>
<artifactId>mybatis_plus</artifactId>
mysql安装教程菜鸟课程<version>0.0.1-SNAPSHOT</version>
<name>mybatis_plus</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
transformer4
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
linux启用日志配置模块可使用的命令<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--hutool-->
<dependency>源代码下载软件
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
(3)配置⽂件
#激活开发环境
spring.profiles.active=dev
#mysql数据库连接信息
spring.datasource.username=root
spring.datasource.password=123
spring.datasource.url=jdbc:mysql://localhost:3306/test1?userSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 spring.datasource.sql.cj.jdbc.Driver
#配置⽇志
#figuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#配置逻辑删除
#没有逻辑删除为0,已经逻辑删为1
mybatis-plus.global-config.db-config.logic-delete-field=deleted
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
⼆、简单⼊门
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
创建实体
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("user")
public class User {
private Long id;
private String name;
private Integer age;
private String email;
private Integer deleted;
private String password;
private String phone;
private LocalDateTime createTime;
private LocalDateTime updateTime;
private Integer version;
}
在主启动类中添加要扫描的包(mapper)路径
batis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//扫描mapper⽂件夹
@MapperScan("com.zsc.mapper")
@SpringBootApplication
public class MybatisPlusApplication {
public static void main(String[] args){
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
创建持久层接⼝UserMapper
package com.zsc.mapper;
import ditions.Wrapper;
import apper.BaseMapper;
import com.zsc.pojo.User;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 在对应的mapper接⼝上继承BaseMapper类,即可完成所有的crud操作
*/
@Repository
public interface UserMapper extends BaseMapper<User>{
//⾄此所有的crud操作都已经完成resources英文
}
BaseMapper之后,到此为⽌,所有的crud操作都已经完成,下⾯开始测试测试
vscode输入了代码怎么运行
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论