SpringBoot MyBatis基础配置

来源:互联网 发布:规划算法 pdf 编辑:程序博客网 时间:2024/05/16 19:38

习惯了spring的xml配置,结构清晰明了,springboot改为了java代码配置,这种配置和JFinal有点类似。

/** * MyBatis基础配置 * * @author   * @since 2015-12-19 10:11 */@Configuration@EnableTransactionManagementpublic class MyBatisConfig implements TransactionManagementConfigurer {    @Autowired    DataSource dataSource;    @Bean(name = "sqlSessionFactory")    public SqlSessionFactory sqlSessionFactoryBean() {    //配置框架数据源    ApplicationContextHelper.setDataSource(dataSource);    PackagesSqlSessionFactoryBean bean = new PackagesSqlSessionFactoryBean();        bean.setDataSource(dataSource);                bean.setTypeAliasesPackage("com.sys.module.*.entity");        bean.setTypeAliases(new Class[]{CommonParamEntity.class});                //增加自编写分页插件        MyBatisPageInterceptor page = new MyBatisPageInterceptor();        page.databaseType = "DB2";        page.pageRex = ".*ByPage$";        //添加插件        bean.setPlugins(new Interceptor[]{page});        //此段代码用来解决实体没有被扫描的问题        bean.setVfs(SpringBootVFS.class);                //添加XML目录        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();        try {            bean.setMapperLocations(resolver.getResources("classpath:com/sys/module/*/dao/*.mapper.xml"));                        return bean.getObject();        } catch (Exception e) {            e.printStackTrace();            throw new RuntimeException(e);        }    }    @Bean    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {        return new SqlSessionTemplate(sqlSessionFactory);    }    @Bean    @Override    public PlatformTransactionManager annotationDrivenTransactionManager() {        return new DataSourceTransactionManager(dataSource);    }}


原创粉丝点击