加油,新时代打工人!
MyBatis-Plus快速入门–环境搭建
MyBatis-Plus–三种方式整合之一 Mybatis+MP整合
MyBatis-Plus–三种方式整合之二 Spring+MP整合
数据库脚本和更多MP整合方式,参考以上文章
Spring Boot+MP整合
一、创建项目
Spring Boot不和我们前面创建的父子工程依赖,需要单独创建工程
导入pom.xml
<?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.github/itboywh</groupId>
<artifactId>itboywh-mp-springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--简化代码的工具包-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--mybatis-plus的springboot支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
二、pojo和mapper和spring boot启动类
2.1 pojo
package itboywh.mp.pojo;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author wh
* @date 2021年11月26日23:00
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_user")
public class User {
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
private String email;
}
2.2 mapper
我们只需要继承BaseMApper<>即可,不需要写方法了
package itboywh.mp.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import itboywh.mp.pojo.User;
public interface UserMapper extends BaseMapper<User> {
}
2.3 spring boot启动类
@SpringBootApplication 启动类注解
@MapperScan 扫描mapper接口
package itboywh.mp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author wh
* @date 2021年11月26日23:06
*/
@SpringBootApplication
@MapperScan("itboywh.mp.mapper")
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class,args);
}
}
三、测试运行和结果截图
3.1测试类
package itboywh.mp;
import itboywh.mp.mapper.UserMapper;
import itboywh.mp.pojo.User;
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.SpringJUnit4ClassRunner;
import java.util.List;
/**
* @author wh
* @date 2021年11月26日23:05
*/
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSpringbootMP(){
List<User> users = this.userMapper.selectList(null);
for (User user : users) {
System.out.println(user);
}
}
}
3.1 结果截图
四、错误总结
测试类所在的包和java实体类和mapper包不同
解决:测试包相同