Basic steps for using the JDBC template

Source: Internet
Author: User

1.appliactioncontext.xml Configuration

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans"xmlns:p= "http://www.springframework.org/schema/p"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--data Source configuration--<bean id= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name= "driverclassname" value= "com . Mysql.jdbc.Driver "></property> <property name=" url "value=" jdbc:mysql:///spring "></property&gt         ; <property name= "username" value= "root" ></property> <property name= "password" value= "" ></prope rty> </bean> <bean id= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate" > <property name= "dataSource" ref= "DataSource" ></propert y> </bean> <bean id= "Userdao"class= "Cn.happy.impl.UserDAOImpl" > <property name= "jdbctemplate" ref= "JdbcTemplate" ></property> </ Bean> </beans>

2. Interface: Iuserdao.java

  Public InterfaceIuserdao { Public voidaddUser (user user);  Public voidDeleteUser (intID);  Public voidupdateUser (user user);  PublicString Searchusername (intID);  PublicUser Searchuser (intID);  PublicList<user>FindAll (); }

3. Interface implementation class: Userdaoimpl.java

Spring provides the Jdbcdaosupport support class, and all DAO inherits this class, and it automatically gets JdbcTemplate (provided the datasource is injected).

class= "Org.springframework.jdbc.core.JdbcTemplate" >         <property name= "DataSource" ref= " DataSource "></property>     </bean>               class=" Cn.happy.impl.UserDAOImpl ">         < Property Name= "JdbcTemplate" ref= "JdbcTemplate" ></property>     </bean>

JdbcTemplate mainly provides the following methods:

1. Execute method: Can be used to execute any SQL statements, generally used to execute DDL statements;

2, Update method and BatchUpdate method: Update method is used to execute new, modify, delete and other statements; The BatchUpdate method is used to execute batch-related statements;

3, Query method and Queryforxxx method: Used to execute query related statements;

4. Call method: Used to execute stored procedures, function-related statements.

 Public classUserdaoimplextendsJdbcdaosupportImplementsIuserdao { Public voidaddUser (user user) {String SQL= "INSERT into user values (?,?,?)";  This. Getjdbctemplate (). Update (SQL, User.getid (), User.getusername (), User.getpassword ()); }       Public voidDeleteUser (intID) {String SQL= "Delete from user where id=?";  This. Getjdbctemplate (). Update (SQL, ID); }       Public voidupdateUser (user user) {String SQL= "Update user set username=?,password=?" where id=? ";  This. Getjdbctemplate (). Update (SQL, User.getusername (), User.getpassword (), User.getid ()); }       PublicString Searchusername (intID) {//simple query, query by ID, return stringString sql = "Select username from user where id=?"; //The return type is String (string.class)         return  This. Getjdbctemplate (). queryForObject (SQL, String.class, id); }       PublicList<user> FindAll () {//complex query returns a list collectionString sql = "SELECT * from User"; return  This. Getjdbctemplate (). query (SQL,NewUserrowmapper ()); }       PublicUser Searchuser (intID) {String SQL= "SELECT * from user where id=?"; return  This. Getjdbctemplate (). queryForObject (SQL,NewUserrowmapper (), id); }      classUserrowmapperImplementsRowmapper<user>{//RS is the return result set, encapsulated in each behavior unit          PublicUser Maprow (ResultSet RS,intRowNum)throwsSQLException {User User=NewUser (); User.setid (Rs.getint ("id")); User.setusername (Rs.getstring ("Username")); User.setpassword (Rs.getstring ("Password")); returnuser; }     }  }

4. Test class: Usertest.java

 Public classusertest {@Test//Increase      Public voiddemo1 () {User User=NewUser (); User.setid (3); User.setusername ("Admin"); User.setpassword ("123456"); ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Iuserdao DAO= (Iuserdao) applicationcontext.getbean ("Userdao");              Dao.adduser (user); } @Test//Change      Public voidDemo2 () {User User=NewUser (); User.setid (1); User.setusername ("Admin"); User.setpassword ("Admin"); ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Iuserdao DAO= (Iuserdao) applicationcontext.getbean ("Userdao");     Dao.updateuser (user); } @Test//Delete      Public voidDemo3 () {ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Iuserdao DAO= (Iuserdao) applicationcontext.getbean ("Userdao"); Dao.deleteuser (3); } @Test//Check (simple query, return string)      Public voidDemo4 () {ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Iuserdao DAO= (Iuserdao) applicationcontext.getbean ("Userdao"); String name=dao.searchusername (1);     SYSTEM.OUT.PRINTLN (name); } @Test//Check (simple query, return object)      Public voidDemo5 () {ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Iuserdao DAO= (Iuserdao) applicationcontext.getbean ("Userdao"); User User=dao.searchuser (1);     System.out.println (User.getusername ()); } @Test//Check (complex query, return object collection)      Public voidDemo6 () {ApplicationContext ApplicationContext=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Iuserdao DAO= (Iuserdao) applicationcontext.getbean ("Userdao"); List<User> users=Dao.findall ();     System.out.println (Users.size ()); }            }

Basic steps for using the JDBC template

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.