Skip to content

Spring boot h2 oracle #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# Spring Boot Web Application
This repository has the project files for a tutorial series on Spring Boot available from by website at [Spring Framework Guru](https://springframework.guru)
##Using H2 and Oracle for a Spring Boot Web Application

How to use H2 and Oracle with a Spring Boot Web Application using Spring Framework profiles.

You can see the full blog post about using Spring Boot with H2 and Oracle at [Spring Framework Guru](https://springframework.guru/using-h2-and-oracle-with-spring-boot/)

21 changes: 19 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,29 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>


<!--WebJars-->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/guru/springframework/bootstrap/ProductLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package guru.springframework.bootstrap;

import guru.springframework.domain.Product;
import guru.springframework.repositories.ProductRepository;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class ProductLoader implements ApplicationListener<ContextRefreshedEvent> {

private ProductRepository productRepository;

private Logger log = Logger.getLogger(ProductLoader.class);

@Autowired
public void setProductRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {

long productCount = productRepository.count();

log.info("Product Count is:" + productCount);

if(productCount == 0){
Product shirt = new Product();
shirt.setDescription("Spring Framework Guru Shirt");
shirt.setPrice(new BigDecimal("18.95"));
shirt.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_shirt-rf412049699c14ba5b68bb1c09182bfa2_8nax2_512.jpg");
shirt.setProductId("235268845711068308");
productRepository.save(shirt);

log.info("Saved Shirt - id: " + shirt.getId());

Product mug = new Product();
mug.setDescription("Spring Framework Guru Mug");
mug.setImageUrl("https://springframework.guru/wp-content/uploads/2015/04/spring_framework_guru_coffee_mug-r11e7694903c348e1a667dfd2f1474d95_x7j54_8byvr_512.jpg");
mug.setProductId("168639393495335947");
mug.setPrice(new BigDecimal("11.95"));
productRepository.save(mug);

log.info("Saved Mug - id:" + mug.getId());
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package guru.springframework.configuration;

import oracle.jdbc.pool.OracleDataSource;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import javax.validation.constraints.NotNull;
import java.sql.SQLException;

/**
* Un-comment the annoations to use the Oracle Java config
*/
//@Configuration
//@ConfigurationProperties("oracle")
public class OracleConfiguration {
@NotNull
private String username;

@NotNull
private String password;

@NotNull
private String url;

public void setUsername(String username) {
this.username = username;
}

public void setPassword(String password) {
this.password = password;
}

public void setUrl(String url) {
this.url = url;
}

@Bean
DataSource dataSource() throws SQLException {

OracleDataSource dataSource = new OracleDataSource();
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setURL(url);
dataSource.setImplicitCachingEnabled(true);
dataSource.setFastConnectionFailoverEnabled(true);
return dataSource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package guru.springframework.configuration;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
.authorizeRequests().antMatchers("/console/**").permitAll();

httpSecurity.csrf().disable();
httpSecurity.headers().frameOptions().disable();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package guru.springframework.configuration;

import org.h2.server.web.WebServlet;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfiguration {
@Bean
ServletRegistrationBean h2servletRegistration(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
registrationBean.addUrlMappings("/console/*");
return registrationBean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package guru.springframework.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
@RequestMapping("/")
String index(){
return "index";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package guru.springframework.controllers;

import guru.springframework.domain.Product;
import guru.springframework.services.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class ProductController {

private ProductService productService;

@Autowired
public void setProductService(ProductService productService) {
this.productService = productService;
}

@RequestMapping(value = "/products", method = RequestMethod.GET)
public String list(Model model){
model.addAttribute("products", productService.listAllProducts());
return "products";
}

@RequestMapping("product/{id}")
public String showProduct(@PathVariable Integer id, Model model){
model.addAttribute("product", productService.getProductById(id));
return "productshow";
}

@RequestMapping("product/edit/{id}")
public String edit(@PathVariable Integer id, Model model){
model.addAttribute("product", productService.getProductById(id));
return "productform";
}

@RequestMapping("product/new")
public String newProduct(Model model){
model.addAttribute("product", new Product());
return "productform";
}

@RequestMapping(value = "product", method = RequestMethod.POST)
public String saveProduct(Product product){
productService.saveProduct(product);
return "redirect:/product/" + product.getId();
}

@RequestMapping("product/delete/{id}")
public String delete(@PathVariable Integer id){
productService.deleteProduct(id);
return "redirect:/products";
}

}
68 changes: 68 additions & 0 deletions src/main/java/guru/springframework/domain/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package guru.springframework.domain;

import javax.persistence.*;
import java.math.BigDecimal;

@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "prooduct_id_seq")
@SequenceGenerator(name="prooduct_id_seq", sequenceName = "PRODUCT_ID_SEQ", allocationSize = 100)
private Integer id;

@Version
private Integer version;

private String productId;
private String description;
private String imageUrl;
private BigDecimal price;

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public String getImageUrl() {
return imageUrl;
}

public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}

public BigDecimal getPrice() {
return price;
}

public void setPrice(BigDecimal price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package guru.springframework.repositories;

import guru.springframework.domain.Product;
import org.springframework.data.repository.CrudRepository;

public interface ProductRepository extends CrudRepository<Product, Integer>{
}
14 changes: 14 additions & 0 deletions src/main/java/guru/springframework/services/ProductService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package guru.springframework.services;


import guru.springframework.domain.Product;

public interface ProductService {
Iterable<Product> listAllProducts();

Product getProductById(Integer id);

Product saveProduct(Product product);

void deleteProduct(Integer id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package guru.springframework.services;

import guru.springframework.domain.Product;
import guru.springframework.repositories.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {
private ProductRepository productRepository;

@Autowired
public void setProductRepository(ProductRepository productRepository) {
this.productRepository = productRepository;
}

@Override
public Iterable<Product> listAllProducts() {
return productRepository.findAll();
}

@Override
public Product getProductById(Integer id) {
return productRepository.findOne(id);
}

@Override
public Product saveProduct(Product product) {
return productRepository.save(product);
}

@Override
public void deleteProduct(Integer id) {
productRepository.delete(id);
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application-h2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring.datasource.url=jdbc:h2:mem:testdb;Mode=Oracle
spring.datasource.platform=h2
spring.jpa.hibernate.ddl-auto=none
spring.datasource.continue-on-error=true
8 changes: 8 additions & 0 deletions src/main/resources/application-oracle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring.jpa.hibernate.ddl-auto=validate

#Basic Spring Boot Config for Oracle
spring.datasource.url=jdbc:oracle:thin:@//springframework.guru.csi0i9rgj9ws.us-east-1.rds.amazonaws.com:1521/ORCL
spring.datasource.username=scott
spring.datasource.password=tiger
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

Loading