Skip to content

Commit aae90b8

Browse files
committed
Spring Boot MVC DB MySQL H2 Thyme
1 parent aaf1ece commit aae90b8

File tree

19 files changed

+517
-25
lines changed

19 files changed

+517
-25
lines changed

Part-5 Spring Boot Web Application/SpringMultipleDataStructure/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@
5959
</exclusion>
6060
</exclusions>
6161
</dependency>
62+
<dependency>
63+
<groupId>org.springframework.boot</groupId>
64+
<artifactId>spring-boot-configuration-processor</artifactId>
65+
<optional>true</optional>
66+
</dependency>
67+
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
68+
<dependency>
69+
<groupId>org.apache.tomcat</groupId>
70+
<artifactId>tomcat-jasper</artifactId>
71+
<version>10.0.0-M4</version>
72+
</dependency>
73+
74+
<dependency>
75+
<groupId>org.springframework.boot</groupId>
76+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
77+
</dependency>
6278
</dependencies>
6379

6480
<build>

Part-5 Spring Boot Web Application/SpringMultipleDataStructure/src/main/java/spring/mvc/MvcApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import org.springframework.transaction.annotation.EnableTransactionManagement;
99

1010
@SpringBootApplication(
11-
exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
11+
exclude = {DataSourceAutoConfiguration.class,
12+
HibernateJpaAutoConfiguration.class,
1213
DataSourceTransactionManagerAutoConfiguration.class
1314
}
1415
)
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,90 @@
11
package spring.mvc.config;
22

3+
import org.springframework.core.env.Environment;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
6+
import org.springframework.boot.context.properties.ConfigurationProperties;
7+
import org.springframework.boot.jdbc.DataSourceBuilder;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.core.io.ClassPathResource;
11+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
12+
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
13+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
14+
import org.springframework.orm.jpa.JpaTransactionManager;
15+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
16+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
17+
import org.springframework.transaction.PlatformTransactionManager;
18+
19+
import javax.persistence.EntityManagerFactory;
20+
import javax.sql.DataSource;
21+
import java.util.Properties;
22+
323
/**
424
* @Created 08 / 04 / 2020 - 5:44 PM
525
* @project SpringMultipleDataStructure
626
* @Author Hamdamboy
727
*/
28+
29+
@Configuration
30+
@EnableJpaRepositories(
31+
basePackages = "spring.mvc.orders.repository",
32+
entityManagerFactoryRef = "ordersEntityManagerFactory",
33+
transactionManagerRef = "ordersTransactionManager"
34+
)
835
public class OrderDataSourceConfig {
36+
//
37+
@Autowired
38+
private Environment environment;
39+
40+
@Bean
41+
@ConfigurationProperties(prefix = "datasource.orders")
42+
public DataSourceProperties orderDataSourceProperties(){
43+
return new DataSourceProperties();
44+
}
45+
46+
@Bean
47+
public DataSource ordersDataSource(){
48+
DataSourceProperties primaryDataSourceProperties = orderDataSourceProperties();
49+
return DataSourceBuilder.create()
50+
.driverClassName(primaryDataSourceProperties.getDriverClassName())
51+
.url(primaryDataSourceProperties.getUrl())
52+
.username(primaryDataSourceProperties.getUsername())
53+
.password(primaryDataSourceProperties.getPassword())
54+
.build();
55+
}
56+
57+
@Bean
58+
public PlatformTransactionManager ordersTransactionManger(){
59+
EntityManagerFactory factory = ordersEntityManagerFactory().getObject();
60+
return new JpaTransactionManager(factory);
61+
}
62+
63+
@Bean
64+
public LocalContainerEntityManagerFactoryBean ordersEntityManagerFactory(){
65+
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
66+
factory.setDataSource(ordersDataSource());
67+
factory.setPackagesToScan(new String[] {
68+
"spring.mvc.orders.repository"
69+
});
70+
factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
71+
72+
Properties jpaProperties = new Properties();
73+
jpaProperties.put("hibernate.hbm2ddl.auto", environment.getProperty("spring.jpa.hibernate.ddl-auto"));
74+
jpaProperties.put("hibernate.show-sql", environment.getProperty("spring.jpa.show-sql"));
75+
factory.setJpaProperties(jpaProperties);
76+
77+
return factory;
78+
}
79+
80+
@Bean
81+
public DataSourceInitializer ordersDataSrouceInitializer(){
82+
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
83+
dataSourceInitializer.setDataSource(ordersDataSource());
84+
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
85+
databasePopulator.addScript(new ClassPathResource("orders-data.sql"));
86+
dataSourceInitializer.setDatabasePopulator(databasePopulator);
87+
dataSourceInitializer.setEnabled(environment.getProperty("datasource.orders.initialize", Boolean.class, false));
88+
return dataSourceInitializer;
89+
}
990
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,90 @@
11
package spring.mvc.config;
22

3+
import org.springframework.core.env.Environment;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
6+
import org.springframework.boot.context.properties.ConfigurationProperties;
7+
import org.springframework.boot.jdbc.DataSourceBuilder;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.core.io.ClassPathResource;
11+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
12+
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
13+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
14+
import org.springframework.orm.jpa.JpaTransactionManager;
15+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
16+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
17+
import org.springframework.transaction.PlatformTransactionManager;
18+
19+
import javax.persistence.EntityManagerFactory;
20+
import javax.sql.DataSource;
21+
import java.util.Properties;
22+
323
/**
424
* @Created 08 / 04 / 2020 - 5:44 PM
525
* @project SpringMultipleDataStructure
626
* @Author Hamdamboy
727
*/
28+
@Configuration
29+
@EnableJpaRepositories(
30+
basePackages = "spring.mvc.security.repository",
31+
entityManagerFactoryRef = "securityEntityManagerFactory",
32+
transactionManagerRef = "securityTransactionManager"
33+
)
834
public class SecurityDataSoruceConfig {
35+
36+
@Autowired
37+
private Environment environment;
38+
39+
@Bean
40+
@ConfigurationProperties(prefix = "datasource.security")
41+
public DataSourceProperties securityDataSourceProperties(){
42+
return new DataSourceProperties();
43+
}
44+
45+
@Bean
46+
public DataSource securityDataSource(){
47+
DataSourceProperties securityDataSourceProperties = securityDataSourceProperties();
48+
return DataSourceBuilder.create()
49+
.driverClassName(securityDataSourceProperties.getDriverClassName())
50+
.url(securityDataSourceProperties.getUrl())
51+
.username(securityDataSourceProperties.getUsername())
52+
.password(securityDataSourceProperties.getPassword())
53+
.build();
54+
}
55+
56+
@Bean
57+
public PlatformTransactionManager securityTransactionManager(){
58+
EntityManagerFactory factory = securityEntityManagerFactory().getObject();
59+
return new JpaTransactionManager(factory);
60+
}
61+
62+
@Bean
63+
public LocalContainerEntityManagerFactoryBean securityEntityManagerFactory(){
64+
65+
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
66+
factory.setDataSource(securityDataSource());
67+
factory.setPackagesToScan(new String[]{"spring.mvc.security.entity"});
68+
factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
69+
70+
Properties jpaProperties = new Properties();
71+
jpaProperties.put("hibernate.hbm2ddl.auto", environment.getProperty("spring.jpa.hibernate.ddl-auto"));
72+
jpaProperties.put("hibernate.show-sql", environment.getProperty("spring.jpa.show-sql"));
73+
factory.setJpaProperties(jpaProperties);
74+
75+
return factory;
76+
}
77+
78+
@Bean
79+
public DataSourceInitializer securityDataSourceInitializer() {
80+
DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
81+
dataSourceInitializer.setDataSource(securityDataSource());
82+
ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
83+
databasePopulator.addScript(new ClassPathResource("security-data-sql"));
84+
dataSourceInitializer.setDatabasePopulator(databasePopulator);
85+
dataSourceInitializer.setEnabled(environment.getProperty("datasource.security.initialize", Boolean.class, false));
86+
return dataSourceInitializer;
87+
}
88+
89+
990
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
package spring.mvc.config;
22

3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter;
6+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
7+
38
/**
49
* @Created 08 / 04 / 2020 - 5:44 PM
510
* @project SpringMultipleDataStructure
611
* @Author Hamdamboy
712
*/
8-
public class WebMvcConfig {
13+
14+
@Configuration
15+
public class WebMvcConfig extends WebMvcConfigurerAdapter {
16+
//
17+
18+
@Bean
19+
public OpenEntityManagerInViewFilter securityOpenEntityManagerInViewFilter()
20+
{
21+
OpenEntityManagerInViewFilter osivFilter = new OpenEntityManagerInViewFilter();
22+
osivFilter.setEntityManagerFactoryBeanName("securityEntityManagerFactory");
23+
return osivFilter;
24+
}
25+
26+
@Bean
27+
public OpenEntityManagerInViewFilter ordersOpenEntityManagerInViewFilter()
28+
{
29+
OpenEntityManagerInViewFilter osivFilter = new OpenEntityManagerInViewFilter();
30+
osivFilter.setEntityManagerFactoryBeanName("ordersEntityManagerFactory");
31+
return osivFilter;
32+
}
933
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
11
package spring.mvc.controller;
22

3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RequestMethod;
8+
import spring.mvc.service.entity.UserOrdersService;
9+
310
/**
411
* @Created 08 / 04 / 2020 - 5:44 PM
512
* @project SpringMultipleDataStructure
613
* @Author Hamdamboy
714
*/
15+
@Controller
816
public class HomeController {
17+
//
18+
@Autowired
19+
private UserOrdersService userOrdersService;
20+
21+
@RequestMapping(value = {"/", "/app/users"}, method = RequestMethod.GET)
22+
public String getUsers(Model model){
23+
model.addAttribute("users", userOrdersService.getUsers());
24+
model.addAttribute("orders", userOrdersService.getOrders());
25+
26+
return "users";
27+
}
928
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,57 @@
11
package spring.mvc.orders.entities;
22

3+
import javax.persistence.*;
4+
import java.util.Set;
5+
36
/**
47
* @Created 08 / 04 / 2020 - 5:46 PM
58
* @project SpringMultipleDataStructure
69
* @Author Hamdamboy
710
*/
11+
@Entity
12+
@Table(name = "ORDERS")
813
public class Order {
14+
//
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.AUTO)
17+
private Integer id;
18+
@Column(nullable = false, name = "cust_name")
19+
private String customerName;
20+
@Column(nullable = false, name = "cust_email")
21+
private String customerEmail;
22+
23+
@OneToMany(mappedBy = "order")
24+
private Set<OrderItem> orderItems;
25+
26+
public Integer getId() {
27+
return id;
28+
}
29+
30+
public void setId(Integer id) {
31+
this.id = id;
32+
}
33+
34+
public String getCustomerName() {
35+
return customerName;
36+
}
37+
38+
public void setCustomerName(String customerName) {
39+
this.customerName = customerName;
40+
}
41+
42+
public String getCustomerEmail() {
43+
return customerEmail;
44+
}
45+
46+
public void setCustomerEmail(String customerEmail) {
47+
this.customerEmail = customerEmail;
48+
}
49+
50+
public Set<OrderItem> getOrderItems() {
51+
return orderItems;
52+
}
53+
54+
public void setOrderItems(Set<OrderItem> orderItems) {
55+
this.orderItems = orderItems;
56+
}
957
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,58 @@
11
package spring.mvc.orders.entities;
22

3+
import javax.persistence.*;
4+
35
/**
46
* @Created 08 / 04 / 2020 - 5:46 PM
57
* @project SpringMultipleDataStructure
68
* @Author Hamdamboy
79
*/
10+
11+
@Entity
12+
@Table(name = "ORDER_ITEMS")
813
public class OrderItem {
14+
//
15+
@Id
16+
@GeneratedValue(strategy = GenerationType.AUTO)
17+
private Integer id;
18+
private String productCode;
19+
private int quantity;
20+
@ManyToOne
21+
@JoinColumn(name = "order_id")
22+
private Order order;
23+
24+
public Integer getId(){
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getProductCode() {
33+
return productCode;
34+
}
35+
36+
public void setProductCode(String productCode) {
37+
this.productCode = productCode;
38+
}
39+
40+
public int getQuantity() {
41+
return quantity;
42+
}
43+
44+
public void setQuantity(int quantity) {
45+
this.quantity = quantity;
46+
}
47+
48+
public Order getOrder() {
49+
return order;
50+
}
51+
52+
public void setOrder(Order order) {
53+
this.order = order;
54+
}
55+
56+
57+
958
}

0 commit comments

Comments
 (0)