JdbcTemplate经典案例
一、JdbcTemplate案例配置式
(1)导入依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
(2)jdbc.properties连接数据库
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/account?useUniCode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=
(3)Accounts实体类
package cn.spring.accountstest.entity;
public class Accounts {
private Integer accountid;
private String accountname;
private double balance;
public Integer getAccountid() {
return accountid;
}
public void setAccountid(Integer accountid) {
this.accountid = accountid;
}
public String getAccountname() {
return accountname;
}
public void setAccountname(String accountname) {
this.accountname = accountname;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
(4)Accountsdao层
package cn.spring.accountstest.dao;
import cn.spring.accountstest.entity.Accounts;
import java.util.List;
public interface AccountsDao {
//查询所有的账户
List<Accounts> getList();
}
(5)AccountdaoImpl实现类
package cn.spring.accountstest.dao.impl; import cn.spring.accountstest.dao.AccountsDao;
import cn.spring.accountstest.entity.Accounts;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List; public class AccountDaoImpl extends JdbcDaoSupport implements AccountsDao {
@Override
public List<Accounts> getList() {
JdbcTemplate jdbcTemplate = this.getJdbcTemplate();
String sql="select * from accounts";
List<Accounts> lists = jdbcTemplate.query(sql, new RowMapper<Accounts>() {
/**
*
* @param rs 普通的rs 系统给的 读取器
* @param i 读取器读取的第几条数据
* @return accounts 单个对象
* @throws SQLException
*/
@Override
public Accounts mapRow(ResultSet rs, int i) throws SQLException {
Accounts accounts = new Accounts();
accounts.setAccountid(rs.getInt("accountid"));
accounts.setAccountname(rs.getString("accountname"));
accounts.setBalance(rs.getDouble("balance"));
return accounts;
}
});
return lists;
}
}
(6)AccountService层
package cn.spring.accountstest.service;
import cn.spring.accountstest.entity.Accounts;
import java.util.List;
public interface AccountsService {
//查询所有的账户
List<Accounts> getList();
}
(7)AccountServiceImpl实现类
package cn.spring.accountstest.service.impl; import cn.spring.accountstest.dao.AccountsDao;
import cn.spring.accountstest.entity.Accounts;
import cn.spring.accountstest.service.AccountsService;
import org.springframework.stereotype.Service; import javax.annotation.Resource;
import java.util.List;
public class AccountsServiceImpl implements AccountsService{
//使用Spring IOC 将AccountDao对象注入
AccountsDao dao;
@Override
public List<Accounts> getList() {
return dao.getList();
} public AccountsDao getDao() {
return dao;
} public void setDao(AccountsDao dao) {
this.dao = dao;
}
}
(8)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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
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"> <!--1.配置数据源 spring内置的数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean> <!--2.引入属性文件-->
<context:property-placeholder location="jdbc.properties"></context:property-placeholder> <!--3.构建jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--4.dao-->
<bean id="accountsDao" class="cn.spring.accountstest.dao.impl.AccountDaoImpl">
<!--为jdbcTemplate配置数据源-->
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!--5.service-->
<bean id="accountsService" class="cn.spring.accountstest.service.impl.AccountsServiceImpl">
<property name="dao" ref="accountsDao"></property>
</bean> <!--扫描注解:包扫描器-->
<context:component-scan base-package="cn.spring"></context:component-scan> <!--开启AOP注解支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
(9)测试类
package cn.spring; import cn.spring.accountstest.entity.Accounts;
import cn.spring.accountstest.service.AccountsService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List; public class AccountsTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
//从Spring容器中获取Service对象
AccountsService accountsService=(AccountsService) context.getBean(AccountsService.class);
List<Accounts> list = accountsService.getList();
//输出数据
for (Accounts accounts:list) {
System.out.println("账户名:"+accounts.getAccountname()+",余额为:"+accounts.getBalance());
}
}
}
(10)控制台

JdbcTemplate经典案例的更多相关文章
- javascript的理解及经典案例
js的简介: JavaScript是一种能让你的网页更加生动活泼的程式语言,也是目前网页中设计中最容易学又最方便的语言. 你可以利用JavaScript轻易的做出亲切的欢迎讯息.漂亮的数字钟.有广告效 ...
- jQuery基础的工厂函数以及定时器的经典案例
1. jQuery的基本信息: 1.1 定义: jQuery是JavaScript的程序库之一,它是JavaScript对象和实用函数的封装, 1.2 作用: 许多使用JavaScript能实现的交 ...
- Linux运维之道(大量经典案例、问题分析,运维案头书,红帽推荐)
Linux运维之道(大量经典案例.问题分析,运维案头书,红帽推荐) 丁明一 编 ISBN 978-7-121-21877-4 2014年1月出版 定价:69.00元 448页 16开 编辑推荐 1 ...
- 经典案例:那些让人赞不绝口的创新 HTML5 网站
在过去的10年里,网页设计师使用 Flash.JavaScript 或其他复杂的软件和技术来创建网站.但现在你可以前所未有的快速.轻松地设计或创造互动的.有趣好看的网站.如何创建?答案是 HTML5 ...
- Altera OpenCL用于计算机领域的13个经典案例(转)
英文出自:Streamcomputing 转自:http://www.csdn.net/article/2013-10-29/2817319-the-application-areas-opencl- ...
- php中foreach()函数与Array数组经典案例讲解
//php中foreach()函数与Array数组经典案例讲解 function getVal($v) { return $v; //可以加任意检查代码,列入要求$v必须是数字,或过滤非法字符串等.} ...
- 阿里云资深DBA专家罗龙九:云数据库十大经典案例分析【转载】
阿里云资深DBA专家罗龙九:云数据库十大经典案例分析 2016-07-21 06:33 本文已获阿里云授权发布,转载具体要求见文末 摘要:本文根据阿里云资深DBA专家罗龙九在首届阿里巴巴在线峰会的&l ...
- 经典案例之MouseJack
引言:在昨天的文章<无线键鼠监听与劫持>中,我们提到今天会向您介绍一个无线键鼠的监听与劫持的经典案例,<MouseJack>:MouseJack能利用无线鼠标和键盘存在的一些问 ...
- HTML5 CSS3 经典案例:无插件拖拽上传图片 (支持预览与批量) (二)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/31513065 上一篇已经实现了这个项目的整体的HTML和CSS: HTML5 C ...
随机推荐
- Go Modules使用教程
Go Modules 不完全教程 文章转载自公众号 Golang 成神之路 , 作者 L Go Modules 是 Golang 官方最近几个版本推出的原生的包管理方式,在此之前,社区也不乏多种包管理 ...
- 实训第六天(mybatis)
今天实训第六天,我们学习了mybatis这个数据库框架,虽然说框架的环境搭建非常的繁琐,但是在了解原理和流程之后是非常的舒服的.因为有一个强大的工具被我掌握了,所以今天感觉非常的开心. 首先我们是在s ...
- Python目录教程集和资料分享
以下整理的是python的基础笔记,需要python视频资料或更多的请关注我的公众号! 查看内容点击链接: Python简介及安装 Python的3种执行方式 变量及变量计算和引用 if, elif, ...
- ResultSet RS_resultxtgg=connDbBean.executeQuery(sqlxtgg);
<%String sqlxtgg="select * from dx where leibie='系统公告'"; ResultSet RS_resultxtgg=connDb ...
- mysql 事务四要素杂谈
事务四要素 对于数据库来说,并发性和准确性是数据库需要权衡的两个点. 类似于我们的应用系统,又要要性能还要要准确. 数据准确性这一条来说,最好的控制就是串行化,都别急,一个一个来.这样数据就没问题了. ...
- swoole为什么不能代替nginx
Swoole不能代替Apache和Nginx这些通用的HTTP服务器. 但基于Swoole开发的PHP应用不依赖Apache和Nginx也能提供生产级别的HTTP服务. 有需要学习交流的友人请加入交流 ...
- python的安装以及前景
1.检查软件是否安装:在cmd命令行上输oython 假如环境已经配置好环境变量 则会显示为python的inf: 2.下载并安装python 打开python官网:https://www.pytho ...
- Zabbix-(七)分布式监控
Zabbix-(七)分布式监控 一.前言 Zabbix提供了一套分布式监控的方案,即使用Zabbix Proxy,本文记录使用Zabbix Proxy进行分布式监控. 官方所述Proxy的使用场景如下 ...
- Python 爬虫从入门到进阶之路(四)
之前的文章我们做了一个简单的例子爬取了百度首页的 html,我们用到的是 urlopen 来打开请求,它是一个特殊的opener(也就是模块帮我们构建好的).但是基本的 urlopen() 方法不支持 ...
- 如何获得大学教材的PDF版本?
最近急需一本算法书的配套答案,这本配套单独出售,好像在市面上还买不到,在淘宝上搜索也只是上一个版本,并没有最新版本,让我很无奈.加上平时肯定会有这么一种情况,想看一些书,但买回来也看不了几次,加上计算 ...