一、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经典案例的更多相关文章

  1. javascript的理解及经典案例

    js的简介: JavaScript是一种能让你的网页更加生动活泼的程式语言,也是目前网页中设计中最容易学又最方便的语言. 你可以利用JavaScript轻易的做出亲切的欢迎讯息.漂亮的数字钟.有广告效 ...

  2. jQuery基础的工厂函数以及定时器的经典案例

    1. jQuery的基本信息:  1.1 定义: jQuery是JavaScript的程序库之一,它是JavaScript对象和实用函数的封装, 1.2 作用: 许多使用JavaScript能实现的交 ...

  3. Linux运维之道(大量经典案例、问题分析,运维案头书,红帽推荐)

    Linux运维之道(大量经典案例.问题分析,运维案头书,红帽推荐) 丁明一 编   ISBN 978-7-121-21877-4 2014年1月出版 定价:69.00元 448页 16开 编辑推荐 1 ...

  4. 经典案例:那些让人赞不绝口的创新 HTML5 网站

    在过去的10年里,网页设计师使用 Flash.JavaScript 或其他复杂的软件和技术来创建网站.但现在你可以前所未有的快速.轻松地设计或创造互动的.有趣好看的网站.如何创建?答案是 HTML5 ...

  5. Altera OpenCL用于计算机领域的13个经典案例(转)

    英文出自:Streamcomputing 转自:http://www.csdn.net/article/2013-10-29/2817319-the-application-areas-opencl- ...

  6. php中foreach()函数与Array数组经典案例讲解

    //php中foreach()函数与Array数组经典案例讲解 function getVal($v) { return $v; //可以加任意检查代码,列入要求$v必须是数字,或过滤非法字符串等.} ...

  7. 阿里云资深DBA专家罗龙九:云数据库十大经典案例分析【转载】

    阿里云资深DBA专家罗龙九:云数据库十大经典案例分析 2016-07-21 06:33 本文已获阿里云授权发布,转载具体要求见文末 摘要:本文根据阿里云资深DBA专家罗龙九在首届阿里巴巴在线峰会的&l ...

  8. 经典案例之MouseJack

    引言:在昨天的文章<无线键鼠监听与劫持>中,我们提到今天会向您介绍一个无线键鼠的监听与劫持的经典案例,<MouseJack>:MouseJack能利用无线鼠标和键盘存在的一些问 ...

  9. HTML5 CSS3 经典案例:无插件拖拽上传图片 (支持预览与批量) (二)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/31513065 上一篇已经实现了这个项目的整体的HTML和CSS: HTML5 C ...

随机推荐

  1. java编译报错: 找不到或无法加载主类 Demo.class 的解决方法

    原因:java 命令后面的文件不能有后缀名. 解决方法:运行java时候,后面的文件去掉后缀名.

  2. 监控利器-Prometheus安装与部署+实现邮箱报警

    Prometheus(普罗米修斯)监控 环境准备: 三台docker主机(centos7):docker01:172.16.1.30部署服务:Prometheus server,Grafana,Nod ...

  3. 12c新特性 在线操作数据文件

    我们都知道,oracle pre-12c之前,若是想要把一个数据文件改名或者迁移, 必须在归档模式下先把这个数据文件offline之后, 然后进行OS上的copy或者rename 操作, 最后在sql ...

  4. NFS挂载遇到的问题

    问题描述:生产环境中需要经常运用NFS挂载,就在测试环境中测试一下,将服务器中192.168.1.4 /u01/app/oracle/product/11.2.0/dbhome_1/dbs  挂载到1 ...

  5. Lua 5.1 学习笔记

    1 简介 2 语法 2.1 语法约定 2.1.1 保留关键字 2.1.2 操作符 2.1.3 字符串定义 2.2 值与类型 2.2.1 强制转换 2.3 变量 2.3.1 索引 2.3.2 环境表 2 ...

  6. Java基础 - volatile

    volatile的作用:对与volatile修饰的变量, 1,保证该变量对所有线程的可见性. 2,禁止指令重排序. Java内存模型(JMM) 原子性 i = 2; 把i加载到工作内存副本i,副本i= ...

  7. 关于大数据T+1执行流程

    关于大数据T+1执行流程 前提: 搭建好大数据环境(hadoop hive hbase sqoop zookeeper oozie hue) 1.将所有数据库的数据汇总到hive (这里有三种数据源 ...

  8. 记一次Tomcat启动报错Failed to start component [StandardEngine[Catalina].Standard

    今天启动项目的时候,发现tomcat一直报错,之前都一直没有问题的啊,提示       org.apache.catalina.LifecycleException: Failed to start ...

  9. SpringCloud微服务(05):Zuul组件,实现路由网关控制

    本文源码:GitHub·点这里 || GitEE·点这里 一.Zuul组件简介 1.基础概念 Zuul 网关主要提供动态路由,监控,弹性,安全管控等功能.在分布式的微服务系统中,系统被拆为了多个微服务 ...

  10. Django之web框架原理

    Web框架原理 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 先写一个 原始的web框架 imp ...