简体   繁体   English

使用MyBatis连接到数据库时出现Nullpointer异常

[英]Nullpointer exception while connecting to DB using MyBatis

I am having the following issue: when I am connecting to db using MyBatis I get an NPE. 我遇到以下问题:当我使用MyBatis连接到db时,我得到了NPE。

Here is the class for setting connection: 这是设置连接的类:

public class SetDBConnection {

    private Mapper mapper;
    private SqlSession session;
    private Configuration configuration;

    public SetDBConnection() {


        configuration = new Configuration();
        configuration.setJdbcTypeForNull(JdbcType.VARCHAR);
        configuration.setLogImpl(Slf4jImpl.class);
        configuration.setDefaultExecutorType(ExecutorType.REUSE);
        configuration.addMapper(Mapper.class);

        Properties properties = null;
        try {
            properties = readProperties();
        } catch (IOException e) {
            e.printStackTrace();
        }
        configuration.setVariables(properties);

    }


    public Mapper openSession() {

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        session = sqlSessionFactory.openSession();

        mapper = session.getMapper(Mapper.class);
        return mapper;
    }

    public void closeSession() {
        session.commit();
        session.close();
    }

    private Properties readProperties() throws IOException {
        Properties properties = new Properties();
        InputStream inputStream = getClass().getClassLoader()
                .getResourceAsStream("connection.properties");


        if (inputStream != null) {
            properties.load(inputStream);
        } else {
            throw new FileNotFoundException("property file  not found in the classpath");
        }
        inputStream.close();

        return properties;
    }
}

And here I call it and try to Insert data SetDBConnection conn = new SetDBConnection(); 在这里我称它为“尝试插入数据”。SetDBConnection conn = new SetDBConnection();

    Person p = new Person();
    p.setName("Alex");
    p.setLastName("Bondar");
    p.setTelephone("+79267157256");
    p.setPersonalId("777-216");
    Mapper mapper=conn.openSession();
    try {
        System.out.println("All set to go");
        mapper.saveOrUpdatePerson(p);
    } finally {
        conn.closeSession();
    }

Stacktrace is the following: Stacktrace如下:

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 
### Error opening session.  Cause: java.lang.NullPointerException
### The error may exist in org/abondar/experimental/mybatisdemo/mappers/Mapper.java (best guess)
### Cause: java.lang.NullPointerException
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:100)
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:47)
    at org.abondar.experimental.mybatisdemo.SetDBConnection.openSession(SetDBConnection.java:51)
    at org.abondar.experimental.mybatisdemo.Main.main(Main.java:19)
    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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.NullPointerException
    at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:95)
    ... 8 more

What can be wrong and is there any way not to re-establish DB-connection for every action(select,insert or delete)? 可能是什么问题,有没有办法不为每个操作(选择,插入或删除)重新建立数据库连接?

It seems you have no Environment and TransactionFactory defined. 似乎您没有定义Environment和TransactionFactory。 According to the docs you should initialize MyBatis somehow like this: 根据文档,您应该以某种方式初始化MyBatis:

DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource); 
Configuration configuration = new Configuration(environment);

Thanks Frank for DataSource idea. 感谢Frank提供DataSource的想法。 I used default pooled dataSource and this problem looks to be solved 我使用了默认的合并数据源,此问题似乎可以解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 JDBC 在 Java 中连接到 DB2 时出现异常 - Exception while connecting to DB2 in java using JDBC 在Spring中使用多线程时出现NullPointer异常 - NullPointer exception while using Multithread in Spring 使用Spring Data ElasticSearch时出现Nullpointer异常 - Nullpointer Exception while using Spring Data ElasticSearch 在 Windows 机器中连接到 postgress DB 时出现异常 - Exception while connecting to postgress DB in Windows Machine 使用Play框架连接到mysql db时发生异常 - Exception While Connecting to mysql db with play framework 在黄瓜中使用Pojo类时获取Nullpointer异常 - Getting Nullpointer Exception while using pojo class in cucumber 使用 swagger codegen for openapi 3.0 生成代码时出现 NullPointer 异常 - NullPointer exception while generating code using swagger codegen for openapi 3.0 在 Selenium 中尝试读取 Excel 时出现空指针异常 - Nullpointer Exception While Using Trying to Read Excel in Selenium 使用xml运行测试时TestNG中的NullPointer异常 - NullPointer exception in TestNG while run test using xml 在RichFaces中,在使用电子邮件验证时,我收到NullPointer异常吗? - In RichFaces, While using Email validation I am getting NullPointer Exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM