【问题标题】:MyBatis annotation configuration autowired field nullMyBatis 注解配置 autowired 字段 null
【发布时间】:2020-02-28 19:18:21
【问题描述】:

我尝试制作控制台 myBatis 应用程序并摆脱多次使用相同的代码来初始化会话、执行提交和事务。这段代码在 Annotations_Example 类的“sm”字段中为我提供了“空指针值”。我做错了什么?在类初始化中,一切正常。但现在,它有了 NPE。

数据源类

public class DataSourceStudent_Mapper {

        @Bean
     public Student_mapper getDataSource() {
            String user = "postgres";
             String password = "postgres";
             String databasenameURL = "jdbc:postgresql://localhost:5432/postgres";
             String dbDriver = "org.postgresql.Driver";
             DataSource dataSource = new org.apache.ibatis.datasource.pooled.PooledDataSource(
                     dbDriver, databasenameURL, user, password);
             TransactionFactory transactionFactory = new JdbcTransactionFactory();
             Environment environment = new Environment("development",
                     transactionFactory, dataSource);
             Configuration configuration = new Configuration(environment);
             SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
                     .build(configuration);

             SqlSession session = sqlSessionFactory.openSession();
             session.getConfiguration().addMapper(Student_mapper.class);

            return session.getMapper(Student_mapper.class);
            }

        }

主类

public class Annotations_Example {

    @Autowired
    public Student_mapper sm;

    public static void main(String args[]) throws IOException {

        Student student = new Student();

        student.setName("zara");
        student.setBranch("EEE");
        student.setEmail("zara@gmail.com");
        student.setPercentage(90);
        student.setPhone(123412341);

        sm.insert(student);

        sm.getMaxId();

        session.commit();
        session.close();



    }

}

【问题讨论】:

    标签: null annotations mybatis autowired inject


    【解决方案1】:

    您需要正确初始化 Spring。
    如果您想要快速解决方案,请尝试Spring Boot 和MyBatis-Spring-Boot-Starter。

    假设您熟悉Maven,则需要对您的 pom.xml 进行一些修改。

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-dependencies</artifactId>
          <version>2.2.5.RELEASE</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    
    <dependencies>
      <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.1</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
      </dependency>
      <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
      </dependency>
    </dependencies>
    
    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>2.2.5.RELEASE</version>
          <executions>
            <execution>
              <goals>
                <goal>repackage</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
    

    主类如下所示:

    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import mapper.Student_mapper;
    
    @SpringBootApplication
    @MapperScan("mapper")
    public class Application implements CommandLineRunner {
    
      @Autowired
      private Student_mapper sm;
    
      public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
      }
    
      @Override
      public void run(String... args) throws Exception {
        ...
        sm.insert(student);
        ...
      }
    }
    

    在@MapperScan 注解中,您需要指定包含映射器的包。
    然后你应该能够使用@Autowired 注入映射器。

    虽然这可能不是必需的,但似乎建议将以下行添加到 application.properties。

    spring.main.web-application-type=NONE
    

    使用上面的 pom.xml,mvn package 将生成一个可执行的 JAR 文件(带有java -jar)。

    这是一个可执行文件demo project。

    【讨论】:

      猜你喜欢
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      相关资源
      最近更新 更多