一、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. VS2017无法打开Razor视图文件提示:引发类型"System.Exception"异常

    背景介绍 由于电脑装了R#(吃内存大户),导致VS2017打开项目慢以及卡顿,因此在扩展和更新这个功能里面将没用的插件关闭了,导致.NET CORE的Razor视图文件打不开(真心是一脸懵逼,关个插件 ...

  2. Virtual Box复制虚拟机

    用惯了VM换BOX还是有点不大一样的,比如复制 我用了2个网卡,所以看起来麻烦了一次 注释了UUID 应该有更好的办法来操作,下面的看起来有点啰嗦,先凑合着这样弄... 1.关机 2.复制 3.改名称 ...

  3. Shell—详解$0、$1、$2、$#、$*、$@、$?、$$变量

    预定义变量:常用来获取命令行的输入 变量 作用 $0 当前Shell脚本本身的文件名称 $1 脚本接收的第一个参数($1-$9:第1-9个命令行参数名) $2 脚本接收的第二个参数($1-$9:第1- ...

  4. combination sum && combination sum II

    1.Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), f ...

  5. BitMap原理

    BitMap原理  

  6. springboot 多环境

    springboot 多环境 --spring.profiles.active=dev 查看 Ioc 容器 PostProcessorRegistrationDelegate

  7. 【Linux命令】用户身份(useradd,groupadd,usermod,passwd,userdel)

    目录 用户身份 useradd userdel usermod groupadd groupdel passwd chage 用户身份 在linux系统中和windows一样有用户之分.root用户为 ...

  8. ubuntu18.04 安装 flameshot截图工具

    安装flameshot:https://github.com/lupoDharkael/flameshot sudo apt-get install flameshot 然后设置一个快捷键,设置> ...

  9. (转)深入解析TensorFlow中滑动平均模型与代码实现

    本文链接:https://blog.csdn.net/m0_38106113/article/details/81542863 指数加权平均算法的原理 TensorFlow中的滑动平均模型使用的是滑动 ...

  10. 松软科技带你学前端:JavaScript 使用

    <script> 标签 在 HTML 中,JavaScript 代码必须位于 <script> 与 </script> 标签之间. 实例 <script> ...