Continue with the previous article, Spring JdbcTemplate (i)--connection pooling to create multiple connections
Now let's take a look at how to use spring's jdbctemplate to do the incremental pruning check.
1. Increase:
public int Insert (JdbcTemplate jdbctemplate) {
String sql = ' INSERT into student (id,name,age,address) VALUES (?,?,?,?) ";
int row = jdbctemplate.update (SQL, 2, "Xiaohong", "BBB");
return row;
}
2, modify:
public int update (JdbcTemplate jdbctemplate) {
String sql = Update student set name=? where id=? ";
int row = jdbctemplate.update (sql, "Xiaoxiao", 1);
return row;
}
3, delete
public int Delete (JdbcTemplate jdbctemplate) {
String sql = ' Delete from student where id=? ';
int row = jdbctemplate.update (SQL, 2);
return row;
}
4, inquiry
Query--The query returns list<map<string,object> with no parameters
Public list<map<string, object>> query (JdbcTemplate jdbctemplate) {
String sql = ' SELECT * from student '; return
jdbctemplate.queryforlist (SQL);
Query--Query with parameters return to list<map<string,object>>
Public list<map<string, object>> querywithobject (JdbcTemplate jdbctemplate) {
String sql = ' SELECT * From student where id=? ";
return jdbctemplate.queryforlist (SQL, 1);
}
Query--Returns the custom type, list<student>
Public list<student> Queryandreturnobject (JdbcTemplate jdbctemplate) {
String sql = ' select * from Student '; c1/>list<student> students = jdbctemplate.query (SQL, new rowmapper<student> () {
@Override
public Student Maprow (ResultSet ResultSet, int i) throws SQLException {Student Student
= new Student ();
Student.setid (Resultset.getint ("id"));
Student.setaddress (resultset.getstring ("Address"));
Student.setname (resultset.getstring ("name"));
Student.setage (Resultset.getint ("Age"));
return student;
}
});
return students;
}
Query--Returns the custom type, List<studentnew>, doing rowmapper in the entity class
public class Studentnew implements Rowmapper<studentnew>, Serializable {private int id;
private String name;
private int age;
Private String address;
Public studentnew () {} public studentnew (int ID, string name, int age, string address) {this.id = ID;
THIS.name = name;
This.age = age;
this.address = address;
public int getId () {return id;
The public void setId (int id) {this.id = ID;
Public String GetName () {return name;
public void SetName (String name) {this.name = name;
public int getage () {return age;
public void Setage (int age) {this.age = age;
Public String getaddress () {return address;
public void setaddress (String address) {this.address = address; @Override public studentnew Maprow (ResultSet ResultSet, int i) throws SQLException {StUdentnew studentnew = new Studentnew ();
Studentnew.setid (Resultset.getint ("id"));
Studentnew.setage (Resultset.getint ("Age"));
Studentnew.setname (resultset.getstring ("name"));
Studentnew.setaddress (resultset.getstring ("Address"));
return studentnew; }
}
Public list<studentnew> QueryAndReturnObject2 (JdbcTemplate jdbctemplate) {
String sql = ' SELECT * FROM student ";
list<studentnew> studentnews = jdbctemplate.query (sql, New Studentnew ());
return studentnews;
}
Query--return list<studentnew> with conditional query
Public list<studentnew> queryAndReturnObject3 (JdbcTemplate jdbctemplate) {
String sql = ' SELECT * FROM student WHERE id =? ";
list<studentnew> studentnews = jdbctemplate.query (sql, New Studentnew (), 1);
return studentnews;
}
In summary, it is in spring jdbctemplate how to make the increase of the check.