我有一个我的 batis (3.2.7) 应用程序,我正在使用 java 代码(不是 xml )创建配置,如下所示。
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;
}
Run Code Online (Sandbox Code Playgroud)
使用上述配置创建 sql 会话工厂。
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
Run Code Online (Sandbox Code Playgroud)
我的映射器是 xml 格式的(我需要 xml 格式的这些),目前与映射器接口在同一个包中。我正在使用以下代码添加映射器。
configuration.addMapper(CrudMapper.class);
Run Code Online (Sandbox Code Playgroud)
这将自动添加与映射器接口 (CrudMapper) 位于同一文件夹中的 xml 映射器。但我需要将这些 xml 文件移动到资源文件夹。所以映射器接口将在一个位置,而 xml 映射器在不同的位置。我找不到任何方法将 xml 映射器添加到配置中。有没有办法做到这一点 ?
您可能正在将 xml 映射器添加到资源文件夹的根目录。Configuration.addMapper如果您在资源文件夹中保留包结构,以便映射器接口com.company.app.MyMapper对应的 xml 映射存储在resources/com/company/app/MyMapper.xml.
在这种情况下,xml 映射将在编译期间放置到完全相同的包中。