创建一张数据表account

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);




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.dym</groupId><artifactId>day02_eesy_02account_xmlioc</artifactId><version>1.0-SNAPSHOT</version><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.6</source><target>1.6</target></configuration></plugin></plugins></build><packaging>jar</packaging><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.2.RELEASE</version></dependency><dependency><groupId>commons-dbutils</groupId><artifactId>commons-dbutils</artifactId><version>1.4</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1.2</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies>
</project>

Account.java

package com.itheima.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 +'}';}
}

IAccountDao.java

package com.itheima.dao;import com.itheima.domain.Account;import java.util.List;/*** 账户的持久层接口*/
public interface IAccountDao {/*** 查询所有* @return*/List<Account> findAllAccount();/*** 查询一个* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 删除* @param acccountId*/void deleteAccount(Integer acccountId);
}

AccountDaoImpl.java

package com.itheima.dao.impl;import com.itheima.dao.IAccountDao;
import com.itheima.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 IAccountDao {private QueryRunner runner;public void setRunner(QueryRunner runner) {this.runner = runner;}@Overridepublic List<Account> findAllAccount() {try{return runner.query("select * from account",new BeanListHandler<Account>(Account.class));}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic 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);}}@Overridepublic void saveAccount(Account account) {try{runner.update("insert into account(name,money) values(?,?)",account.getName(),account.getMoney());}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic 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);}}@Overridepublic void deleteAccount(Integer accountId) {try{runner.update("delete from account where id=?",accountId);}catch (Exception e) {throw new RuntimeException(e);}}
}

IAccountService.java

package com.itheima.service;import com.itheima.domain.Account;import java.util.List;/*** 账户的业务层接口*/
public interface IAccountService {/*** 查询所有* @return*/List<Account> findAllAccount();/*** 查询一个* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 删除* @param acccountId*/void deleteAccount(Integer acccountId);
}

AccountServiceImpl.java

package com.itheima.service.impl;import com.itheima.dao.IAccountDao;
import com.itheima.domain.Account;
import com.itheima.service.IAccountService;import java.util.List;/*** 账户的业务层实现类*/
public class AccountServiceImpl implements IAccountService{private IAccountDao accountDao;public void setAccountDao(IAccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic List<Account> findAllAccount() {return accountDao.findAllAccount();}@Overridepublic Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}@Overridepublic void saveAccount(Account account) {accountDao.saveAccount(account);}@Overridepublic void updateAccount(Account account) {accountDao.updateAccount(account);}@Overridepublic void deleteAccount(Integer acccountId) {accountDao.deleteAccount(acccountId);}
}

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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 配置Service --><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><!-- 注入dao --><property name="accountDao" ref="accountDao"></property></bean><!--配置Dao对象--><bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"><!-- 注入QueryRunner --><property name="runner" ref="runner"></property></bean><!--配置QueryRunner--><bean id="runner" 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="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property><property name="user" value="root"></property><property name="password" value="root"></property></bean>
</beans>

AccountServiceTest.java

package com.itheima.test;import com.itheima.domain.Account;
import com.itheima.service.IAccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.List;/*** 使用Junit单元测试*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {@Autowiredprivate  IAccountService as;@Testpublic void testFindAll() {//3.执行方法List<Account> accounts = as.findAllAccount();for(Account account : accounts){System.out.println(account);}}@Testpublic void testFindOne() {//3.执行方法Account account = as.findAccountById(1);System.out.println(account);}@Testpublic void testSave() {Account account = new Account();account.setName("test");account.setMoney(12345f);//3.执行方法as.saveAccount(account);}@Testpublic void testUpdate() {//3.执行方法Account account = as.findAccountById(4);account.setMoney(23456f);as.updateAccount(account);}@Testpublic void testDelete() {//3.执行方法as.deleteAccount(4);}
}

基于XML的IOC案例相关推荐

  1. 【Spring】基于XML的IOC案例

    代码结构: bean.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&quo ...

  2. spring框架的概述以及spring中基于XML的IOC配置——概念

    1.spring的概述     spring是什么     spring的两大核心     spring的发展历程和优势     spring体系结构 2.程序的耦合及解耦     曾经案例中问题   ...

  3. 一步一步手绘Spring IOC运行时序图二(基于XML的IOC容器初始化)

    相关内容: 架构师系列内容:架构师学习笔记(持续更新) 一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程) 一步一步手绘Spring IOC运行时序图二(基于XM ...

  4. 基于Xml 的IOC 容器-载入<list>的子元素

    在BeanDefinitionParserDelegate 类中的parseListElement()方法就是具体实现解析<property>元素中的<list>集合子元素,源 ...

  5. 基于Xml 的IOC 容器-载入<bean>元素

    Bean 配置信息中的<import>和<alias>元素解析在DefaultBeanDefinitionDocumentReader 中已经完成,对Bean 配置信息中使用最 ...

  6. 基于Xml 的IOC 容器-将配置载入内存

    BeanDefinitionDocumentReader 接口通过registerBeanDefinitions() 方法调用其实现类DefaultBeanDefinitionDocumentRead ...

  7. 基于Xml 的IOC 容器-分配解析策略

    XmlBeanDefinitionReader 类中的doLoadBeanDefinition()方法是从特定XML 文件中实际载入Bean 配置资源的方法,该方法在载入Bean 配置资源之后将其转换 ...

  8. 基于Xml 的IOC 容器-准备文档对象

    DocumentLoader 将Bean 配置资源转换成Document 对象的源码如下: //使用标准的JAXP将载入的Bean定义资源转换成document对象 @Override public ...

  9. 基于Xml 的IOC 容器-获得配置路径

    通过分析ClassPathXmlApplicationContext 的源代码可以知道, 在创建ClassPathXmlApplicationContext 容器时,构造方法做以下两项重要工作: 首先 ...

最新文章

  1. [转]Python UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 的解决办法...
  2. MCU,RTOS,物联网之间的关系。
  3. select * 和select 所有字段的区别
  4. django集成ansibe实现自动化
  5. Mysql学习总结(66)——设置MYSQL数据库编码为UTF-8
  6. tomcat中conf\Catalina\localhost目录下的J2EE项目META-INF配置文件
  7. BDD(行为驱动开发)
  8. 大数据滥用 借贷平台肆意妄为的背后
  9. 三菱PLC史上最全视频教程!(视频+数据+例程)
  10. macOS 使用软件(外加装逼特效)
  11. [教程]教你如何制作彩色的3D打印Groot
  12. 正弦稳态电路的阻抗和功率
  13. 机器学习-28-Conditional Generation by RNNAttention(条件生成和注意力机制)
  14. 开源权限引擎可能理解了骇客帝国
  15. vue-cli 实现反向代理获取猫眼数据
  16. Linux定时任务及企业级案例故障模拟
  17. arecord录制音频和aplay播放音频用法说明
  18. 笔记本wifi只剩下飞行模式
  19. 直流稳压稳流开关电源使用说明(LW-6020)
  20. 企业应用业务需求变化的分析与应对-常见的业务需求变化

热门文章

  1. C#在DataTable中使用LINQ
  2. Maven快速创建SpringMVC web(1)
  3. JavaScript权威指南--window对象
  4. asp.net 页面全生命周期
  5. Windows 7玩魔兽争霸冰封王座3的解决方法
  6. [原创软件测试工作技能
  7. Fedora Core 4配置本地yum源
  8. 开机启动加载驱动过程中调用PostMessage函数出错
  9. 是啥意思_227大团结是什么梗啥意思 微博227大团结事件始末介绍
  10. 神奇的HyperLogLog算法