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" ?> < ...
随机推荐
- Ubuntu添加中文输入法
1.点击左上角Dash home 2.输入keyboard input methods 3.点击keyboard input methods,选择input methods,打钩. 4.点击下三角,选 ...
- 最小生成树 kruskal算法&prim算法
(先更新到这,后面有时间再补,嘤嘤嘤) 今天给大家简单的讲一下最小生成树的问题吧!(ps:本人目前还比较菜,所以最小生成树最后的结果只能输出最小的权值,不能打印最小生成树的路径) 本Tianc在刚学的 ...
- python语法_内置函数
a = filter(函数名,序列) 返回一个迭代器对象/.函数里必须加过滤条件 ret = ['a','b','c','d','e'] def ft(s): if s != 'a': return ...
- Lua学习----零碎知识点
Jit(just in time) 动态即时编译,边运行时边编译---->lua (主要是面向进程) Aot(ahead of time) 静态提前编译,运行前编译---->C#(主要是面 ...
- APP测试工具与技术
AndroidDevTools Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的Android SDK.开发中用到的工具.An ...
- PHP全栈从入门到精通1
thinkphp框架,是一堆代码(常量,方法,和类)的集合,框架是一个半成品的应用,还包含一些优秀的设计模式. 框架的使用,代码风格不一样,维护难,项目生命周期短,功能扩展存在局限,好处为,简单,快捷 ...
- [Swift]LeetCode343. 整数拆分 | Integer Break
Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...
- [Swift]LeetCode537. 复数乘法 | Complex Number Multiplication
Given two strings representing two complex numbers. You need to return a string representing their m ...
- Python数据写入csv格式文件
(只是传递,基础知识也是根基) Python读取数据,并存入Excel打开的CSV格式文件内! 这里需要用到bs4,csv,codecs,os模块. 废话不多说,直接写代码!该重要的内容都已经注释了, ...
- Centos7中文乱码问题的解决
刚安装centos7之后,语言默认不是中文,导致中文路径或中文文件在系统中显示为乱码,查了些资料解决了这个问题. 1 查看和安装中文库 [root@bogon ~]# echo $LANG zh_CN ...