MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
一、创建数据库
mybatis_plus
二、创建数据表
DROP TABLE IF EXISTS user;
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‘);
四、新建项目
五、导入相关依赖
导入mybatis-plus就不能导入mybatis的依赖,它们不能共存!
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
六、连接数据库application.yaml
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/mybatis_plus?useSSL=true&useUnicode=false&characterEncoding=utf-8&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
七、编写实体类
package com.kuang.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
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;
}
八、编写mapper
package com.kuang.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.study.pojo.User;
import org.springframework.stereotype.Repository;
//在对应的mapper上面继承 BaseMapper
@Repository //代表持久层
//@Mapper
public interface UserMapper extends BaseMapper<User> {
//所有的CRUD都编写完成,不需要像以前的配置mapper.xml
}
九、在MybatisPulsApplication主启动类中添加@MapperScan("com.study.mapper")扫描mapper文件夹,或者在Mapper中添加@Mapper注解
package com.kuang;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.study.mapper") //扫描mapper文件夹,或者在Mapper中添加@Mapper注解
@SpringBootApplication
public class MybatisPulsApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPulsApplication.class, args);
}
}
十、编写测试,查询全部用户
@SpringBootTest
class MybatisPulsApplicationTests {
//继承了BaseMapper,所有的方法都来自父类,我们也可以编写自己的扩展方法
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
//参数queryWrapper是一个条件构造器
//查询全部用户
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}
}
十一、运行查询成功
思考问题?
1、SQL谁帮我们写的?
Mybatis-Plus
2、方法哪来的?
Mybatis-Plus都帮我们写好了
编写application.yaml
#日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
运行测试
Springboot(十一)——整合mybatis-plus(一)
原文:https://www.cnblogs.com/mengbingpeng1010/p/15072755.html