Java spring-jdbctemplate additions and deletions change

Source: Internet
Author: User

2017-11-11 21:13:13

The Spring framework provides classes for technical support for persistence layers:

    • JDBC:org.springframework.jdbc.core.support.JdbcDaoSupport
    • Hibernate 3.0:org.springframework.orm.hibernate3.support.hibernatedaosupport
    • IBatis:org.springframework.orm.ibatis.support.SqlMapClientDaoSupport

Adding, deleting, and modifying operations is still easier because there is no need for a return value.

Add, delete, and modify the code:

Userpublic class User {private Integer ID;    private String name;    Public Integer GetId () {return id;    } public void SetId (Integer id) {this.id = ID;    } public String GetName () {return name;    } public void SetName (String name) {this.name = name;  } @Override Public String toString () {return "user{" + "id=" + ID + ", name= '"    + name + ' \ ' + '} '; }}//Userdaopublic class Userdao extends Jdbcdaosupport {public void Add (user user) {String sql = ' INSERT INTO        User values (?,?) ";    Getjdbctemplate (). Update (Sql,user.getid (), User.getname ());        } public void Delete (user user) {String sql = ' Delete from User where id=? ';    Getjdbctemplate (). Update (SQL, User.getid ()); public void update (user user) {String sql = "Update user set name=?")        where id=? ";    Getjdbctemplate (). Update (SQL, User.getname (), User.getid ()); }}//Test @runwith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:config4.xml") public class JDBC3 {@Resource (name = "    Userdao ") Private Userdao Userdao;        @Test public void Demo () {User user= new User ();        User.setname ("Liu Yifei");        User.setid (001);        Userdao.add (user);        User.setname ("Hu");        Userdao.update (user);    Userdao.delete (user); }}

Finding operations is relatively cumbersome because there is usually a return value.

For the return value of the basic type, can be more convenient to convert;

For the return value is an object, a custom conversion method is required;

Query code:

Userdaopublic class Userdao extends Jdbcdaosupport {public void Add (user user) {String sql = ' INSERT INTO US        ER values (?,?) ";    Getjdbctemplate (). Update (Sql,user.getid (), User.getname ());        } public void Delete (user user) {String sql = ' Delete from User where id=? ';    Getjdbctemplate (). Update (SQL, User.getid ()); public void update (user user) {String sql = "Update user set name=?")        where id=? ";    Getjdbctemplate (). Update (SQL, User.getname (), User.getid ());        } public int Findcount () {String sql = ' SELECT COUNT (*) from user ';    Return This.getjdbctemplate (). queryForObject (Sql,integer.class);        } public String Findnamebyid (int id) {String sql = ' Select name from user where id =? ';    Return Getjdbctemplate (). queryForObject (Sql,string.class,id);        Public user Finduserbyid (int id) {String sql = ' SELECT * from User where id =? '; Return Getjdbctemplate (). queryForObject (SQL, New Myrowmap (),ID);        } public list<user> FindAll () {String sql = ' select * from User ';    Return getjdbctemplate (). Query (Sql,new myrowmap ()); }//Anonymous inner class for custom type conversions Class Myrowmap implements rowmapper<user>{/** * * @param resultse        T: Result set containing all results * @param i: line number * @return The data of each row is converted to the data type of the desired object by itself * @throws SQLException */ @Override Public User Maprow (ResultSet ResultSet, int i) throws SQLException {User user = new User            ();            User.setname (resultset.getstring ("name"));            User.setid (Resultset.getint ("id"));        return user; }}}//Query @runwith (springjunit4classrunner.class) @ContextConfiguration ("Classpath:config4.xml") public class JDBC3 {@    Resource (name = "Userdao") private Userdao Userdao;        @Test public void Demo () {User user= new User ();        User.setname ("Zhang Ziyi");        User.setid (002);    Userdao.add (user); } @Test public voidDemo2 () {System.out.println (Userdao.findcount ());        list<user> ls = Userdao.findall ();        for (user user:ls) {System.out.println (user); }    }}

Java spring-jdbctemplate additions and deletions change

Related Article

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.