設定mybatis 是否列印日誌

八英里發表於2024-07-30
    @Bean(name = "dbzyqsV102SqlSessionFactory")
    // @Qualifier表示查詢Spring容器中名字為blogDataSource的物件
    public SqlSessionFactory dbzyqsV102SqlSessionFactory(@Qualifier("dbzyqsV102DataSource") DataSource datasource)
            throws Exception {
        MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        mybatisSqlSessionFactoryBean.setDataSource(datasource);
        MybatisConfiguration configuration = new MybatisConfiguration();
        // 關閉駝峰
        configuration.setMapUnderscoreToCamelCase(false);
        configuration.addInterceptor(new PaginationInterceptor());
        if ("uat".equals(active)) {
            configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        }
        mybatisSqlSessionFactoryBean.setConfiguration(configuration);
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        properties.setProperty("helperDialect", "mysql");
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("supportMethodsArguments", "true");
        properties.setProperty("params", "pageNum=pageNumKey;pageSize=pageSizeKey;");
        interceptor.setProperties(properties);
        mybatisSqlSessionFactoryBean.setPlugins(new Interceptor[]{interceptor});
        mybatisSqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/dbzyqsV102/*.xml"));
        return mybatisSqlSessionFactoryBean.getObject();
    }

相關文章