在spring boot 集成使用jdbctemplate,首先在pom文件中引入相應的依賴
<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中配置數據源:
#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,我們該如何正確地使用。
@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; }
一、RowMapper的基本使用
使用過原生JDBC的朋友應該知道,從數據庫查詢出來的記錄全都被保存在ResultSet結果集中,我們需要將結果集中的數據一條條地獲取並設置到具體的實體類上,如此,該實體類才能在接下來的程序中使用。然而問題是,每次都要這么操作實在是太麻煩了,Spring就不應該提供什么功能來替我們做這些事情嗎?答案當然是有的,那就是本文的主角——RowMapper。
Spring JDBC中目前有兩個主要的RowMapper實現,使用它們應該能解決大部分的場景了。
1.1 SingleColumnRowMapper
通過名字我們就能大概了解,在查詢返回單列數據的時候,就該使用這個RowMapper,下面我們來看看具體的代碼:
@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()); }
1.2 BeanPropertyRowMapper
當查詢數據庫返回的是多列數據,且你需要將這些多列數據映射到某個具體的實體類上,那么就該使用這個Row Mapper,下面是具體的使用代碼:
@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或者其它。
二、定義自己的RowMapper
當然,如果你SQL查詢出來的數據列名就是和實體類的屬性名不一樣,或者想按照自己的規則來裝配實體類,那么就可以定義並使用自己的Row Mapper。
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()); }
作者:力羊
鏈接:https://www.jianshu.com/p/be60a81e2fe7
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。
