Use spring's jdbctemplate to further manipulate JDBC
First, the general configuration
Springjdbctemplate connect to the database and manipulate the data
1.applicationcontext.xml
1.1 Build Datasouce Bean for connection to database (including Driverclassname,url,username,password)
1.2 Configuring the JdbcTemplate bean and referencing the datasource bean
For example:
<bean id= "Springdsn"
class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "Driverclassname"
Value= "Com.microsoft.jdbc.sqlserver.SQLServerDriver" >
</property>
<property name= "url"
Value= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=bbs" >
</property>
<property name= "username" value= "sa" ></property>
<property name= "password" value= "sa" ></property>
</bean>
<bean id= "JdbcTemplate"
class= "Org.springframework.jdbc.core.JdbcTemplate" abstract= "false"
Lazy-init= "false" autowire= "default" dependency-check= "Default" >
<property name= "DataSource" >
<ref bean= "Springdsn"/>
</property>
</bean>
2.springutil.java class
Create a new class to parse beans from the Applicationcontext.xml configuration file (Getbean)
For example:
Public final class Springutil {
private static ApplicationContext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
public static Object Getbean (String beanname) {
Return Ctx.getbean (Beanname);
}
}
3.XXDao class
3.1 Instantiate a JdbcTemplate object using the Springutil (Parse Bean Class) method
3.2 crud Data with JdbcTemplate objects (additional deletions)
For example:
......
Private JdbcTemplate JDBCT = (jdbctemplate) springutil.getbean ("JdbcTemplate");
Public List FindALL () {
String sql = "SELECT * from BookInfo";
return jdbct.queryforlist (SQL);
}
......
Second, through the method of dependency injection (through the method of dependency injection, we can omit the parsing Jdbctemplatebean class, and set it directly inside the configuration file)
1.applicationcontext.xml
1.1 Build Datasouce Bean for connection to database (including Driverclassname,url,username,password)
1.2 Configuring the JdbcTemplate bean and referencing the datasource bean
1.3 Create a bean of the Xxdao class and inject the JdbcTemplate bean into the Xxdao class
For example:
<bean id= "Springdsn" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name= "Driverclassname"
Value= "Com.microsoft.jdbc.sqlserver.SQLServerDriver" >
</property>
<property name= "url"
Value= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=bbs" >
</property>
<property name= "username" value= "sa" ></property>
<property name= "password" value= "sa" ></property>
</bean>
<bean id= "JdbcTemplate"
class= "Org.springframework.jdbc.core.JdbcTemplate" abstract= "false"
Lazy-init= "false" autowire= "default" dependency-check= "Default" >
<property name= "DataSource" >
<ref bean= "Springdsn"/>
</property>
</bean>
<bean id= "Bookdao" class= "Com.yy.struts.dao.BookDao" >
<property name= "JDBCT" >
<ref bean= "JdbcTemplate"/>
</property>
</bean>
2.XXDao class
2.1 Instantiate a JdbcTemplate object using the Springutil (Parse Bean Class) method
2.2 Crud Data with JdbcTemplate objects (additional deletions)
For example:
......
Private JdbcTemplate JDBCT;
Public List FindALL () {
String sql = "SELECT * from BookInfo";
return jdbct.queryforlist (SQL);
}
......
Code instance Source: http://www.cnblogs.com/Fskjb/archive/2009/11/18/1605622.html
Springjdbctemplate Detailed configuration: http://www.blogjava.net/hyljava/archive/2013/02/22/spring-jdbctemplate.html
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spring-springjdbctemlate Configuration Introduction