原文地址为:
使用Spring的jdbcTemplate进一步简化JDBC操作
Code
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<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>
</beans>
Code
package com.r.dao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class SpringUtil {
private static ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
public static Object getBean(String beanName){
return ctx.getBean(beanName);
}
}
Code
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import com.r.vo.Book;
public class BookDao {
private JdbcTemplate jdbcT = (JdbcTemplate) SpringUtil
.getBean("jdbcTemplate");
public List findALL() {
String sql = "select * from BookInfo";
return jdbcT.queryForList(sql);
}
public List<Book> findALLBooks() {
List<Book> books = new ArrayList<Book>();;
String sql = "select * from BookInfo";
List list = jdbcT.queryForList(sql);
Iterator iterator = list.iterator();
Book book = null;
while (iterator.hasNext()) {
Map map4book = (Map) iterator.next();
book = new Book();
book.setBid((Integer) map4book.get("bid"));
book.setBookName((String)map4book.get("bookName"));
book.setBookType((String)map4book.get("bookType"));
book.setBookPic(((BigDecimal)map4book.get("bookPic")).doubleValue() );
book.setCount((Integer) map4book.get("count"));
books.add(book);
}
return books;
}
public int delete(int bid){
String sql = "delete from BookInfo where bid =?";
return jdbcT.update(sql, new Object[]{bid});
}
public static void main(String[] args) {
List<Book> books = new BookDao().findALLBooks();;
for(Book book:books){
System.out.println(book.getBid()+","+book.getBookName()+","+book.getBookType());
}
}
}
转载请注明本文地址: 使用Spring的jdbcTemplate进一步简化JDBC操作
先看applicationContext.xml配置文件:
![9310e85a14af99de4811ff4c77f1f911.jpe](https://i-blog.csdnimg.cn/blog_migrate/a9af314cccc3b1c20a00c16e7c3fb329.png)
![24a924a57ba6b3f2b51fc9edb7ea4186.jpe](https://i-blog.csdnimg.cn/blog_migrate/156b3a452247379d066a7687077a8e2f.png)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<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>
</beans>
在看SpringUtil类
![9310e85a14af99de4811ff4c77f1f911.jpe](https://i-blog.csdnimg.cn/blog_migrate/a9af314cccc3b1c20a00c16e7c3fb329.png)
![24a924a57ba6b3f2b51fc9edb7ea4186.jpe](https://i-blog.csdnimg.cn/blog_migrate/156b3a452247379d066a7687077a8e2f.png)
package com.r.dao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class SpringUtil {
private static ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
public static Object getBean(String beanName){
return ctx.getBean(beanName);
}
}
最后看DAO:
![9310e85a14af99de4811ff4c77f1f911.jpe](https://i-blog.csdnimg.cn/blog_migrate/a9af314cccc3b1c20a00c16e7c3fb329.png)
![24a924a57ba6b3f2b51fc9edb7ea4186.jpe](https://i-blog.csdnimg.cn/blog_migrate/156b3a452247379d066a7687077a8e2f.png)
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import com.r.vo.Book;
public class BookDao {
private JdbcTemplate jdbcT = (JdbcTemplate) SpringUtil
.getBean("jdbcTemplate");
public List findALL() {
String sql = "select * from BookInfo";
return jdbcT.queryForList(sql);
}
public List<Book> findALLBooks() {
List<Book> books = new ArrayList<Book>();;
String sql = "select * from BookInfo";
List list = jdbcT.queryForList(sql);
Iterator iterator = list.iterator();
Book book = null;
while (iterator.hasNext()) {
Map map4book = (Map) iterator.next();
book = new Book();
book.setBid((Integer) map4book.get("bid"));
book.setBookName((String)map4book.get("bookName"));
book.setBookType((String)map4book.get("bookType"));
book.setBookPic(((BigDecimal)map4book.get("bookPic")).doubleValue() );
book.setCount((Integer) map4book.get("count"));
books.add(book);
}
return books;
}
public int delete(int bid){
String sql = "delete from BookInfo where bid =?";
return jdbcT.update(sql, new Object[]{bid});
}
public static void main(String[] args) {
List<Book> books = new BookDao().findALLBooks();;
for(Book book:books){
System.out.println(book.getBid()+","+book.getBookName()+","+book.getBookType());
}
}
}
细心你,会发现JdbcTemplate的实例中有一系列的方法如:queryForXXX,update,delete大大简化了JDBC操作。
当然,还可以再进一步的优化一下,就是通过依赖注入,直接把jdbcTemplate注入到dao类的jdbcT字段。
先看新的applicationContext.xml配置文件:
<?
xml version="1.0" encoding="UTF-8"
?>
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
< 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 >
</ beans >
< beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
< 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 >
</ beans >
那么新的DAO类:
import
java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import com.r.vo.Book;
public class BookDao {
private JdbcTemplate jdbcT;
public List findALL() {
String sql = " select * from BookInfo " ;
return jdbcT.queryForList(sql);
}
public List < Book > findALLBooks() {
List < Book > books = new ArrayList < Book > ();;
String sql = " select * from BookInfo " ;
List list = jdbcT.queryForList(sql);
Iterator iterator = list.iterator();
Book book = null ;
while (iterator.hasNext()) {
Map map4book = (Map) iterator.next();
book = new Book();
book.setBid((Integer) map4book.get( " bid " ));
book.setBookName((String)map4book.get( " bookName " ));
book.setBookType((String)map4book.get( " bookType " ));
book.setBookPic(((BigDecimal)map4book.get( " bookPic " )).doubleValue() );
book.setCount((Integer) map4book.get( " count " ));
books.add(book);
}
return books;
}
public int delete( int bid){
String sql = " delete from BookInfo where bid =? " ;
return jdbcT.update(sql, new Object[]{bid});
}
public static void main(String[] args) {
List < Book > books = new BookDao().findALLBooks();;
for (Book book:books){
System.out.println(book.getBid() + " , " + book.getBookName() + " , " + book.getBookType());
}
}
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.JdbcTemplate;
import com.r.vo.Book;
public class BookDao {
private JdbcTemplate jdbcT;
public List findALL() {
String sql = " select * from BookInfo " ;
return jdbcT.queryForList(sql);
}
public List < Book > findALLBooks() {
List < Book > books = new ArrayList < Book > ();;
String sql = " select * from BookInfo " ;
List list = jdbcT.queryForList(sql);
Iterator iterator = list.iterator();
Book book = null ;
while (iterator.hasNext()) {
Map map4book = (Map) iterator.next();
book = new Book();
book.setBid((Integer) map4book.get( " bid " ));
book.setBookName((String)map4book.get( " bookName " ));
book.setBookType((String)map4book.get( " bookType " ));
book.setBookPic(((BigDecimal)map4book.get( " bookPic " )).doubleValue() );
book.setCount((Integer) map4book.get( " count " ));
books.add(book);
}
return books;
}
public int delete( int bid){
String sql = " delete from BookInfo where bid =? " ;
return jdbcT.update(sql, new Object[]{bid});
}
public static void main(String[] args) {
List < Book > books = new BookDao().findALLBooks();;
for (Book book:books){
System.out.println(book.getBid() + " , " + book.getBookName() + " , " + book.getBookType());
}
}
}
通过依赖注入,对象之间的关系由SPRING来维护,这样能降低类与类的耦合度。
转载请注明本文地址: 使用Spring的jdbcTemplate进一步简化JDBC操作