The DAO layer class Userdaoimpl inherits Jdbcdaosupport and cancels annotations on the class, overwrites the previous temp object to Getjdbctemplate (), and then configures the Userdaoimpl Bean in the spring IOC container. and inject the DataSource in.
The global configuration file Beans.xml is as follows:
<?xml version="1.0"encoding="UTF-8"? ><beans xmlns="Http://www.springframework.org/schema/beans"XMLNS:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"Xmlns:context="Http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http:www.springframework.org/schema/Context http:www.springframework.org/schema/context/spring-context.xsd http:www.springframework.org/schema/AOP http:Www.springframework.org/schema/aop/spring-aop.xsd"><context:component-scan base-package="com"/> <bean id="DS"class="Com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="User"Value="Root"/> <property name="Password"Value="Root"/> <property name="Jdbcurl"Value="jdbc:mysql:///db_test"/> <property name="Driverclass"Value="Com.mysql.jdbc.Driver"/> </bean> <bean id="DAO"class="Com.dao.UserDaoImpl"> <property name="DataSource"ref="DS"/> </bean></beans>
The Userdaoimpl code is as follows:
Public class Userdaoimpl extends Jdbcdaosupport implements Userdao {@Override public boolean Add (user user) { String SQL="INSERT into user values (?,?,?,?)"; int update=getjdbctemplate (). Update (SQL, User.getid (), User.getname (), User.getage (), user.getaddress ()); returnUpdate >0?true:false; } @Override public boolean update (user user) {String SQL="Update user set name=?, age=?, address=? where id=?"; int update=getjdbctemplate (). Update (SQL, User.getname (), User.getage (), user.getaddress (), User.getid ()); returnUpdate >0?true:false; }
}
Spring's jdbc "Inherit Jdbcdaosupport"