7.spring基于案例6的注解版

1.看图

7.spring基于案例6的注解版

2.业务层接口:IAccountService

package com.service;

import com.domin.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);

}

 3.业务层实现类AccountServiceImp

package com.service.Imp;

import com.dao.IAccountDao;
import com.domin.Account;
import com.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*交给spring容器去管理*/
@Service("accountService")
/*交给spring容器<bean id="accountService" class="com.service.Imp.AccountServiceImp">*/

public class AccountServiceImp implements IAccountService {
    /*交给spring容器去管理:配置accountService中的accountDao*/

    @Autowired
    //调用持久层
    private IAccountDao accountDao;
    /*当使用注解的时候,set方法就不是必须的了*/
    /*public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }*/


    @Override
    public List<Account> findAllAccount() {
        return accountDao.findAllAccount();
    }

    @Override
    public Account findAccountById(Integer accountId) {

        return accountDao.findAccountById(accountId);
    }

    @Override
    public void saveAccount(Account account) {
        accountDao.saveAccount(account);
    }

    @Override
    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    @Override
    public void deleteAccount(Integer accountId) {
        deleteAccount(accountId);
    }
}

4.持久层接口IAccountDao

package com.dao;

import com.domin.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);
}

5.持久层接口:AccountDaoImp

package com.dao.Imp;
import com.dao.IAccountDao;
import com.domin.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;

/*交给spring去管理*/
/*原来:<bean id="accountDao" class="com.dao.Imp.AccountDaoImp">*/
@Repository("accountDao")
public class AccountDaoImp implements IAccountDao {
    @Autowired
    private QueryRunner runner;/*自动注入它的方法:当使用注解的时候set的方法既不是必须的了*/
    /*public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }*/



    @Override
    public List<Account> findAllAccount() {

        /*泛型使用字解码创建对象*/
        try{
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    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);
        }
    }

    @Override
    public void saveAccount(Account account) {
        try{
            runner.update("insert into account(name,money)value(?,?)",account.getName(),account.getMoney());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    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);
        }
    }

    @Override
    public void deleteAccount(Integer accountId) {
        try{
            runner.update("delete from account where id=?",accountId);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}

6.bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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-4.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.0.xsd" >
    <!--配置IAccountService的实例-->
<!--    <bean id="accountService" class="com.service.Imp.AccountServiceImp">-->
<!--        <property name="accountDao" ref="accountDao"></property>-->
<!--    </bean>-->
<!--    <bean id="accountDao" class="com.dao.Imp.AccountDaoImp">-->
<!--        <property name="runner" ref="runner"></property>-->
<!--    </bean>-->

    <!--注解扫描:开启spring容器的注解扫描-->
    <context:component-scan base-package="com"></context:component-scan>

    <bean id="runner" 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.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/eesy"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
</beans>

7.测试类

package com.itheima;

import com.domin.Account;
import com.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class TestCode {
    @Test
    public void findAllAccount() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = (IAccountService) ac.getBean("accountService");
        /*类型要注意*/
        List<Account> accounts = as.findAllAccount();
        for (Account account :accounts){
            System.out.println(account);
        }

    }
    @Test
    public void findAccountById() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = (IAccountService) ac.getBean("accountService");
        Account acc = as.findAccountById(1);
        System.out.println(acc);

    }
    @Test
    public void saveAccount() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = (IAccountService) ac.getBean("accountService");
        Account acc = new Account();
        acc.setName("lalala");
        acc.setMoney((float)10000000.00);
        as.saveAccount(acc);
        System.out.println("执行成功");

    }
    @Test
    public void updateAccount() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = (IAccountService) ac.getBean("accountService");
        Account id =  as.findAccountById(9);
        id.setName("zhou");
        id.setMoney((float)100000.00);
        /*调用存放*/
        as.updateAccount(id);
    }
    @Test
    public void deleteAccount() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = (IAccountService) ac.getBean("accountService");
        as.deleteAccount(9);
    }
}

8.pom.xml依赖

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
<!--spring框架依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>
<!--C3P0依赖-->
    <dependency>
      <groupId>c3p0</groupId>
      <artifactId>c3p0</artifactId>
      <version>0.9.1.2</version>
    </dependency>
<!--jdbc依赖-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
<!--dubcy依赖-->
    <dependency>
      <groupId>commons-dbutils</groupId>
      <artifactId>commons-dbutils</artifactId>
      <version>1.6</version>
    </dependency>
<!--    mysql-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>
<!--    -->
  </dependencies>