Skip to content

Commit d40b69d

Browse files
committed
my sql database connection added
1 parent 7b3222d commit d40b69d

14 files changed

+282
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# crud_spring_mvc
22
Based on the tutorial <a href="https://github.com/springframeworkguru/spring5-recipe-app/tree/master">Spring Framework 5 - Beginner to Guru</a> by
3-
John Thompson, demo project for Spring Boot, Spring MVC, Spring Data JPA, Hibernate, Crud operations, test-driven design
3+
John Thompson, demo project for Spring Boot, Spring MVC, Spring Data JPA, Hibernate, MySQL, Crud operations, test-driven design

pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
<groupId>org.springframework.boot</groupId>
3838
<artifactId>spring-boot-starter-data-jpa</artifactId>
3939
</dependency>
40+
<dependency>
41+
<groupId>mysql</groupId>
42+
<artifactId>mysql-connector-java</artifactId>
43+
</dependency>
4044
<dependency>
4145
<groupId>com.h2database</groupId>
4246
<artifactId>h2</artifactId>

src/main/java/net/joedoe/recipe/configs/DevBootstrap.java src/main/java/net/joedoe/recipe/configs/DefaultBootstrap.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
import net.joedoe.recipe.services.RecipeService;
77
import net.joedoe.recipe.services.UnitOfMeasureService;
88
import org.springframework.context.ApplicationListener;
9+
import org.springframework.context.annotation.Profile;
910
import org.springframework.context.event.ContextRefreshedEvent;
1011
import org.springframework.stereotype.Component;
1112

1213
import javax.transaction.Transactional;
1314
import java.math.BigDecimal;
1415

1516
@Component
16-
public class DevBootstrap implements ApplicationListener<ContextRefreshedEvent> {
17+
@Profile("default")
18+
public class DefaultBootstrap implements ApplicationListener<ContextRefreshedEvent> {
1719
private final CategoryService categoryService;
1820
private final IRecipeService<Recipe> recipeService;
1921
private final UnitOfMeasureService unitOfMeasureService;
2022

21-
public DevBootstrap(CategoryService categoryService, RecipeService recipeService, UnitOfMeasureService unitOfMeasureService) {
23+
public DefaultBootstrap(CategoryService categoryService, RecipeService recipeService, UnitOfMeasureService unitOfMeasureService) {
2224
this.categoryService = categoryService;
2325
this.recipeService = recipeService;
2426
this.unitOfMeasureService = unitOfMeasureService;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package net.joedoe.recipe.configs;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import net.joedoe.recipe.domains.*;
5+
import net.joedoe.recipe.services.CategoryService;
6+
import net.joedoe.recipe.services.IRecipeService;
7+
import net.joedoe.recipe.services.RecipeService;
8+
import net.joedoe.recipe.services.UnitOfMeasureService;
9+
import org.springframework.context.ApplicationListener;
10+
import org.springframework.context.annotation.Profile;
11+
import org.springframework.context.event.ContextRefreshedEvent;
12+
import org.springframework.stereotype.Component;
13+
14+
import java.math.BigDecimal;
15+
16+
17+
@Slf4j
18+
@Component
19+
@Profile({"dev", "prod"})
20+
public class DevProdBootstrap implements ApplicationListener<ContextRefreshedEvent> {
21+
private final CategoryService categoryService;
22+
private final IRecipeService<Recipe> recipeService;
23+
private final UnitOfMeasureService unitOfMeasureService;
24+
25+
public DevProdBootstrap(CategoryService categoryService, RecipeService recipeService, UnitOfMeasureService unitOfMeasureService) {
26+
this.categoryService = categoryService;
27+
this.recipeService = recipeService;
28+
this.unitOfMeasureService = unitOfMeasureService;
29+
}
30+
31+
@Override
32+
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
33+
if (categoryService.count() == 0L) {
34+
log.debug("Loading Categories");
35+
loadCategories();
36+
}
37+
if (unitOfMeasureService.count() == 0L) {
38+
log.debug("Loading UOMs");
39+
loadUom();
40+
}
41+
if (recipeService.count() == 0L) {
42+
log.debug("Loading recipes");
43+
loadRecipes();
44+
}
45+
}
46+
47+
private void loadCategories() {
48+
Category cat1 = new Category();
49+
cat1.setDescription("American");
50+
categoryService.save(cat1);
51+
52+
Category cat2 = new Category();
53+
cat2.setDescription("Italian");
54+
categoryService.save(cat2);
55+
56+
Category cat3 = new Category();
57+
cat3.setDescription("Mexican");
58+
categoryService.save(cat3);
59+
60+
Category cat4 = new Category();
61+
cat4.setDescription("Fast Food");
62+
categoryService.save(cat4);
63+
}
64+
65+
private void loadUom() {
66+
UnitOfMeasure uom1 = new UnitOfMeasure();
67+
uom1.setDescription("Teaspoon");
68+
unitOfMeasureService.save(uom1);
69+
70+
UnitOfMeasure uom2 = new UnitOfMeasure();
71+
uom2.setDescription("Tablespoon");
72+
unitOfMeasureService.save(uom2);
73+
74+
UnitOfMeasure uom3 = new UnitOfMeasure();
75+
uom3.setDescription("Cup");
76+
unitOfMeasureService.save(uom3);
77+
78+
UnitOfMeasure uom4 = new UnitOfMeasure();
79+
uom4.setDescription("Pinch");
80+
unitOfMeasureService.save(uom4);
81+
82+
UnitOfMeasure uom5 = new UnitOfMeasure();
83+
uom5.setDescription("Ounce");
84+
unitOfMeasureService.save(uom5);
85+
86+
UnitOfMeasure uom6 = new UnitOfMeasure();
87+
uom6.setDescription("Each");
88+
unitOfMeasureService.save(uom6);
89+
90+
UnitOfMeasure uom7 = new UnitOfMeasure();
91+
uom7.setDescription("Pint");
92+
unitOfMeasureService.save(uom7);
93+
94+
UnitOfMeasure uom8 = new UnitOfMeasure();
95+
uom8.setDescription("Dash");
96+
unitOfMeasureService.save(uom8);
97+
}
98+
99+
@SuppressWarnings("Duplicates")
100+
private void loadRecipes(){
101+
UnitOfMeasure each = unitOfMeasureService.findByDescription("Each").orElse(null);
102+
UnitOfMeasure tablespoon = unitOfMeasureService.findByDescription("Tablespoon").orElse(null);
103+
UnitOfMeasure teaspoon = unitOfMeasureService.findByDescription("Teaspoon").orElse(null);
104+
UnitOfMeasure dash = unitOfMeasureService.findByDescription("Dash").orElse(null);
105+
UnitOfMeasure pint = unitOfMeasureService.findByDescription("Pint").orElse(null);
106+
UnitOfMeasure cup = unitOfMeasureService.findByDescription("Cup").orElse(null);
107+
108+
//guacamole recipe
109+
Recipe guacamole = new Recipe();
110+
guacamole.setDescription("Perfect Guacamole");
111+
guacamole.setPrepTime(10);
112+
guacamole.setCookTime(0);
113+
guacamole.setDifficulty(Difficulty.EASY);
114+
guacamole.setDirections("1 Cut avocado, remove flesh: Cut the avocados in half. Remove seed. Score the inside of the avocado with a blunt knife and scoop out the flesh with a spoon\n" +
115+
"2 Mash with a fork: Using a fork, roughly mash the avocado. (Don't overdo it! The guacamole should be a little chunky.)\n" +
116+
"3 Add salt, lime juice, and the rest: Sprinkle with salt and lime (or lemon) juice. The acid in the lime juice will provide some balance to the richness of the avocado and will help delay the avocados from turning brown.\n" +
117+
"Add the chopped onion, cilantro, black pepper, and chiles. Chili peppers vary individually in their hotness. So, start with a half of one chili pepper and add to the guacamole to your desired degree of hotness.\n" +
118+
"Remember that much of this is done to taste because of the variability in the fresh ingredients. Start with this recipe and adjust to your taste.\n" +
119+
"4 Cover with plastic and chill to store: Place plastic wrap on the surface of the guacamole cover it and to prevent air reaching it. (The oxygen in the air causes oxidation which will turn the guacamole brown.) Refrigerate until ready to serve.\n" +
120+
"Chilling tomatoes hurts their flavor, so if you want to add chopped tomato to your guacamole, add it just before serving.");
121+
guacamole.setNotes(new Notes("For a very quick guacamole just take a 1/4 cup of salsa and mix it in with your mashed avocados.\n" +
122+
"Feel free to experiment! One classic Mexican guacamole has pomegranate seeds and chunks of peaches in it (a Diana Kennedy favorite). Try guacamole with added pineapple, mango, or strawberries.\n" +
123+
"The simplest version of guacamole is just mashed avocados with salt. Don't let the lack of availability of other ingredients stop you from making guacamole.\n" +
124+
"To extend a limited supply of avocados, add either sour cream or cottage cheese to your guacamole dip. Purists may be horrified, but so what? It tastes great."));
125+
guacamole.setUrl("http://www.simplyrecipes.com/recipes/perfect_guacamole/");
126+
guacamole.setServings(4);
127+
guacamole.setSource("Simply recipes");
128+
129+
//add ingredients
130+
guacamole.addIngredient(new Ingredient("ripe avocados", new BigDecimal(2), each));
131+
guacamole.addIngredient(new Ingredient("Kosher salt", new BigDecimal(".5"), teaspoon));
132+
guacamole.addIngredient(new Ingredient("fresh lime juice or lemon juice", new BigDecimal(2), tablespoon));
133+
guacamole.addIngredient(new Ingredient("minced red onion or thinly sliced green onion", new BigDecimal(2), tablespoon));
134+
guacamole.addIngredient(new Ingredient("serrano chiles, stems and seeds removed, minced", new BigDecimal(2), each));
135+
guacamole.addIngredient(new Ingredient("Cilantro", new BigDecimal(2), tablespoon));
136+
guacamole.addIngredient(new Ingredient("freshly grated black pepper", new BigDecimal(2), dash));
137+
guacamole.addIngredient(new Ingredient("ripe tomato, seeds and pulp removed, chopped", new BigDecimal(".5"), each));
138+
139+
//add categories
140+
guacamole.addCategory(categoryService.findByDescription("American").orElse(null));
141+
guacamole.addCategory(categoryService.findByDescription("Mexican").orElse(null));
142+
143+
recipeService.save(guacamole);
144+
145+
//Tacos recipe
146+
Recipe tacos = new Recipe();
147+
tacos.setDescription("Spicy Grilled Chicken Taco");
148+
tacos.setCookTime(9);
149+
tacos.setPrepTime(20);
150+
tacos.setDifficulty(Difficulty.MODERATE);
151+
tacos.setDirections("1 Prepare a gas or charcoal grill for medium-high, direct heat.\n" +
152+
"2 Make the marinade and coat the chicken: In a large bowl, stir together the chili powder, oregano, cumin, sugar, salt, garlic and orange zest. Stir in the orange juice and olive oil to make a loose paste. Add the chicken to the bowl and toss to coat all over.\n" +
153+
"Set aside to marinate while the grill heats and you prepare the rest of the toppings.\n\n\n" +
154+
"3 Grill the chicken: Grill the chicken for 3 to 4 minutes per side, or until a thermometer inserted into the thickest part of the meat registers 165F. Transfer to a plate and rest for 5 minutes.\n" +
155+
"4 Warm the tortillas: Place each tortilla on the grill or on a hot, dry skillet over medium-high heat. As soon as you see pockets of the air start to puff up in the tortilla, turn it with tongs and heat for a few seconds on the other side.\n" +
156+
"Wrap warmed tortillas in a tea towel to keep them warm until serving.\n" +
157+
"5 Assemble the tacos: Slice the chicken into strips. On each tortilla, place a small handful of arugula. Top with chicken slices, sliced avocado, radishes, tomatoes, and onion slices. Drizzle with the thinned sour cream. Serve with lime wedges.");
158+
tacos.setNotes(new Notes("We have a family motto and it is this: Everything goes better in a tortilla.\n" +
159+
"Any and every kind of leftover can go inside a warm tortilla, usually with a healthy dose of pickled jalapenos. I can always sniff out a late-night snacker when the aroma of tortillas heating in a hot pan on the stove comes wafting through the house.\n" +
160+
"Today’s tacos are more purposeful – a deliberate meal instead of a secretive midnight snack!\n" +
161+
"First, I marinate the chicken briefly in a spicy paste of ancho chile powder, oregano, cumin, and sweet orange juice while the grill is heating. You can also use this time to prepare the taco toppings.\n" +
162+
"Grill the chicken, then let it rest while you warm the tortillas. Now you are ready to assemble the tacos and dig in. The whole meal comes together in about 30 minutes!"));
163+
tacos.setUrl("http://www.simplyrecipes.com/recipes/spicy_grilled_chicken_tacos/");
164+
tacos.setServings(4);
165+
tacos.setSource("Simply recipes");
166+
167+
//add ingredients
168+
tacos.addIngredient(new Ingredient("Ancho Chili Powder", new BigDecimal(2), tablespoon));
169+
tacos.addIngredient(new Ingredient("Dried Oregano", new BigDecimal(1), teaspoon));
170+
tacos.addIngredient(new Ingredient("Dried Cumin", new BigDecimal(1), teaspoon));
171+
tacos.addIngredient(new Ingredient("Sugar", new BigDecimal(1), teaspoon));
172+
tacos.addIngredient(new Ingredient("Salt", new BigDecimal(".5"), teaspoon));
173+
tacos.addIngredient(new Ingredient("Clove of Garlic, Choppedr", new BigDecimal(1), each));
174+
tacos.addIngredient(new Ingredient("finely grated orange zestr", new BigDecimal(1), tablespoon));
175+
tacos.addIngredient(new Ingredient("fresh-squeezed orange juice", new BigDecimal(3), tablespoon));
176+
tacos.addIngredient(new Ingredient("Olive Oil", new BigDecimal(2), tablespoon));
177+
tacos.addIngredient(new Ingredient("boneless chicken thighs", new BigDecimal(4), tablespoon));
178+
tacos.addIngredient(new Ingredient("small corn tortillasr", new BigDecimal(8), each));
179+
tacos.addIngredient(new Ingredient("packed baby arugula", new BigDecimal(3), cup));
180+
tacos.addIngredient(new Ingredient("medium ripe avocados, slic", new BigDecimal(2), each));
181+
tacos.addIngredient(new Ingredient("radishes, thinly sliced", new BigDecimal(4), each));
182+
tacos.addIngredient(new Ingredient("cherry tomatoes, halved", new BigDecimal(".5"), pint));
183+
tacos.addIngredient(new Ingredient("red onion, thinly sliced", new BigDecimal(".25"), each));
184+
tacos.addIngredient(new Ingredient("Roughly chopped cilantro", new BigDecimal(4), each));
185+
tacos.addIngredient(new Ingredient("cup sour cream thinned with 1/4 cup milk", new BigDecimal(4), cup));
186+
tacos.addIngredient(new Ingredient("lime, cut into wedges", new BigDecimal(4), each));
187+
188+
//add categories
189+
tacos.addCategory(categoryService.findByDescription("American").orElse(null));
190+
tacos.addCategory(categoryService.findByDescription("Mexican").orElse(null));
191+
192+
recipeService.save(tacos);
193+
}
194+
}

src/main/java/net/joedoe/recipe/services/CategoryService.java

+8
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@ public Set<Category> findAll() {
2929
repository.findAll().iterator().forEachRemaining(categories::add);
3030
return categories;
3131
}
32+
33+
public Long count() {
34+
return repository.count();
35+
}
36+
37+
public void save(Category cat) {
38+
repository.save(cat);
39+
}
3240
}

src/main/java/net/joedoe/recipe/services/IRecipeService.java

+2
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ public interface IRecipeService<T> {
1616
RecipeCommand saveRecipeCommand(RecipeCommand testRecipeCommand);
1717

1818
void deleteById(Long id);
19+
20+
Long count();
1921
}

src/main/java/net/joedoe/recipe/services/IUnitOfMeasureService.java

+4
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ public interface IUnitOfMeasureService {
1111
Optional<UnitOfMeasure> findByDescription(String description);
1212

1313
Set<UnitOfMeasureCommand> listAllUoms();
14+
15+
Long count();
16+
17+
void save(UnitOfMeasure uom);
1418
}

src/main/java/net/joedoe/recipe/services/RecipeService.java

+5
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,9 @@ public RecipeCommand saveRecipeCommand(RecipeCommand command) {
6767
public void deleteById(Long id) {
6868
repository.deleteById(id);
6969
}
70+
71+
@Override
72+
public Long count() {
73+
return repository.count();
74+
}
7075
}

src/main/java/net/joedoe/recipe/services/UnitOfMeasureService.java

+10
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,14 @@ public Set<UnitOfMeasureCommand> listAllUoms() {
3434
.map(unitOfMeasureToUnitOfMeasureCommand::convert)
3535
.collect(Collectors.toSet());
3636
}
37+
38+
@Override
39+
public Long count() {
40+
return unitOfMeasureRepository.count();
41+
}
42+
43+
@Override
44+
public void save(UnitOfMeasure uom) {
45+
unitOfMeasureRepository.save(uom);
46+
}
3747
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#refers to data-h2.sql
2+
spring.datasource.platform=h2
3+
spring.jpa.show-sql=true
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
spring:
2+
datasource:
3+
url: jdbc:mysql://localhost:3306/joe_dev
4+
username: joe_dev
5+
password: doe
6+
platform: mysql
7+
jpa:
8+
hibernate:
9+
ddl-auto: validate
10+
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
11+
database: mysql
12+
show-sql: true
13+
# to generate the db schema as joe-database-create-sql in root-folder
14+
# and use commands in sql to generate corresponding tables (one of multiple methods)
15+
# properties:
16+
# javax:
17+
# persistence:
18+
# schema-generation:
19+
# create-source: metadata
20+
# scripts:
21+
# action: create
22+
# create-target: create-mysql-db.sql
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
spring:
2+
datasource:
3+
url: jdbc:mysql://localhost:3306/joe_prod
4+
username: joe_prod
5+
password: doe
6+
platform: mysql
7+
jpa:
8+
hibernate:
9+
ddl-auto: validate
10+
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
11+
database: mysql
12+
show-sql: false
File renamed without changes.
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- docker run --name mysqldb -p 3306:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -d mysql
2+
3+
-- Create db
4+
-- CREATE DATABASE joe_dev;
5+
-- CREATE DATABASE joe_prod;
6+
7+
-- Create service accounts using wildcard for host
8+
-- CREATE USER 'joe_dev'@'%' IDENTIFIED BY 'doe';
9+
-- CREATE USER 'joe_prod'@'%' IDENTIFIED BY 'doe';
10+
11+
-- Grants for users accessing their corresponding db
12+
-- GRANT SELECT, INSERT, DELETE, UPDATE ON joe_dev.* TO 'joe_dev'@'%';
13+
-- GRANT SELECT, INSERT, DELETE, UPDATE ON joe_prod.* TO 'joe_prod'@'%';

0 commit comments

Comments
 (0)