0

Yeah, I mean pure java. I have my mybatis configed like this. Now most of my project is using java base config but 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>

How can I config these in java file? This below is my 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;
    }

}
Gabriel.ge
  • 168
  • 3
  • 15

2 Answers2

0

@Bean public SqlSessionFactoryBean sqlSessionFactory() {

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

Try this.

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;
elkad
  • 1