如何将mybatis设置转换为java base config

时间:2016-10-17 06:51:54

标签: java spring-mybatis

是的,我的意思是纯粹的java。我的mybatis像这样被包围了。现在我的项目大部分都是使用java base config而不是mybatis。

<configuration>
    <settings>
        <setting name="cacheEnabled" value="true" />
        <setting name="lazyLoadingEnabled" value="true" />
        <setting name="multipleResultSetsEnabled" value="true" />
        <setting name="useColumnLabel" value="true" />
        <setting name="useGeneratedKeys" value="false" />
        ...
    </settings>
</configuration>

如何在java文件中配置这些? 以下是我的db config。

@Configuration
@MapperScan("com.xxxx.basis.dao")
public class DBConfig {
    @Resource
    private Environment env;

    @Bean(destroyMethod = "close")
    public DruidDataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        // ...
        return dataSource;
    }

    @Bean
    public DataSourceTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setTypeAliasesPackage("com.xxx.basis.domain");
        return sessionFactory;
    }

}

2 个答案:

答案 0 :(得分:0)

@Bean     public SqlSessionFactoryBean sqlSessionFactory(){

    SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setConfigLocation(applicationContext.getResource("classpath:mybatis.xml"));
    sqlSessionFactoryBean.setDataSource(dataSource());
    return sqlSessionFactoryBean;
}

答案 1 :(得分:0)

试试这个。

SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setTypeAliasesPackage("com.xxx.basis.domain");

SqlSessionFactory factory = sessionFactory.getObject();
factory.getConfiguration().setCacheEnabled(true);
factory.getConfiguration().setUseColumnLabel(true);
return sessionFactory;