本文介绍了Mybatis,将映射器xml文件添加到Java创建的配置中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个batis(3.2.7)应用程序,并且正在使用Java代码(不是xml)创建配置,如下所示.

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

使用以上配置创建了sql会话工厂.

Using above configuration sql session factory is created.

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

我的映射器是xml格式的(我需要xml格式的映射器),并且当前与mapper界面位于同一软件包中.我正在使用以下代码添加映射器.

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

这将自动添加与映射器界面(CrudMapper)所在的文件夹中的xml映射器.但是我需要将这些xml文件移动到资源文件夹.因此,映射器接口将位于一个位置,而xml映射器将位于不同的位置.我找不到任何将xml映射器添加到配置的方法.有办法吗?

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 ?

推荐答案

您可能正在将xml映射器添加到资源文件夹的根目录中.如果将包结构保留在资源文件夹中,则Configuration.addMapper应该工作正常,以便对于映射器接口com.company.app.MyMapper,相应的xml映射存储在resources/com/company/app/MyMapper.xml中.

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.

在这种情况下,xml映射将在编译过程中放入完全相同的包中.

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

这篇关于Mybatis,将映射器xml文件添加到Java创建的配置中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:26