英文:
Could you offer some tips on configuring CASE_IGNORING to an embedded H2 database?
问题
I am using spring, mybatis plus, and unit test with @MybatisPlusTest annotation.
我正在使用Spring、Mybatis Plus以及带有@MybatisPlusTest注解的单元测试。
I believe that @MybatisPlusTest will assist me in building an embedded database server.
我相信@MybatisPlusTest将帮助我构建嵌入式数据库服务器。
Here are the logs relating to the database when the spring container starts.
以下是Spring容器启动时与数据库相关的日志。
2023-06-08 10:36:02.187 INFO 1911 --- [ main] beddedDataSourceBeanFactoryPostProcessor : Replacing 'dataSource' DataSource bean with embedded version
2023-06-08 10:36:02.749 INFO 1911 --- [ main] o.s.j.d.e.EmbeddedDatabaseFactory : Starting embedded database: url='jdbc:h2:mem:e9cde56b-3395-44fb-8251-c79b0b36e653;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
here is schema-h2.sql
以下是schema-h2.sql:
create table if not exists person
(
id int auto_increment primary key,
name varchar(50) not null,
age int not null
);
here is the PersonMapper
以下是PersonMapper:
@Repository
public interface PersonMapper extends BaseMapper<Person> {
@Select("select name, age from person")
public List<Map<String, Object>> selectCertainColumns();
}
After I call personMapper.selectCertainColumns()
, I get a List<Map<String, Object>> rows
, and I want to get the value of the column name
:
调用personMapper.selectCertainColumns()
后,我获得一个List<Map<String, Object>> rows
,我想获取列name
的值:
Expected:
期望结果:
rows.get(0).get("name")
Actual:
实际结果:
rows.get(0).get("NAME")
from JDBC URL with ignorecase is not working for H2 database connection, I understand that I should change the h2 datasource bean's url as follows:
根据JDBC URL with ignorecase is not working for H2 database connection,我了解到我应该将H2数据源的URL更改如下:
jdbc:h2:mem:xxxx;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;CASE_INSENSITIVE_IDENTIFIERS=TRUE
Problem is:
问题是:
I am unable to modify the configuration or url used to build the embedded H2 database.
我无法修改用于构建嵌入式H2数据库的配置或URL。
Could you offer some tips on setting up an embedded H2 database?
你能提供一些关于设置嵌入式H2数据库的提示吗?
Any advice is appreciated.
非常感谢任何建议。
英文:
I am using spring, mybatis plus, and unit test with @MybatisPlusTest annotation.
I believe that @MybatisPlusTest will assist me in building an embedded database server.
Here are the logs relating to the database when the spring container starts.
2023-06-08 10:36:02.187 INFO 1911 --- [ main] beddedDataSourceBeanFactoryPostProcessor : Replacing 'dataSource' DataSource bean with embedded version
2023-06-08 10:36:02.749 INFO 1911 --- [ main] o.s.j.d.e.EmbeddedDatabaseFactory : Starting embedded database: url='jdbc:h2:mem:e9cde56b-3395-44fb-8251-c79b0b36e653;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
here is schema-h2.sql
create table if not exists person
(
id int auto_increment primary key,
name varchar(50) not null,
age int not null
);
here is the PersonMapper
@Repository
public interface PersonMapper extends BaseMapper<Person> {
@Select("select name, age from person")
public List<Map<String, Object>> selectCertainColumns();
}
After I call personMapper.selectCertainColumns()
, I get a List<Map<String, Object>> rows
, and I want to get value of column name
:
Expected:
rows.get(0).get("name")
Actual:
rows.get(0).get("NAME")
from JDBC URL with ignorecase is not working for H2 database connection, I understand that I should change the h2 datasource bean's url as follows:
jdbc:h2:mem:xxxx;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;CASE_INSENSITIVE_IDENTIFIERS=TRUE
Problem is:
I am unable to modify the configuration or url used to build the embedded H2 database.
Could you offer some tips on setting up an embedded H2 database?
Any advice is appreciated.
答案1
得分: 1
以下是您要翻译的内容:
One way to achieve this is by creating a custom configuration for test to overrides the default configuration provided by @MybatisPlusTest. You can create a new configuration and annotate it with @TestConfiguration. Then you can define a bean for the embedded H2 database and set its JDBC URL with the required parameters.
for example I defined a bean for the embedded H2 database and set its JDBC URL with the required parameters: CASE_INSENSITIVE_IDENTIFIERS=TRUE
@TestConfiguration
public class TestConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema-h2.sql")
.build();
}
@Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(DataSource dataSource) {
MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
sessionFactory.setConfiguration(configuration);
Resource[] mapperLocations = new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/*.xml");
sessionFactory.setMapperLocations(mapperLocations);
return sessionFactory;
}
@Primary
@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new ClassPathResource("schema-h2.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(dataSource);
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
sessionFactory.setConfiguration(configuration);
Resource[] mapperLocations = new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/*.xml");
sessionFactory.setMapperLocations(mapperLocations);
return sessionFactory.getObject();
}
}
Afterward to use custom configuration in your tests, annotate your test class with @ContextConfiguration(classes = TestConfig.class)
.
I hope it solve the problem.
英文:
One way to achieve this is by creating a custom configuration for test to overrides the default configuration provided by @MybatisPlusTest. You can create a new configuration and annotate it with @TestConfiguration. Then you can define a bean for the embedded H2 database and set its JDBC URL with the required parameters.
for example I defined a bean for the embedded H2 database and set its JDBC URL with the required parameters: CASE_INSENSITIVE_IDENTIFIERS=TRUE
@TestConfiguration
public class TestConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("schema-h2.sql")
.build();
}
@Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(DataSource dataSource) {
MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
sessionFactory.setConfiguration(configuration);
Resource[] mapperLocations = new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/*.xml");
sessionFactory.setMapperLocations(mapperLocations);
return sessionFactory;
}
@Primary
@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) {
ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
resourceDatabasePopulator.addScript(new ClassPathResource("schema-h2.sql"));
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
dataSourceInitializer.setDataSource(dataSource);
dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
return dataSourceInitializer;
}
@Bean
public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
sessionFactory.setConfiguration(configuration);
Resource[] mapperLocations = new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/*.xml");
sessionFactory.setMapperLocations(mapperLocations);
return sessionFactory.getObject();
}
}
Afterward to use custom configuration in your tests, annotate your test class with @ContextConfiguration(classes = TestConfig.class)
.
I hope it solve the problem
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论