MyBatis批注配置自动连接的字段为空

问题描述 投票:0回答:1

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

DataSource类

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);
            }

        }

Main 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("[email protected]");
        student.setPercentage(90);
        student.setPhone(123412341);

        sm.insert(student);

        sm.getMaxId();

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



    }

}
null annotations mybatis autowired inject
1个回答
0
投票

您需要正确初始化Spring。如果需要快速解决方案,请尝试Spring BootMyBatis-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

© www.soinside.com 2019 - 2024. All rights reserved.