Created
May 8, 2020 10:28
-
-
Save soscler/043a91b6dba786d1a58793d903e6115a to your computer and use it in GitHub Desktop.
jdbc with spring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Timestamp; | |
import java.sql.Types; | |
import java.util.Arrays; | |
import java.util.Date; | |
import org.springframework.jdbc.core.JdbcTemplate; | |
import org.springframework.jdbc.core.PreparedStatementCreator; | |
import org.springframework.jdbc.core.PreparedStatementCreatorFactory; | |
import org.springframework.jdbc.support.GeneratedKeyHolder; | |
import org.springframework.jdbc.support.KeyHolder; | |
import org.springframework.stereotype.Repository; | |
import tacos.Ingredient; | |
import tacos.Taco; | |
@Repository | |
public class JdbcTacoRepository implements TacoRepository { | |
private JdbcTemplate jdbc; | |
public JdbcTacoRepository(JdbcTemplate jdbc) { | |
this.jdbc = jdbc; | |
} | |
@Override | |
public Taco save(Taco taco) { | |
long tacoId = saveTacoInfo(taco); | |
taco.setId(tacoId); | |
for (Ingredient ingredient : taco.getIngredients()) { | |
saveIngredientToTaco(ingredient, tacoId); | |
} | |
return taco; | |
} | |
private long saveTacoInfo(Taco taco) { | |
taco.setCreatedAt(new Date()); | |
PreparedStatementCreator psc = | |
new PreparedStatementCreatorFactory( | |
"insert into Taco (name, createdAt) values (?, ?)", | |
Types.VARCHAR, Types.TIMESTAMP | |
).newPreparedStatementCreator( | |
Arrays.asList( | |
taco.getName(), | |
new Timestamp(taco.getCreatedAt().getTime()))); | |
KeyHolder keyHolder = new GeneratedKeyHolder(); | |
jdbc.update(psc, keyHolder); | |
return keyHolder.getKey().longValue(); | |
} | |
private void saveIngredientToTaco( | |
Ingredient ingredient, long tacoId) { | |
jdbc.update( | |
"insert into Taco_Ingredients (taco, ingredient) " + | |
"values (?, ?)", | |
tacoId, ingredient.getId()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment