1、springboot 的pom.xml引入依赖
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
2、application.yml配置
spring:
application:
name: mp-service
#数据库配置
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/teststock?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
username: root
password: 123456
#mybatis-plus配置
mybatis-plus:
#开启日志
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
call-setters-on-nulls: true
#mapper文件位置
mapper-locations: classpath:mapper/*.xml
3、配置mapper类
将mapper类继承BaseMapper<T>,T为泛型,指后续要操作的实体类
public interface StockMapper extends BaseMapper<Stock> {
}
BaseMapper提供了以下基础方法
用法和之前Mybatis差不多,自动注入即可使用
@Resource
StockMapper stockMapper;
@Test
public void select() {
Stock stock = stockMapper.selectById(12);
System.out.println(stock);
}