一 首先创建数据库表和相应的字段,并创建约束

二 建立项目,导入jar包(ioc,aop,dao,数据库驱动,连接池)并且将applicationContext.xml文件放在src下

三 开启组件扫描,并且配置数据源

四 编写一个实体类(提供get set)方法,toString(),有参和无参以及序列化

五 设置一个接口根据银行账号完成增删改查

六 实现Dao接口,继承jdbcDaoSupport,在实现类上加上对应的标注,将实现类对象放入容器中,给jdbcDaoSupport赋值dataSource

七 测试结果

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: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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 开启组件扫描 -->
<context:component-scan base-package="com.xdl"></context:component-scan>
<!-- 引入外部db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>

数据库表

 drop table xdl_bank_account cascade constraints;
create table xdl_bank_account(
id number constraint xdl_bank_account_id_pk primary key,
acc_no varchar2(30) constraint xdl_bank_account_acc_no_uk unique,
acc_password varchar2(30),
acc_money number
);
drop sequence xdl_bank_account_id_seq;
create sequence xdl_bank_account_id_seq;
insert into xdl_bank_account values (xdl_bank_account_id_seq.nextval,'acc_abc1','1231',1234561);

Bean

 package com.xdl.bean;

 import java.io.Serializable;

 public class XdlBankAccount implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String acc_no;
private String acc_password;
private String acc_money; public XdlBankAccount(int id) {
super();
this.id = id;
} public XdlBankAccount(String acc_no, String acc_password, String acc_money) {
super();
this.acc_no = acc_no;
this.acc_password = acc_password;
this.acc_money = acc_money;
} public XdlBankAccount(int id, String acc_no, String acc_password, String acc_money) {
super();
this.id = id;
this.acc_no = acc_no;
this.acc_password = acc_password;
this.acc_money = acc_money;
} public XdlBankAccount() {
super();
// TODO Auto-generated constructor stub
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getAcc_no() {
return acc_no;
} public void setAcc_no(String acc_no) {
this.acc_no = acc_no;
} public String getAcc_password() {
return acc_password;
} public void setAcc_password(String acc_password) {
this.acc_password = acc_password;
} public String getAcc_money() {
return acc_money;
} public void setAcc_money(String acc_money) {
this.acc_money = acc_money;
} @Override
public String toString() {
return "XdlBankAccount [id=" + id + ", acc_no=" + acc_no + ", acc_password=" + acc_password + ", acc_money="
+ acc_money + "]\n";
}
}

Dao

 package com.xdl.dao;

 import java.util.List;

 import com.xdl.bean.XdlBankAccount;

 public interface XdlBankAccountDao {
// 根据银行账户acc_no查询银行账户对象
XdlBankAccount getBankAccountByAccNo(String acc_no); // 根据id查询银行账户对象
XdlBankAccount getBanAccountByAccId(int id); // 查询所有的银行账户
List<XdlBankAccount> getBankAccountAll(); // 更新数据
int updateBankAccount(XdlBankAccount account); // 向银行表中插入数据
int insertBankAccount(XdlBankAccount account); // 根据id删除银行账户
int deleteBankAccount(XdlBankAccount account);
}

Dao的实现类

 package com.xdl.impl;

 import java.util.List;

 import javax.annotation.Resource;
import javax.sql.DataSource; import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository; import com.sun.org.apache.bcel.internal.generic.RET;
import com.xdl.bean.XdlBankAccount;
import com.xdl.dao.XdlBankAccountDao;
import com.xdl.mapper.XdlBankAccountMapper; @Repository("bankDao")
public class XdlBankAccountDaoOracleImpl extends JdbcDaoSupport implements XdlBankAccountDao {
@Resource(name = "dataSource")
public void setMyDataSource(DataSource dataSource) {
// 赋值给父类
super.setDataSource(dataSource);
} // 根据银行账户acc_no查询银行账户对象
@Override
public XdlBankAccount getBankAccountByAccNo(String acc_no) {
String sql = "select * from xdl_bank_account where acc_no = ?";
// return super.getJdbcTemplate().queryForObject(sql, requiredType, acc_no);
try {
return super.getJdbcTemplate().queryForObject(sql, new XdlBankAccountMapper(), acc_no);
} catch (DataAccessException e) {
e.printStackTrace();
}
return null;
/**
* Spring Dao框架没有做吧结果集翻译成对象过程
*/
} // 根据id查询银行账户对象
/*
* @Override public XdlBankAccount getBanAccountByAccId(int id) { String sql =
* "select * from xdl_bank_account where id = ?"; try { return
* super.getJdbcTemplate().queryForObject(sql, new XdlBankAccountMapper(), id);
* } catch (DataAccessException e) { e.printStackTrace(); } return null; }
*/
// 根据id查询银行账户对象
@Override
public XdlBankAccount getBanAccountByAccId(int id) {
String sql = "select * from xdl_bank_account where id = ?";
List<XdlBankAccount> accounts = super.getJdbcTemplate().query(sql, new XdlBankAccountMapper(), id);
return accounts.isEmpty() ? null : accounts.get(0);
} // 查询所有的银行账户信息
@Override
public List<XdlBankAccount> getBankAccountAll() {
String sql = "select * from xdl_bank_account";
return super.getJdbcTemplate().query(sql, new XdlBankAccountMapper());
} // 更新数据
@Override
public int updateBankAccount(XdlBankAccount account) {
String sql = "update xdl_bank_account set acc_password = ?,acc_money = ? where id = ? and acc_no = ?";
return super.getJdbcTemplate().update(sql, account.getAcc_password(), account.getAcc_money(), account.getId(),
account.getAcc_no());
} // 根据id删除银行账户
@Override
public int deleteBankAccount(XdlBankAccount account) {
String sql = "delete from xdl_bank_account where id = ?";
try {
return super.getJdbcTemplate().update(sql, account.getId());
} catch (DataAccessException e) {
e.printStackTrace();
}
return 0;
} // 向银行表中插入数据
@Override
public int insertBankAccount(XdlBankAccount account) {
String sql = "insert into xdl_bank_account values (xdl_bank_account_id_seq.nextval,?,?,?)";
try {
return super.getJdbcTemplate().update(sql, account.getAcc_no(), account.getAcc_password(),
account.getAcc_money());
} catch (DataAccessException e) {
e.printStackTrace();
}
return 0;
}
}

RowMapper(行映射)

 package com.xdl.mapper;

 import java.sql.ResultSet;
import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; import com.xdl.bean.XdlBankAccount; //T代表返回的类型
public class XdlBankAccountMapper implements RowMapper<XdlBankAccount> { @Override
// rs是结果集,index代表数据到了第几条
public XdlBankAccount mapRow(ResultSet rs, int index) throws SQLException {
// 在这里写结果集中的数据如何转换成银行账户对象
return new XdlBankAccount(rs.getInt("id"), rs.getString("acc_no"), rs.getString("acc_password"),
rs.getString("acc_money"));
} }

测试类

 package com.xdl.test;

 import static org.junit.Assert.*;

 import java.util.List;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.xdl.bean.XdlBankAccount;
import com.xdl.dao.XdlBankAccountDao; public class Test {
ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
XdlBankAccountDao bankAccountDao = ioc.getBean("bankDao", XdlBankAccountDao.class); // 根据银行账户acc_no查询银行账户对象
@org.junit.Test
public void testGetBankAccountByAccNo() {
XdlBankAccount bankAccount = bankAccountDao.getBankAccountByAccNo("acc_abb");
System.out.println(bankAccount);
} // 根据id查询银行账户对象
@org.junit.Test
public void testGetBankAccountById() {
XdlBankAccount bankAccount = bankAccountDao.getBanAccountByAccId(2);
System.out.println(bankAccount);
} // 查询所有的银行账户信息
@org.junit.Test
public void testGetBankAccountAll() {
List<XdlBankAccount> accounts = bankAccountDao.getBankAccountAll();
System.out.println(accounts);
} // 更新数据
@org.junit.Test
public void testUpdateBankAccount() {
int updateBankAccount = bankAccountDao.updateBankAccount(new XdlBankAccount(2, "acc_abb", "456", "111"));
System.out.println(updateBankAccount);
} // 根据id删除银行账户数据
@org.junit.Test
public void testDeleteBankAccount() {
int deleteBankAccount = bankAccountDao.deleteBankAccount(new XdlBankAccount(2));
System.out.println(deleteBankAccount);
} // 向银行表中插入数据
@org.junit.Test
public void testInsertBankAccount() {
int insertBankAccount = bankAccountDao.insertBankAccount(new XdlBankAccount("wangcai", "12345", "5555"));
System.out.println(insertBankAccount);
} }

Spring(二)继承jdbcDaoSupport的方式实现(增删改查)的更多相关文章

  1. Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查

    之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...

  2. MyBatis学习(三)MyBatis基于动态代理方式的增删改查

    1.前言 上一期讲到MyBatis-Statement版本的增删改查.可以发现.这种代码写下来冗余的地方特别多.写一套没啥.如果涉及到多表多查询的时候就容易出现问题.故.官方推荐了一种方法.即MyBa ...

  3. BitAdminCore框架应用篇:(二)创建一个简单的增删改查模块

    NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookie ...

  4. Android-Sqlite-OOP方式操作增删改查

    之前写的数据库增删改查,是使用SQL语句来实现的,Google 就为Android开发人员考虑,就算不会SQL语句也能实现增删改查,所以就有了OOP面向对象的增删改查方式 其实这种OOP面向对象的增删 ...

  5. 02.Mybatis的动态代理方式实现增删改查

    动态代理的方式实现增删改查: 通过约定的方式定位sql语句 约定 > 配置文件 > 硬编码 约定的目标是省略掉通过硬编码的方式定位sql的代码,通过接口直接定位出sql语句,以下代码为通过 ...

  6. Mybatis学习笔记(二) 之实现数据库的增删改查

    开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载 ...

  7. Day 18 :面向对象[基础,继承,组合]类的增删改查

    有的人说,编程有3种范式: 1.面向过程:就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用就可以了. 2.面向函数:面向函数是面向过程的升级版,也就是把每个 ...

  8. SQLAlchemy(二):SQLAlchemy对数据的增删改查操作、属性常用数据类型详解

    SQLAlchemy02 /SQLAlchemy对数据的增删改查操作.属性常用数据类型详解 目录 SQLAlchemy02 /SQLAlchemy对数据的增删改查操作.属性常用数据类型详解 1.用se ...

  9. MyBatis学习(二)MyBatis-Statement方式的增删改查

    1.前期准备 项目骨架图如下所示 1.配置conf.xml <?xml version="1.0" encoding="UTF-8" ?> < ...

随机推荐

  1. unittest中的测试固件

    运行下面的两段代码,看看有什么不同? 第一段: import unittest from selenium import webdriver class F2(unittest.TestCase): ...

  2. Content-Type: application/www-form-urlencoded

    默认的方式 1.Content-Type: application/www-form-urlencoded id=3&fgf=56&908rr=767 2.Content-Type:a ...

  3. AI应用开发实战

    AI应用开发实战 出发点 目前,人工智能在语音.文字.图像的识别与解析领域带来了跨越式的发展,各种框架.算法如雨后春笋一般,互联网上随处可见与机器学习有关的学习资源,各大mooc平台.博客.公开课都推 ...

  4. 【RL-TCPnet网络教程】第17章 RL-TCPnet之UDP通信

    第17章      RL-TCPnet之UDP通信 本章节为大家讲解RL-TCPnet的UDP通信实现,学习本章节前,务必要优先学习第16章UDP用户数据报协议基础知识.有了这些基础知识之后,再搞本章 ...

  5. [Swift]LeetCode872. 叶子相似的树 | Leaf-Similar Trees

    Consider all the leaves of a binary tree.  From left to right order, the values of those leaves form ...

  6. Markdown语法简介

    1.标题 在想要设置为标题的文字前面加#来表示一个#代表一级标题,总共六级 2.字体 加粗要加粗的文字左右分别用两个*号包起来 斜体要倾斜的文字左右分别用一个*号包起来 斜体加粗要倾斜和加粗的文字左右 ...

  7. Elasticsearch基础知识分享

    1. Elasticsearch背景介绍 Elasticsearch 是一个基于 Lucene 的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful web 接口.Elast ...

  8. VMware修改为静态ip

    选择编辑-虚拟机网路编辑器-NAT模式记录 本机cmd执行命令:ipconfig /all  查看VMnet8的ip地址,跟虚拟机子网ip一个网段 确定. su - root 切换到root用户下 修 ...

  9. Redis Windows下安装方法

    一.安装 首先在网上下载Redis,下载地址:https://github.com/MicrosoftArchive/redis/releases 根据电脑系统的实际情况选择32位还是64位,在这里我 ...

  10. Nginx篇--解读nginx配置

    一.前述 之前讲解了Nginx的源码安装与加载到系统服务中去,http://www.cnblogs.com/LHWorldBlog/p/8298226.html今天详细讲解Nginx中的具体配置. 二 ...