使用注解方式实现账户的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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import java.sql.SQLException;
import java.util.List; @Repository
public class AccountDaoImpl implements IAccountDao { @Autowired
private 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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import java.util.List; @Service
public class AccountServiceImpl implements IAccountService { @Autowired
private IAccountDao 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="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.xsd"> <!--告知Spring创建容器的时候要扫描的包-->
<context:component-scan base-package="com.sunxiaping.spring5"></context:component-scan>
<!--配置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 test() { IAccountService accountService = context.getBean(IAccountService.class);
Account account = new Account();
account.setName("xxx");
account.setMoney(1.2);
accountService.save(account);
} }
使用注解方式实现账户的CRUD的更多相关文章
- 使用纯注解方式实现账户的CRUD
1 需求和技术要求 1.1 需求 实现账户的CRUD. 1.2 技术要求 使用Spring的IOC实现对象的管理. 使用QueryRunner作为持久层的解决方案. 使用C3p0作为数据源. 2 搭建 ...
- 使用XML的方式实现账户的CRUD
1 需求和技术要求 1.1 需求 实现账户的CRUD. 1.2 技术要求 使用Spring的IOC实现对象的管理. 使用QueryRunner作为持久层的解决方案. 使用C3p0作为数据源. 2 环境 ...
- MyBatis使用注解方式实现CRUD操作
一.使用注解后就不需要写SysGroupDaoMapper.xml 只需要在Dao的抽象方法前加上相应的注解就可以. package cn.mg39.ssm01.dao; import java.ut ...
- hibernate annotation注解方式来处理映射关系
在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式 ...
- hibernate注解方式来处理映射关系
在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式 ...
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- (转)Spring使用AspectJ进行AOP的开发:注解方式
http://blog.csdn.net/yerenyuan_pku/article/details/69790950 Spring使用AspectJ进行AOP的开发:注解方式 之前我已讲过Sprin ...
- Spring Boot整合Mybatis(注解方式和XML方式)
其实对我个人而言还是不够熟悉JPA.hibernate,所以觉得这两种框架使用起来好麻烦啊. 一直用的Mybatis作为持久层框架, JPA(Hibernate)主张所有的SQL都用Java代码生成, ...
- MyBatis 及 MyBatis Plus 纯注解方式配置(Spring Boot + Postgresql)
说明 当前的版本为 MyBatis 3.5.9 MyBatis Plus 3.5.1 Spring Boot 2.6.4 Postgresql 42.3.3 与 Spring Boot 结合使用 My ...
随机推荐
- WPF RTSP存储到一个文件中的位置
Vlc.DotNet.Core.Medias.MediaBase media1 = new Vlc.DotNet.Core.Medias.PathMedia(" CodeGo.net med ...
- Python基本语法_函数属性 & 参数类型 & 偏函数的应用
目录 目录 前言 软件环境 Python Module的程序入口 函数的属性 Python函数的创建 函数的参数 必备参数 缺省参数 命名参数 不定长参数 匿名参数 偏函数的应用 前言 Python除 ...
- 60第K个排列
题目:给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列.按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" &quo ...
- Flume采集日志
角色 Source 数据来源 (exec, kafka, http…)Channel 数据通道 (memory,file,jdbc)Sink 数据目的地 (kafka,hdfs,es…) Agent ...
- DocX 在文档中插入图片时,为什么不能按实际设置的大小插入,而Spire.Doc却可以
我的目标目标要求:将一个图片插入到页面中,页面边界为0,使用下面的代码去实现(button1UseDocX_Click函数),生成的文档不能达到目的.而使用Spire.Doc却能达到目的button1 ...
- 短信验证码api
最近遇到的项目需要个随机短信验证码实现注册用户 选用的是“云信使”,因为有15条免费的测试短信可以验证代码是否正确调用该短信api 地址 进入短信平台 一.实名认证 + 短信模板 用户认证完成后,创建 ...
- Linux解决Python调用Matlab函数无法导入matlab.engine问题及其他注意事项
问题描述 Linux系统,根据matlab官方文档说明,利用Matlab中的API来实现Python调用Matlab函数.具体方法见文档: https://ww2.mathworks.cn/help/ ...
- vue如何实现热更新
我们都知道,对于node来说,前端vue代码的迭代节奏是很快的,可能一周要迭代几次,但是node的迭代却没那么平凡,可能一周更新一次甚至更久,那么为了node服务的稳定,减少node服务的发布次数,是 ...
- vue,基于element的tree组件封装
封装组件代码 // 组件:树 /* 参数说明-属性: 1.treeData:展示数据(array) 2.treeEmptyText:内容为空的时候展示的文本(String) 3.treeNodeKey ...
- java script 的注释与分号
// 单行注释 /**/多行注释 在js 中 变量.函数和操作符都是区分大小写的 什么是标识符 变量.函数.属性的名字.或者函数的参数. 变量的命名规范:不能以数字开头. 变量声明: var nam ...