首先需要整合Mybatis-plus
1. 在pom中导入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<?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">
<parent>
<artifactId>gulimall</artifactId>
<groupId>com.example.gulimall</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gulimall-common</artifactId>
<description>每一个微服务公共的依赖、bean、工具类等</description>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- mybatisplus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.14</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- Mysql驱动-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.6.7</version>
</dependency>
</dependencies>
</project>
2. 创建application.yml并配置数据源,同时设置主键自增
然后使用@MapperScan并告诉Mybatisplus。sql映射文件地址
spring:
datasource:
username: root
password: root
url: jdbc:mysql://192.168.159.135:3306/gulimall_pms
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
package com.example.gulimall.product;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 1. 整合Mybatis-plus
* 1)、导入依赖
* <dependency>
* <groupId>com.baomidou</groupId>
* <artifactId>mybatis-plus-boot-starter</artifactId>
* <version>3.3.1</version>
* </dependency>
* 2)、配置
* 1、配置数据源:
* 1)、导入数据库的驱动
* 2) 、在application.yml中配置数据源相关信息
* 2、配置Mybatis-plus:
* 1)、使用@MapperScan
* 2)、告诉Mybatisplus。sql映射文件地址
*/
@MapperScan("com/example/gulimall/product/dao")
@SpringBootApplication
public class GulimailProductApplication {
public static void main(String[] args) {
SpringApplication.run(GulimailProductApplication.class, args);
}
}
3. 在测试类
GulimailProductApplicationTests
测试添加功能
package com.example.gulimall.product;
import com.example.gulimall.product.entity.BrandEntity;
import com.example.gulimall.product.service.BrandService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class GulimailProductApplicationTests {
@Autowired
private BrandService brandService;
@Test
void contextLoads() {
BrandEntity brand = new BrandEntity();
brand.setName("三星");
brandService.save(brand);
System.out.println("保存成功!");
}
}
查看数据库中的pms_brand是否添加了数据
成功