使用Spring的jdbcTemplate进一步简化JDBC操作 先看applicationContext.xml配置文件:
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>在看SpringUtil类
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);
}
}
最后看DAO:
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());
}
}
}
细心你,会发现JdbcTemplate的实例中有一系列的方法如:queryForXXX,update,delete大大简化了JDBC操作。
当然,还可以再进一步的优化一下,就是通过依赖注入,直接把jdbcTemplate注入到dao类的jdbcT字段。
先看新的applicationContext.xml配置文件:
按 Ctrl+C 复制代码
<?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>
按 Ctrl+C 复制代码 那么新的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());
}
}
}
通过依赖注入,对象之间的关系由SPRING来维护,这样能降低类与类的耦合度
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>在看SpringUtil类
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);
}
}
最后看DAO:
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());
}
}
}
细心你,会发现JdbcTemplate的实例中有一系列的方法如:queryForXXX,update,delete大大简化了JDBC操作。
当然,还可以再进一步的优化一下,就是通过依赖注入,直接把jdbcTemplate注入到dao类的jdbcT字段。
先看新的applicationContext.xml配置文件:
按 Ctrl+C 复制代码
<?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>
按 Ctrl+C 复制代码 那么新的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());
}
}
}
通过依赖注入,对象之间的关系由SPRING来维护,这样能降低类与类的耦合度