JDBC and MyBatis
JDBC and MyBatis
⎆ Spring JDBC
⎆ Spring MyBatis
2
Spring JDBC
3
Spring JDBC (cont.)
Main Dependencies:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
4
Spring JDBC (cont.)
⎊ Java Configuration
@Configuration
public class JdbcConfiguration {
@Bean
public DataSource myPostgresDb() {
DataSourceBuilder dataSource = DataSourceBuilder.create();
dataSource.driverClassName("org.postgresql.Driver");
dataSource.url("jdbc:postgresql://localhost:5432/postgres");
dataSource.username("postgres");
dataSource.password("qwer");
return dataSource.build();
}
}
5
Spring JDBC (cont.)
6
Spring JDBC (cont.)
⎊ The JdbcTemplate is the main API through which you’ll access most of the functionality:
⚬ Creation and closing of connections
⚬ Executing statements and stored procedure calls
⚬ Iterating over the ResultSet and returning results
⎊ Example
⎊ Example
8
Spring JDBC (cont.)
⎊ Example
9
Spring JDBC (cont.)
⎊ Example
@Repository
public class JdbcNoteRepository implements NoteRepository {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<NoteEntity> findAll() {
return jdbcTemplate.query(
"select * from notes",
(rs, rowNum) -> new NoteEntity(
rs.getInt("id"),
rs.getString("title"),
rs.getString("desc")
)
);
}
} 10
Spring JDBC (cont.)
⎊ Example
@RestController
@RequestMapping("/db")
public class NoteController {
@GetMapping()
public List<NoteEntity> findAllNotes() {
return noteRepository.findAll();
}
}
11
Spring JDBC (cont.)
13
Spring MyBatis (cont.)
15
Spring MyBatis (cont.)
16
Spring MyBatis (cont.)
⎊ Maven Dependency
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
⎊ Quick Setup
⚬ MyBatis-Spring-Boot-Starter will:
◦ Auto detect an existing DataSource
◦ Will create and register an instance of a SqlSessionFactory passing that DataSource
as an input using the SqlSessionFactoryBean
17
Spring MyBatis (cont.)
return driverManagerDataSource;
}
}
18
Spring MyBatis (cont.)
19
Spring MyBatis (cont.)
@Repository
public interface NoteRepository {
@Select("select * from notes")
List<Note> findAll();
}
21
Spring MyBatis (cont.)
@GetMapping
public List<Note> findAll() {
return noteService.findAllNotesAndAddPrefix("KSHRD");
}
} 23
Spring MyBatis (cont.)
⎊ Logger
27
Spring MyBatis (cont.)
⎊ MyBatis Annotations
⚬ @Insert, @Select, @Update, @Delete - those annotations represent SQL statements to
be executed by calling annotated class as the previous example.
⚬ @Results - it is a list of result mappings that contain the details of how the database
columns are mapped to Java class properties and it uses @Result to map a single instance
of Result out of list of @Results.
@Select("select * from notes")
@Results({
@Result(property = "noteId", column = "note_id"),
@Result(property = "noteTitle", column = "note_title"),
@Result(property = "noteCategory", javaType = List.class)
})
List<Note> findAll();
28
Spring MyBatis (cont.)
29
Spring MyBatis (cont.)
31
Spring MyBatis (cont.)
}
33
Spring MyBatis (cont.)
35
Spring MyBatis (cont.)
System.out.println(noteRepository.selectNotesByWhere(note));
}
}
38
THANKS!
Please try hard to study..!
39