EBean是无状态的ORM。 http://ebean-orm.github.io/
EBean提供Spring Boot工件。 http://ebean-orm.github.io/docs/setup/spring#spring-boot https://github.com/ebean-orm/avaje-ebeanorm-spring
但在这种情况下,EBean直接读取jdbc连接细节,如下所示。
ebean.db.ddl.generate=true
ebean.db.ddl.run=true
datasource.db.username=sa
datasource.db.password=
datasource.db.databaseUrl=jdbc:h2:mem:tests
datasource.db.databaseDriver=org.h2.Driver
我想利用我现有的HikariCP DataSource和Spring Boot以及EBean。
我们该怎么做?
答案 0 :(得分:0)
你可以在这里检查工作的EBean + Spring Boot示例:
https://github.com/bwajtr/java-persistence-frameworks-comparison
在该存储库中,有SpringBoot配置:https://github.com/bwajtr/java-persistence-frameworks-comparison/blob/master/src/main/java/com/clevergang/dbtests/DbTestsApplication.java
在该类中,您可以看到如何配置EBean以及如何将DataSource连接到其中 - >实际上你可以使用任何实现DataSource接口的东西,包括HikariCP ......所以使用DbTestsApplication中的EBean配置并为SpringBoot设置HikariCP(请查看如何操作:How do I configure HikariCP in my Spring Boot app in my application.properties files?)
答案 1 :(得分:0)
我这样做,现在可以正常运作。
1.application.properties
# hikariCP
spring.datasource.type=com.zaxxer.HikariDataSource
spring.datasource.driver-class-name=@datasource.db.databaseDriver@
spring.datasource.url=@datasource.db.databaseUrl@
spring.datasource.username=@datasource.db.username@
spring.datasource.password=@datasource.db.password@
spring.datasource.poolName=SpringBootHikariCP
spring.datasource.maximumPoolSize=60
spring.datasource.minimumIdle=3
spring.datasource.maxLifetime=2000000
spring.datasource.connectionTimeout=30000
spring.datasource.idleTimeout=30000
spring.datasource.pool-prepared-statements=true
spring.datasource.max-open-prepared-statements=250
Hikari config
@Configuration
@Component
class DataSourceConfig {
@Value("${spring.datasource.username}")
private String user;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.url}")
private String dataSourceUrl;
@Value("${spring.datasource.poolName}")
private String poolName;
@Value("${spring.datasource.connectionTimeout}")
private int connectionTimeout;
@Value("${spring.datasource.maxLifetime}")
private int maxLifetime;
@Value("${spring.datasource.maximumPoolSize}")
private int maximumPoolSize;
@Value("${spring.datasource.minimumIdle}")
private int minimumIdle;
@Value("${spring.datasource.idleTimeout}")
private int idleTimeout;
public DataSource primaryDataSource() {
HikariConfig config = new HikariConfig();
config.setPoolName(poolName);
config.setJdbcUrl(dataSourceUrl);
config.setUsername(user);
config.setPassword(password);
config.setConnectionTimeout(connectionTimeout);
config.setMinimumIdle(minimumIdle);
config.setIdleTimeout(idleTimeout);
config.setMaximumPoolSize(maximumPoolSize);
config.setMaxLifetime(idleTimeout);
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
HikariDataSource ds = new HikariDataSource(config);
return ds;
}
}
ebean
@Component
public class EbeanFactoryBean implements FactoryBean<EbeanServer>, EnvironmentAware {
@Autowired
CurrentUserProv currentUser;
@Autowired
DataSourceConfig dataSourceConfig;
/**
* Properties used to configure EbeanServer instance
* (loaded from spring boot application properties).
*/
Properties properties = new Properties();
public EbeanFactoryBean() {
}
@Override
public EbeanServer getObject() throws Exception {
ServerConfig config = new ServerConfig();
config.setName("db");
config.setCurrentUserProvider(currentUser);
config.setDataSource(dataSourceConfig.primaryDataSource());
config.setDefaultServer(true);
config.setRegister(true);
return EbeanServerFactory.create(config);
}
@Override
public Class<?> getObjectType() {
return EbeanServer.class;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void setEnvironment(Environment environment) {
loadProperties((AbstractEnvironment) environment);
}
/**
* Load into Properties (from Spring PropertySource implementations).
*/
private void loadProperties(AbstractEnvironment environment) {
MutablePropertySources propertySources = environment.getPropertySources();
// reverse the order of the property sources
List<MapPropertySource> props = new ArrayList<>();
for (PropertySource propertySource : propertySources) {
if (propertySource instanceof MapPropertySource) {
props.add(0, (MapPropertySource) propertySource);
}
}
// merge them into the single Properties
for (MapPropertySource propertySource : props) {
properties.putAll(propertySource.getSource());
}
}
}