步骤
-
创建工程:
-
导入MybatisPlus的起步依赖
-
创建配置文件
application.yaml
-
创建引导类
-
-
创建实体类:类上用
@TableName("表名")
,主键字段上加@TableId(type=IdType.AUTO)
-
创建映射器:空的接口继承
BaseMapper<泛型>
即可 -
在启动类上:使用Mybatis提供的
@MapperScan
扫描映射器接口
导入依赖
<?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.itheima</groupId>
<artifactId>springboot-05</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!--打包时跳过单元测试-->
<skipTests>true</skipTests>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- mybatis plus:导入MybatisPlus,就不要再导入Mybatis的包了 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<!-- 设置打包后的包名称 -->
<finalName>mp</finalName>
<plugins>
<!--配置SpringBoot的打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在src\main\resources
里创建application.yaml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///springboot_db?useSSL=false
username: root
password: root
logging:
level:
com.itheima: debug
创建引导类
package com.itheima;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.itheima.mapper")
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
2. 创建JavaBean
package com.itheima.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
@Data
@TableName("tb_user")
public class User {
//主键生成策略。
//如果不设置,会由MybatisPlus根据时间戳和随机数(雪花算法)生成Long主键值,适用于分布式数据库
//加上此注解,设置为AUTO,表示使用数据库的主键自增
//@TableId(type = IdType.AUTO)
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
private Integer sex;
private Date birthday;
private String note;
private Date created;
private Date updated;
}
创建映射器接口
package com.itheima.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.domain.User;
public interface UserMapper extends BaseMapper<User> {
}
映射配置文件
如果需要的话,MybatisPlus默认从classpath:/mapper/**/*.xml
读取
但实际上,几乎不需要我们再配置映射文件
4. 开启映射器扫描
package com.itheima;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.itheima.mapper")
@SpringBootApplication
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
5. 功能测试
package com.itheima.test;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
public class MybatisPlusTest {
@Autowired
private UserMapper userMapper;
/**
* 根据主键查询一个
*/
@Test
public void testSelectOne(){
User user = userMapper.selectById(1);
System.out.println(user);
}
}
小结
-
准备SpringBoot的基本环境:依赖、配置、引导类
-
准备实体类JavaBean:
-
类上要加
@TableName("表名称")
-
类里主键字段上加
@TableId(type=IdType.AUTO)
-
-
创建映射器接口:要继承
BaseMapper<实体类>
-
在引导类上加注解:扫描映射器
@MapperScan("com.itheima.mapper")