使用MyBatis

时间:2016-02-03 12:51:29

标签: java mybatis

我遇到以下问题:当我使用MyBatis连接到数据库时,我得到了一个N​​PE。

以下是设置连接的类:

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

在这里我调用它并尝试插入数据      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如下:

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

有什么不对,并且有没有办法不为每个动作重新建立数据库连接(选择,插入或删除)?

2 个答案:

答案 0 :(得分:4)

您似乎没有定义Environment和TransactionFactory。根据{{​​3}},您应该以某种方式初始化MyBatis:

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

答案 1 :(得分:0)

感谢Frank for DataSource的想法。我使用默认的池化dataSource,这个问题看起来有待解决