Spring复习笔记:3
Spring基于xml的案例实践
在数据库中创建一张新的表
create table account(id int primary key auto_increment,name varchar(40),money float
)character set utf8 collate utf8_general_ci;
往表中导入数据
insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);
创建Maven项目,导入坐标
<?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>org.example</groupId><artifactId>Spring06</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.8.RELEASE</version></dependency><dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.7</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.20</version></dependency><dependency><groupId>com.mchange</groupId><artifactId>c3p0</artifactId><version>0.9.5.5</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13</version><scope>test</scope></dependency></dependencies></project>
配置文件
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置Service--><bean id="accountService" class="com.service.impl.AccountServiceImpl"><!--注入dao对象--><property name="accountDao" ref="accountDao"/></bean><bean id="accountDao" class="com.dao.impl.AccountDaoImpl"><!--注入QueryRunner--><property name="runner" ref="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://localhost:3306/spring1"/><property name="user" value="root"/><property name="password" value="123456"/></bean>
</beans>
我们可以看到,QueryRunner对象这里我们使用了多例操作,这是因为单例操作同一个数据可能会造成线程安全问题,所以我们使用多例操作。
创建Dao接口
package com.dao;import com.domain.Account;import java.util.List;/*** 账户的持久层接口*/
public interface AccountDao {/*** 查询所有* @return*/List<Account> findAllAccount();/*** 查询一个* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 删除* @param accountId*/void deleteAccount(Integer accountId);}
创建Dao实体类
package com.dao.impl;import com.dao.AccountDao;
import com.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;import java.util.List;/*** 账户的持久层实现类*/
public class AccountDaoImpl implements AccountDao {private QueryRunner runner;public void setRunner(QueryRunner runner) {this.runner = runner;}public List<Account> findAllAccount() {try{return runner.query("select * from account",new BeanListHandler<Account>(Account.class));}catch (Exception e){throw new RuntimeException(e);}}public Account findAccountById(Integer accountId) {try{return runner.query("select * from account where id = ? ",new BeanHandler<Account>(Account.class),accountId);}catch (Exception e){throw new RuntimeException(e);}}public void saveAccount(Account account) {try{runner.update("insert into account(name,money) value(?,?)",account.getName(),account.getMoney());}catch (Exception e){throw new RuntimeException(e);}}public void updateAccount(Account account) {try{runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());}catch (Exception e){throw new RuntimeException(e);}}public void deleteAccount(Integer accountId) {try{runner.update("delete from account where id=?",accountId);}catch (Exception e){throw new RuntimeException(e);}}
}
创建Service接口
package com.service;import com.domain.Account;import java.util.List;/*** 账户的业务层接口*/
public interface AccountService {/*** 查询所有* @return*/List<Account> findAllAccount();/*** 查询一个* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 删除* @param accountId*/void deleteAccount(Integer accountId);
}
创建Service实体类
package com.service.impl;import com.dao.AccountDao;
import com.domain.Account;
import com.service.AccountService;import java.util.List;/*** 账户的业务层实现类*/
public class AccountServiceImpl implements AccountService {private AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}public List<Account> findAllAccount() {return accountDao.findAllAccount();}public Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}public void saveAccount(Account account) {accountDao.saveAccount(account);}public void updateAccount(Account account) {accountDao.updateAccount(account);}public void deleteAccount(Integer accountId) {accountDao.deleteAccount(accountId);}
}
domain实体类
package com.domain;import java.io.Serializable;/*** 账户的实体类*/
public class Account implements Serializable {private Integer id;private String name;private Float 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 Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';}
}
测试类
package com.test;import com.domain.Account;
import com.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;/*** 使用Junit单元测试,测试我们的配置*/
public class AccountServiceTest {@Testpublic void testFindAll(){//1.获取容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//2.得到业务层对象AccountService accountService = applicationContext.getBean("accountService",AccountService.class);List<Account> accounts = accountService.findAllAccount();for (Account account:accounts){System.out.println(account);}}@Testpublic void testFindOne(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");AccountService accountService = applicationContext.getBean("accountService",AccountService.class);Account account = accountService.findAccountById(1);System.out.println(account);}@Testpublic void testSave(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");AccountService accountService = applicationContext.getBean("accountService",AccountService.class);Account account = new Account();account.setName("test");account.setMoney(12345f);accountService.saveAccount(account);}@Testpublic void testUpdate(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");AccountService accountService = applicationContext.getBean("accountService",AccountService.class);Account account = accountService.findAccountById(4);account.setName("test1");account.setMoney(1111f);accountService.updateAccount(account);}@Testpublic void testDelete(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");AccountService accountService = applicationContext.getBean("accountService",AccountService.class);accountService.deleteAccount(1);}
}
项目视图
部分运行结果:
Spring基于注解的案例实践
基于注解的很简单,只要修改一下上面的几个文件就可以了
修改配置文件
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--告知Spring在创建容器时要扫描的包--><context:component-scan base-package="com"/><!--配置QueryRunner对象--><bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"><!--注入数据源--><constructor-arg name="ds" ref="dateSource"/></bean><!--配置数据源--><bean id="dateSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--配置数据库的必备信息--><property name="driverClass" value="com.mysql.cj.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring1"/><property name="user" value="root"/><property name="password" value="123456"/></bean>
</beans>
修改Dao实体类
package com.dao.impl;import com.dao.AccountDao;
import com.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.util.List;/*** 账户的持久层实现类*/
@Repository(value = "accountDao")
public class AccountDaoImpl implements AccountDao {@Autowired //使用注解按类型自动注入private QueryRunner runner;public List<Account> findAllAccount() {try{return runner.query("select * from account",new BeanListHandler<Account>(Account.class));}catch (Exception e){throw new RuntimeException(e);}}public Account findAccountById(Integer accountId) {try{return runner.query("select * from account where id = ? ",new BeanHandler<Account>(Account.class),accountId);}catch (Exception e){throw new RuntimeException(e);}}public void saveAccount(Account account) {try{runner.update("insert into account(name,money) value(?,?)",account.getName(),account.getMoney());}catch (Exception e){throw new RuntimeException(e);}}public void updateAccount(Account account) {try{runner.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());}catch (Exception e){throw new RuntimeException(e);}}public void deleteAccount(Integer accountId) {try{runner.update("delete from account where id=?",accountId);}catch (Exception e){throw new RuntimeException(e);}}
}
修改Service实体类
package com.service.impl;import com.dao.AccountDao;
import com.domain.Account;
import com.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;/*** 账户的业务层实现类*/
@Service(value = "accountService")
public class AccountServiceImpl implements AccountService {@Autowired //使用注解按类型自动注入private AccountDao accountDao;public List<Account> findAllAccount() {return accountDao.findAllAccount();}public Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}public void saveAccount(Account account) {accountDao.saveAccount(account);}public void updateAccount(Account account) {accountDao.updateAccount(account);}public void deleteAccount(Integer accountId) {accountDao.deleteAccount(accountId);}
}
其他的保持和原来的不变即可
部分运行结果
相关文章:

Blender多米诺骨牌动画学习教程 The Impossible Domino Run in Blender
流派:电子学习| MP4 |视频:h264,1280720 |音频:AAC,48.0 KHz 语言:英语中英文字幕(根据原英文字幕机译更准确)|大小:8.53 GB 含课程文件 |时长:8h 20m Blender的运动跟踪,建模,渲染和合成工具集…

unity3d游戏开发猜想——当程序猿老去
程序猿将代码注入生命去打造互联网的浪潮之巅。当有一天他们老了。会走向那里,会做些什么?非常多年以后,在我60岁的那天早晨,天刚蒙蒙亮我就起床了,先去公园晨练,然后回来做早餐(50岁的时候我学…

【JavaScript】JavaScript基础-变量、运算符与控制语句
一.变量 变量: 定义一个变量,系统会为之分配一块内存,程序可以用变量名来表示这块内存中的数据。 由于javascript采用的是弱类型的变量形式,因此,在声明一个变量的时候,我们不必声明它的类型,但…

ROW_NUMBER() OVER()函数用法详解 (分组排序 例子多)
ROW_NUMBER() OVER()函数用法详解 (分组排序 例子多) https://blog.csdn.net/qq_25221835/article/details/82762416 posted on 2019-09-05 01:00 竹径风声 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/girl1314/p/11462711.html

Blender+Substance Painter全流程制作真实的机器人学习教程
MP4 |视频:h264,1280720 |音频:AAC,44.1 KHz,2 Ch 语言:英语中英文字幕(根据原英文字幕机译更准确) |时长:44节课(10h 52m) |大小解压后:9.9 GB 含课程素材 如何使用Blender 2.8和Substance Painter制作真…

Spring复习笔记:4
在复习笔记三中我们进行的案例的编写,我们可以发现,就算使用了注解的方式,xml配置文件文件还是不能够删除,现在我们来将一些新的注解可以让我们去掉xml配置文件。 Configuration 作用:指定当前类是一个配置类 细节&a…

Extjs PROXY查询params无法传参,改用extraParams
转载于:https://www.cnblogs.com/cocoat/p/5153009.html

详解Paint的setPathEffect(PathEffect effect)
一、setPathEffect() 这个方法一看就和path有关,顾名思义,它就是给path设置样式(效果)的。PathEffect这个路径效果类没有具体的实现,效果是由它的六个子类实现的: 这六个子类分别可以实现不同的路径效果&am…

返回手势导致页面卡死并且UI错乱的问题解决
问题记录:在做了部分页面的转场动画之后,返回手势不灵了,快速连续返回的话会卡住,App退到后台再重新激活之后页面不卡了,但是UI错乱. 解决方案: 1. 在UINavigationController子类实现代理UIGestureRecognizerDelegate,并在viewDidLoad方法中增加代理设置: - (void)viewDidLoad …

Spring学习笔记:3(面向切面AOP)
AOP:Aspect Oriented Program(面向切面) 我们再回顾一下AOP的一些术语: 通知(Advice) 就是你想要的功能,也就是的安全、事物、日志等。先定义好,然后在想用的地方用一下。 连接…

Blender全流程制作真实感3D产品学习教程
MP4 |视频:h264,1280720 |音频:AAC,44.1 KHz,2 Ch 语言:英语中英文字幕(根据原英文字幕机译更准确) |时长:41节课(4h 29m) |大小解压后:4.53 GB 仅使用blender 2.8进行建模、纹理、光照和渲染,…

如何给iOS应用添加原生的二维码扫描功能
之前总觉得二维码扫描很高大上,其实apple工程师早就为我们提供了便捷的方法。二维码扫描第三方的库也挺多的,不过效率高的当属系统提供的扫描方法。 二维码扫描主要用到了以下几个类:AVCaptureDevice,AVCaptureDeviceInput,AVCaptureMetadata…

2021-2027年中国市医疗电子场投资分析及前景预测报告
【报告类型】产业研究 【报告价格】4500起 【出版时间】即时更新(交付时间约3个工作日) 【发布机构】智研瞻产业研究院 【报告格式】PDF版 本报告介绍了中国医疗电子行业市场行业相关概述、中国医疗电子行业市场行业运行环境、分析了中国医疗电子行…

RabbitMQ入门(4)--路由
2019独角兽企业重金招聘Python工程师标准>>> ###路由 ###(使用Java客户端) 在先前的指南中,我们建立了一个简单的日志系统。我们可以将我们的日志信息广播到多个接收者。 在这部分的指南中,我们将要往其中添加一个功能…

从一个数组中寻找出现奇数次的数字
假设给定了数组nums为[0,1,2,3,4,5,6,7,8,9,10,10,9,8,7,6,5,4,1,2,3,3,0] 其中3出现了3次 而其他数字都出现了两次 则我们应该得到结果为3 第一种方式:使用Hash 1 /**2 * 使用hash3 * */4 public static int singleNumber_1(int[] nums) {5 …

Blender写实建筑场景制作学习教程 Exterior Visualization in Blender 2.9
MP4 |视频:h264,1280720 |音频:AAC,44.1 KHz,2 Ch 语言:英语中英文字幕(根据原英文字幕机译更准确) |时长:26节课(3h 41m) |大小:3.3 GB 使用Blender创建惊人的外部渲染。 你会学到: Blender中的建模、着色…

Postman增删改查接口测试
查 一.选择Get方式 二.点击Send开始测试,得出结果 增 一.选择Post方式 二.输入需要增添的数据 三.点击Send测试 四.没有报错,添加成功 查看一下,发现确实添加进去了 删 一.选择Delete方式 二.根据id删除,在请求路径下直接写出…

新安装Ubuntu加载时提示“为/检查磁盘时发生严重错误”的解决方法
本文部分内容转载自: http://jingyan.baidu.com/article/0aa22375bbffbe88cc0d6419.html http://www.aichengxu.com/view/35086 解决方法: 1. 进入Ubuntu启动菜单时按e 键进入启动项编辑模式: 2. 找到代码【ro rootflagsync】,将其…

android binder机制之——(创建binder服务)
Binder机制编程前面的几篇文章具体介绍了android中binder机制的方方面面,相信你对binder机制已经有了较深刻的理解。俗话说得好“学以致用”,以下我们就通过在android系统中创建一个我们自己的binder服务,来加深对binder机制的理解。…

通过NSProxy来解决NSTimer使用不当造成内存泄漏的问题
NSTimer的一般使用: 1 interface ViewController : UIViewController2 property (nonatomic, strong) NSTimer *timer;3 end4 5 implementation ViewController6 - (void)viewDidLoad {7 [super viewDidLoad];8 [self startTimer];9 } 10 11 - (void)startTimer { 12 …

Blender 2.9中的真实感三维产品全流程制作学习教程
MP4 |视频:h264,1280720 |音频:AAC,44.1 KHz,2 Ch 语言:英语中英文字幕(根据原英文字幕机译更准确) |时长:41节课(6h 23m) |大小:5.83 GB 含课程文件 使用blender 2.9建模、纹理、光照和渲染真实的吉他 …

SpringBoot中的SFL4J日志
SpringBoot:底层是Spring框架,Spring框架默认使用的是JCL日志的抽象层 SpringBoot选用SLF4J和logback 如何系统地在系统中使用SLF4J 在开发的使用,日志记录方法的调用,不应该来直接调用日志的实现类,而是调用日志抽象…

web开发性能优化---用户体验篇
怎样从技术角度怎样增强用户体验。都是非常多平台都在做的事情,依据个人实际经验碰到几种体验做下总结。1、降低页面刷新白屏适当使用ajax技术。改善刷新白屏现象。2、信息提醒,邮件、站内信、短信在购物流程、售后流程适当添加信息温馨提醒环节…

linux命令2--cd和pwd
2019独角兽企业重金招聘Python工程师标准>>> cd是linux中最为常见的一个命令,其作用就是切换到某一个路径下 例子1 到自己的用户目录下 cd ~ 也可以直接cd 例子2 返回进入当前目录之前的目录 cd - 例子3 把上一个命令作为cd的参数命令 cd !$ 参考文档&am…

COALESCE语句解救sql的sum问题
mysqlmybatis有一个sql语句是统计用的 <select id"getNum" resultType"map"> 结果是一个map, select语句的结果是一些sum select sum(t.anum),sum(t.bnum)from tableSum twhere t.id #{id} 调试发现,数据库明明记录已经落入&…

在UE5创造一个多山的松树森林场景学习教程
UE5游戏场景设计制作视频教程 大小解压后:4.37G 1920X1080 mp4 语言:英语中英字幕(机译)时长:5小时 20分 课程获取:在UE5创造一个多山的松树森林场景学习教程

编写纳新网站后端的相关知识总结
使用HSSFWorkbook导出数据库中的数据 导入Apache POI Maven jar包 <!-- Apache POI --> <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.6</version> </dependency>在控制层编…

IOSUIcontrol事件
UIKit提供了一组控件:UISwitch开关、UIButton按钮、UISegmentedControl分段控件、UISlider滑块、UITextField文本字段控件、UIPageControl分页控件。 控件是对UIView派生类的实用增强及补充,并可以直接附着于导航栏、表格单元,甚至更大的对象…

Xcode Debugging
程序员日常开发中有大量时间都会花费在 debug 上,从事 iOS 开发不可避免地需要使用 Xcode。这篇博客就主要介绍了 Xcode 中几种能够大幅提升代码调试效率的方式。“If debugging is the process of removing bugs, then programming must be the process of putting…

css小技巧 -- 单标签实现单行文字居中,多行文字居左
可能出现的尺寸场景: 代码如下: <!DOCTYPE html> <html lang"zh"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><meta http-equiv&q…