冬马和纱 发表于 2019-8-26 20:49

在学习Springboot中 用到通用Mapper 调用其中的方法报NullPointerException

本帖最后由 冬马和纱 于 2019-8-27 00:57 编辑

各位大哥帮我看看吧package pers.my.mapper;

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 pers.my.pojo.User;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {

    @Autowired
    private UserMapper userMapper;

    @Test
    public void test(){
      System.out.println("1111111"+userMapper);
      User user=userMapper.selectByPrimaryKey(1L);
      System.out.println(user);
    }
}package pers.my.mapper;

import pers.my.pojo.User;
import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper<User> {

}
package pers.my.pojo;

import lombok.Data;

import javax.persistence.Id;
import javax.persistence.Table;

@Data
@Table(name="t_user")
public class User {
    @Id
    private Integer id;
    private String username;
    private String password;
    private String birthday;
    private String gender;

}

package pers.my;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication
@MapperScan("pers.my.mapper")
public class BootDemoApplication {
    public static void main(String[] args) {
      SpringApplication.run(BootDemoApplication.class);
    }
}
<?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>pers.my.boot</groupId>
    <artifactId>spring-boot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.4.RELEASE</version>
    </parent>

    <dependencies>
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
      </dependency>

      <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
      </dependency>

      <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
      </dependency>
<!--连接池用了通用mapper 不用自己配置了-->
      <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
      </dependency>-->
<!--    驱动    -->
      <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
      </dependency>
<!--mybatis用了通用mapper 不用自己配置了-->
      <!--<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
      </dependency>-->
<!--通用mapper-->
      <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.3</version>
      </dependency>

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
      </dependency>
    </dependencies>

</project>server:
servlet:
    path: /
spring:
datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/edu
    username: root
    password: 123456
mybatis:
#configuration:   用了通用mapper 不用自己配置了
#    map-underscore-to-camel-case: true
type-aliases-package: pers.my.pojo

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.Cause: java.lang.NullPointerException
### The error may exist in pers/my/mapper/UserMapper.java (best guess)
### The error may involve pers.my.mapper.UserMapper.selectByPrimaryKey
### The error occurred while executing a query
### Cause: java.lang.NullPointerException

      at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
      at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
      at com.sun.proxy.$Proxy71.selectOne(Unknown Source)
      at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
      at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:83)
      at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
      at com.sun.proxy.$Proxy83.selectByPrimaryKey(Unknown Source)
      at pers.my.mapper.UserMapperTest.test(UserMapperTest.java:21)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      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.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
      at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)




调用Mapper里面的方法 给我报NullPointerException 其他方法也是报这个

左手凯 发表于 2019-8-27 00:05

我把你的代码 复制了下来 跑了跑 没有问题...你对一下看看 我就数据库改了一下 因为字段不一样

namedlxd 发表于 2019-8-26 21:45

你有mapper吗?

冬马和纱 发表于 2019-8-26 21:57

namedlxd 发表于 2019-8-26 21:45
你有mapper吗?

啥意思我那个UserMapper继承了Mapper在之后打印userMapper也有对象 不是null

namedlxd 发表于 2019-8-26 22:03

冬马和纱 发表于 2019-8-26 21:57
啥意思我那个UserMapper继承了Mapper在之后打印userMapper也有对象 不是null

放SQL的xml呢?

冬马和纱 发表于 2019-8-26 22:15

namedlxd 发表于 2019-8-26 22:03
放SQL的xml呢?

用了这个通用mapper就不用xml了吧

namedlxd 发表于 2019-8-26 22:32

冬马和纱 发表于 2019-8-26 22:15
用了这个通用mapper就不用xml了吧

你看下通用mapper的实现

huang0817 发表于 2019-8-26 22:43

冬马和纱 发表于 2019-8-26 22:15
用了这个通用mapper就不用xml了吧

我记得应该是要xml的,只是里面不写SQL,我也是萌新:lol

冬马和纱 发表于 2019-8-26 23:07

namedlxd 发表于 2019-8-26 22:32
你看下通用mapper的实现

我看的这个视频 他说这些基本方法的sql它在底层帮你动态生成了我看他视频里面也确实没有配mapper.xml

冬马和纱 发表于 2019-8-26 23:08

huang0817 发表于 2019-8-26 22:43
我记得应该是要xml的,只是里面不写SQL,我也是萌新

哈哈一起讨论 共同提升:lol
页: [1] 2
查看完整版本: 在学习Springboot中 用到通用Mapper 调用其中的方法报NullPointerException