下面我们来写一下基于XML的IOC案例
Maven工程
在src、main、java下新建com.itheima包,
建立一个service包,在包下新建一个接口IAccountService,
账户的业务层接口,方法都是查询所有、查询一个、保存、修改、删除。
package com.itheima.service; import com.itheima.domain.Account; import java.util.List; //账户的业务层接口 public interface IAccountService { //查询所有 List<Account> findAllAccount(); //查询一个 Account findAccountById(Integer accountId); //保存 void saveAccount(Account account); //修改 void updateAccount(Account account); //删除 void deleteAccount(Integer accountId); }在com.itheima下新建一个包domain,在domain下新建一个类Account,账户实体类
对了,在这之前在mysql中已经创建了一个数据库
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);
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; } @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } }
在com.itheima下新建一个包dao,在dao下有一个接口IAccountDao,
package com.itheima.dao; import com.itheima.domain.Account; import java.util.List; // 账户的持久层接口 public interface IAccountDao { //查询所有 List<Account> findAllAccount(); //查询一个 Account findAccountById(Integer accountId); //保存 void saveAccount(Account account); //修改 void updateAccount(Account account); //删除 void deleteAccount(Integer accountId); }我们的持久层技术选型是dbutils。
我们在dao包下新建一个包impl,在impl下新建AccountDaoImpl,
在service层下新建一个类impl.AccountServiceImpl,
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; } 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) values(?,?)",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层下新建一个类impl.AccountServiceImpl,
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; } 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); } }
我们需要在resources中的bean.xml中配置Service、配置Dao、配置QueryRunner
<?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 https://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="123"></property> </bean> </beans>在test包下新建一个AccountServiceTest类,测试类。
package com.itheima.test; import com.itheima.domain.Account; import com.itheima.service.IAccountService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.security.AccessControlContext; import java.util.List; /** * 使用Junit单元测试:测试我们的配置 */ public class AccountServiceTest { @Test public void testFindAll() { //1、获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2、得到业务层对象 IAccountService as = ac.getBean("accountService",IAccountService.class); //3、执行方法 List<Account> accounts = as.findAllAccount(); for (Account account:accounts) { System.out.println(account); } } @Test public void testFindOne() { //1、获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2、得到业务层对象 IAccountService as = ac.getBean("accountService",IAccountService.class); //3、执行方法 Account account = as.findAccountById(1); System.out.println(account); } @Test public void testSave() { Account account = new Account(); account.setName("test"); account.setMoney(12345f); //1、获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2、得到业务层对象 IAccountService as = ac.getBean("accountService",IAccountService.class); as.saveAccount(account); } @Test public void testUpdate() { //1、获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2、得到业务层对象 IAccountService as = ac.getBean("accountService",IAccountService.class); Account account = as.findAccountById(4); account.setName("test11"); account.setMoney(23456f); as.updateAccount(account); } @Test public void testDelete() { //1、获取容器 ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); //2、得到业务层对象 IAccountService as = ac.getBean("accountService",IAccountService.class); as.deleteAccount(4); } }
在pom.xml中我们需要注册的依赖有spring、dbutils、mysql、junit、c3p0。
<?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.itheima</groupId> <artifactId>day02_eesy_02account_xmlioc</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</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.32</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>