注解IOC案例-把自己编写的类使用注解配置
生活随笔
收集整理的這篇文章主要介紹了
注解IOC案例-把自己编写的类使用注解配置
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?
<?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.learn</groupId><artifactId>day03_learn_03account_annoioc</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>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.45</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> package com.learn.dao.impl;import com.learn.dao.IAccountDao; import com.learn.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;/*** 賬戶的持久層實(shí)現(xiàn)類*/ @Repository("accountDao") public class AccountDaoImpl implements IAccountDao {@Autowiredprivate QueryRunner 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);}} } package com.learn.dao;import com.learn.domain.Account;import java.util.List;/*** 賬戶的持久層接口*/ public interface IAccountDao {/*** 查詢所有* @return*/List<Account> findAllAccount();/*** 查詢一個(gè)* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 刪除* @param acccountId*/void deleteAccount(Integer acccountId); } package com.learn.domain;import java.io.Serializable;/*** 賬戶的實(shí)體類*/ 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 +'}';} } package com.learn.service;import com.learn.domain.Account;import java.util.List;/*** 賬戶的業(yè)務(wù)層接口*/ public interface IAccountService {/*** 查詢所有* @return*/List<Account> findAllAccount();/*** 查詢一個(gè)* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 刪除* @param acccountId*/void deleteAccount(Integer acccountId);} package com.learn.service.impl;import com.learn.dao.IAccountDao; import com.learn.domain.Account; import com.learn.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import java.util.List;/*** 賬戶的業(yè)務(wù)層實(shí)現(xiàn)類*/ @Service("accountService") public class AccountServiceImpl implements IAccountService{@Autowiredprivate IAccountDao 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 acccountId) {accountDao.deleteAccount(acccountId);} } <?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在創(chuàng)建容器時(shí)要掃描的包 --><context:component-scan base-package="com.learn"></context:component-scan><!--配置QueryRunner--><bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"><!--注入數(shù)據(jù)源--><constructor-arg name="ds" ref="dataSource"></constructor-arg></bean><!-- 配置數(shù)據(jù)源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--連接數(shù)據(jù)庫的必備信息--><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property><property name="user" value="root"></property><property name="password" value="123456"></property></bean> </beans> package com.learn.test;import com.learn.domain.Account; import com.learn.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.得到業(yè)務(wù)層對象IAccountService as = ac.getBean("accountService",IAccountService.class);//3.執(zhí)行方法List<Account> accounts = as.findAllAccount();for(Account account : accounts){System.out.println(account);}}@Testpublic void testFindOne() {//1.獲取容易ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.得到業(yè)務(wù)層對象IAccountService as = ac.getBean("accountService",IAccountService.class);//3.執(zhí)行方法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.得到業(yè)務(wù)層對象IAccountService as = ac.getBean("accountService",IAccountService.class);//3.執(zhí)行方法as.saveAccount(account);}@Testpublic void testUpdate() {//1.獲取容易ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.得到業(yè)務(wù)層對象IAccountService as = ac.getBean("accountService",IAccountService.class);//3.執(zhí)行方法Account account = as.findAccountById(4);account.setMoney(23456f);as.updateAccount(account);}@Testpublic void testDelete() {//1.獲取容易ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.得到業(yè)務(wù)層對象IAccountService as = ac.getBean("accountService",IAccountService.class);//3.執(zhí)行方法as.deleteAccount(4);} }?
超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生總結(jié)
以上是生活随笔為你收集整理的注解IOC案例-把自己编写的类使用注解配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: XMLIOC案例-编写spring的Io
- 下一篇: spring整合junit问题分析