Spring JDBC: Difference between revisions
From My Limbic Wiki
(Page créée avec « Spring Jdbc Abstraction Layer. The Spring-JDBC component is a part of the Spring framework and is an abstraction on top of the standard Java JDBC API. It takes care of all... ») |
No edit summary |
||
Line 4: | Line 4: | ||
public class CandyDataService{ | public class CandyDataService{ | ||
private static final String INSERT_QUERY = "INSERT INTO CANDY(param1,param2) VALUES(?,?)"; | private static final String '''INSERT_QUERY''' = "INSERT INTO CANDY(param1,param2) VALUES(?,?)"; | ||
private static final String DELETE_QUERY = "DELETE FROM CANDY WHERE ID=?"; | private static final String '''DELETE_QUERY''' = "DELETE FROM CANDY WHERE ID=?"; | ||
private static final String SELECT_ALL_QUERY = "SELECT * FROM CANDY"; | private static final String '''SELECT_ALL_QUERY''' = "SELECT * FROM CANDY"; | ||
//Local database declarated, just for this examble | '''//Local database declarated, just for this examble''' | ||
HsqlDatabase db = new HsqlDatabase(); | HsqlDatabase db = new HsqlDatabase(); | ||
//Database connection | '''//Database connection''' | ||
JdbcTemplate jdbcTemplate = new JdbcTemplate( | JdbcTemplate jdbcTemplate = new JdbcTemplate( | ||
new SingleConnectionDataSource(db.conn, false)); | new SingleConnectionDataSource(db.conn, false)); | ||
//Insert | '''//Insert''' | ||
private void insertCandy(Candy candy) { | private void insertCandy(Candy candy) { | ||
jdbcTemplate.update(INSERT_QUERY, param1, param2, param...); | jdbcTemplate.update(INSERT_QUERY, param1, param2, param...); | ||
} | } | ||
//Multi-Inserts | '''//Multi-Inserts''' | ||
public void insertCandies(List<Candy> candies) { | public void insertCandies(List<Candy> candies) { | ||
for (Candy candy : candies) { | for (Candy candy : candies) { | ||
Line 27: | Line 27: | ||
} | } | ||
//Delete Method | '''//Delete Method''' | ||
public void deleteCandy(int id) { | public void deleteCandy(int id) { | ||
jdbcTemplate.update(DELETE_QUERY, id); | jdbcTemplate.update(DELETE_QUERY, id); | ||
} | } | ||
//Get all candies with Auto-Mapping | '''//Get all candies with Auto-Mapping''' | ||
public List<Candy> retrieveAllCandies() throws SQLException{ | public List<Candy> retrieveAllCandies() throws SQLException{ | ||
jdbcTemplate.query(SELECT_ALL_QUERY, | jdbcTemplate.query(SELECT_ALL_QUERY, | ||
Line 38: | Line 38: | ||
} | } | ||
//Get all candies with Custom-Mapping | '''//Get all candies with Custom-Mapping''' | ||
public List<Candy> retrieveAllCandies() throws SQLException{ | public List<Candy> retrieveAllCandies() throws SQLException{ | ||
jdbcTemplate.query(SELECT_ALL_QUERY, new RowMapper<Candy>(){ | jdbcTemplate.query(SELECT_ALL_QUERY, new RowMapper<Candy>(){ | ||
public Candy mapRow(ResultSet rs, int rowNum) throws SQLException{ | public Candy mapRow(ResultSet rs, int rowNum) throws SQLException{ | ||
//OR: Solution 1 | '''//OR: Solution 1''' | ||
//Take the results set and set into the Candy object | //Take the results set and set into the Candy object | ||
return new Candy(rs.getInt(1), rs.getString(2), rs.getBoolean(3),...); | return new Candy(rs.getInt(1), rs.getString(2), rs.getBoolean(3),...); | ||
//OR: Solution 2 | '''//OR: Solution 2''' | ||
//Using encapsulation | //Using encapsulation | ||
Candy candy = new Candy(); | Candy candy = new Candy(); | ||
Line 55: | Line 55: | ||
} | } | ||
} | } | ||
//OR: Solution 3 | '''//OR: Solution 3''' | ||
new CandyMapper()); | new CandyMapper()); | ||
} | } | ||
Line 61: | Line 61: | ||
/** | /** | ||
* Create a Mapper to help organizing the code in Custom-Mapping method | * '''Create a Mapper to help organizing the code''' in Custom-Mapping method | ||
* So it becomes reusable | * So it becomes reusable | ||
* Can also be declared as "Public class" and move it to a different Package | * Can also be declared as "Public class" and move it to a different Package |
Revision as of 22:08, 20 September 2019
Spring Jdbc Abstraction Layer. The Spring-JDBC component is a part of the Spring framework and is an abstraction on top of the standard Java JDBC API. It takes care of all the low-level API-calls and
- Online Compiler: http://tpcg.io/1kfiQn
//lets imagine a case with an object named "Candy" public class CandyDataService{ private static final String INSERT_QUERY = "INSERT INTO CANDY(param1,param2) VALUES(?,?)"; private static final String DELETE_QUERY = "DELETE FROM CANDY WHERE ID=?"; private static final String SELECT_ALL_QUERY = "SELECT * FROM CANDY"; //Local database declarated, just for this examble HsqlDatabase db = new HsqlDatabase(); //Database connection JdbcTemplate jdbcTemplate = new JdbcTemplate( new SingleConnectionDataSource(db.conn, false)); //Insert private void insertCandy(Candy candy) { jdbcTemplate.update(INSERT_QUERY, param1, param2, param...); } //Multi-Inserts public void insertCandies(List<Candy> candies) { for (Candy candy : candies) { insertCandy(candy); } } //Delete Method public void deleteCandy(int id) { jdbcTemplate.update(DELETE_QUERY, id); } //Get all candies with Auto-Mapping public List<Candy> retrieveAllCandies() throws SQLException{ jdbcTemplate.query(SELECT_ALL_QUERY, new BeanPropertyRowMapper<Candy>(Candy.class)); } //Get all candies with Custom-Mapping public List<Candy> retrieveAllCandies() throws SQLException{ jdbcTemplate.query(SELECT_ALL_QUERY, new RowMapper<Candy>(){ public Candy mapRow(ResultSet rs, int rowNum) throws SQLException{ //OR: Solution 1 //Take the results set and set into the Candy object return new Candy(rs.getInt(1), rs.getString(2), rs.getBoolean(3),...); //OR: Solution 2 //Using encapsulation Candy candy = new Candy(); candy.setParam1(rs.getInt(1)); candy.setParam1(rs.getString(2)); candy.setParam3(rs.getBoolean(3)); return candy; } } //OR: Solution 3 new CandyMapper()); } } /** * Create a Mapper to help organizing the code in Custom-Mapping method * So it becomes reusable * Can also be declared as "Public class" and move it to a different Package */ class CandyMapper implements RowMapper<Candy>{ public Candy mapRow(ResultSet rs, int rowNum) throws SQLException{ Candy candy = new Candy(); candy.setParam1(rs.getInt(1)); candy.setParam1(rs.getString(2)); candy.setParam3(rs.getBoolean(3)); return candy; } }