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 ...
随机推荐
- iOS多线程知识梳理
iOS多线程知识梳理 线程进程基础概念 进程 进程是指在系统中正在运行的一个应用程序 每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 线程 1个进程要想执行任务,必须得有线程(每1个 ...
- 列表list常用的方法
列表 name = ['Lisa', 'Mike', 'Jone', 'Lucy'] name.sort() # 排序,根据对应的ASCII码值,可以排各种类型的字符 name.append('Ms ...
- Test Case:: 12C ASMCMD New feature (Doc ID 1589249.1)
Test Case:: 12C ASMCMD New feature (Doc ID 1589249.1) APPLIES TO: Oracle Database - Enterprise Editi ...
- sublime相关操作
装插件 1,安装过Package Control ctrl + shift + p 输入install package 选择 Package Control: Install Package 搜索自己 ...
- java 主动信任证书
java 主动信任证书 SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mid.toCharArray() ...
- Vue 小练习01
有红, 黄, 蓝三个按钮, 以及一个200X200px的矩形box, 点击不同的按钮, box的颜色会被切换为指定的颜色 <!DOCTYPE html> <html lang=&qu ...
- Python三级菜单作业实现
数据结构: menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, ...
- 真正的RISC-V开发板——VEGA织女星开发板开箱评测
前言 由于最近ARM公司要求员工"停止所有与华为及其子公司正在生效的合约.支持及未决约定",即暂停与华为的相关合作,大家纷纷把注意力投向了另一个的处理器架构RISC-V,它是基于精 ...
- PAT 1012 The Best Rank 排序
To evaluate the performance of our first year CS majored students, we consider their grades of three ...
- PDF转换成DXF文件?PDF转DXF的操作方法
在CAD工作中,经常就需要将绘制完成的图纸文件的格式进行转换,那怎么将PDF文件转换成DXF格式的呢?具体要怎么来进行操作呢?本编教程小编就来教教大家具体操作方法,具体操作如下: 一.工具转换 推荐指 ...