Question

I'm a beginner and haven't used MyBatis before and decided to start by working through the MyBatis getting started guide on the official Github site: http://mybatis.github.io/mybatis-3/getting-started.html

However, I ran into some trouble early on and I was hoping it is something simple that someone can help me with. Specifically, I'm stuck trying to do the example under the header "Building SqlSessionFactory without XML". The example is as follows:

If you prefer to directly build the configuration from Java, rather than XML, or create your own configuration builder, MyBatis provides a complete Configuration class that provides all of the same configuration options as the XML file.

DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

I then looked through the JavaDoc for the latest mybatis-3.2.5 release and couldn't find some of the classes I was looking for, like BlogDataSourceFactory and BlogMapper. Also, everything is under org.apache.ibatis when I would have expected it to be under org.mybatis (apparently ibatis is the old project name when it was maintained by Apache). I also checked the pre-release mybatis-3.2.6-SNAPSHOT but it had the same package structure and I still couldn't find those missing classes.

Sorry if this is a really dumb question/I'm missing something obvious, I'm not that experienced of a Java developer either and I appreciate any help!

Was it helpful?

Solution

I thought that I was going to find some example code with BlogDataSourceFactory.getBlogDataSource() but there was none. But I have managed to create my own implementation:

package com.zetcode.jconfig;

import org.apache.ibatis.annotations.Select;

public interface MyMapper {

    @Select("SELECT COUNT(*) FROM MyBooks")
    public int getNumberOfBooks();
}

This is a MyMapper class that has one SELECT statement.

package com.zetcode.jconfig;

import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.datasource.DataSourceFactory;
import org.apache.ibatis.datasource.pooled.PooledDataSource;

public class MyDataSourceFactory implements DataSourceFactory {

    private Properties prop;

    @Override
    public DataSource getDataSource() {

        PooledDataSource ds = new PooledDataSource();

        ds.setDriver(prop.getProperty("driver"));
        ds.setUrl(prop.getProperty("url"));
        ds.setUsername(prop.getProperty("user"));
        ds.setPassword(prop.getProperty("password"));

        return ds;
    }

    @Override
    public void setProperties(Properties prprts) {

        prop = prprts;
    }
}

MyDataSourceFactory creates a PooledDataSource. It implements MyBatis' DataSourceFactory interface.

package com.zetcode.client;

import com.zetcode.jconfig.MyDataSourceFactory;
import com.zetcode.jconfig.MyMapper;
import java.io.IOException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

public class MyBatisJavaConfClient {

    private static SqlSessionFactory sesFact = null;

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

        Properties prop = new Properties();
        prop.setProperty("driver", "com.mysql.jdbc.Driver");
        prop.setProperty("url", "jdbc:mysql://localhost:3306/testdb");
        prop.setProperty("user", "testuser");
        prop.setProperty("password", "test623");

        MyDataSourceFactory mdsf = new MyDataSourceFactory();
        mdsf.setProperties(prop);
        DataSource ds = mdsf.getDataSource();

        TransactionFactory trFact = new JdbcTransactionFactory();
        Environment environment = new Environment("development", trFact, ds);
        Configuration config = new Configuration(environment);
        config.addMapper(MyMapper.class);

        sesFact = new SqlSessionFactoryBuilder().build(config);

        try (SqlSession session = sesFact.openSession()) {

            int numOfBooks = session.selectOne("getNumberOfBooks");
            System.out.format("There are %d books", numOfBooks);
        }
    }
}

MyBatisJavaConfClient contains code that replaces XML configuration and builds the session that executes our SQL code. You can find out more about MyBatis in my MyBatis tutorial.

OTHER TIPS

Well, I'm not sure if I should delete this question, but basically the answer from maba in the comments seems to be right. I misinterpreted the documentation to mean that BlogMapper was an implemented example provided in the library, but that doesn't seem to be the case, and the implementation of it is left up to the reader.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top