Spring JDBC抽象框架所带来的价值将在以下几个方面得以体现:(注:使用了Spring JDBC抽象框架之后,应用开发人员只需要完成斜体红字部分的编码工作。)
-
指定数据库连接参数
-
打开数据库连接
-
声明SQL语句
-
预编译并执行SQL语句
-
遍历查询结果(如果需要的话)
-
处理每一次遍历操作
-
处理抛出的任何异常
-
处理事务
-
关闭数据库连接
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="dataSource"
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="dataSource" />
</property>
</bean>
</beans>
SpringUtil类
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);
}
}
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来维护,这样能降低类与类的耦合度:
<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.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;
//针对jdbcT,提供get和set方法,这里省略
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());
}
}
}
相关推荐
赠送jar包:spring-jdbc-5.3.15.jar; 赠送原API文档:spring-jdbc-5.3.15-javadoc.jar; 赠送源代码:spring-jdbc-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-jdbc-5.3.15.pom; 包含翻译后的API文档:...
开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6.RELEASE开发工具 spring-jdbc-4.3.6....
赠送jar包:spring-jdbc-5.3.15.jar 赠送原API文档:spring-jdbc-5.3.15-javadoc.jar 赠送源代码:spring-jdbc-5.3.15-sources.jar 包含翻译后的API文档:spring-jdbc-5.3.15-javadoc-API文档-中文(简体)-英语-...
赠送jar包:spring-jdbc-4.3.20.RELEASE.jar 赠送原API文档:spring-jdbc-4.3.20.RELEASE-javadoc.jar 赠送源代码:spring-jdbc-4.3.20.RELEASE-sources.jar 包含翻译后的API文档:spring-jdbc-4.3.20.RELEASE-...
赠送jar包:spring-jdbc-5.3.10.jar; 赠送原API文档:spring-jdbc-5.3.10-javadoc.jar; 赠送源代码:spring-jdbc-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-jdbc-5.3.10.pom; 包含翻译后的API文档:...
赠送jar包:spring-jdbc-5.2.0.RELEASE.jar; 赠送原API文档:spring-jdbc-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-jdbc-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-jdbc-5.2.0.RELEASE....
spring-jdbc连接jar包,spring对于jdbc技术的封装,spring容器组成部分
赠送jar包:spring-jdbc-5.2.7.RELEASE.jar; 赠送原API文档:spring-jdbc-5.2.7.RELEASE-javadoc.jar; 赠送源代码:spring-jdbc-5.2.7.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-jdbc-5.2.7.RELEASE....
赠送jar包:spring-jdbc-5.3.7.jar; 赠送原API文档:spring-jdbc-5.3.7-javadoc.jar; 赠送源代码:spring-jdbc-5.3.7-sources.jar; 赠送Maven依赖信息文件:spring-jdbc-5.3.7.pom; 包含翻译后的API文档:spring...
《Spring JDBC:构建高效数据访问层》 Spring JDBC是Spring框架的一个重要组成部分,它提供了一种简化传统JDBC编程的抽象层,使得开发者能够更轻松、更安全地处理数据库交互。"spring-jdbc jar包"包含了Spring框架...
赠送jar包:spring-jdbc-5.0.10.RELEASE.jar; 赠送原API文档:spring-jdbc-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-jdbc-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-jdbc-5.0.10....
赠送jar包:spring-jdbc-5.0.8.RELEASE.jar; 赠送原API文档:spring-jdbc-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-jdbc-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-jdbc-5.0.8.RELEASE....
spring-jdbc-5.1.5.RELEASE.jar
Spring-JDBC是Spring框架的一部分,它提供了一个统一的接口来处理数据库操作,简化了与Java Database Connectivity (JDBC) API的交互。这个框架的核心目标是减少编写繁琐的JDBC代码,提高可测试性和可维护性。Spring...
spring-jdbc-4.0.0.RELEASE-sources.jar
spring-jdbc-3.2.5.RELEASE.jar spring-jdbc-3.2.5.RELEASE.jar
spring-jdbc-4.0.0.RELEASE-javadoc.jar
java web 开发中需要用到的外部spring jar spring-jdbc-5.2.3.RELEASE.jar
JAVAWEB前端课程所需要的五个jar包 - commons-logging-1.2.jar,spring-beans-5.0.0.RELEASE.jar,spring-core-5.0.0.RELEASE.jar,spring-jdbc-5.0.0.RELEASE.jar,spring-tx-5.0.2.RELEASE.jar
spring-jdbc-4.3.6.RELEASE.jar