Spring's DAO module provides support for DAO layers such as JDBC, Hibernate, JDO, and so on.
DAO module relies on Commons-pool.jar, Commons-collections.jar
PackageCom.dao;Importjava.util.ArrayList;Importjava.util.Date;Importjava.util.List;ImportJava.util.Map;ImportOrg.springframework.jdbc.core.support.JdbcDaoSupport; Public classIpeopledaoimplextendsJdbcdaosupportImplementsipeopledao{ Public voidInitdatabase () {//Create a tableString sql= "CREATE Table people (" + "ID int primary key auto_increment," + "name varchar (100 ) NOT NULL, "+" age int, "+" sex varchar (Ten), "+" Birthday DateTime) "; Getjdbctemplate (). Execute (SQL); } Public voidAddpeople (People people) {String SQL= "INSERT into people (Name,sex,age,birthday)" + "VALUES (?,?,?,?)"; Getjdbctemplate (). Update (SQL,Newobject[]{people.getname (), People.getsex (), People.getage (), People.getbirthday () }); } Public intGetpeoplecount () {String SQL= "SELECT count (*) from people"; intCount=getjdbctemplate (). queryForInt (SQL); returncount; } PublicString Getpeoplename (intID) {String SQL= "SELECT name from people where id=" +ID; String name=getjdbctemplate (). queryForObject (SQL, String.class); returnname; } PublicList<people>Listpeoples () {String SQL= "SELECT * from people"; List<Map<String,Object>> list=getjdbctemplate (). queryForList (SQL); List<People> peoplelist=NewArraylist<people>(); for(map<string,object>row:list) {People People=Newpeople (); People.setid ((Integer) Row.get ("id")); People.setname (String) row.get ("Name")); People.setsex (String) row.get ("Sex")); People.setbirthday (Date) row.get ("Birthday")); People.setage ((Integer) Row.get ("Age")); Peoplelist.add (people); } returnpeoplelist; }}<?xml version= "1.0" encoding= "UTF-8"?><Beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" > <!--<bean id= "Aopmethodbeforeinterceptor"class= "Org.springframework.aop.support.NameMatchMethodPointcutAdvisor" > <property name= "Advice" > < ; beanclass= "Com.aop.MethodBeforeInterceptor" ></bean> </property> <property name= "Mappedname" value= " WITHAOP "></property> </bean>--><!--<bean id=" Aopmethodafterinterceptor "class= "Org.springframework.aop.support.NameMatchMethodPointcutAdvisor" > <property name= "Advice" > < ; beanclass= "Com.aop.MethodAfterInterceptor" > </bean> </property> <property name= "Mappedna Me "value=" WITHAOP "></property> </bean> <!--<bean id=" Aopmethodafterinterceptor "class= "Org.springframework.aop.support.RegexpMethodPointcutAdvisor" > <property name= "Advice" > <be Anclass= "Com.aop.MethodAfterInterceptor" > </bean> </property> <property name= "patterns "> <list> <value>.*withAop</value> <value>.*like.*< /value> </list> </property> </bean> <bean id= "Aopthrowsinterceptor"class= "Org.springframework.aop.support.NameMatchMethodPointcutAdvisor" > <property name= "Advice" > < ; beanclass= "Com.aop.ThrowsInterceptor" ></bean> </property> <property name= "Mappedname" value= "Withao P "></property> </bean>--><!--service objects, installing into Proxyfactorybean objects--<!--<bean Id= "Aopservice"class= "Org.springframework.aop.framework.ProxyFactoryBean" > <property name= "interceptornames" > <l Ist> <value>aopMethodBeforeInterceptor</value> <value>aopmethodafterint Erceptor</value> <value>aopThrowsInterceptor</value> </list> < /property> <property name= "target" > <beanclass= "Com.aop.AopServiceImpl" > <property name= "name" value= "SSS" ></property> </bea n> </property> </bean>--> <bean id= "Configproperties"class= "Org.springframework.beans.factory.config.PropertiesFactoryBean" > <property name= "Location" value= " Jdbc.properties "/> </bean> <bean id=" Propertyconfigurer "class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "Properties" ref = "Configproperties"/> </bean> <bean id= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" > <property name= "url" value= "${jdbc.url}"/ > <property name= "username" value= "${jdbc.username}"/> <property name= "password" value= "${jdbc. Password} "/> <property name=" driverclassname "value=" ${jdbc.driver} "/> </bean> <!--<be An id= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource" destroy-method= "Close" > <property name= "url" value= "jdbc:mysql:// Localhost:3306/test_1?characterencoding=utf-8 "/> <property name=" username "value=" Guodaxia "/> < ;p roperty name= "password" value= "Guodaxia"/> <property name= "driverclassname" value= "Com.mysql.jdbc.Driver" /> </bean>--><bean id= "Peopledao"class= "Com.dao.IPeopleDaoImpl" depends-on= "DataSource" > <property name= "dataSource" ref= "DataSource"/> < ;/bean> </beans> PackageCom.dao;Importjava.util.ArrayList;Importjava.util.Date;Importjava.util.List;ImportJava.util.Map;ImportOrg.springframework.jdbc.core.support.JdbcDaoSupport; Public classIpeopledaoimplextendsJdbcdaosupportImplementsipeopledao{ Public voidInitdatabase () {//Create a tableString sql= "CREATE Table people (" + "ID int primary key auto_increment," + "name varchar (100 ) NOT NULL, "+" age int, "+" sex varchar (Ten), "+" Birthday DateTime) "; Getjdbctemplate (). Execute (SQL); } Public voidAddpeople (People people) {String SQL= "INSERT into people (Name,sex,age,birthday)" + "VALUES (?,?,?,?)"; Getjdbctemplate (). Update (SQL,Newobject[]{people.getname (), People.getsex (), People.getage (), People.getbirthday () }); } Public intGetpeoplecount () {String SQL= "SELECT count (*) from people"; intCount=getjdbctemplate (). queryForInt (SQL); returncount; } PublicString Getpeoplename (intID) {String SQL= "SELECT name from people where id=" +ID; String name=getjdbctemplate (). queryForObject (SQL, String.class); returnname; } PublicList<people>Listpeoples () {String SQL= "SELECT * from people"; List<Map<String,Object>> list=getjdbctemplate (). queryForList (SQL); List<People> peoplelist=NewArraylist<people>(); for(map<string,object>row:list) {People People=Newpeople (); People.setid ((Integer) Row.get ("id")); People.setname (String) row.get ("Name")); People.setsex (String) row.get ("Sex")); People.setbirthday (Date) row.get ("Birthday")); People.setage ((Integer) Row.get ("Age")); Peoplelist.add (people); } returnpeoplelist; }} PackageCom.dao;Importjava.util.List; Public InterfaceIpeopledao { PublicString Getpeoplename (intID); Public voidAddpeople (People people); Public intGetpeoplecount (); PublicList<people>listpeoples ();} Packagecom. Test;Importjava.text.ParseException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importjava.util.List;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.dao.IPeopleDao;Importcom.dao.People; Public classTestdao {/*Public static void Main (string[] args) {String time= "2011-12-07"; System.out.println (Transacttodate (Time). toLocaleString ()); }*/ Public Static voidMain (string[] args) {Test1 (); } Public Static voidTest1 () {ApplicationContext con=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Ipeopledao Persondao= (Ipeopledao) con.getbean ("Peopledao"); /*people P=new people (); P.setname ("Helloween"); P.setsex ("male"); P.setage (12); P.setbirthday (Transacttodate ("2005-12-3")); Persondao.addpeople (p);*/System.out.println (Persondao.getpeoplecount ()); List<People> peo=Persondao.listpeoples (); System.out.println (Peo.get (0). GetName ()); } Public StaticDate transacttodate (string d) {string pattern= "Yyyy-mm-dd"; SimpleDateFormat DF=NewSimpleDateFormat (pattern); Date Date=NULL; Try{Date=Df.parse (d); } Catch(ParseException e) {e.printstacktrace (); } returndate; }}
View Code
Analysis Examples:
Daoimpl inheriting Jdbcdaosupport class
Jdbcdaosupport has a DataSource variable that configures the value of the data source in the configuration file, uses Getjdbctemplate () to get the JdbcTemplate object, and uses the JdbcTemplate object to manipulate the database
Here are a few ways I'm using:
Getjdbctemplate (). Execute (SQL);//I use it to build a table.
Getjdbctemplate (). Update (
Sql,new object[]{
People.getname (), People.getsex (), People.getage (), People.getbirthday ()
});//Perform the update operation and use the? wildcard character in the SQL statement to use the array assignment
Getjdbctemplate (). queryForInt (SQL);//query operation returns an int value
Getjdbctemplate (). queryForObject (SQL, String.class);//Convert the result of a query into an object
Getjdbctemplate (). queryForList (SQL);//Convert the structure of the query into a list collection of result sets
Spring's DAO module