Method One: Inherit JdbcTemplate to implement
1, Configuration ApplicationContext
1 <!--Get Data source connection DBCP - 2 <BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource">3 < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"></ Property>4 < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/hibernatetest"></ Property>5 < Propertyname= "username"value= "root"></ Property>6 < Propertyname= "Password"value= "MySQL"></ Property>7 </Bean> 8 9 <!-- ********************************** -Ten <BeanID= "Persondao"class= "Cn.test.spring.jdbc.PersonDaoImpl"> One < Propertyname= "DataSource"> A <refBean= "DataSource"/> - </ Property> - </Bean>
2. Inheriting ApplicationContext
1 Public class extends Implements Persondao {23 Public void Saveperson () {4 this. getjdbctemplate (). Execute ("INSERT to Person (pid,pname,page) VALUES (2, ' John Doe ',") "); 5 6 }
3. Testing
@Test Public void Saveperson () { applicationcontext applicationcontext=new classpathxmlapplicationcontext ("cn /test/spring/jdbc/applicationcontext.xml "); Persondao Persondao= (Persondao) applicationcontext.getbean ("Persondao"); Persondao.saveperson (); }
Method Two: JdbcTemplate as attributes into
1, Configuration Applicationcontext.xml
<!--Get Data source connection DBCP - <BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource">< Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"></ Property>< Propertyname= "url"value= "Jdbc:mysql://localhost:3306/hibernatetest"></ Property>< Propertyname= "username"value= "root"></ Property>< Propertyname= "Password"value= "MySQL"></ Property></Bean> <!-- ********************************** -<BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate"> < Propertyname= "DataSource"> <refBean= "DataSource"/> </ Property></Bean><BeanID= "PersonDao2"class= "CN.TEST.SPRING.JDBC.PERSONDAOIMPL2"> < Propertyname= "JdbcTemplate"> <refBean= "JdbcTemplate"/> </ Property></Bean>
2. Declaring fields
1 Public classPersonDaoImpl2ImplementsPersondao {2 3 Privatejdbctemplate JdbcTemplate;4 5 PublicJdbcTemplate getjdbctemplate () {6 returnJdbcTemplate;7 }8 9 Public voidsetjdbctemplate (JdbcTemplate jdbctemplate) {Ten This. JdbcTemplate =JdbcTemplate; One } A - - Public voidSaveperson () { the This. Getjdbctemplate (). Execute ("INSERT into person (pid,pname,page) VALUES (22, ' Li 42 ', 502)"); - -}
3. Testing
@Test Public void SavePerson2 () { applicationcontext applicationcontext=new classpathxmlapplicationcontext (" Cn/test/spring/jdbc/applicationcontext.xml "); Persondao Persondao= (Persondao) applicationcontext.getbean ("PersonDao2"); Persondao.saveperson (); }
Method Three: Through the constructor function
1, Configuration Applicationcontext.xml
<!--Get Data source connection DBCP - <BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource">< Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"></ Property>< Propertyname= "url"value= "Jdbc:mysql://localhost:3306/hibernatetest"></ Property>< Propertyname= "username"value= "root"></ Property>< Propertyname= "Password"value= "MySQL"></ Property></Bean> <!-- ********************************** -<BeanID= "PersonDao3"class= "Cn.test.spring.jdbc.PersonDaoImpl3"> <Constructor-argIndex= "0"ref= "DataSource"></Constructor-arg></Bean>
2. Constructor function
Public class extends JdbcTemplate implements Persondao { public PersonDaoImpl3 (DataSource DataSource) { super(DataSource); } Public void Saveperson () { this. Execute ("INSERT to Person (pid,pname,page) VALUES (23, ' Li 43 ', 503)" ); }
3. Testing
@Test Public void SavePerson3 () { applicationcontext applicationcontext=new classpathxmlapplicationcontext (" Cn/test/spring/jdbc/applicationcontext.xml "); Persondao Persondao= (Persondao) applicationcontext.getbean ("PersonDao3"); Persondao.saveperson (); }
Note: The above methods can only be added and deleted, can not be searched
Find:
Target method:
Public List<person> getpersons () { returnthisnew personrowmapper ()); }
Personrowmapper.java
Public class Implements RowMapper { publicintthrows SQLException { person person = New Person (); Person.setpid (Rs.getlong ("pid")); Person.setpname (rs.getstring ("Pname")); Person.setpage (rs.getstring ("page")); return Person ; }
Test
@Test Public void Testquery () { applicationcontext applicationcontext=new classpathxmlapplicationcontext ("cn/ Test/spring/jdbc/applicationcontext.xml "); Persondao Persondao= (Persondao) applicationcontext.getbean ("Persondao"); List<Person> personlist = persondao.getpersons (); System.out.println (Personlist.size ()); }
Spring and JDBC Demo