Spring Boot used JdbcTemplate

Personally feel JdbcTemplate compared to MyBaits, Hibernate and other frameworks easier to use the database, SQL operations are also more intuitive and convenient, so the project is also a good choice. Open in Spring Boot JdbcTemplate very simple, only need to introduce the spring-boot-starter-jdbc dependence can be. JdbcTemplate encapsulates many SQL operations, specifically refer to the official documentation https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html .

The introduction of dependence

spring-boot-starter-jdbc:

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

 

Druid database driver is employed ojdbc6, the data source. Specific reference https://mrbird.cc/Spring-Boot%E4%B8%AD%E4%BD%BF%E7%94%A8Mybatis.html .

Coding

data preparation:

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 ');

 

Here the main demonstration in Dao implementation class JdbcTemplate, so do not write other modules of code shows, specifically the end of the reference source text.

StudentDaoImp ​​Class Code:

@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;
      }
  }
}

 

The introduction of spring-boot-starter-jdbc rear drive, JdbcTemplate may be injected directly in the class. Can be found by the above codes, the save operation for the two different methods, in the case where the inserted table fields more recommended the NamedParameterJdbcTemplate .

For return results, you can directly use List <Map <String, Object >> to receive, which is the recommended way to use personally, after all, relatively simple and convenient; can also be received using a database table corresponding entity object, but this time we need to manually create a realization org.springframework.jdbc.core.RowMapper object for an entity object properties and database table fields correspondence:

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;
  }
}

 

test

The final project directory as shown below:

Start the project, test data is inserted HTTP: // localhost:? 8080 / Web / addStudent SnO = 004 & name = & Maria Sex = F :

All student data query HTTP: // localhost: 8080 / Web / queryallstudent :

Test delete HTTP: // localhost: 8080 / Web / deletestudent SnO = 004? :

Guess you like

Origin www.cnblogs.com/7788IT/p/11626862.html