一、Spring基于纯 XML 的IoC配置


第一步:新建实体类 Account.java 来映射上面的数据库表

package cn.lemon.domain;public class Account {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;}
}

第二步:新建数据库访问层(dao)
IAccountDao.java接口

package cn.lemon.dao;import cn.lemon.domain.Account;import java.util.List;public interface IAccountDao {void addAccount(Account account);void deleteAccount(Integer accountId);void updateAccount(Account account);Account findOne(Integer accountId);List<Account> findAll();
}

接口的实现类 AccountDaoImpl.java

package cn.lemon.dao.impl;import cn.lemon.dao.IAccountDao;
import cn.lemon.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;import java.util.List;public class AccountDaoImpl implements IAccountDao {private JdbcTemplate jdbcTemplate;public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}@Overridepublic void addAccount(Account account) {try{jdbcTemplate.update("insert into account (name,money) values (?,?)",account.getName(),account.getMoney());}catch (Exception e){throw new RuntimeException(e);}}@Overridepublic void deleteAccount(Integer accountId) {try{jdbcTemplate.update("delete from account where id = ?",accountId);}catch (Exception e){throw new RuntimeException(e);}}@Overridepublic void updateAccount(Account account) {try{jdbcTemplate.update("update account set name = ?,money = ? where id = ?",account.getName(),account.getMoney(),account.getId());}catch (Exception e){throw new RuntimeException(e);}}@Overridepublic Account findOne(Integer accountId) {try{return jdbcTemplate.queryForObject("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);}catch (Exception e){throw new RuntimeException(e);}}@Overridepublic List<Account> findAll() {try{return jdbcTemplate.query("select * from account",new BeanPropertyRowMapper<Account>(Account.class));}catch (Exception e){throw new RuntimeException(e);}}
}

第三步:新建业务逻辑层(service)
IAccountService.java接口

package cn.lemon.service;import cn.lemon.domain.Account;import java.util.List;public interface IAccountService {void addAccount(Account account);void deleteAccount(Integer accountId);void updateAccount(Account account);Account findOne(Integer accountId);List<Account> findAll();
}

接口的实现类 AccountServiceImpl.java

package cn.lemon.service.impl;import cn.lemon.dao.IAccountDao;
import cn.lemon.domain.Account;
import cn.lemon.service.IAccountService;import java.util.List;public class AccountServiceImpl implements IAccountService {private IAccountDao iAccountDao;public void setiAccountDao(IAccountDao iAccountDao) {this.iAccountDao = iAccountDao;}@Overridepublic void addAccount(Account account) {iAccountDao.addAccount(account);}@Overridepublic void deleteAccount(Integer accountId) {iAccountDao.deleteAccount(accountId);}@Overridepublic void updateAccount(Account account) {iAccountDao.updateAccount(account);}@Overridepublic Account findOne(Integer accountId) {return iAccountDao.findOne(accountId);}@Overridepublic List<Account> findAll() {return iAccountDao.findAll();}
}

第四步:新建 xml Spring配置文件 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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><property name="url" value="jdbc:mysql:///db_account"></property><property name="username" value="root"></property><property name="password" value="lemon"></property>
</bean><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property></bean><bean id="accountDao" class="cn.lemon.dao.impl.AccountDaoImpl"><property name="jdbcTemplate" ref="jdbcTemplate"></property></bean><bean id="accountService" class="cn.lemon.service.impl.AccountServiceImpl"><property name="iAccountDao" ref="accountDao"></property></bean>
</beans>

第五步:新建测试类 AccountServiceImplTest.java ,实现数据库的增删改查

package cn.lemon.service.impl;import cn.lemon.domain.Account;
import cn.lemon.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;public class AccountServiceImplTest {private IAccountService iAccountService;public AccountServiceImplTest() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");iAccountService = (IAccountService) applicationContext.getBean("accountService");}@Testpublic void addAccount() {Account account = new Account();account.setName("马东敏");account.setMoney(10d);iAccountService.addAccount(account);}@Testpublic void deleteAccount() {iAccountService.deleteAccount(14);}@Testpublic void updateAccount() {Account account = iAccountService.findOne(17);account.setName("张英");iAccountService.updateAccount(account);}@Testpublic void findOne() {Account account = iAccountService.findOne(1);System.out.println("ID:" + account.getId() + "\tName:" + account.getName() + "\tMoney:" + account.getMoney());}@Testpublic void findAll() {List<Account> accountList = iAccountService.findAll();for (Account account : accountList) {System.out.println("ID:" + account.getId() + "\tName:" + account.getName() + "\tMoney:" + account.getMoney());}}
}

二、Spring基于 XML和注解的IoC配置


通过 @Component 和 @Autowired 修改上面的代码

注意:使用 @Autowired 时,set 方法可以省略

第一步:修改数据访问层的实现类 AccountDaoImpl.java

package cn.lemon.dao.impl;import cn.lemon.dao.IAccountDao;
import cn.lemon.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;import java.util.List;/*** Component:在spring 中产生对象,相当于 <bean id="",class=""></bean>* 默认的id为:accountDaoImpl(类名的首字母小写),可以使用默认的* @Repository 专门用于 dao 层*/
@Component
//@Repository
public class AccountDaoImpl implements IAccountDao {/*** @Autowired :依赖注入的注解* 默认找Spring容器中,跟属性类型相同的对象注入* 可以不写set 方法* 如果有多个类型相同的对象,或者没有响应类型的对象,会报错* 使用 @Qualifier 指定 id 注入*/@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic void addAccount(Account account) {try {jdbcTemplate.update("insert into account (name,money) values (?,?)", account.getName(), account.getMoney());} catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void deleteAccount(Integer accountId) {try {jdbcTemplate.update("delete from account where id = ?", accountId);} catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void updateAccount(Account account) {try {jdbcTemplate.update("update account set name = ?,money = ? where id = ?", account.getName(), account.getMoney(), account.getId());} catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic Account findOne(Integer accountId) {try {return jdbcTemplate.queryForObject("select * from account where id = ?", new BeanPropertyRowMapper<Account>(Account.class), accountId);} catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic List<Account> findAll() {try {return jdbcTemplate.query("select * from account", new BeanPropertyRowMapper<Account>(Account.class));} catch (Exception e) {throw new RuntimeException(e);}}
}

第二步:修改业务逻辑层的实现类 AccountServiceImpl.java

package cn.lemon.service.impl;import cn.lemon.dao.IAccountDao;
import cn.lemon.domain.Account;
import cn.lemon.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.List;/*** Component:在spring 中产生对象,相当于 <bean id="",class=""></bean>* 默认的id为:accountServiceImpl(类名的首字母小写),可以使用默认的** @Service 专门用于 Service 层*/
@Component("accountServiceImpl")
//@Service
public class AccountServiceImpl implements IAccountService {/*** @Autowired :依赖注入的注解* 默认找Spring容器中,跟属性类型相同的对象注入* 可以不写set 方法* 如果有多个类型相同的对象,或者没有响应类型的对象,会报错* 使用 @Qualifier 指定 id 注入,或者在 xml 配置文件中使用 primary = true* @Resource 依赖注入的注解*///@Autowired//@Qualifier("accountDaoImpl")@Resource(name = "accountDaoImpl")private IAccountDao iAccountDao;@Value("这是下面 name 的值")private String name;@Overridepublic void addAccount(Account account) {iAccountDao.addAccount(account);}@Overridepublic void deleteAccount(Integer accountId) {iAccountDao.deleteAccount(accountId);}@Overridepublic void updateAccount(Account account) {iAccountDao.updateAccount(account);}@Overridepublic Account findOne(Integer accountId) {return iAccountDao.findOne(accountId);}@Overridepublic List<Account> findAll() {System.out.println(name);return iAccountDao.findAll();}
}

第三步:修改测试类 AccountServiceImplTest.java

package cn.lemon.service.impl;import cn.lemon.domain.Account;
import cn.lemon.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;public class AccountServiceImplTest {private IAccountService iAccountService;public AccountServiceImplTest() {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");iAccountService = (IAccountService) applicationContext.getBean("accountServiceImpl");}@Testpublic void addAccount() {Account account = new Account();account.setName("马东敏");account.setMoney(10d);iAccountService.addAccount(account);}@Testpublic void deleteAccount() {iAccountService.deleteAccount(14);}@Testpublic void updateAccount() {Account account = iAccountService.findOne(17);account.setName("张英");iAccountService.updateAccount(account);}@Testpublic void findOne() {Account account = iAccountService.findOne(1);System.out.println("ID:" + account.getId() + "\tName:" + account.getName() + "\tMoney:" + account.getMoney());}@Testpublic void findAll() {List<Account> accountList = iAccountService.findAll();for (Account account : accountList) {System.out.println("ID:" + account.getId() + "\tName:" + account.getName() + "\tMoney:" + account.getMoney());}}
}

三、Spring基于纯注解的 IoC 配置

请点击 Spring 基于注解的AOP配置

Spring基于 XML 的IoC配置(实现账户的CURD)相关推荐

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

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

  2. Java spring基于XML的aop配置实现

    1.依赖包 2.文件结构 3.接口类ISomeService package com.buckwheats.test;public interface ISomeService {public voi ...

  3. java day58【 案例:使用 spring 的 IoC 的实现账户的 CRUD 、 基于注解的 IOC 配置 、 Spring 整合 Junit[掌握] 】...

    第1章 案例:使用 spring 的 IoC 的实现账户的 CRUD 1.1 需求和技术要求 1.1.1 需求 1.1.2 技术要求 1.2 环境搭建 1.2.1 拷贝 jar 包 1.2.2 创建数 ...

  4. spring中基于XML的AOP配置步骤

    spring中基于XML的AOP配置步骤 IAccountService.java package com.itheima.service;/*** 账户的业务层接口*/ public interfa ...

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

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

  6. spring基于XML的AOP-编写必要的代码

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  7. 基于注解的 IOC 配置

    基于注解的 IOC 配置 学习基于注解的 IoC 配置,大家脑海里首先得有一个认知,即注解配置和 xml 配置要实现的功能都是一样 的,都是要降低程序间的耦合.只是配置的形式不一样. 关于实际的开发中 ...

  8. 基于XML的AOP配置

    创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...

  9. spring 基于XML的申明式AspectJ通知的执行顺序

    spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...

  10. 基于注解的 IOC 配置——创建对象(Component、Controller、Service、Repository)注入数据(Autowired、Qualifier、Resource、Value)

    基于注解的 IOC 配置 注解配置和 xml 配置要实现的功能都是一样的,都是要降低程序间的耦合.只是配置的形式不一样. XML的配置: 用于创建对象的 用于注入数据的 用于改变作用范围的 生命周期相 ...

最新文章

  1. 2021年大数据Spark(五十二):Structured Streaming 事件时间窗口分析
  2. c++ doxygen 注释规范_C语言代码注释参考
  3. LinkedList详解,看这篇就够了
  4. C++ I/O 流 格式控制(上)
  5. 在jupyter编写代码列出HTML,Jupyter ~ 像写文章般的 Coding (附:同一个ipynb文件,执行多语言代码)...
  6. URLLoader 类和 URLVariables 类
  7. 关于概率性事件的产品性能和客户体验讨论
  8. Git和Github详细教程
  9. HDU 3790最短路径问题 [最短路最小花费]
  10. 手机摄像头变成PC电脑摄像头
  11. 数据挖掘工具主要有哪几种?
  12. stripe海外支付php教程
  13. 【sklearn-cookbook-zh】第一章 模型预处理
  14. ARM到底是冯诺依曼结构还是哈佛结构
  15. 乍得“随军”记 ——写在结婚一周年
  16. [原]as3 flash web 应用 (2)批量上传之php页面接收flash传递的数据
  17. 密码学系列 - 椭圆曲线签名的基本原理
  18. 项目风险常见清单列表库--再也不愁不能提前预知风险了
  19. 代码分享 | EEG数据的等效偶极子源定位
  20. html转exe 酷狗,exe视频格式转换器

热门文章

  1. 智慧厕所 智能公厕控制系统-工业dtu
  2. (四)金融数据分析--股票移动平均计算
  3. IWBI针对组合式资产总盘WELL认证业务(WELL Portfolio)成立全球顾问网络
  4. 【FAQ】调用华为云空间文件管理接口出现“errorCode“:“21000403“
  5. 用大白话告诉你 :Java 后端到底是在做什么?
  6. 微服务项目(maven父子级项目)怎么打包
  7. 成功的趋势交易者的入场点
  8. 广州大学2019计算机录取,广州大学2019年录取分数线
  9. 谷歌建议的目标每次转化费用是怎么来的?
  10. my god,昨天忘写blog了;