Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package id.ac.ipb.ilkom.training; import java.util.Properties; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.apache.commons.dbcp2.BasicDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; /** * * @author ifnu.b.fatkhan */ @Configuration @EnableWebMvc @ComponentScan(basePackages = "id.ac.ipb.ilkom.training") @EnableJpaRepositories(basePackages = { "id.ac.ipb.ilkom.training" }) @EnableTransactionManagement public class ApplicationConfiguration extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/views/"); viewResolver.setSuffix(".jsp"); return viewResolver; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/js/**").addResourceLocations("/js/"); registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/"); registry.addResourceHandler("/css/**").addResourceLocations("/css/"); registry.addResourceHandler("/img/**").addResourceLocations("/img/"); registry.addResourceHandler("/scss/**").addResourceLocations("/scss/"); registry.addResourceHandler("/index.html").addResourceLocations("/index.html"); registry.addResourceHandler("/product.html").addResourceLocations("/product.html"); } @Bean DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setUrl("jdbc:mysql://localhost:3306/trainingjava"); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("root"); dataSource.setInitialSize(20); dataSource.setMaxIdle(20); dataSource.setMaxTotal(100); return dataSource; } @Bean LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(dataSource); entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); entityManagerFactoryBean.setPackagesToScan("id.ac.ipb.ilkom.training"); Properties jpaProperties = new Properties(); //Configures the used database dialect. This allows Hibernate to create SQL //that is optimized for the used database. jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); //Specifies the action that is invoked to the database when the Hibernate //SessionFactory is created or closed. jpaProperties.put("hibernate.hbm2ddl.auto", "update"); //If the value of this property is true, Hibernate writes all SQL //statements to the console. jpaProperties.put("hibernate.show_sql", "true"); //If the value of this property is true, Hibernate will format the SQL //that is written to the console. jpaProperties.put("hibernate.format_sql", "true"); entityManagerFactoryBean.setJpaProperties(jpaProperties); return entityManagerFactoryBean; } @Bean JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory); return transactionManager; } }