Spring基于XML配置文件的IoC案例——实现单表CRUD
2020-04-21 19:58
309 查看
文章目录
一、前期准备
1、创建Maven工程
打开IDEA,新建一个Maven工程,命名为Spring_xml_IoC
数据库代码:
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);
2、在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>org.example</groupId> <artifactId>Spring01_xml_IOC</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>6</source> <target>6</target> </configuration> </plugin> </plugins> </build> <packaging>jar</packaging> <dependencies> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- dbutils --> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.4</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</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>
3、创建实体类
账户实体类,并且实现序列化接口
import java.io.Serializable; import java.math.BigInteger; /** * 账户实体类,并且实现序列化接口 */ 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; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }
4、创建持久层接口
/** * 账户的持久层接口 */ public interface IAccountDao { /** * 查询所有 */ List<Account> findAll(); /** * 查询一个 */ List<Account> findById(Integer id); /** * 保存账户 */ void saveAccount(Account account); /** * 更新 */ void updateAccount(Account account); /** *删除 */ void deleteAccount(Integer id); }
编写持久层接口实现类
/** * 账户的持久层实现类 */ public class AccountDaoImpl implements IAccountDao { private QueryRunner queryRunner; @Override public List<Account> findAll() { try { return queryRunner.query("select * from account",new BeanListHandler<Account>(Account.class)); } catch (SQLException e) { throw new RuntimeException(e); } } @Override public Account findById(Integer id) { try { return queryRunner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),id); } catch (SQLException e) { throw new RuntimeException(e); } } @Override public void saveAccount(Account account) { try { queryRunner.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 { queryRunner.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 id) { try { queryRunner.update("delete from account where id=?",id); } catch (SQLException e) { throw new RuntimeException(e); } } public void setQueryRunner(QueryRunner queryRunner) { this.queryRunner = queryRunner; } }
5、创建业务层接口
/** * 账户业务层的接口 */ public interface IAccountService { /** * 查询所有 */ List<Account> findAll(); /** * 查询一个 */ List<Account> findById(Integer id); /** * 保存账户 */ void saveAccount(Account account); /** * 更新 */ void updateAccount(Account account); /** *删除 */ void deleteAccount(Integer id); }
编写业务层实现类
/** * 账户的业务层实现类 */ public class AccountServiceImpl implements IAccountService { private IAccountDao accountDao; public void setAccountDao(IAccountDao accountDao) { this.accountDao = accountDao; } @Override public List<Account> findAll() { return accountDao.findAll(); } @Override public Account findById(Integer id) { return accountDao.findById(id); } @Override public void saveAccount(Account account) { accountDao.saveAccount(account); } @Override public void updateAccount(Account account) { accountDao.updateAccount(account); } @Override public void deleteAccount(Integer id) { accountDao.deleteAccount(id); } }
二、编写配置文件
class路径需要根据实际情况更改
<?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.Answer.service.impl.AccountServiceImpl"> <!-- set方法注入 --> <property name="accountDao" ref="accountDao"></property> </bean> <!-- 配置 dao --> <bean id="accountDao" class="com.Answer.dao.impl.AccountDaoImpl"> <property name="queryRunner" ref="queryRunner"></property> </bean> <!-- 配置QueryRunner--> <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"> <!-- 构造函数注入 --> <constructor-arg name="ds" ref="dataSource"></constructor-arg> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///sTest"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean> </beans>
三、项目测试
编写测试类
public class AccountServiceTest { /** * 测试保存 */ @Test public void testSaveAccount() { Account account = new Account(); account.setName("aaa"); account.setMoney(100000f); ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IAccountService as = ac.getBean("accountService", IAccountService.class); as.saveAccount(account); } /** * 测试查询一个 */ @Test public void testFindAccountById() { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IAccountService as = ac.getBean("accountService",IAccountService.class); Account account = as.findById(2); System.out.println(account); } /** * 测试更新 */ @Test public void testUpdateAccount() { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IAccountService as = ac.getBean("accountService",IAccountService.class); Account account = as.findById(4); account.setMoney(20301050f); as.updateAccount(account); } /** * 测试删除 */ @Test public void testDeleteAccount() { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IAccountService as = ac.getBean("accountService",IAccountService.class); as.deleteAccount(1); } /** * 测试查询所有 */ @Test public void testFindAllAccount() { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IAccountService as = ac.getBean("accountService",IAccountService.class); List<Account> list = as.findAll(); for(Account account : list) { System.out.println(account); } } }
查询所有测试结果:
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Spring基于注解的IoC案例——实现单表的CRUD(一)
- SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑
- spring 框架中的依赖注入(IOC--设值注入)---使用xml简单配置文件---的具体实例的简单实现
- MyBatis学习总结(一)---简单配置使用(基于XML方式实现CRUD以及加载配置文件的三种方式)
- 依赖注入(DI)和控制反转(IOC)的详细讲解 spring容器(spring注解实现,而不是xml配置文件)
- Spring--IoC--基于XML的DI-为应用指定多个配置文件-平等关系
- 基于配置文件的工厂设计模式实现,并且做到对象的单例,类似于spring的ioc
- Spring-IOC实现[02-XML配置文件方式]
- Spring 基于XML配置的IOC入门案例
- Spring--IoC--基于XML的DI-为应用指定多个配置文件-包含关系
- SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑
- Springmvc案例1----基于spring2.5的采用xml配置
- Spring依赖注入的XML配置文件的实现思想(1)——准备工作:如何读取XML的文件的内容
- 基于注解的Spring MVC(所需jar包,web xml配置,Spring文件配置, Controller Req
- 基于XML的配置文件访问接口设计和实现
- Spring中的AOP(七)——基于XML配置文件方式的AOP
- Spring-IOC实现(xml配置)
- Spring框架学习(3.1)基于xml配置文件的AOP实现
- 【Java.Spring.Core】【IoC】基于注解的Bean装配(非XML配置)
- Spring-通过xml配置文件实现切面(AOP)