文章目录
- 22.1 官方文档
- 22.2 基本介绍
- 22.3 整合 MyBatis-Plus 实例
- 22.3.1 需求说明/图解
- 22.3.2 代码实现
- 22.3.2.1 创建数据库和表
- 22.3.2.2 创建 springboot-mybatisplus 项目
- 22.3.2.3 修改 pom.xml 引入 MyBatis-Plus starter
- 22.3.2.4 创建 resources/application.yml 配置数据源参数
- 22.3.2.5 切换数据源为 druid
- 22.3.2.6 创建 Application.java 项目启动程序
- 22.3.2.7 创建 bean
- 22.3.2.8 创建 Mapper 接口
- 22.3.2.9 创建 Service 接口
- 22.3.2.10 创建 Service 实现类
- 22.3.2.11 创建 Controller
- 22.3.2.12 修改 Application.java ,加入对 Mapper 的扫描
- 22.3.3 启动项目,完成测试
- 22.4 整合 MyBatis-Plus 注意事项和细节
22.1 官方文档
- MyBatis-Plus 官网 https://baomidou.com
22.2 基本介绍
- MyBatis-Plus (简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生
- 强大的 CRUD 操作:内置通用 Mapper、通用 Service,通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求
22.3 整合 MyBatis-Plus 实例
22.3.1 需求说明/图解
- 将 Spring Boot 和 MyBatis-Plus 整合
- 查询数据,如图
22.3.2 代码实现
22.3.2.1 创建数据库和表
CREATE DATABASE `springboot_mybatisplus`
USE `springboot_mybatisplus`
CREATE TABLE `monster` (
`id` INT NOT NULL AUTO_INCREMENT,
`age` INT NOT NULL,
`birthday` DATE DEFAULT NULL,
`email` VARCHAR(255) DEFAULT NULL,
`gender` CHAR(1) DEFAULT NULL,
`name` VARCHAR(255) DEFAULT NULL,
`salary` DOUBLE NOT NULL,
PRIMARY KEY (`id`)
) CHARSET=utf8
SELECT * FROM `monster`
INSERT INTO monster VALUES(NULL, 20, '2000-11-11', 'xzj@sohu.com', '男', '蝎子精', 15000.88);
INSERT INTO monster VALUES(NULL, 10, '2011-11-11', 'ytj@sohu.com', '女', '玉兔精', 18000.88);
22.3.2.2 创建 springboot-mybatisplus 项目
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xjs</groupId>
<artifactId>springboot-mybatisplus</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.5.3</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>
</dependencies>
</project>
22.3.2.3 修改 pom.xml 引入 MyBatis-Plus starter
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
22.3.2.4 创建 resources/application.yml 配置数据源参数
server:
port: 9090
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot_mybatisplus?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: hsp
22.3.2.5 切换数据源为 druid
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>
- 并且 创建配置类 com/xjs/springboot/mybatisplus/config/DruidDataSourceConfig.java
package com.xjs.springboot.mybatisplus.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DruidDataSourceConfig {
@ConfigurationProperties("spring.datasource")
@Bean
public DataSource dataSource() {
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
}
22.3.2.6 创建 Application.java 项目启动程序
- 测试是否能正确启动项目,注意观察 mybatis-plus 是否引入成功
22.3.2.7 创建 bean
- 创建 com/xjs/springboot/mybatisplus/bean/Monster.java
package com.xjs.springboot.mybatisplus.bean;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
@Data
@TableName(value = "monster_")
public class Monster {
private Integer id;
private Integer age;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date birthday;
private String email;
private String name;
private String gender;
private Double salary;
}
22.3.2.8 创建 Mapper 接口
- 创建 com/xjs/springboot/mybatisplus/mapper/MonsterMapper.java
package com.xjs.springboot.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xjs.springboot.mybatisplus.bean.Monster;
import org.apache.ibatis.annotations.Mapper;
public interface MonsterMapper extends BaseMapper<Monster> {
}
22.3.2.9 创建 Service 接口
- 创建 com/xjs/springboot/mybatisplus/service/MonsterService.java
package com.xjs.springboot.mybatisplus.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xjs.springboot.mybatisplus.bean.Monster;
public interface MonsterService extends IService<Monster> {
}
22.3.2.10 创建 Service 实现类
- 创建 com/xjs/springboot/mybatisplus/service/impl/MonsterServiceImpl.java
package com.xjs.springboot.mybatisplus.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjs.springboot.mybatisplus.bean.Monster;
import com.xjs.springboot.mybatisplus.mapper.MonsterMapper;
import com.xjs.springboot.mybatisplus.service.MonsterService;
import org.springframework.stereotype.Service;
@Service
public class MonsterServiceImpl
extends ServiceImpl<MonsterMapper, Monster>
implements MonsterService {
}
22.3.2.11 创建 Controller
- 创建 com/xjs/springboot/mybatisplus/controller/MonsterController.java
package com.xjs.springboot.mybatisplus.controller;
import com.xjs.springboot.mybatisplus.bean.Monster;
import com.xjs.springboot.mybatisplus.service.MonsterService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
@Controller
public class MonsterController {
@Resource
private MonsterService monsterService;
@GetMapping("/monster")
@ResponseBody
public Monster getMonsterById(@RequestParam(value = "id") Integer id) {
return monsterService.getById(id);
}
@GetMapping("/list")
@ResponseBody
public List<Monster> monsterList() {
List<Monster> monsters = monsterService.list();
return monsters;
}
}
22.3.2.12 修改 Application.java ,加入对 Mapper 的扫描
- 修改 com/xjs/springboot/mybatisplus/Application.java ,加入对 Mapper 的扫描
package com.xjs.springboot.mybatisplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan(basePackages = {"com.xjs.springboot.mybatisplus.mapper"})
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
22.3.3 启动项目,完成测试
22.4 整合 MyBatis-Plus 注意事项和细节
- @TableName 作用
如果这个类名 Monster 和 表名 monster 一致,可以映射上,则 @TableName 可以省略
如果这个类名 Monster 和 表名 不一致,不能映射上,则可以通过 @TableName 指定
- MyBatis-Plus starter 到底引入了哪些依赖?
- 为了开发方便,可以安装 MyBatisX 插件
- 参考文档:https://baomidou.com/pages/ba5b24
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)