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=123

(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)控制台

  

原文地址:https://www.cnblogs.com/Zzzzn/p/11781261.html

时间: 2024-08-29 23:51:10

JdbcTemplate经典案例的相关文章

多线程十大经典案例之一 双线程读写队列数据

本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5136035 转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8646902 欢迎关注微博:http://weibo.com/MoreWindows 在<秒杀多线程系列>的前十五篇中介绍多线程的相关概念,多线程同步互斥问题<秒杀多线程第四篇一个经典的多线程同步问题>及解决多线程同步互斥的常用方法

java多线程经典案例

/** * 典型案例:子线程执行10次,主线程执行100次,两者交替50次. */ package cn.itcast.lesson4; public class TestWaitNotify { public static void main(String[] args){ final Business business= new Business(); new Thread( new Runnable() { public void run() { for(int i=1;i<=50;i++

秒杀多线程第十六篇 多线程十大经典案例之一 双线程读写队列数据

版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 本文配套程序下载地址为:http://download.csdn.net/detail/morewindows/5136035 转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8646902 欢迎关注微博:http://weibo.com/MoreWindows 在<秒杀多线程系列>的前十五篇中介绍多线程的相关概念,多线程同步互斥问题<秒杀多

网络机器人的识别与攻防的经典案例

本文我们介绍一个网络机器人的识别与攻防的经典案例.使用到的代码见本人的superword项目: https://github.com/ysc/superword/blob/master/src/main/java/org/apdplat/superword/tools/ProxyIp.java 我们的目的是要使用机器人自动获取站点http://ip.qiaodm.com/ 和站点http://proxy.goubanjia.com/ 的免费高速HTTP代理IP和端口号. 不过他们未对机器人进行识

拍卖行与邮件系统——设计者与开发者协作的经典案例

此文仅代表作者本人观点,如有槽点,欢迎吐槽. 设计者与开发者 设计者与开发者,游戏行业内直白的说法就是策划.美术跟程序.但不管在任何行业,这两者之间的关系都既像战友又像敌人.设计者的想法往往是完美的,而开发者的想法却是实际的,就好像梦想与现实一样.我听说过这样的话:美术设计的效果是100%的话,程序呈现出的效果能达到80%就已经很完美了.这当然不是在黑程序,程序出于某些原因无法完全实现效果(资源尺寸,资源通用性,性能考虑等等),毕竟梦想和现实是有差距的.在此案例中,我们仅讨论策划与程序. 拍卖行

(Mirage系列之八)Mirage经典案例之数据更新和恢复

在(Mirage系列之四)Mirage经典案例之集中桌面管理中我们介绍过,Mirage将客户端的数据根据策略备份到服务器上.备份数据的一个最重要的目的就是用户数据恢复,这次我们来讲如何从客户端恢复用户数据. 从客户端恢复用户数据,有以下几种情况: 1.      把文件恢复到以前的某个版本 2.      从存档中恢复文件和文件夹 3.      恢复删除的文件和文件夹 这里,存档指客户端在服务器上的备份.服务器会根据策略按预定的间隔备份客户端数据,从而产生多个存档. 以上三种方式本质上都是一样

(Mirage系列之七)Mirage经典案例之管理和发布应用层

在(Mirage系列之二)VMware Horizon Mirage的经典用户用例及真实案例分析中我们介绍过,Mirage从逻辑上把终端桌面分层了三层:系统层(包括驱动和基础层),应用层,以及用户数据层.在(Mirage系列之五)Mirage经典案例之桌面驱动和基础层管理中我们讲到Mirage可以灵活方便的管理终端的驱动并且发布基础层.本文将介绍Mirage如何管理终端的应用层. 一个公司往往有很多部门,各部门所需要的工作软件也不尽相同.Mirage通过分层这个核心技术,将应用层剥离出来,使得管

Spark - 经典案例

初识 Spark 大数据处理,目前还只是小白阶段,初步搭建起运行环境,慢慢学习之. 本文熟悉下 Spark 数据处理的几个经典案例. Word Count import org.apache.spark.SparkConf import org.apache.spark.SparkContext object WordCount { def main(args: Array[String]) { if (args.length < 1) { System.err.println("Usag

javascript的理解及经典案例

js的简介: JavaScript是一种能让你的网页更加生动活泼的程式语言,也是目前网页中设计中最容易学又最方便的语言. 你可以利用JavaScript轻易的做出亲切的欢迎讯息.漂亮的数字钟.有广告效果的跑马灯及简易的选举,还可以显示浏览器停留的时间.让这些特殊效果提高网页的可观性. javascript现在可以再网页上做很多很多事情,网页特效,操作dom,html5游戏(基于html5和JavaScript的结合),动画等等特效,还可以实现拉去后台数据(通过ajax),不仅可以做前台还可以做后