├── README.md ├── spring-boot-hello-world-tutorial ├── .gitignore ├── README.md ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── roufid │ │ │ └── tutorial │ │ │ ├── Application.java │ │ │ ├── controller │ │ │ └── HelloWorldController.java │ │ │ └── services │ │ │ └── HelloWorldService.java │ └── resources │ │ └── application.properties │ └── test │ ├── java │ └── com │ │ └── roufid │ │ └── tutorial │ │ └── ApplicationTests.java │ └── resources │ └── application.properties ├── spring-boot-multi-database-tutorial ├── .gitignore ├── .mvn │ └── wrapper │ │ ├── maven-wrapper.jar │ │ └── maven-wrapper.properties ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── roufid │ │ │ └── tutorial │ │ │ ├── Application.java │ │ │ ├── configuration │ │ │ ├── MysqlConfiguration.java │ │ │ └── PostgresqlConfiguration.java │ │ │ ├── dao │ │ │ ├── mysql │ │ │ │ └── AuthorRepository.java │ │ │ └── postgresql │ │ │ │ └── BookRepository.java │ │ │ └── entity │ │ │ ├── mysql │ │ │ └── Author.java │ │ │ └── postgresql │ │ │ └── Book.java │ └── resources │ │ ├── application.properties │ │ └── hibernate.properties │ └── test │ ├── java │ └── com │ │ └── roufid │ │ └── tutorial │ │ └── ApplicationTest.java │ └── resources │ ├── application.properties │ └── hibernate.properties └── spring-data-jpa-repository-domain ├── .gitignore ├── README.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── roufid │ │ └── tutorial │ │ ├── Application.java │ │ ├── domain │ │ ├── Author.java │ │ └── Book.java │ │ ├── listner │ │ └── ContextRefreshedEventListner.java │ │ └── repository │ │ ├── AuthorRepository.java │ │ └── BookRepository.java └── resources │ └── application.properties └── test └── java └── com └── roufid └── tutorials └── ApplicationTests.java /README.md: -------------------------------------------------------------------------------- 1 | # spring-boot 2 | Spring boot tutorials 3 | -------------------------------------------------------------------------------- /spring-boot-hello-world-tutorial/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /spring-boot-hello-world-tutorial/README.md: -------------------------------------------------------------------------------- 1 | # Hello word Spring Boot project 2 | 3 | More tutorials on : [www.roufid.com](www.roufid.com ) 4 | -------------------------------------------------------------------------------- /spring-boot-hello-world-tutorial/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.roufid.tutoriel 7 | spring-boot-hello-world-tutorial 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | spring-boot-hello-world-tutorial 12 | Demo project for Spring Boot hello world 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.2.RELEASE 18 | 19 | 20 | 21 | UTF-8 22 | UTF-8 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-web 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-maven-plugin 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /spring-boot-hello-world-tutorial/src/main/java/com/roufid/tutorial/Application.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-boot-hello-world-tutorial/src/main/java/com/roufid/tutorial/controller/HelloWorldController.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.controller; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.web.bind.annotation.RequestMapping; 6 | import org.springframework.web.bind.annotation.ResponseBody; 7 | 8 | import com.roufid.tutorial.services.HelloWorldService; 9 | 10 | @Controller 11 | public class HelloWorldController { 12 | 13 | @Autowired 14 | private HelloWorldService helloWorldService; 15 | 16 | @RequestMapping("/") 17 | @ResponseBody 18 | public String helloWorld() { 19 | return helloWorldService.sayHelloWorld(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /spring-boot-hello-world-tutorial/src/main/java/com/roufid/tutorial/services/HelloWorldService.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.services; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | @Component 6 | public class HelloWorldService { 7 | 8 | public String sayHelloWorld() { 9 | return "Hello World !"; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /spring-boot-hello-world-tutorial/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RadouaneRoufid/spring-boot/a0976b18454eb85a7a30c6f299ce559fb194eea4/spring-boot-hello-world-tutorial/src/main/resources/application.properties -------------------------------------------------------------------------------- /spring-boot-hello-world-tutorial/src/test/java/com/roufid/tutorial/ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.test.context.junit4.SpringRunner; 6 | 7 | @RunWith(SpringRunner.class) 8 | public class ApplicationTests { 9 | 10 | @Test 11 | public void contextLoads() { 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-hello-world-tutorial/src/test/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RadouaneRoufid/spring-boot/a0976b18454eb85a7a30c6f299ce559fb194eea4/spring-boot-hello-world-tutorial/src/test/resources/application.properties -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | 12 | ### IntelliJ IDEA ### 13 | .idea 14 | *.iws 15 | *.iml 16 | *.ipr 17 | 18 | ### NetBeans ### 19 | nbproject/private/ 20 | build/ 21 | nbbuild/ 22 | dist/ 23 | nbdist/ 24 | .nb-gradle/ -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RadouaneRoufid/spring-boot/a0976b18454eb85a7a30c6f299ce559fb194eea4/spring-boot-multi-database-tutorial/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip 2 | -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.roufid.tutorial 7 | spring-boot-multi-database-tutorial 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | SB-Multi-Database 12 | Demo for multi databases Spring Boot project 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.2.RELEASE 18 | 19 | 20 | 21 | UTF-8 22 | UTF-8 23 | 1.8 24 | 25 | 26 | 27 | 28 | org.springframework.boot 29 | spring-boot-starter 30 | 31 | 32 | 33 | org.springframework.boot 34 | spring-boot-starter-data-jpa 35 | 36 | 37 | 38 | mysql 39 | mysql-connector-java 40 | runtime 41 | 42 | 43 | 44 | org.postgresql 45 | postgresql 46 | runtime 47 | 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-starter-test 52 | test 53 | 54 | 55 | 56 | org.springframework.boot 57 | spring-boot-configuration-processor 58 | true 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | org.springframework.boot 67 | spring-boot-maven-plugin 68 | 69 | 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/main/java/com/roufid/tutorial/Application.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class Application { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(Application.class, args); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/main/java/com/roufid/tutorial/configuration/MysqlConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.configuration; 2 | 3 | import java.io.IOException; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.Properties; 7 | import java.util.stream.Collectors; 8 | 9 | import javax.persistence.EntityManagerFactory; 10 | import javax.sql.DataSource; 11 | 12 | import org.springframework.beans.factory.annotation.Qualifier; 13 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 14 | import org.springframework.boot.context.properties.ConfigurationProperties; 15 | import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; 16 | import org.springframework.context.annotation.Bean; 17 | import org.springframework.context.annotation.Configuration; 18 | import org.springframework.core.io.ClassPathResource; 19 | import org.springframework.core.io.Resource; 20 | import org.springframework.core.io.support.PropertiesLoaderUtils; 21 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 22 | import org.springframework.orm.jpa.JpaTransactionManager; 23 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 24 | import org.springframework.transaction.PlatformTransactionManager; 25 | import org.springframework.transaction.annotation.EnableTransactionManagement; 26 | 27 | import com.roufid.tutorial.entity.mysql.Author; 28 | 29 | /** 30 | * Spring configuration of the "mysql" database. 31 | * 32 | * @author Radouane ROUFID. 33 | * 34 | */ 35 | @Configuration 36 | @EnableTransactionManagement 37 | @EnableJpaRepositories( 38 | entityManagerFactoryRef = "mysqlEntityManager", 39 | transactionManagerRef = "mysqlTransactionManager", 40 | basePackages = "com.roufid.tutorial.dao.mysql" 41 | ) 42 | public class MysqlConfiguration { 43 | 44 | /** 45 | * MySQL datasource definition. 46 | * 47 | * @return datasource. 48 | */ 49 | @Bean 50 | @ConfigurationProperties(prefix = "spring.mysql.datasource") 51 | public DataSource mysqlDataSource() { 52 | return DataSourceBuilder 53 | .create() 54 | .build(); 55 | } 56 | 57 | /** 58 | * Entity manager definition. 59 | * 60 | * @param builder an EntityManagerFactoryBuilder. 61 | * @return LocalContainerEntityManagerFactoryBean. 62 | */ 63 | @Bean(name = "mysqlEntityManager") 64 | public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactory(EntityManagerFactoryBuilder builder) { 65 | return builder 66 | .dataSource(mysqlDataSource()) 67 | .properties(hibernateProperties()) 68 | .packages(Author.class) 69 | .persistenceUnit("mysqlPU") 70 | .build(); 71 | } 72 | 73 | /** 74 | * @param entityManagerFactory 75 | * @return 76 | */ 77 | @Bean(name = "mysqlTransactionManager") 78 | public PlatformTransactionManager mysqlTransactionManager(@Qualifier("mysqlEntityManager") EntityManagerFactory entityManagerFactory) { 79 | return new JpaTransactionManager(entityManagerFactory); 80 | } 81 | 82 | private Map hibernateProperties() { 83 | 84 | Resource resource = new ClassPathResource("hibernate.properties"); 85 | try { 86 | Properties properties = PropertiesLoaderUtils.loadProperties(resource); 87 | return properties.entrySet().stream() 88 | .collect(Collectors.toMap( 89 | e -> e.getKey().toString(), 90 | e -> e.getValue()) 91 | ); 92 | } catch (IOException e) { 93 | return new HashMap(); 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/main/java/com/roufid/tutorial/configuration/PostgresqlConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.configuration; 2 | 3 | import java.io.IOException; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | import java.util.Properties; 7 | import java.util.stream.Collectors; 8 | 9 | import javax.persistence.EntityManagerFactory; 10 | import javax.sql.DataSource; 11 | 12 | import org.springframework.beans.factory.annotation.Qualifier; 13 | import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 14 | import org.springframework.boot.context.properties.ConfigurationProperties; 15 | import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; 16 | import org.springframework.context.annotation.Bean; 17 | import org.springframework.context.annotation.Configuration; 18 | import org.springframework.context.annotation.Primary; 19 | import org.springframework.core.io.ClassPathResource; 20 | import org.springframework.core.io.Resource; 21 | import org.springframework.core.io.support.PropertiesLoaderUtils; 22 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 23 | import org.springframework.orm.jpa.JpaTransactionManager; 24 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 25 | import org.springframework.transaction.PlatformTransactionManager; 26 | import org.springframework.transaction.annotation.EnableTransactionManagement; 27 | 28 | import com.roufid.tutorial.entity.postgresql.Book; 29 | 30 | /** 31 | * Spring configuration of the "PostgreSQL" database. 32 | * 33 | * @author Radouane ROUFID. 34 | * 35 | */ 36 | @Configuration 37 | @EnableTransactionManagement 38 | @EnableJpaRepositories( 39 | entityManagerFactoryRef = "postgresqlEntityManager", 40 | transactionManagerRef = "postgresqlTransactionManager", 41 | basePackages = "com.roufid.tutorial.dao.postgresql" 42 | ) 43 | public class PostgresqlConfiguration { 44 | 45 | /** 46 | * PostgreSQL datasource definition. 47 | * 48 | * @return datasource. 49 | */ 50 | @Bean 51 | @Primary 52 | @ConfigurationProperties(prefix = "spring.postgresql.datasource") 53 | public DataSource postgresqlDataSource() { 54 | return DataSourceBuilder 55 | .create() 56 | .build(); 57 | } 58 | 59 | /** 60 | * Entity manager definition. 61 | * 62 | * @param builder an EntityManagerFactoryBuilder. 63 | * @return LocalContainerEntityManagerFactoryBean. 64 | */ 65 | @Primary 66 | @Bean(name = "postgresqlEntityManager") 67 | public LocalContainerEntityManagerFactoryBean postgresqlEntityManagerFactory(EntityManagerFactoryBuilder builder) { 68 | return builder 69 | .dataSource(postgresqlDataSource()) 70 | .properties(hibernateProperties()) 71 | .packages(Book.class) 72 | .persistenceUnit("postgresqlPU") 73 | .build(); 74 | } 75 | 76 | @Primary 77 | @Bean(name = "postgresqlTransactionManager") 78 | public PlatformTransactionManager postgresqlTransactionManager(@Qualifier("postgresqlEntityManager") EntityManagerFactory entityManagerFactory) { 79 | return new JpaTransactionManager(entityManagerFactory); 80 | } 81 | 82 | private Map hibernateProperties() { 83 | 84 | Resource resource = new ClassPathResource("hibernate.properties"); 85 | 86 | try { 87 | Properties properties = PropertiesLoaderUtils.loadProperties(resource); 88 | return properties.entrySet().stream() 89 | .collect(Collectors.toMap( 90 | e -> e.getKey().toString(), 91 | e -> e.getValue()) 92 | ); 93 | } catch (IOException e) { 94 | return new HashMap(); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/main/java/com/roufid/tutorial/dao/mysql/AuthorRepository.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.dao.mysql; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | 5 | import com.roufid.tutorial.entity.mysql.Author; 6 | 7 | /** 8 | * Author Repository. 9 | * 10 | * @author Radouane ROUFID. 11 | * 12 | */ 13 | public interface AuthorRepository extends CrudRepository { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/main/java/com/roufid/tutorial/dao/postgresql/BookRepository.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.dao.postgresql; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | 5 | import com.roufid.tutorial.entity.postgresql.Book; 6 | 7 | /** 8 | * Book repository. 9 | * 10 | * @author Radouane ROUFID. 11 | * 12 | */ 13 | public interface BookRepository extends CrudRepository { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/main/java/com/roufid/tutorial/entity/mysql/Author.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.entity.mysql; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table(name = "AUTHOR") 12 | public class Author implements Serializable { 13 | 14 | private static final long serialVersionUID = -1848119459950659679L; 15 | 16 | @Id 17 | @Column(name = "ID") 18 | private Long id; 19 | 20 | @Column(name = "FIRSTNAME") 21 | private String firstname; 22 | 23 | @Column(name = "LASTNAME") 24 | private String lastname; 25 | 26 | /** 27 | * @return the id 28 | */ 29 | public Long getId() { 30 | return id; 31 | } 32 | 33 | /** 34 | * @param id 35 | * the id to set 36 | */ 37 | public void setId(Long id) { 38 | this.id = id; 39 | } 40 | 41 | /** 42 | * @return the firstname 43 | */ 44 | public String getFirstname() { 45 | return firstname; 46 | } 47 | 48 | /** 49 | * @param firstname 50 | * the firstname to set 51 | */ 52 | public void setFirstname(String firstname) { 53 | this.firstname = firstname; 54 | } 55 | 56 | /** 57 | * @return the lastname 58 | */ 59 | public String getLastname() { 60 | return lastname; 61 | } 62 | 63 | /** 64 | * @param lastname 65 | * the lastname to set 66 | */ 67 | public void setLastname(String lastname) { 68 | this.lastname = lastname; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/main/java/com/roufid/tutorial/entity/postgresql/Book.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.entity.postgresql; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.Id; 8 | import javax.persistence.Table; 9 | 10 | @Entity 11 | @Table(name = "BOOK") 12 | public class Book implements Serializable { 13 | 14 | private static final long serialVersionUID = -9019470250770543773L; 15 | 16 | @Id 17 | private Long id; 18 | 19 | @Column 20 | private String name; 21 | 22 | @Column 23 | private Long authorId; 24 | 25 | /** 26 | * @return the id 27 | */ 28 | public Long getId() { 29 | return id; 30 | } 31 | 32 | /** 33 | * @param id the id to set 34 | */ 35 | public void setId(Long id) { 36 | this.id = id; 37 | } 38 | 39 | /** 40 | * @return the name 41 | */ 42 | public String getName() { 43 | return name; 44 | } 45 | 46 | /** 47 | * @param name the name to set 48 | */ 49 | public void setName(String name) { 50 | this.name = name; 51 | } 52 | 53 | /** 54 | * @return the authorId 55 | */ 56 | public Long getAuthorId() { 57 | return authorId; 58 | } 59 | 60 | /** 61 | * @param authorId the authorId to set 62 | */ 63 | public void setAuthorId(Long authorId) { 64 | this.authorId = authorId; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | # ----------------------- 2 | # POSTGRESQL DATABASE CONFIGURATION 3 | # ----------------------- 4 | spring.postgresql.datasource.url=jdbc:postgresql://localhost:5432/book_db 5 | spring.postgresql.datasource.username=postgres 6 | spring.postgresql.datasource.password=postgres 7 | spring.postgresql.datasource.driver-class-name=org.postgresql.Driver 8 | 9 | # ------------------------------ 10 | # MYSQL DATABASE CONFIGURATION 11 | # ------------------------------ 12 | spring.mysql.datasource.url=jdbc:mysql://localhost:3306/author_db?autoReconnect=true&useSSL=false 13 | spring.mysql.datasource.username=root 14 | spring.mysql.datasource.password= 15 | spring.mysql.datasource.driver-class-name=com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/main/resources/hibernate.properties: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # Spring Data JPA CONFIGURATION 3 | # ------------------------------ 4 | hibernate.show_sql=true 5 | hibernate.format_sql=true 6 | hibernate.hbm2ddl.auto=create -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/test/java/com/roufid/tutorial/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.boot.test.context.SpringBootTest; 9 | import org.springframework.test.context.junit4.SpringRunner; 10 | 11 | import com.roufid.tutorial.dao.mysql.AuthorRepository; 12 | import com.roufid.tutorial.dao.postgresql.BookRepository; 13 | import com.roufid.tutorial.entity.mysql.Author; 14 | import com.roufid.tutorial.entity.postgresql.Book; 15 | 16 | 17 | 18 | @RunWith(SpringRunner.class) 19 | @SpringBootTest 20 | public class ApplicationTest { 21 | 22 | @Autowired 23 | private AuthorRepository authorRepository; 24 | 25 | @Autowired 26 | private BookRepository bookRepository; 27 | 28 | private Long bookId = 1L; 29 | 30 | /** 31 | * Will be executed before the methods annotated with code @Test. 32 | */ 33 | @Before 34 | public void init() { 35 | 36 | Author author = new Author(); 37 | author.setId(1L); 38 | author.setFirstname("Radouane"); 39 | author.setLastname("Roufid"); 40 | 41 | authorRepository.save(author); 42 | 43 | Book book = new Book(); 44 | book.setId(bookId); 45 | book.setName("Spring Boot Book"); 46 | book.setAuthorId(author.getId()); 47 | 48 | bookRepository.save(book); 49 | } 50 | 51 | /** 52 | * Test should find the author's book from the PostgreSQL database. 53 | */ 54 | @Test 55 | public void testShouldFindAuthor() { 56 | 57 | Book book = bookRepository.findOne(bookId); 58 | 59 | Assert.assertNotNull(book); 60 | 61 | Author author = authorRepository.findOne(book.getId()); 62 | 63 | Assert.assertNotNull(author); 64 | 65 | System.out.println("The book " + book.getName() + " was written by " + author.getFirstname() + " " + author.getLastname()); 66 | 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/test/resources/application.properties: -------------------------------------------------------------------------------- 1 | 2 | # ----------------------- 3 | # POSTGRESQL DATABASE CONFIGURATION 4 | # ----------------------- 5 | spring.postgresql.datasource.url=jdbc:postgresql://localhost:5432/book_db 6 | spring.postgresql.datasource.username=postgres 7 | spring.postgresql.datasource.password=postgres 8 | spring.postgresql.datasource.driver-class-name=org.postgresql.Driver 9 | 10 | # ------------------------------ 11 | # MYSQL DATABASE CONFIGURATION 12 | # ------------------------------ 13 | spring.mysql.datasource.url=jdbc:mysql://localhost:3306/author_db?autoReconnect=true&useSSL=false 14 | spring.mysql.datasource.username=root 15 | spring.mysql.datasource.password= 16 | spring.mysql.datasource.driver-class-name=com.mysql.jdbc.Driver -------------------------------------------------------------------------------- /spring-boot-multi-database-tutorial/src/test/resources/hibernate.properties: -------------------------------------------------------------------------------- 1 | # ------------------------------ 2 | # Spring Data JPA CONFIGURATION 3 | # ------------------------------ 4 | hibernate.show_sql=true 5 | hibernate.format_sql=true 6 | hibernate.hbm2ddl.auto=create -------------------------------------------------------------------------------- /spring-data-jpa-repository-domain/.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | .mvn 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | 19 | ### NetBeans ### 20 | nbproject/private/ 21 | build/ 22 | nbbuild/ 23 | dist/ 24 | nbdist/ 25 | .nb-gradle/ -------------------------------------------------------------------------------- /spring-data-jpa-repository-domain/README.md: -------------------------------------------------------------------------------- 1 | 2 | # Retrieve Spring JPA repository domain type 3 | 4 | Mini tutorial that retrieves Spring JPA repository domain type using `Repositories` and `DefaultRepositoryMetadata` of Spring data commons. 5 | 6 | 7 | More tutorial on : -------------------------------------------------------------------------------- /spring-data-jpa-repository-domain/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.roufid.tutorials 7 | spring-data-jpa-repository-domain 8 | 0.0.1-SNAPSHOT 9 | jar 10 | 11 | spring-data-jpa-repository-domain 12 | 13 | 14 | 15 | org.springframework.boot 16 | spring-boot-starter-parent 17 | 1.4.3.RELEASE 18 | 19 | 20 | 21 | 22 | UTF-8 23 | UTF-8 24 | 1.8 25 | 26 | 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-starter 31 | 32 | 33 | 34 | org.springframework.boot 35 | spring-boot-starter-data-jpa 36 | 37 | 38 | 39 | org.hsqldb 40 | hsqldb 41 | runtime 42 | 43 | 44 | 45 | org.springframework.boot 46 | spring-boot-starter-test 47 | test 48 | 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-maven-plugin 56 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /spring-data-jpa-repository-domain/src/main/java/com/roufid/tutorial/Application.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 6 | 7 | @SpringBootApplication 8 | @EnableJpaRepositories 9 | public class Application { 10 | 11 | public static void main(String[] args) { 12 | SpringApplication.run(Application.class, args); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-data-jpa-repository-domain/src/main/java/com/roufid/tutorial/domain/Author.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.SequenceGenerator; 11 | import javax.persistence.Table; 12 | 13 | @Entity 14 | @Table(name = "AUTHOR") 15 | public class Author implements Serializable { 16 | 17 | private static final long serialVersionUID = -8210984406202377121L; 18 | 19 | @Id 20 | @SequenceGenerator(name = "idauhtor_seq", sequenceName = "idauhtor_seq", allocationSize = 1) 21 | @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idauhtor_seq") 22 | @Column(name = "AUTHOR_ID") 23 | private Integer id; 24 | 25 | @Column 26 | private String firstname; 27 | 28 | @Column 29 | private String lastname; 30 | 31 | public Integer getId() { 32 | return id; 33 | } 34 | 35 | public void setId(Integer id) { 36 | this.id = id; 37 | } 38 | 39 | public String getFirstname() { 40 | return firstname; 41 | } 42 | 43 | public void setFirstname(String firstname) { 44 | this.firstname = firstname; 45 | } 46 | 47 | public String getLastname() { 48 | return lastname; 49 | } 50 | 51 | public void setLastname(String lastname) { 52 | this.lastname = lastname; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /spring-data-jpa-repository-domain/src/main/java/com/roufid/tutorial/domain/Book.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.domain; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.GenerationType; 9 | import javax.persistence.Id; 10 | import javax.persistence.SequenceGenerator; 11 | import javax.persistence.Table; 12 | 13 | @Entity 14 | @Table(name = "BOOK") 15 | public class Book implements Serializable { 16 | 17 | private static final long serialVersionUID = -8105130932670794882L; 18 | 19 | @Id 20 | @SequenceGenerator(name = "idbook_seq", sequenceName = "idbook_seq", allocationSize = 1) 21 | @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "idbook_seq") 22 | @Column(name = "BOOK_ID") 23 | private Integer id; 24 | 25 | @Column 26 | private String name; 27 | 28 | public Integer getId() { 29 | return id; 30 | } 31 | 32 | public void setId(Integer id) { 33 | this.id = id; 34 | } 35 | 36 | public String getName() { 37 | return name; 38 | } 39 | 40 | public void setName(String name) { 41 | this.name = name; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /spring-data-jpa-repository-domain/src/main/java/com/roufid/tutorial/listner/ContextRefreshedEventListner.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.listner; 2 | 3 | import java.util.Iterator; 4 | 5 | import org.springframework.beans.factory.ListableBeanFactory; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.context.event.ContextRefreshedEvent; 8 | import org.springframework.context.event.EventListener; 9 | import org.springframework.data.repository.Repository; 10 | import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; 11 | import org.springframework.data.repository.support.Repositories; 12 | import org.springframework.stereotype.Component; 13 | 14 | import com.roufid.tutorial.repository.BookRepository; 15 | 16 | @Component 17 | public class ContextRefreshedEventListner { 18 | 19 | @Autowired 20 | private ListableBeanFactory listableBeanFactory; 21 | 22 | @EventListener 23 | public void handleContextRefresh(ContextRefreshedEvent event) { 24 | displayRepositories(); 25 | displayUsingListableBeanFactory(); 26 | } 27 | 28 | @SuppressWarnings("rawtypes") 29 | private void displayRepositories() { 30 | 31 | Repositories repositories = new Repositories(listableBeanFactory); 32 | 33 | Iterator> it = repositories.iterator(); 34 | while (it.hasNext()) { 35 | Class domainType = it.next(); 36 | Repository repository = (Repository) repositories.getRepositoryFor(domainType); 37 | 38 | System.out.println("The domain of the repository [" + repository + "] is : " + domainType); 39 | } 40 | } 41 | 42 | public void displayUsingListableBeanFactory() { 43 | 44 | DefaultRepositoryMetadata drm = new DefaultRepositoryMetadata(BookRepository.class); 45 | Class domainType = drm.getDomainType(); 46 | 47 | System.out.println(domainType); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /spring-data-jpa-repository-domain/src/main/java/com/roufid/tutorial/repository/AuthorRepository.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.repository; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | 5 | import com.roufid.tutorial.domain.Author; 6 | 7 | public interface AuthorRepository extends CrudRepository { 8 | 9 | } -------------------------------------------------------------------------------- /spring-data-jpa-repository-domain/src/main/java/com/roufid/tutorial/repository/BookRepository.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorial.repository; 2 | 3 | import org.springframework.data.repository.CrudRepository; 4 | 5 | import com.roufid.tutorial.domain.Book; 6 | 7 | public interface BookRepository extends CrudRepository { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /spring-data-jpa-repository-domain/src/main/resources/application.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RadouaneRoufid/spring-boot/a0976b18454eb85a7a30c6f299ce559fb194eea4/spring-data-jpa-repository-domain/src/main/resources/application.properties -------------------------------------------------------------------------------- /spring-data-jpa-repository-domain/src/test/java/com/roufid/tutorials/ApplicationTests.java: -------------------------------------------------------------------------------- 1 | package com.roufid.tutorials; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.boot.test.context.SpringBootTest; 6 | import org.springframework.test.context.junit4.SpringRunner; 7 | 8 | @RunWith(SpringRunner.class) 9 | @SpringBootTest 10 | public class ApplicationTests { 11 | 12 | @Test 13 | public void contextLoads() { 14 | } 15 | 16 | } 17 | --------------------------------------------------------------------------------