spring--spring Integrated JDBC,

Source: Internet
Author: User

Spring Integrated JDBC

Spring provides many templates to integrate DAO technology

The JdbcTemplate encapsulates the JDBC technology, which is very similar to Queryrunner in Dbutils.

To combine steps with the Web:

1. Guide Package

4+2
Spring-test
Spring-aop
JUNIT4 Class Library
C3P0 Connection Pool
JDBC Driver
Spring-jdbc
SPRING-TX transactions

2. Writing DAO

How to get JdbcTemplate objects

Way One:

Inherit the Jdbcdaosupport class, using Super.getjdbctemplate () to get
public class Userdaoimpl extends Jdbcdaosupport implements userdao{//private jdbctemplate jt;//public void Setjt (J Dbctemplate JT) {//THIS.JT = jt;//} @Override public void Save (User u) {String sql = "Insert int o User values (null,?)        ";    Super.getjdbctemplate (). Update (Sql,u.getname ());        } @Override public void Delete (Integer ID) {String sql = ' Delete from user where id=? ';    Super.getjdbctemplate (). Update (SQL,ID); } @Override public void update (user u) {String sql = "Update user set name=?")        where id=? ";    Super.getjdbctemplate (). Update (Sql,u.getname (), U.getid ());        } @Override Public user getById (Integer ID) {String sql = ' SELECT * from User where id=? '; User u = super.getjdbctemplate (). queryForObject (SQL, new rowmapper<user> () {@Override public               User Maprow (ResultSet ResultSet, int i) throws SQLException {User U = new User (); U.setid (Resultset.getint ("id"));                U.setname (resultset.getstring ("name"));            return u;        }}, id);    return u;        } @Override public int gettotalcount () {String sql = ' SELECT COUNT (*) from user ';        Integer count = Super.getjdbctemplate (). queryForObject (SQL, Integer.class);    return count;        } @Override Public list<user> getAll () {String sql = ' select * from User '; list<user> list = Super.getjdbctemplate (). query (SQL, new rowmapper<user> () {@Override P                Ublic user Maprow (ResultSet ResultSet, int i) throws SQLException {User U = new user ();                U.setid (Resultset.getint ("id"));                U.setname (resultset.getstring ("name"));            return u;        }        });    return list; }}

  

spring--spring Integrated JDBC,

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.