Vic*_*c K 5 java mysql spring hibernate jpa
我有一个非常简单的 spring-data-jpa + Hibernate 应用程序,它将客户数据存储在 MySql 数据库中(完整的源代码在此处发布)。
问题是它运行插入和选择查询的速度非常慢,与我通过 mysql CLI 所拥有的相比:
有讨论过类似的问题在这里,但在我的情况的测量方式更糟(尽管我不插入批次,它在DB只是一个简单的行)。有没有办法提高 Spring JPA/Hibernate 的性能?
另一个问题,有没有办法减少 spring-data-jpa 和 hibernate-entitymanager 的大小?我能够在不损害程序的情况下排除字节伙伴和 jandex 依赖项,但这只是几个 Mbs(阴影 jar 大小从 19.6Mb 减少到 16.6Mb)?
更新 根据请求,这里是代码(所有来源都在这里):
@Entity
@AttributeAccessor("field")
@Table(name = "customer")
public class Customer implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="id",unique=true, nullable=false, insertable=true, updatable=true)
@Type(type="long")
private Long id;
@Column(name = "name")
private String name;
//+ constructors, getters/setters
}
Run Code Online (Sandbox Code Playgroud)
这是 Spring 应用程序并保存(插入)客户:
public class Application {
private static ApplicationContext applicationContext;
static CustomerRepository customerRepository;
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MySQLAutoconfiguration.class);
customerRepository = ctx.getBean(CustomerRepository.class);
runTest();
}
private static void runTest () throws {
...
//insert
Customer customerJohn = customerRepository.save(new Customer("John"));
//select
Customer foundEntity = customerRepository.findOne(customerJohn.getId());
...
}
Run Code Online (Sandbox Code Playgroud)
和配置:
@Configuration
@ComponentScan
@EnableJpaRepositories (basePackages = "com.vk.dal.repository")
@PropertySource("classpath:mysql.properties")
public class MySQLAutoconfiguration {
@Autowired
private Environment env;
@Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username") != null ? env.getProperty("spring.datasource.username") : "");
dataSource.setPassword(env.getProperty("spring.datasource.password") != null ? env.getProperty("spring.datasource.password") : "");
return dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.vk.dal.domain");
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
Properties properties = additionalProperties();
if (properties != null) {
em.setJpaProperties(properties);
}
return em;
}
@Bean
JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
final Properties additionalProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("mysql-hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("mysql-hibernate.dialect"));
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("mysql-hibernate.show_sql") != null ? env.getProperty("mysql-hibernate.show_sql") : "false");
return hibernateProperties;
}
}
Run Code Online (Sandbox Code Playgroud)
感谢任何帮助。
| 归档时间: |
|
| 查看次数: |
4023 次 |
| 最近记录: |