The JdbcTemplate in spring

Source: Internet
Author: User

Content in the spring core configuration file

<!--inject DataSource -    <BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource">        < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"></ Property>        < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/db_user"></ Property>        < Propertyname= "username"value= "root"></ Property>        < Propertyname= "Password"value= "258764547"></ Property>    </Bean>    <!--inject JdbcTemplate -    <BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate">        <!--injecting the DataSource attribute into the JdbcTemplate -        < Propertyname= "DataSource"ref= "DataSource"></ Property>    </Bean>        <!--Inject Userdaoimpl -    <BeanID= "Userdaoimpl"class= "Com.yudo.dao.impl.UserDaoImpl">        <!--injecting the JdbcTemplate attribute into the Userdaoimpl -        < Propertyname= "JdbcTemplate"ref= "JdbcTemplate"></ Property>    </Bean>

Add, delete, change the code,

 Public classUserdaoimpl {PrivateJdbcTemplate JdbcTemplate;  Public voidsetjdbctemplate (JdbcTemplate jdbctemplate) { This. JdbcTemplate =JdbcTemplate; }     Public voidAdd () {User User=NewUser (); User.setusername ("Guan Yu"); User.setpassword ("456"); User.setage (43); User.setaddress (Jingzhou); String SQL= "INSERT into T_user (username,password,age,address) VALUES (?,?,?,?)";    Jdbctemplate.update (SQL, User.getusername (), User.getpassword (), User.getage (), user.getaddress ()); }     Public voidDelete () {String username= "Zhang Fei"; String SQL= "Delete from T_user where username=?";    Jdbctemplate.update (SQL, username); }     Public voidUpdate () {String SQL= "Update t_user set age=?", address=? where Username=? "; Jdbctemplate.update (SQL,"45", "Shu", "Guan Yu"); }

Query function:

<1. Returning a single object

// returns a single object     Public void Find1 () {        = "SELECT * from T_user where username=?" ;         New Maprow (), "Guan Yu");        SYSTEM.OUT.PRINTLN (user);    }    
Object of New Maprow ()
classMaprowImplementsRowmapper<user>{@Override PublicUser Maprow (ResultSet arg0,intARG1)throwsSQLException {//TODO auto-generated Method StubString username = arg0.getstring ("username"); String Password= arg0.getstring ("Password"); Integer Age= Arg0.getint ("Age"); String Address= arg0.getstring ("Address"); User User=NewUser ();        User.setusername (username);        User.setpassword (password);        User.setage (age);        User.setaddress (address); returnuser; }}

<2. Returning the list collection

// returns the list collection     Public void Find2 () {        = "SELECT * from T_user";        ListNew  Maprow ());          for (user user:list) {            System.out.println (user);        }    }

Test class:

  @Test  public  void   Demotest () {applicationcontext ctx  = new   Classpathxmlapplicationcontext (" ApplicationContext.        XML "); Userdaoimpl Userdaoimpl  = (Userdaoimpl) ctx.getbean ("Userdaoimpl"  //  Userdaoimpl.add ();  //  Userdaoimpl.delete ();  //  Userdaoimpl.update ();  //  Userdaoimpl.find1 ();              Userdaoimpl.find2 (); }

To increase, delete, change, the bulk operation, using the method batchupdate (SQL, Batchargs);

Batchargs as an array of object classes

The JdbcTemplate in spring

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.