- MySQL和MyBatis-Plus包
- SpringBoot配置信息
- @Mapper 或 @MapperScan 注解
- BaseMapper< T >接口
- IService< T > 接口
- ServiceImpl类
- service自定义方法与mapper一样,接口定义方法,service需要实现接口方法
1、准备数据库与表
CREATE DATABASE `mybatis_plus`
USE `mybatis_plus`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_name` VARCHAR(30) CHARACTER SET utf8mb3 COLLATE utf8_general_ci DEFAULT NULL COMMENT '姓名',
`age` INT DEFAULT NULL COMMENT '年龄',
`email` VARCHAR(50) DEFAULT NULL COMMENT '邮箱',
`sex` VARCHAR(10) DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO `user`(`id`,`user_name`,`age`,`email`,`sex`) VALUES
(1,'李四',21,'list@qq.com','男'),
(2,'Billie',24,'test5@qq.com','男'),
(3,'HeXi',20,'test6@qq.com','男'),
(4,'馨馨',21,'xinxin@qq.com','女'),
(5,'小红',22,'xiaohong@qq.com','女'),
(6,'兮兮',20,'xixi@qq.com','女');
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24

3、pom.xml导入jar包配置刷新Maven
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
4、配置application.yml
# 连接数据库信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC
username: root
password: 111111
# MyBatis-Plus全局配置
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 配置日志信息
5、启动类
package com.sgz.mybatisplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.sgz.mybatisplus.mapper")
public class Day66MybatisplusMapperApplication {
public static void main(String[] args) {
SpringApplication.run(Day66MybatisplusMapperApplication.class, args);
}
}
package com.sgz.mybatisplus.pojo;
public class User {
private Integer id;
private String userName;
private Integer age;
private String email;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", age=" + age +
", email='" + email + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
7、mapper接口继承BaseMapper< T >
package com.sgz.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sgz.mybatisplus.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
8、service接口继承IService< T >
package com.sgz.mybatisplus.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.sgz.mybatisplus.pojo.User;
public interface IUserService extends IService<User> {
}
9、serviceImpl实体类继承ServiceImpl和实现 service接口
package com.sgz.mybatisplus.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgz.mybatisplus.mapper.UserMapper;
import com.sgz.mybatisplus.pojo.User;
import com.sgz.mybatisplus.service.IUserService;
import org.springframework.stereotype.Service;
@Service
public class IUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
}
10、测试类
package com.sgz.mybatisplus;
import com.sgz.mybatisplus.mapper.UserMapper;
import com.sgz.mybatisplus.service.IUserService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Day66MybatisplusMapperApplicationTests {
@Autowired
private IUserService userService;
@Test
void contextLoads() {
System.out.println(userService.getById(1));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21