benefit from spring data

  1. Benefits of Using spring data

Reduces boilerplate code

Example: traditional JDBM connnector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public Room getRoomJDBC() throws Exception{
Statement statement = null;
Connection connection = null;
try {
class.forName('org.h2.Driver');
connection = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
String sql = "SELECT * from ROOM where ROOM_NUMBER = 'p1'"
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
Room room = new Room();
room.setId(resultSet.getLong("ROOM_ID"));
room.setBedInfo(resultSet.getString("BED_INFO"));
room.setName(resultSet.getString('ROOM_NAME'));
room.setNumber(resultSet.getString("ROOM_NUMBER"));
return room;
}
}catch (SQLException sqle){
return null;
}finally{
if(statement!=null){
statement.close();
}
if(null != connenction){
connection.close();
}
}
return null;
}

Instead, with Spring Data, it could be just one line of code:

1
2
3
public Room getRoomSpringData(){
return this.repository.findByNumber("PI");
}

reduce a lots of code and time, and reduce the posibility of making error when

writing the 20 lines of code.

Providing ability to swap out datasources much easier

Always focus on the business logic, not on the data access code.

Key Component

Entity