基于注解的IOC案例
生活随笔
收集整理的這篇文章主要介紹了
基于注解的IOC案例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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_03account_annoioc</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>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.10</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 org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository;import java.util.List;/*** 賬戶的持久層實現類*/ @Repository("accountDao") public class AccountDaoImpl implements IAccountDao {@Autowiredprivate QueryRunner 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 org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List;/*** 賬戶的業務層實現類*/ @Service("accountService") public class AccountServiceImpl implements IAccountService{@Autowiredprivate IAccountDao 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"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.itheima"></context:component-scan><!--配置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.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.List;/*** 使用Junit單元測試:測試我們的配置*/ public class AccountServiceTest {@Testpublic 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);}}@Testpublic 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);}@Testpublic 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);//3.執行方法as.saveAccount(account);}@Testpublic void testUpdate() {//1.獲取容易ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.得到業務層對象IAccountService as = ac.getBean("accountService",IAccountService.class);//3.執行方法Account account = as.findAccountById(4);account.setMoney(23456f);as.updateAccount(account);}@Testpublic void testDelete() {//1.獲取容易ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.得到業務層對象IAccountService as = ac.getBean("accountService",IAccountService.class);//3.執行方法as.deleteAccount(4);} }總結
以上是生活随笔為你收集整理的基于注解的IOC案例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于XML的IOC案例
- 下一篇: 基于子类的动态代理