Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mybatis , Adding mapper xml file to Java created Configuration

Tags:

mybatis

I have a my batis (3.2.7) application, and I'm creating configuration using java code ( not xml ) as following.

public static Configuration getConfiguration(DataSet data) {
        if (configuration == null) {
            DataSource dataSource = getDataSource(DRIVER, URL, data.getUsername(), data.getPassword());
            TransactionFactory transactionFactory = new JdbcTransactionFactory();
            Environment environment =
                    new Environment("development", transactionFactory, dataSource);
            configuration = new Configuration(environment);
        }
        return configuration;
}

Using above configuration sql session factory is created.

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

My mappers are in xml format ( I need these in xml format), and currently in same package as mapper interface. I'm using following code to add mappers.

configuration.addMapper(CrudMapper.class);

This will automatically add xml mappers which are in same folder as mapper interface ( CrudMapper). But I need to move these xml file to resource folder. So mapper interface will be in one location, and xml mappers are in different location. I could not found any way to add xml mappers to configuration. Is there a way to do this ?

like image 297
Viraj Avatar asked Oct 14 '25 16:10

Viraj


1 Answers

You are probably adding xml mappers to root of resource folder. Configuration.addMapper should work fine if you preserve package structure in resources folder so that for mapper interface com.company.app.MyMapper corresponding xml mapping is stored in resources/com/company/app/MyMapper.xml.

In this case xml mapping will be put to exactly same package during compilation.

like image 133
Roman Konoval Avatar answered Oct 17 '25 21:10

Roman Konoval