Spring-springjdbctemlate Configuration Introduction

Source: Internet
Author: User

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

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.