目录
一,环境工具准备
二,创建springboot项目
三,修改pom.xml文件,添加mybatis-plus
四,修改配置
?五,创建表并任意添加一条测试数据即可
?六,编码
七,启动
八,测试编码
九,完整代码资源地址
一,环境工具准备
工具:IDEA,JDK8
二,创建springboot项目
三,修改pom.xml文件,添加mybatis-plus
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
完整的pom文件如下
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mybatisplus20220303</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatisplus20220303</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
四,修改配置
application.properties文件重命名成application.yml?,配置文件如下,数据库是MySQL,需要创建库mybatisplus
server:
port: 8088
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatisplus
username: root
password: 123456
?五,创建表并任意添加一条测试数据即可
CREATE TABLE `sys_user` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(255) DEFAULT NULL COMMENT '姓名',
`id_card` varchar(255) DEFAULT NULL COMMENT '身份证号码',
`mobile` varchar(255) DEFAULT NULL COMMENT '手机号码',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;
?六,编码
package com.example.mybatisplus20220303.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* @author YunDuan-Clutch
* @date 2022/3/3 9:35
*/
@Data
@TableName("sys_user")
public class SysUser {
private Long id;
private String name;
private String idCard;
private String mobile;
}
package com.example.mybatisplus20220303.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplus20220303.entity.SysUser;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface SysUserDao extends BaseMapper<SysUser> {
}
package com.example.mybatisplus20220303;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启动类
*/
@MapperScan("com.example.mybatisplus20220303.dao")
@SpringBootApplication
public class Mybatisplus20220303Application {
public static void main(String[] args) {
SpringApplication.run(Mybatisplus20220303Application.class, args);
}
}
七,启动
八,测试编码
package com.example.mybatisplus20220303;
import com.example.mybatisplus20220303.dao.SysUserDao;
import com.example.mybatisplus20220303.entity.SysUser;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
public class Mybatisplus20220303ApplicationTests {
/**
* 这里使用@Resource的原因是,使用@Autowired时报错:Could not autowire. No beans of 'SysUserDao' type found.
*/
@Resource
private SysUserDao sysUserDao;
@Test
void contextLoads() {
System.out.println("========== 测试一下mybatis-plus功能 ==========");
List<SysUser> listSysUser = sysUserDao.selectList(null);
System.out.println("长度是:" + listSysUser.size());
listSysUser.forEach(user -> {
System.out.println(user.toString());
});
}
}
九,完整代码资源地址
springboot集成mybatis-plus的demo基础搭建-Java文档类资源-CSDN下载
|