为什么 Mybatis 对所有请求都要添加一个校验请求?
why Mybatis add a check request for all requests?
我将 Mybatis 与 spring boot 'mybatis-spring-boot-starter version 2.1.0' 和 Athena 一起使用。当我检查我的数据库日志时,Mybatis 调用的每个请求都有两个请求:
第一个是:SELECT * FROM (myRequest) T LIMIT 0
第二个是:myRequest
比如如果他创建一个请求来检查每个请求的数据库可用性,有什么想法吗?
这是我的映射器:
@Mapper
public interface MyMapper {
@Select(value = "select name, adress from db.table limit 10")
List<Object> findAll();
}
和我的数据库配置
@Configuration
@Slf4j
public class AthenaConfig {
@Bean(name = "athena-db")
@ConfigurationProperties(prefix = "athena.spring.datasource.hikari")
public DataSource athenaDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactory(@Qualifier("athena-db") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage("athena.mapper");
sqlSessionFactoryBean.getObject().getConfiguration().setDefaultFetchSize(500);
sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return sqlSessionFactoryBean.getObject();
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() throws Exception {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("athena.mapper");
return mapperScannerConfigurer;
}
}
这是我的 application.properties 文件:
athena.spring.datasource.driver-class-name=com.simba.athena.jdbc.Driver
athena.spring.datasource.url=jdbc:awsathena://AwsRegion=eu-west-1;AwsCredentialsProviderClass=com.amazonaws.auth.DefaultAWSCredentialsProviderChain;S3OutputLocation=s3://xxxx/;
当我使用 JDBC 模板时,我没有额外的查询,这是我使用 JDBC 模板进行的测试:
@Repository
public class MyRepository {
@Autowired
JdbcTemplate jdbcTemplate;
List<Object> findAll(){
return jdbcTemplate.query("select name, adress from db.table limit 10",new BeanPropertyRowMapper(Object.class));
}
}
如何避免这种无用的调用?提前致谢
我通过将 @Options(statementType = StatementType.STATEMENT) 添加到我的映射器来实现它:
@Mapper
public interface MyMapper {
@Select(value = "select name, adress from db.table limit 10")
@Options(statementType = StatementType.STATEMENT)
List<Object> findAll();
}
感谢@ave
,不再有额外的请求
我将 Mybatis 与 spring boot 'mybatis-spring-boot-starter version 2.1.0' 和 Athena 一起使用。当我检查我的数据库日志时,Mybatis 调用的每个请求都有两个请求:
第一个是:SELECT * FROM (myRequest) T LIMIT 0
第二个是:myRequest
比如如果他创建一个请求来检查每个请求的数据库可用性,有什么想法吗?
这是我的映射器:
@Mapper
public interface MyMapper {
@Select(value = "select name, adress from db.table limit 10")
List<Object> findAll();
}
和我的数据库配置
@Configuration
@Slf4j
public class AthenaConfig {
@Bean(name = "athena-db")
@ConfigurationProperties(prefix = "athena.spring.datasource.hikari")
public DataSource athenaDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactory(@Qualifier("athena-db") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
sqlSessionFactoryBean.setTypeAliasesPackage("athena.mapper");
sqlSessionFactoryBean.getObject().getConfiguration().setDefaultFetchSize(500);
sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return sqlSessionFactoryBean.getObject();
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() throws Exception {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("athena.mapper");
return mapperScannerConfigurer;
}
}
这是我的 application.properties 文件:
athena.spring.datasource.driver-class-name=com.simba.athena.jdbc.Driver
athena.spring.datasource.url=jdbc:awsathena://AwsRegion=eu-west-1;AwsCredentialsProviderClass=com.amazonaws.auth.DefaultAWSCredentialsProviderChain;S3OutputLocation=s3://xxxx/;
当我使用 JDBC 模板时,我没有额外的查询,这是我使用 JDBC 模板进行的测试:
@Repository
public class MyRepository {
@Autowired
JdbcTemplate jdbcTemplate;
List<Object> findAll(){
return jdbcTemplate.query("select name, adress from db.table limit 10",new BeanPropertyRowMapper(Object.class));
}
}
如何避免这种无用的调用?提前致谢
我通过将 @Options(statementType = StatementType.STATEMENT) 添加到我的映射器来实现它:
@Mapper
public interface MyMapper {
@Select(value = "select name, adress from db.table limit 10")
@Options(statementType = StatementType.STATEMENT)
List<Object> findAll();
}
感谢@ave
,不再有额外的请求