Spring(二)继承jdbcDaoSupport的方式实现(增删改查)
一 首先创建数据库表和相应的字段,并创建约束
二 建立项目,导入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的方式实现(增删改查)的更多相关文章
- Spring Boot入门系列(十八)整合mybatis,使用注解的方式实现增删改查
之前介绍了Spring Boot 整合mybatis 使用xml配置的方式实现增删改查,还介绍了自定义mapper 实现复杂多表关联查询.虽然目前 mybatis 使用xml 配置的方式 已经极大减轻 ...
- MyBatis学习(三)MyBatis基于动态代理方式的增删改查
1.前言 上一期讲到MyBatis-Statement版本的增删改查.可以发现.这种代码写下来冗余的地方特别多.写一套没啥.如果涉及到多表多查询的时候就容易出现问题.故.官方推荐了一种方法.即MyBa ...
- BitAdminCore框架应用篇:(二)创建一个简单的增删改查模块
NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/cookie ...
- Android-Sqlite-OOP方式操作增删改查
之前写的数据库增删改查,是使用SQL语句来实现的,Google 就为Android开发人员考虑,就算不会SQL语句也能实现增删改查,所以就有了OOP面向对象的增删改查方式 其实这种OOP面向对象的增删 ...
- 02.Mybatis的动态代理方式实现增删改查
动态代理的方式实现增删改查: 通过约定的方式定位sql语句 约定 > 配置文件 > 硬编码 约定的目标是省略掉通过硬编码的方式定位sql的代码,通过接口直接定位出sql语句,以下代码为通过 ...
- Mybatis学习笔记(二) 之实现数据库的增删改查
开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载 ...
- Day 18 :面向对象[基础,继承,组合]类的增删改查
有的人说,编程有3种范式: 1.面向过程:就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用就可以了. 2.面向函数:面向函数是面向过程的升级版,也就是把每个 ...
- SQLAlchemy(二):SQLAlchemy对数据的增删改查操作、属性常用数据类型详解
SQLAlchemy02 /SQLAlchemy对数据的增删改查操作.属性常用数据类型详解 目录 SQLAlchemy02 /SQLAlchemy对数据的增删改查操作.属性常用数据类型详解 1.用se ...
- MyBatis学习(二)MyBatis-Statement方式的增删改查
1.前期准备 项目骨架图如下所示 1.配置conf.xml <?xml version="1.0" encoding="UTF-8" ?> < ...
随机推荐
- C盘突然爆满
C盘突然爆满!幸好还开的机!~~ 因为是突然就爆满了,想着应该是虚拟内存的原因!于是就开始了探索.... 1.文件夹选项中把所有文件都显示出来. 2.在C盘你就会看到一个“pagefile.sys”的 ...
- ava怎样将"1413863429"字符串转换成datetime格式
.....一般来说应该是一个 毫秒数 String str ="1413863429"; Long timeLong = Long.parseLong(str); SimpleDa ...
- 干货分享:Neutron的PPT,帮助你理解Neutron的各种细节
深入解析Neutron http://files.cnblogs.com/popsuper1982/Neutron.pptx 经典的三节点部署 架构 怎么理解? 更加深入:Tap Interface ...
- RabbitMQ in Action (2): Running and administering Rabbit
Server management the Erlang node and the Erlang application Starting nodes multiple Erlang applicat ...
- [Swift]LeetCode509. 斐波那契数 | Fibonacci Number
The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such th ...
- [Swift]LeetCode929. 独特的电子邮件地址 | Unique Email Addresses
Every email consists of a local name and a domain name, separated by the @ sign. For example, in ali ...
- vue项目安装vux
本文章默认基于“vue init webpack myproject”已经搭好基本的项目, 而且本文是从我有道笔记拷贝稍加修改过来的 本来我私人笔记写给自己看的所以有些地方可能描述不够清晰 需要修改的 ...
- 【Storm篇】--Storm从初始到分布式搭建
一.前述 Storm是一个流式处理框架,相比较于SparkStreaming是一个微批处理框架,hadoop是一个批处理框架. 二 .搭建流程 1.集群规划 Nimbus Supervisor ...
- WebSocket(3)---实现一对一聊天功能
实现一对一聊天功能 功能介绍:实现A和B单独聊天功能,即A发消息给B只能B接收,同样B向A发消息只能A接收. 本篇博客是在上一遍基础上搭建,上一篇博客地址:[WebSocket]---实现游戏公告功能 ...
- 详解intellij idea搭建SSM框架(spring+maven+mybatis+mysql+junit)(下)
在上一篇(详解intellij idea 搭建SSM框架(spring+maven+mybatis+mysql+junit)(上))博文中已经介绍了关于SSM框架的各种基础配置,(对于SSM配置不熟悉 ...