MyBatis 注解配置 autowired 字段 null
MyBatis annotation configuration autowired field null
我尝试制作控制台 myBatis 应用程序并摆脱多次使用相同的代码来初始化会话、执行提交和事务处理。此代码在 Annotations_Example class 字段 "sm" 中为我提供了 "Null Pointer Value"。我做错了什么?在 class 初始化中一切正常。但是现在,它有NPE。
数据源class
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);
}
}
主要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();
}
}
您需要正确初始化 Spring。
如果您想要快速解决方案,请尝试 Spring Boot and 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>
主要 class 如下所示:
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文件(withjava -jar
).
这是一个可执行文件demo project。
我尝试制作控制台 myBatis 应用程序并摆脱多次使用相同的代码来初始化会话、执行提交和事务处理。此代码在 Annotations_Example class 字段 "sm" 中为我提供了 "Null Pointer Value"。我做错了什么?在 class 初始化中一切正常。但是现在,它有NPE。
数据源class
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);
}
}
主要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();
}
}
您需要正确初始化 Spring。
如果您想要快速解决方案,请尝试 Spring Boot and 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>
主要 class 如下所示:
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文件(withjava -jar
).
这是一个可执行文件demo project。