使用XML的方式实现账户的CRUD
1 需求和技术要求
1.1 需求
- 实现账户的CRUD。
1.2 技术要求
- 使用Spring的IOC实现对象的管理。
- 使用QueryRunner作为持久层的解决方案。
- 使用C3p0作为数据源。
2 环境搭建并配置
2.1 导入所需要的依赖jar包的maven坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.4</version>
</dependency>
2.2 数据库脚本
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0; -- ----------------------------
-- Table structure for account
-- ----------------------------
DROP TABLE IF EXISTS `account`;
CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '名称',
`money` double(11, 0) NULL DEFAULT NULL COMMENT '余额',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ----------------------------
-- Records of account
-- ----------------------------
INSERT INTO `account` VALUES (1, 'aaa', 1000);
INSERT INTO `account` VALUES (2, 'bbb', 1000); SET FOREIGN_KEY_CHECKS = 1;
2.3 编写实体类
- Account.java
package com.sunxiaping.spring5.domain; import java.io.Serializable; /**
* 账户
*/
public class Account implements Serializable { private Integer id; private String name; private Double money; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Double getMoney() {
return money;
} public void setMoney(Double money) {
this.money = money;
}
}
2.4 编写持久层代码
- IAccountDao.java
package com.sunxiaping.spring5.dao; import com.sunxiaping.spring5.domain.Account; import java.util.List; /**
* 账户的持久层接口
*/
public interface IAccountDao { /**
* 保存账户
*
* @param account
*/
void save(Account account); /**
* 更新账户
*
* @param account
*/
void update(Account account); /**
* 删除账户
*
* @param id
*/
void delete(Integer id); /**
* 根据主键查询账户信息
*
* @param id
* @return
*/
Account findById(Integer id); /**
* 查询所有
*
* @return
*/
List<Account> findAll(); }
- AccountDaoImpl.java
package com.sunxiaping.spring5.dao.impl; import com.sunxiaping.spring5.dao.IAccountDao;
import com.sunxiaping.spring5.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler; import java.sql.SQLException;
import java.util.List; public class AccountDaoImpl implements IAccountDao { private QueryRunner queryRunner; public void setQueryRunner(QueryRunner queryRunner) {
this.queryRunner = queryRunner;
} @Override
public void save(Account account) {
try {
queryRunner.update("insert into account(name,money) values (?,?)", account.getName(), account.getMoney());
} catch (SQLException e) {
e.printStackTrace();
}
} @Override
public void update(Account account) {
try {
queryRunner.update("update account set name=?,money=? where id = ?", account.getName(), account.getMoney(), account.getId());
} catch (SQLException e) {
e.printStackTrace();
}
} @Override
public void delete(Integer id) {
try {
queryRunner.update("delete from account where id = ?", id);
} catch (SQLException e) {
e.printStackTrace();
}
} @Override
public Account findById(Integer id) {
try {
return queryRunner.query("select * from account where id = ?", new BeanHandler<>(Account.class), id);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
} @Override
public List<Account> findAll() {
try {
return queryRunner.query("select * from account", new BeanListHandler<>(Account.class));
} catch (SQLException e) {
e.printStackTrace();
} return null;
}
}
2.5 编写业务层代码
- IAccountService.java
package com.sunxiaping.spring5.service;
import com.sunxiaping.spring5.domain.Account;
import java.util.List;
public interface IAccountService {
/**
* 保存账户
*
* @param account
*/
void save(Account account);
/**
* 更新账户
*
* @param account
*/
void update(Account account);
/**
* 删除账户
*
* @param id
*/
void delete(Integer id);
/**
* 根据主键查询账户信息
*
* @param id
* @return
*/
Account findById(Integer id);
/**
* 查询所有
*
* @return
*/
List<Account> findAll();
}
- AccountServiceImpl.java
package com.sunxiaping.spring5.service.impl; import com.sunxiaping.spring5.dao.IAccountDao;
import com.sunxiaping.spring5.domain.Account;
import com.sunxiaping.spring5.service.IAccountService; import java.util.List; public class AccountServiceImpl implements IAccountService { private IAccountDao accountDao; public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
} @Override
public void save(Account account) {
accountDao.save(account);
} @Override
public void update(Account account) {
accountDao.update(account);
} @Override
public void delete(Integer id) {
accountDao.delete(id);
} @Override
public Account findById(Integer id) {
return accountDao.findById(id);
} @Override
public List<Account> findAll() {
return accountDao.findAll();
}
}
2.6 创建并配置applicationContext.xml
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置service-->
<bean id="accountService" class="com.sunxiaping.spring5.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--配置dao-->
<bean id="accountDao" class="com.sunxiaping.spring5.dao.impl.AccountDaoImpl">
<property name="queryRunner" ref="queryRunner"/>
</bean>
<!--配置QueryRunner-->
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean> <!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true"></property>
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean> </beans>
2.7 测试
- 示例:
package com.sunxiaping; import com.sunxiaping.spring5.domain.Account;
import com.sunxiaping.spring5.service.IAccountService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class spring5Test {
ApplicationContext context = null; @Before
public void before() {
context = new ClassPathXmlApplicationContext("applicationContext.xml");
} @Test
public void testSave() {
Account account = new Account();
account.setName("ddd");
account.setMoney(20.34); IAccountService accountService = context.getBean("accountService", IAccountService.class);
accountService.save(account);
} }
使用XML的方式实现账户的CRUD的更多相关文章
- 使用注解方式实现账户的CRUD
1 需求和技术要求 1.1 需求 实现账户的CRUD. 1.2 技术要求 使用Spring的IOC实现对象的管理. 使用QueryRunner作为持久层的解决方案. 使用C3p0作为数据源. 2 环境 ...
- 使用纯注解方式实现账户的CRUD
1 需求和技术要求 1.1 需求 实现账户的CRUD. 1.2 技术要求 使用Spring的IOC实现对象的管理. 使用QueryRunner作为持久层的解决方案. 使用C3p0作为数据源. 2 搭建 ...
- xml解析方式之JAXP解析入门
XML解析 1 引入 xml文件除了给开发者看,更多的情况使用[程序读取xml文件]的内容.这叫做xml解析 2 XML解析方式(原理不同) DOM解析 SAX解析 3 XML解析工具 DOM解析原理 ...
- 转-springAOP基于XML配置文件方式
springAOP基于XML配置文件方式 时间 2014-03-28 20:11:12 CSDN博客 原文 http://blog.csdn.net/yantingmei/article/deta ...
- Android中的三种XML解析方式
在Android中提供了三种解析XML的方式:SAX(Simple API XML),DOM(Document Objrect Model),以及Android推荐的Pull解析方式.下面就对三种解析 ...
- struts_20_对Action中所有方法、某一个方法进行输入校验(基于XML配置方式实现输入校验)
第01步:导包 第02步:配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app ...
- struts2视频学习笔记 22-23(基于XML配置方式实现对action的所有方法及部分方法进行校验)
课时22 基于XML配置方式实现对action的所有方法进行校验 使用基于XML配置方式实现输入校验时,Action也需要继承ActionSupport,并且提供校验文件,校验文件和action类 ...
- hibernate 联合主键生成机制(组合主键XML配置方式)
hibernate 联合主键生成机制(组合主键XML配置方式) 如果数据库中用多个字段而不仅仅是一个字段作为主键,也就是联合主键,这个时候就可以使用hibernate提供的联合主键生成策略. 具体 ...
- web.xml中配置Spring中applicationContext.xml的方式
2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...
随机推荐
- 三十六:数据库之SQLAlchemy外建之一对一关系
relationship()的uselist参数默认为True,即一对多,如果要一对一,则需让uselist=False 准备工作 from sqlalchemy import create_engi ...
- ABAP 实现内表自定义的F4功能
“实现多列内容的F4功能 REPORT Z_TAB_TEST. TYPES: shlp_descr TYPE shlp_descr . DATA: BEGIN OF itab OCCURS 0 ...
- Linux进程的虚拟内存
简介 用户进程的虚拟地址空间是Linux的一个重要的抽象:它为每个运行进程提供了同样的系统视图,这使得多个进程可以同时运行,而不会干扰到其他进程内存中的内容. 每个应用程序都有自己的线性地址空间,与所 ...
- 一张图包含SEO一切要点
看到一张好图 from http://www.rongyipiao.com/?p=8
- webdriver中判断元素是否存在的方法
selenium.webdriver中没有内置的判断元素是否存在的方法,所以定义一个方法,如果找到该元素则返回True,否则返回False: from selenium import webdrive ...
- Linux系统中使用Nignx配置反向代理负载均衡
目录 使用nginx实现动静分离的负载均衡集群 使用nginx实现负载均衡和动静分离 使用nginx实现动静分离的负载均衡集群 Nginx官网源码包下载链接:http://nginx.org/en/d ...
- springboot - 应用实践(1)认识springboot
1.为什么要推出springboot springboot设计的目的是用来简化新spring应用的初始搭建以及开发过程.springboot遵循“约定优于配置”原则. 2.springboot默认的配 ...
- 开启linux服务器防火墙
启用ufwsudo ufw enablesudo ufw default deny运行以上两条命令后,开启了防火墙,并在系统启动时自动开启.关闭所有外部对本机的访问,但本机访问外部正常. 开启和禁用s ...
- [codeforces1234F]Yet Another Substring Reverse
题目链接 大致题意为将某个子串进行翻转后,使得不包含相同字符的字符子串长度最长.只能翻转一次或零次. 设一个子串的状态为包含字符的二进制.如子串为$abacd$,则状态为$00000000000000 ...
- 三校联训 小澳的葫芦(calabash) 题解
题面:小澳的葫芦[ 题目描述]小澳最喜欢的歌曲就是<葫芦娃>.一日表演唱歌,他尽了洪荒之力,唱响心中圣歌.随之,小澳进入了葫芦世界.葫芦世界有 n 个葫芦,标号为 1~ n. n 个葫芦由 ...