Spring Boot中使用JdbcTemplate

我的以爲JdbcTemplate相較於MyBaits,Hibernate等數據庫框架更容易上手,對SQL的操做也更爲直觀方便,因此在項目中也是一個不錯的選擇。在Spring Boot開啓JdbcTemplate很簡單,只須要引入spring-boot-starter-jdbc依賴便可。JdbcTemplate封裝了許多SQL操做,具體可查閱官方文檔https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.htmlhtml

引入依賴

spring-boot-starter-jdbc:java

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

 

數據庫驅動爲ojdbc6,數據源採用Druid。具體可參考https://mrbird.cc/Spring-Boot%E4%B8%AD%E4%BD%BF%E7%94%A8Mybatis.htmlweb

代碼編寫

數據準備:spring

CREATE TABLE "SCOTT"."STUDENT" (
  "SNO" VARCHAR2(3 BYTE) NOT NULL ,
  "SNAME" VARCHAR2(9 BYTE) NOT NULL ,
  "SSEX" CHAR(2 BYTE) NOT NULL
);

INSERT INTO "SCOTT"."STUDENT" VALUES ('001', 'KangKang', 'M ');
INSERT INTO "SCOTT"."STUDENT" VALUES ('002', 'Mike', 'M ');
INSERT INTO "SCOTT"."STUDENT" VALUES ('003', 'Jane', 'F ');

 

這裏主要演示在Dao的實現類裏使用JdbcTemplate,因此其它模塊代碼的編寫就不展現了,具體可參考文末的源碼。sql

StudentDaoImp類代碼:數據庫

@Repository("studentDao")
public class StudentDaoImp implements StudentDao {

  @Autowired
  private JdbcTemplate jdbcTemplate;
   
  @Override
  public int add(Student student) {
      // String sql = "insert into student(sno,sname,ssex) values(?,?,?)";
      // Object[] args = { student.getSno(), student.getName(), student.getSex() };
      // int[] argTypes = { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR };
      // return this.jdbcTemplate.update(sql, args, argTypes);
      String sql = "insert into student(sno,sname,ssex) values(:sno,:name,:sex)";
      NamedParameterJdbcTemplate npjt = new NamedParameterJdbcTemplate(this.jdbcTemplate.getDataSource());
      return npjt.update(sql, new BeanPropertySqlParameterSource(student));
  }
   
  @Override
  public int update(Student student) {
      String sql = "update student set sname = ?,ssex = ? where sno = ?";
      Object[] args = { student.getName(), student.getSex(), student.getSno() };
      int[] argTypes = { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR };
      return this.jdbcTemplate.update(sql, args, argTypes);
  }
   
  @Override
  public int deleteBysno(String sno) {
      String sql = "delete from student where sno = ?";
      Object[] args = { sno };
      int[] argTypes = { Types.VARCHAR };
      return this.jdbcTemplate.update(sql, args, argTypes);
  }
   
  @Override
  public List<Map<String, Object>> queryStudentsListMap() {
      String sql = "select * from student";
      return this.jdbcTemplate.queryForList(sql);
  }
   
  @Override
  public Student queryStudentBySno(String sno) {
      String sql = "select * from student where sno = ?";
      Object[] args = { sno };
      int[] argTypes = { Types.VARCHAR };
      List<Student> studentList = this.jdbcTemplate.query(sql, args, argTypes, new StudentMapper());
      if (studentList != null && studentList.size() > 0) {
          return studentList.get(0);
      } else {
          return null;
      }
  }
}

 

在引入spring-boot-starter-jdbc驅動後,可直接在類中注入JdbcTemplate。由上面代碼可發現,對於保存操做有兩種不一樣的方法,當插入的表字段較多的狀況下,推薦使用NamedParameterJdbcTemplateapi

對於返回結果,能夠直接使用List<Map<String, Object>>來接收,這也是我的比較推薦使用的方式,畢竟比較簡單方便;也能夠使用庫表對應的實體對象來接收,不過這時候咱們就須要手動建立一個實現了org.springframework.jdbc.core.RowMapper的對象,用於將實體對象屬性和庫表字段一一對應:app

public class StudentMapper implements RowMapper<Student>{
  @Override
  public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
      Student student = new Student();
      student.setSno(rs.getString("sno"));
      student.setName(rs.getString("sname"));
      student.setSex(rs.getString("ssex"));
      return student;
  }
}

 

測試

最終項目目錄以下圖所示:框架

啓動項目,測試插入數據http://localhost:8080/web/addstudent?sno=004&name=Maria&sex=Fide

查詢全部學生數據http://localhost:8080/web/queryallstudent:

測試刪除http://localhost:8080/web/deletestudent?sno=004

相關文章
相關標籤/搜索