Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --APPLICATION PROPERTIES--
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- spring.datasource.url=jdbc:mysql://localhost:3306/bookstore
- spring.datasource.username=root
- spring.datasource.password=sean
- #Hibernate Configuration
- hibernate.dialect=org.hibernate.dialect.H2Dialect
- hibernate.hbm2ddl.auto=create-drop
- hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
- hibernate.show_sql=false
- hibernate.format_sql=true
- --CONFIG--
- package com.sean.books;
- import org.apache.tomcat.jdbc.pool.DataSource;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.boot.orm.jpa.EntityScan;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.core.env.Environment;
- 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 javax.persistence.EntityManagerFactory;
- import java.util.Properties;
- /**
- * Created by Sean on 12/02/2016.
- */
- @Configuration
- @EnableAutoConfiguration
- @EntityScan("com.sean.books")
- @EnableJpaRepositories(basePackages = {"com.sean.books"})
- @EnableTransactionManagement
- public class BookRepoConfig {
- @Bean(destroyMethod = "close")
- DataSource dataSource(Environment env) {
- DataSource dataSourceConfig = new DataSource();
- dataSourceConfig.setDriverClassName(env.getRequiredProperty("spring.datasource.driver-class-name"));
- dataSourceConfig.setUrl(env.getRequiredProperty("spring.datasource.url"));
- dataSourceConfig.setUsername(env.getRequiredProperty("spring.datasource.username"));
- dataSourceConfig.setPassword(env.getRequiredProperty("spring.datasource.password"));
- return new DataSource(dataSourceConfig);
- }
- @Bean
- LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource,
- Environment env) {
- LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
- entityManagerFactoryBean.setDataSource(dataSource);
- entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
- entityManagerFactoryBean.setPackagesToScan("com.sean.books");
- 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", env.getRequiredProperty("hibernate.dialect"));
- //Specifies the action that is invoked to the database when the Hibernate
- //SessionFactory is created or closed.
- jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto")
- );
- //Configures the naming strategy that is used when Hibernate creates
- //new database objects and schema elements
- jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy")
- );
- //If the value of this property is true, Hibernate writes all SQL
- //statements to the console.
- jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql")
- );
- //If the value of this property is true, Hibernate will format the SQL
- //that is written to the console.
- jpaProperties.put("hibernate.format_sql",
- env.getRequiredProperty("hibernate.format_sql")
- );
- entityManagerFactoryBean.setJpaProperties(jpaProperties);
- return entityManagerFactoryBean;
- }
- @Bean
- JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
- JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory(entityManagerFactory);
- return transactionManager;
- }
- }
- --REPO--
- package com.sean.books;
- import org.hibernate.annotations.NamedNativeQueries;
- import org.hibernate.annotations.NamedNativeQuery;
- import org.springframework.data.domain.Page;
- import org.springframework.data.domain.Pageable;
- import org.springframework.data.jpa.repository.Query;
- import org.springframework.data.repository.CrudRepository;
- import org.springframework.data.repository.Repository;
- import org.springframework.data.repository.query.Param;
- import java.util.List;
- /**
- * Created by Sean on 12/02/2016.
- */
- public interface BookRepository extends Repository<Book, Long> {
- @Query(value = "SELECT * FROM books", nativeQuery = true)
- List<Book> findAllBooks();
- @Query(value = "SELECT * FROM cart", nativeQuery = true)
- List<Book> findAllCartItems();
- Book save(Book book);
- void delete(Book deleted);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement