项目结构

image.png
如上图所示,本项目中含持久层接口IAccount及其实现类AccountDaoImpl、业务层接口IAccountService及其实现类AccountServiceImpl、数据表封装类Account、测试类AccountServiceTest。
设计思路:持久层AccountDaoImpl中有一些方法可操作数据库,并能把结果封装到Account对象中---业务层AccountServiceImpl利用持久层方法对数据库实现操作(业务层看不到方法是怎样实现的,细节都封装在类持久层)---测试类通过spring获得业务层对象。

jar包

<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>8.0.19</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>

对象

数据表封装类
需要实现getter/setter方法、toString方法,这里省略了。

public class Account implements Serializable {

    private Integer id;
    private String name;
    private float money;
    }

持久层对象
AccountDaoImpl类因为要实现封装数据表操作,所需要借助c3p0包中的QueryRunner对数据库进行操作,所以它依赖于QueryRunner对象。我们要注入QueryRunner很简单,在此处生成setRunner方法,再在之后把QueryRunner对象放入spring容器中即可。

public class AccountDaoImpl implements IAccountDao {

  //通过set注入runner对象;
    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);
        }
    }
}

业务层对象
业务层对象AccountServiceImpl需要调用持久层对象才能进行业务操作,所以它对持久层IAccountDao有依赖关系,此处需要生成setAccountDao方法,后面在把它放入spring容器,就能通过set方法实现自动注入了。妙啊!

public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao;
    public void setAccountDao(IAccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public List<Account> findAllAccounts() {
        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);
    }
}

配置spring容器

<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
       <property name="accountDao" ref="accountDao"></property>
   </bean>

    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
        <property name="runner" ref="runner"></property>
    </bean>

    <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.cj.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/myspring?serverTimezone=GMT%2B8"></property>
        <property name="user" value="root"></property>
        <property name="password" value="52wendyma"></property>
    </bean>

测试

public class AccountServiceTest {

    @Test
    public void testFindAlldao() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountDao ad = ac.getBean("accountDao", IAccountDao.class);
        //3.执行方法
        List<Account> accounts = ad.findAllAccount();
        for(Account account : accounts){
            System.out.println(account);
        }
    }

    @Test
    public void testFindAll() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //3.执行方法
        List<Account> accounts = as.findAllAccounts();
        for(Account account : accounts){
            System.out.println(account);
        }
    }

    @Test
    public void testFindOne() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        //3.执行方法
        Account account = as.findAccountById(1);
        System.out.println(account);
    }

    @Test
    public void testSave() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = ac.getBean("accountService",IAccountService.class);
        Account account = new Account();
        account.setName("test");
        account.setMoney(12345f);
        //3.执行方法
        as.saveAccount(account);

    }
}

wendyma
4 声望0 粉丝