`

第十一章 Spring4 JDBC

阅读更多
一,配置数据源 dbcp;


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- apache dbcp 读取jdbc.properties中的配置信息-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!-- 应用参数配置 -->
	<context:property-placeholder location="jdbc.properties"/>
    
    <!-- 数据库的操作类,注入dataSource -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
	
	<!-- 数据库操作类 -->
	<bean id="studentDao" class="com.fx.dao.impl.StudentDaoImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
	</bean> 
	
	<!-- 业务操作类 -->
	<bean id="studentService" class="com.fx.service.impl.StudentServiceImpl">
		<property name="studentDao" ref="studentDao"></property>
	</bean> 
	
</beans>





二,使用 JdbcTemplate


JdbcTemplate简介

  Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。

  JdbcTemplate位于spring-jdbc-4.0.6.RELEASE.jar中。其全限定命名为org.springframework.jdbc.core.JdbcTemplate。要使用JdbcTemlate还需一个spring-tx-4.0.6.RELEASE.jar,这个包包含了一下事务和异常控制。

  

JdbcTemplate主要提供以下五类方法:

execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;

update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;

query方法及queryForXXX方法:用于执行查询相关语句;

call方法:用于执行存储过程、函数相关语句。



package com.fx.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.fx.dao.StudentDao;
import com.fx.model.Student;

public class StudentDaoImpl implements StudentDao{

	private JdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int addStudent(Student student) {
		String sql="insert into t_student values(null,?,?)";
		Object []params=new Object[]{student.getName(),student.getAge()};
		return jdbcTemplate.update(sql,params);
	}

	@Override
	public int updateStudent(Student student) {
		String sql="update t_student set name=?,age=? where id=?";
		Object []params=new Object[]{student.getName(),student.getAge(),student.getId()};
		return jdbcTemplate.update(sql,params);
	}

	@Override
	public int deleteStudent(int id) {
		String sql="delete from t_student where id=?";
		Object []params=new Object[]{id};
		return jdbcTemplate.update(sql,params);
	}

	@Override
	public List<Student> findStudents() {
		String sql="select * from t_student";
		final List<Student> studentList=new ArrayList<Student>();
		jdbcTemplate.query(sql, new RowCallbackHandler(){

			@Override
			public void processRow(ResultSet rs) throws SQLException {
				Student student=new Student();
				student.setId(rs.getInt("id"));
				student.setName(rs.getString("name"));
				student.setAge(rs.getInt("age"));
				studentList.add(student);
			}
			
		});
		return studentList;
	}

}



分享到:
评论

相关推荐

    Spring攻略PDF版

     第11章 整合Spring与其他Web框架   第12章 Spring对测试的支持  第三部分 高级主题  第13章 Spring Security框架   第14章 Spring Portlet MVC框架   第15章 Spring Web Flow   第16章 ...

    Spring攻略中文版PDF

     第11章 整合Spring与其他Web框架   第12章 Spring对测试的支持  第三部分 高级主题  第13章 Spring Security框架   第14章 Spring Portlet MVC框架   第15章 Spring Web Flow   第16章 ...

    Spring攻略英文版(附带源码)

     第11章 整合Spring与其他Web框架   第12章 Spring对测试的支持  第三部分 高级主题  第13章 Spring Security框架   第14章 Spring Portlet MVC框架   第15章 Spring Web Flow   第16章 Spring...

    《精通Spring2.X企业应用开发详解》随书源码1-15章

    Spring AOP基础 第7章 基于@AspectJ和Schema的 第7章 AOP 第3篇 数据库访问 第8章 Spring对DAO的支持 第9章 Spring的事务管理 第10章 使用Spring JDBC访问数据库 第11章 使用JPA访问数据库 ...

    springlive(共11章)

    11. **第十一章:Spring Cloud** - 微服务概述:介绍微服务架构,以及Spring Cloud在其中的作用。 - Spring Cloud组件:了解Eureka、Zuul、Ribbon、Hystrix等核心组件的用法,构建微服务生态。 通过阅读《Spring...

    《精通Spring2.X企业应用开发详解》16-19章

    Spring AOP基础 第7章 基于@AspectJ和Schema的 第7章 AOP 第3篇 数据库访问 第8章 Spring对DAO的支持 第9章 Spring的事务管理 第10章 使用Spring JDBC访问数据库 第11章 使用JPA访问数据库 ...

    第十二章 Spring4 支持参数命名的JdbcTemplate

    在本章中,我们将深入探讨Spring框架的第四版本(Spring 4)中引入的一个特性——支持参数命名的JdbcTemplate。JdbcTemplate是Spring提供的一种数据库访问工具,它简化了数据库操作,提高了代码的可读性和可维护性。...

    spring4源码1

    1. 第十二讲:这可能涉及Spring4的高级特性或某一关键组件的深入解析。 2. 第十讲:可能涵盖了Spring4的核心概念,如IoC(Inversion of Control)容器和Bean的生命周期管理。 3. 第九讲:可能讲解了Spring的AOP机制...

    Java Web程序设计教程4

    第4章 sql与jdbc 64 第5章 struts 2框架基础 80 第6章 struts 2高级应用 110 第7章 struts 2中应用模板语言 145 第8章 hibernate框架基础 173 第9章 hibernate查询 188 第10章 hibernate性能优化 208 ...

    spring_in_action-sixth-edition.pdf

    "Spring 实战第六版" Spring Framework 是一个广泛使用的 Java 应用程序框架,它提供了一个通用的编程模型和配置机制,帮助开发者快速构建企业级应用程序。下面是对 Spring Framework 的详细知识点总结: 1. 什么...

    Java Web程序设计教程2

    第4章 sql与jdbc 64 第5章 struts 2框架基础 80 第6章 struts 2高级应用 110 第7章 struts 2中应用模板语言 145 第8章 hibernate框架基础 173 第9章 hibernate查询 188 第10章 hibernate性能优化 208 ...

    Java Web程序设计教程1

    第4章 sql与jdbc 64 第5章 struts 2框架基础 80 第6章 struts 2高级应用 110 第7章 struts 2中应用模板语言 145 第8章 hibernate框架基础 173 第9章 hibernate查询 188 第10章 hibernate性能优化 208 ...

    Java Web程序设计教程3

    第4章 sql与jdbc 64 第5章 struts 2框架基础 80 第6章 struts 2高级应用 110 第7章 struts 2中应用模板语言 145 第8章 hibernate框架基础 173 第9章 hibernate查询 188 第10章 hibernate性能优化 208 ...

    《精通Spring2.X企业应用开发详解》20-23

    Spring AOP基础 第7章 基于@AspectJ和Schema的 第7章 AOP 第3篇 数据库访问 第8章 Spring对DAO的支持 第9章 Spring的事务管理 第10章 使用Spring JDBC访问数据库 第11章 使用JPA访问数据库 ...

    Java Web程序设计教程5

    第4章 sql与jdbc 64 第5章 struts 2框架基础 80 第6章 struts 2高级应用 110 第7章 struts 2中应用模板语言 145 第8章 hibernate框架基础 173 第9章 hibernate查询 188 第10章 hibernate性能优化 208 ...

    Spring+3.x企业应用开发实战光盘源码(全)

     第11章:讲解了如何使用Spring JDBC进行数据访问操作,我们还重点讲述了LOB字段处理、主键产生和获取等难点知识。  第12章:讲解了如何在Spring中集成Hibernate、myBatis等数据访问框架,同时,读者还将学习到ORM...

    pro_spring_3.pdf (英文原版非扫描 看到小便知)

    第4章:详细介绍了Spring中的控制反转(IoC)和依赖注入(DI)概念。 第5章:深入讲解了Spring的配置细节。 第6章:介绍了Spring面向切面编程(AOP)的基础知识。 第7章:探讨了Spring AOP的高级话题和注解的使用...

    Pro Spring 3(带目录版)

    第11章:介绍了如何在Spring中整合MyBatis持久层框架,以及如何配置和使用MyBatis的映射文件。 第12章:讨论了如何设计和实现基于Spring的应用程序,包括服务层、数据访问层和表现层的设计原则和实践。 第13章:...

Global site tag (gtag.js) - Google Analytics