mybatisplus开始学习时,常出现的错误

按照mybatisplus官网的快速开始,步骤内容没有错误。但是运行测试报错,遇到两个错误,这是第一个错误:
java.lang.NullPointerException: Cannot invoke "com.nhc.mybatis.mapper.UserMapper.selectList(com.baomidou.mybatisplus.core.conditions.Wrapper)" because "this.userMapper" is nullat com.nhc.mybatis.test.TestUser.insert(TestUser.java:31)at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)at java.base/java.lang.reflect.Method.invoke(Method.java:577)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

几经查找,在测试类上加上这两个注解就好了

@SpringBootTest @RunWith(SpringRunner.class)

还有一个常见错误,错误如下:

============================
CONDITIONS EVALUATION REPORT
============================Positive matches:
-----------------AopAutoConfiguration matched:- @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)AopAutoConfiguration.ClassProxyingConfiguration matched:- @ConditionalOnMissingClass did not find unwanted class 'org.aspectj.weaver.Advice' (OnClassCondition)- @ConditionalOnProperty (spring.aop.proxy-target-class=true) matched (OnPropertyCondition)DataSourceAutoConfiguration matched:- @ConditionalOnClass found required classes 'javax.sql.DataSource', 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType' (OnClassCondition)- @ConditionalOnMissingBean (types: io.r2dbc.spi.ConnectionFactory; SearchStrategy: all) did not find any beans (OnBeanCondition)DataSourceAutoConfiguration.PooledDataSourceConfiguration matched:- AnyNestedCondition 1 matched 1 did not; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.PooledDataSourceAvailable PooledDataSource found supported DataSource; NestedCondition on DataSourceAutoConfiguration.PooledDataSourceCondition.ExplicitType @ConditionalOnProperty (spring.datasource.type) did not find property 'type' (DataSourceAutoConfiguration.PooledDataSourceCondition)- @ConditionalOnMissingBean (types: javax.sql.DataSource,javax.sql.XADataSource; SearchStrategy: all) did not find any beans (OnBeanCondition)DataSourceConfiguration.Hikari matched:

这个在springboot的启动类添加

@MapperScan("com.nhc.mybatis.mapper")

或者在mapper类加上

@Mapper

都可以解决报错问题。

再聊一下,整个测试过程及源码,我使用的工具是idea,jdk版本18(当前最新),框架springboot。

项目架构:

开始第一步,新建一个maven工程,这个步骤我省略了。

我的pom文件:


4.0.0org.examplemybatisplustest1.0-SNAPSHOTorg.springframework.bootspring-boot-starter-parent2.5.141818UTF-8mysqlmysql-connector-java5.1.46org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-testjunitjunit4.12com.baomidoumybatis-plus-boot-starter3.0.5




org.springframework.bootspring-boot-maven-plugin

我的application.properties文件(改成你自己的):

spring.datasource.url=jdbc:mysql://localhost:3306/artplatform?characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=hz980614

 我的springboot的启动类:

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("com.nhc.mybatis.mapper")
public class MybatisApplication {public static void main(String[] args) {SpringApplication.run(MybatisApplication.class);}}

我的mapper类:

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nhc.mybatis.pojo.User;
import org.apache.ibatis.annotations.Mapper;@Mapper
public interface UserMapper extends BaseMapper {}

我的实体类(pojo):

//import lombok.Data;//@Data
public class User {private Long id;private String name;private Integer age;private String email;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public User(Long id, String name, Integer age, String email) {this.id = id;this.name = name;this.age = age;this.email = email;}@Overridepublic String toString() {return "User{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", email='" + email + '\'' +'}';}
}

我的测试类(test):

import com.nhc.mybatis.mapper.UserMapper;
import com.nhc.mybatis.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.SpringRunner;import javax.sql.DataSource;
import java.util.List;@SpringBootTest
@RunWith(SpringRunner.class)
public class TestUser {@Autowiredprivate UserMapper userMapper;@AutowiredDataSource dataSource;@org.junit.jupiter.api.Testvoid test() {System.out.println("------------------");System.out.println(dataSource.getClass());System.out.println("------------------");}@Testpublic void insert() {List users = userMapper.selectList(null);users.forEach(System.out::println);}
}

test()是测试数据库连接,能成功运行,表示与mysql的连接成功

insert()是测试数据库的插入,打印数据库的数据,表示成功

成功运行界面:

.   ____          _            __ _ _/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/  ___)| |_)| | | | | || (_| |  ) ) ) )'  |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot ::               (v2.5.14)2022-09-28 11:13:00.377  INFO 6728 --- [           main] com.nhc.mybatis.test.TestUser            : Starting TestUser using Java 18.0.2 on PC-20190830RKBI with PID 6728 (started by Administrator in D:\实训\mybatisplustest)
2022-09-28 11:13:00.381  INFO 6728 --- [           main] com.nhc.mybatis.test.TestUser            : No active profile set, falling back to 1 default profile: "default"_ _   |_  _ _|_. ___ _ |    _ 
| | |\/|_)(_| | |_\  |_)||_|_\ /               |         3.0.5 
2022-09-28 11:13:01.853  INFO 6728 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2022-09-28 11:13:02.277  INFO 6728 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2022-09-28 11:13:02.975  INFO 6728 --- [           main] com.nhc.mybatis.test.TestUser            : Started TestUser in 3.297 seconds (JVM running for 5.813)
User{id=1, name='Jone', age=18, email='test1@baomidou.com'}
User{id=2, name='Jack', age=20, email='test2@baomidou.com'}
User{id=3, name='Tom', age=28, email='test3@baomidou.com'}
User{id=4, name='Sandy', age=21, email='test4@baomidou.com'}
User{id=5, name='Billie', age=24, email='test5@baomidou.com'}
2022-09-28 11:13:03.642  INFO 6728 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2022-09-28 11:13:03.650  INFO 6728 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.进程已结束,退出代码0


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部