一、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. Oracle11G_R2中共享服务器模式和专用服务器模式参数解释及设置

    sys@MYTESTDB> show parameterNAME TYPE VALUE------------------------------------ ----------- ----- ...

  2. Oracle - SPM固定执行计划

    1. 通过dbms_xplan.display_cursor查看指定sql都有哪些执行计划 SQL> select * from table(dbms_xplan.display_cursor( ...

  3. rocksdb学习笔记

    rocksdb是在leveldb的基础上优化而得,解决了leveldb的一些问题. 主要的优化点 1.增加了column family,这样有利于多个不相关的数据集存储在同一个db中,因为不同colu ...

  4. Keepalived集群软件高级使用(工作原理和状态通知)

    1.介绍 Keeaplived主要有两种应用场景,一个是通过配置keepalived结合ipvs做到负载均衡(LVS+Keepalived),有此需求者可参考以往博文:http://lizhenlia ...

  5. Redhat Linx使用Centos yum源

    一.故障现象: 在安装了Read linux后,使用yum安装软件,出现如下提示:[root@localhost~]# yum install xxxLoaded plugins: product-i ...

  6. kubernets学习笔记

    K8s CI :持续集成CD :持续交付 D --DeliveryCD :持续部署 D --Deployment Kubernetes Cluster: Masters: (3-host 做高可用)A ...

  7. Pro Micro

    选择这块Arduino板主要是因为它便宜(淘宝上20元左右搞定),引脚相对较多,体积小,而且其使用的处理器核心ATmega32U4(兼容Arduino Leonardo)可用于模拟HID设备,可以配合 ...

  8. Netty服务端Channel的创建与初始化

    Netty创建服务端Channel时,从服务端 ServerBootstrap 类的 bind 方法进入,下图是创建服务端Channel的函数调用链.在后续代码中通过反射的方式创建服务端Channel ...

  9. Add an Item to the Navigation Control 将项目添加到导航控件

    In this lesson, you will learn how to add an item to the navigation control. For this purpose, the N ...

  10. vue发送ajax请求

    一.vue-resource 1.简介 一款vue插件,用于处理ajax请求,vue1.x时广泛应用,现不被维护. 2.使用流程 step1:安装 [命令行输入] npm install vue-re ...