使用注解方式实现账户的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 ...
随机推荐
- Web(八) commons-fileupload上传下载
在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6479405.html>,在此仅供学习参考之用. 一.上传 ...
- JavaScript日常学习5
JavaScript字符串属性和方法 eg :var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; var sln = txt.length; ...
- 阶段3 2.Spring_03.Spring的 IOC 和 DI_3 spring基于XML的IOC环境搭建和入门
创建新项目 修改为jar包的方式 把上一个工程内的代码 java下的com复制过来 由于配置文件没有,所以一运行就会报错 factory文件夹整个删除 dao的实现类 这里删除 测试类保留这两行代码 ...
- python调用不同目录中类的终极方法
1.在需要导入别的类包中加入这两行代码 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))sys.path.a ...
- Linux下源码安装MySQL-5.6.25
从mysql-5.5起,mysql源码安装开始使用cmake了,因此我们得先安装cmake,配置安装目录./configure --perfix=/.....的时候和以前的会有些区别. 一.安装cma ...
- Delphi实现带有格式的Excel导出功能
功能预览 运行预览 模板样式 存储返参 导出的Excel 2. 代码实现 //执行sql的函数 procedure TForm1.GetReportData(astrsql:string); var ...
- Unity 相机
相机属性 1.相机的Clear属性:Skybo背景会渲染天空盒:solid color背景为颜色:depth only仅仅深度,相当于优先级:Don`t Clear背景是上一帧的图像:2.Projec ...
- pandas的.columns和.index
可以通过.columns和.index着两个属性返回数据集的列索引和行索引 设data是pandas的一个DataFram类型的数据集. 则data.index返回一个index类型的行索引列表,da ...
- Pandas中关于 loc \ iloc 用法的理解
转载至:https://blog.csdn.net/w_weiying/article/details/81411257 loc函数:通过行索引 "Index" 中的具体值来取行数 ...
- Akka系列(四):Akka中的共享内存模型
前言...... 通过前几篇的学习,相信大家对Akka应该有所了解了,都说解决并发哪家强,JVM上面找Akka,那么Akka到底在解决并发问题上帮我们做了什么呢? 共享内存 众所周知,在处理并发问题上 ...