Spring基于xml的CRUD
基于xml的CRUD
- 使用C3P0连接池
- 使用dbutils包中的QueryRunner类来对数据库进行操作
代码实现
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zjw</groupId>
<artifactId>day02_eesy_02account_xmlioc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<encoding>UTF-8</encoding>
<spring.version>6.1.1</spring.version>
<lombok.version>1.18.30</lombok.version>
<mysql.version>8.0.33</mysql.version>
<dbutils.version>1.7</dbutils.version>
<c3p0.version>0.9.1.2</c3p0.version>
<junit.version>4.13.2</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>${dbutils.version}</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
bean.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置Service-->
<bean id="accountService" class="com.zjw.service.impl.AccountServiceImpl">
<!--注入dao-->
<property name="accountDao" ref="accountDao"/>
</bean>
<!--配置Dao对象-->
<bean id="accountDao" class="com.zjw.dao.impl.AccountDaoImpl">
<!--使用spring 方法注入创建多例的runner Spring应用了CGLIB(动态代理)-->
<lookup-method name="getRunner" bean="runner"/>
</bean>
<!--配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--注入数据源-->
<constructor-arg name="ds" ref="dataSource"/>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/eesy_spring?useSSL=false&serverTimeZone=Asia\Shanghai"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
</beans>
Dao层实现
package com.zjw.dao.impl;
import com.zjw.dao.IAccountDao;
import com.zjw.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;
/**
* 账户的持久层实现类
*/
//@Getter
public class AccountDaoImpl implements IAccountDao {
private QueryRunner runner;
// 虽然有get方法,但是并不会执行,而是由容器提供一个动态代理的实现
public QueryRunner getRunner() {
System.out.println("getRunner方法执行....");
return runner;
}
@Override
public List<Account> findAllAccount() {
try {
return getRunner().query("SELECT * FROM account",new BeanListHandler<>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public Account findAccountById(Integer accountId) {
try {
return getRunner().query("SELECT * FROM account WHERE id=?",new BeanHandler<>(Account.class),accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void saveAccount(Account account) {
try {
getRunner().update("INSERT INTO account(name,money) VALUES(?,?)",account.getName(),account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void updateAccount(Account account) {
try {
getRunner().update("UPDATE account SET name=?,money=? WHERE id=?",account.getName(),account.getMoney(),account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void deleteAccount(Integer accountId) {
try {
getRunner().update("DELETE FROM account WHERE id=?",accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
Service层实现
package com.zjw.service.impl;
import com.zjw.dao.IAccountDao;
import com.zjw.domain.Account;
import com.zjw.service.IAccountService;
import lombok.Setter;
import java.util.List;
/**
* 账户的业务层实现类
* @author zjw
*/
@Setter
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
@Override
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
@Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
@Override
public void saveAccount(Account account) {
accountDao.saveAccount(account);
}
@Override
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}
@Override
public void deleteAccount(Integer accountId) {
accountDao.deleteAccount(accountId);
}
}
测试
package com.zjw.test;
import com.zjw.domain.Account;
import com.zjw.service.IAccountService;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* 使用Junit单元测试,测试我们的配置
*/
public class AccountServiceTest {
private ApplicationContext ac ;
/**
* 1. 初始化容器
*/
@Before
public void init(){
ac = new ClassPathXmlApplicationContext("bean.xml");
}
@Test
public void testFindAll(){
IAccountService accountService = ac.getBean("accountService", IAccountService.class);
List<Account> accountList = accountService.findAllAccount();
for (Account account : accountList) {
System.out.println(account);
}
}
@Test
public void testFindAccountById(){
IAccountService accountService = ac.getBean("accountService", IAccountService.class);
Account account = accountService.findAccountById(1);
System.out.println(account);
}
@Test
public void testSaveAccount(){
IAccountService accountService = ac.getBean("accountService", IAccountService.class);
Account account = new Account();
account.setId(7);
account.setName("abc");
account.setMoney(9999f);
accountService.saveAccount(account);
}
@Test
public void testUpdateAccount(){
IAccountService accountService = ac.getBean("accountService", IAccountService.class);
Account account = accountService.findAccountById(1);
System.out.println(account);
account.setMoney(888F);
accountService.updateAccount(account);
}
@Test
public void testDeleteAccount(){
IAccountService accountService = ac.getBean("accountService", IAccountService.class);
accountService.deleteAccount(7);
}
}
Spring基于xml的CRUD的更多相关文章
- spring基于xml的声明式事务控制配置步骤
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring 基于xml的申明式AspectH中的后置通知的返回值获取
spring 基于xml的申明式AspectH中的后置通知的返回值获取 1. 配置文件 <aop:config> <aop:aspect ref="myAspect&quo ...
- spring 基于XML的申明式AspectJ通知的执行顺序
spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于XML装配Bean
Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...
- spring 基于XML和注解的两种事务配置方式
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring基于XML的声明式事务控制
<?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.sp ...
- Spring 基于XML配置
基于XML的配置 对于基于XML的配置,Spring 1.0的配置文件采用DTD格式,Spring2.0以后采用Schema格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...
- spring基于xml的事务控制
opm配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...
- Spring入门之使用 spring 的 IOC 解决程序耦合(Spring 基于 XML 的 IOC 细节[掌握])(03-02)
3.3Spring 基于 XML 的 IOC 细节[掌握] 3.3.1 spring 中工厂的类结构图 3.3.1.1 BeanFactory 和 ApplicationContext 的区别 Bea ...
- Spring基于XML方式的使用
一.IoC配置 IoC的配置是通过Spring的xml文件的bean标签进行的. 1.bean标签介绍 bean标签一般是在xml文件进行配置的,xml文件一般样式如下: <?xml versi ...
随机推荐
- [白话解析] 通俗解析集成学习之GBDT
[白话解析] 通俗解析集成学习之GBDT 目录 [白话解析] 通俗解析集成学习之GBDT 0x00 摘要 0x01 定义 & 简述 1. GBDT(Gradient Boosting Deci ...
- Ansible之二playbook
反星系 连接https://galaxy.ansible.com下载相应的roles 列出所有安装的 galaxy ansible-galaxy list 安装galaxy ansibl ...
- 闲话 6.19/CF1938M
CF1938M 计数以下序列 \(\lang a\rang\) 的个数: \[\sum_{i=1}^m a_i=n\\ \forall 1<i<m,(a_i-a_{i-1})(a_i-a_ ...
- Flink同步kafka到iceberg(cos存储)
一.flink到logger 1.source create table source_table ( id bigint comment '唯一编号' ,order_number bigint co ...
- 从SSH远程到Git Push:在Windows上一步到位实现免密码登录
前言 我一直希望在Windows上能像在Linux系统中那样,通过SSH密钥实现免密码远程连接.每次远程连接到服务器时,手动输入密码既麻烦又不太安全,尤其是在我需要频繁操作的情况下. 之前的文章中已经 ...
- 1个小技巧彻底解决DeepSeek服务繁忙!
DeepSeek 是国内顶尖 AI 团队「深度求索」开发的多模态大模型,具备数学推理.代码生成等深度能力,堪称"AI界的六边形战士". DeepSeek 最具代表性的标签有以下两个 ...
- Deepseek学习随笔(11)--- 普通人如何抓住DeepSeek红利(附网盘链接)
一.文档简介 这个文档是清华大学新闻与传播学院新媒体研究中心发布的<普通人如何抓住DeepSeek红利>,该文件详细介绍了DeepSeek的功能.应用场景.使用技巧以及如何通过提示词驱动提 ...
- Typecho的Joe主题开启文章导航目录树
引言 发现从typora复制过来的markdown代码中的目录导航[toc]语句没生效, 没有像typora或其他markdown编辑器生成导航目录树, 网上搜了下, 发现个解决方法, 在主题设置里插 ...
- Linux - sshpass的安装与使用
ssh 登陆不能在命令行中指定密码,sshpass 的出现则解决了这一问题.它允许你用 -p 参数指定明文密码,然后直接登录远程服务器,它支持密码从命令行.文件.环境变量中读取. 安装 1.下载ssh ...
- docker - [13] docker网络
smo smo smo "狂神" omz omz omz 一.理解网络 本机回环地址:127.0.0.1,本机IP地址:192.168.2.131,docker地址:172.17. ...