在spring boot 集成使用jdbctemplate,首先在pom文件中引入相應的依賴java
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> <scope>runtime</scope> </dependency>
<!--整合druid數據源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
yml中配置數據源:mysql
#druid數據源 druid: username: root password: 12345 driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/corporationmanagement?useUnicode=true&characterEncoding=utf8 initialSize: 1 minIdle: 1 maxActive: 5 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
本文就來先解釋下什麼是Row Mapper,咱們該如何正確地使用。spring
@Override public Student getStudentByName(String name) { String sql = "select name, gender from test_student where name = ?"; Student student = this.jdbcTemplate.queryForObject(sql, new Object[]{name}, new RowMapper<Student>() { @Override public Student mapRow(ResultSet rs, int i) throws SQLException { Student s = new Student(); s.setName(rs.getString("name")); s.setGender(rs.getString("gender")); return s; } }); return student; } @Override public List<Student> getStudentsByName(String name) { String sql = "select name, gender from test_student where name = ?"; List<Student> students = this.jdbcTemplate.query(sql, new Object[]{name}, new RowMapper<Student>() { @Override public Student mapRow(ResultSet rs, int i) throws SQLException { Student s = new Student(); s.setName(rs.getString("name")); s.setGender(rs.getString("gender")); return s; } }); return students; }
使用過原生JDBC的朋友應該知道,從數據庫查詢出來的記錄全都被保存在ResultSet結果集中,咱們須要將結果集中的數據一條條地獲取並設置到具體的實體類上,如此,該實體類才能在接下來的程序中使用。然而問題是,每次都要這麼操做實在是太麻煩了,Spring就不該該提供什麼功能來替咱們作這些事情嗎?答案固然是有的,那就是本文的主角——RowMapper。sql
Spring JDBC中目前有兩個主要的RowMapper實現,使用它們應該能解決大部分的場景了。數據庫
經過名字咱們就能大概瞭解,在查詢返回單列數據的時候,就該使用這個RowMapper,下面咱們來看看具體的代碼:app
@Override public String getStudentNameById(String id) { String sql = "select name from test_student where id = ?"; return this.jdbcTemplate.queryForObject(sql, new Object[]{id}, new SingleColumnRowMapper<>(String.class)); } @Override public List<String> getStudentNamesByGrade(Integer grade) { String sql = "select name from test_student where grade = ?"; return this.jdbcTemplate.query(sql, new Object[]{grade}, new SingleColumnRowMapper<>(String.class)); } @Test public void getStudentNameById(){ String name = studentService.getStudentNameById("3"); assertEquals("李清照", name); } @Test public void getStudentNamesByGrade(){ List<String> names = studentService.getStudentNamesByGrade(1); assertTrue(2 == names.size()); }
當查詢數據庫返回的是多列數據,且你須要將這些多列數據映射到某個具體的實體類上,那麼就該使用這個Row Mapper,下面是具體的使用代碼:ide
@Override public Student getStudentByName2(String name) { String sql = "select name, gender from test_student where name = ?"; return this.jdbcTemplate.queryForObject(sql, new Object[]{name}, new BeanPropertyRowMapper<>(Student.class)); } @Override public List<Student> getStudentsByName2(String name) { String sql = "select name, gender from test_student where name = ?"; return this.jdbcTemplate.query(sql, new Object[]{name}, new BeanPropertyRowMapper<>(Student.class)); } @Test public void getStudentByName2(){ Student student = studentService.getStudentByName2("杜甫"); assertEquals("杜甫",student.getName()); assertEquals("男",student.getGender()); } @Test public void getStudentsByName2(){ List<Student> studentList = studentService.getStudentsByName("Jack"); assertTrue(2 == studentList.size()); }
這種使用方式有一個前提,那就是數據庫SQL查出來的數據其列名與實體類中的屬性名是一致的,固然個數和順序能夠不一致。好比數據庫SQL查出來的姓名列叫name,那麼對應的實體類中的姓名也必須叫name,而不能叫studentName或者其它。spring-boot
固然,若是你SQL查詢出來的數據列名就是和實體類的屬性名不同,或者想按照本身的規則來裝配實體類,那麼就能夠定義並使用本身的Row Mapper。ui
public class StudentRowMapper implements RowMapper<Student> { @Override public Student mapRow(ResultSet rs, int i) throws SQLException { Student student = new Student(); student.setName(rs.getString("name")); student.setGender(rs.getString("gender")); student.setEmail(rs.getString("email")); return student; } } @Override public Student getStudentByName3(String name) { String sql = "select name, gender, email from test_student where name = ?"; return this.jdbcTemplate.queryForObject(sql, new Object[]{name}, new StudentRowMapper()); } @Override public List<Student> getStudentsByName3(String name) { String sql = "select name, gender, email from test_student where name = ?"; return this.jdbcTemplate.query(sql, new Object[]{name}, new StudentRowMapper()); } @Test public void getStudentByName3(){ Student student = studentService.getStudentByName3("杜甫"); assertEquals("杜甫",student.getName()); assertEquals("男",student.getGender()); assertEquals("dufu@tang", student.getEmail()); } @Test public void getStudentsByName3(){ List<Student> studentList = studentService.getStudentsByName3("李白"); assertTrue(2 == studentList.size()); }