使用注解方式实现账户的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 ...
随机推荐
- Pycharm断点调试入门
断点调试是在开发过程中常用的功能,能清楚看到代码运行的过程,有利于代码问题跟踪.对我这个小白开发来说,还有一个作用是快速熟悉代码,拿到别人写的代码,有时看不太懂或看的很吃力,光这样看很无感,但是通过断 ...
- django 如何传递id 参数
urls.py 注意这里的bid
- CMake下,某些选项的后调整
编译安卓NDK库时,发现在R15的NDK编译出来的库,总是带了-g选项,导致附带调试,文件过大. 搜索一番后,结论是NDK的文件中有问题: https://github.com/android/ndk ...
- 如何在 Spring/Spring Boot 中做参数校验
数据的校验的重要性就不用说了,即使在前端对数据进行校验的情况下,我们还是要对传入后端的数据再进行一遍校验,避免用户绕过浏览器直接通过一些 HTTP 工具直接向后端请求一些违法数据. 本文结合自己在项目 ...
- Day06:方法 / 猜字母游戏
JAVA方法 方法就是处理一个业务所需要编写的代码的代码段 方法特性 一个方法处理一个业务 方法代码编写,不和其他方法冲突 方法定义后可以随意调用 将main方法中的所有代码分散到各个普通方法中 减少 ...
- AGC035 B - Even Degrees【思维·树形结构的妙用】
题目传送门 一句话题意: 首先,每一条边会产生1个入度,1个出度,因此,如果边的数量是奇数的话,图的所有节点的总出度就是奇数,不可能每个节点的出度都是偶数,因此无解. 有解时,我们先找出原图中的一棵生 ...
- [转帖]X86_64平台上利用qemu安装aarch64架构的虚拟机
X86_64平台上利用qemu安装aarch64架构的虚拟机 https://blog.csdn.net/chenxiangneu/article/details/78955462 遇到原作者一样的问 ...
- java 集合 队列(Queue)
特点:特殊线性表,先进先出(FIFO first-in-first-out) 方法: java5中 添加java.util.Queue接口,java.util.Collection 的扩展类 add( ...
- vim编辑Dockerfile时语法高亮
参考Dockerfile构建容器---语法高亮 三个文件扔进相关的目录即可 1. /usr/share/vim/vimfiles/doc/dockerfile.txt *dockerfile.txt* ...
- 通过sohu获取浏览器端IP地址
接口:http://pv.sohu.com/cityjson?ie=utf-8