Spring JdbcTemplate used in spring boot (ii)--adding and removing changes to crud

Source: Internet
Author: User
Tags connection pooling

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&gt, 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.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.