9_案例与xml配置文件读取properties文件

持久层

package com.aping.dao;

import com.aping.domain.Account;

import java.util.List;

public interface IAccountDao {
    /**
     * 查询所有
     * @return
     */
    List<Account> findAllAccount();

    /**
     * 根据id查询
     * @param accountId
     * @return
     */
    Account findByAccount(Integer accountId);

    /**
     * 更新
     * @param account
     */
    void saveAccount(Account account);

    /**
     * 删除账户
     * @param accountId
     */
    void deleteAccount(Integer accountId);

    /**
     *
     * @param account
     */
    void update(Account account);
}

package com.aping.dao.impl;

import com.aping.dao.IAccountDao;
import com.aping.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;


import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;


public class AccounDaoImpl implements IAccountDao {
    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 (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Account findByAccount(Integer accountId) {
        try {
            return runner.query("select * from account where id = ?",new BeanHandler<Account>(Account.class),accountId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

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

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

    public void update(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);
        }
    }
}

业务层

package com.aping.service;

import com.aping.domain.Account;

import java.util.List;

public interface IAccountService {
    /**
     * 查询所有
     * @return
     */
    List<Account> findAllAccount();

    /**
     * 根据id查询
     * @param accountId
     * @return
     */
    Account findByAccount(Integer accountId);

    /**
     * 存储用户
     * @param account
     */
    void saveAccount(Account account);

    /**
     *
     * @param account
     */
    void update(Account account);

    /**
     * 删除账户
     * @param accountId
     */
    void deleteAccount(Integer accountId);
}
package com.aping.service.impl;

import com.aping.dao.IAccountDao;
import com.aping.domain.Account;
import com.aping.service.IAccountService;


import java.util.List;


public class AccountServiceImpl implements IAccountService {
    private IAccountDao dao;

    public void setDao(IAccountDao dao) {
        this.dao = dao;
    }

    public List<Account> findAllAccount() {
        return dao.findAllAccount();
    }

    public Account findByAccount(Integer accountId) {
        return dao.findByAccount(accountId);
    }

    public void saveAccount(Account account) {
        dao.saveAccount(account);
    }

    public void update(Account account) {
        dao.update(account);
    }

    public void deleteAccount(Integer accountId) {
        dao.deleteAccount(accountId);
    }
}

实体类

package com.aping.domain;

import java.io.Serializable;

public class Account implements Serializable {
    private Integer id;
    private String name;
    private Double 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 Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="accountService" class="com.aping.service.impl.AccountServiceImpl">
        <property name="dao" ref="accountDao"></property>
    </bean>

    <bean id="accountDao" class="com.aping.dao.impl.AccounDaoImpl">
        <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.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="aa64221886"></property>
    </bean>
</beans>

Test

package com.aping.test;

import com.aping.domain.Account;
import com.aping.service.IAccountService;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用Junit单元测试,测试我们的配置
 * Spring整合Junit的配置
 *  1.导入spring整合junit的jar(坐标)
 *  2.使用Junit提供的一个注解把原有的main方法替换了,替换成你spring提供的
 *      注解:RunWith
 *  3.告知Spring的运行器,Spring的ioc创建是基于xml还是注解,并说明其位置
 *      ContextConfiguration
 *          locations:指定xml文件的位置,加上classpath关键字,表示在类路径下
 *          classes:指定注解类所在的位置
 *
 *  当使用spring5.x版本的时候,要求junit的jar必须是4.1.2及以上
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class Test {
    //使用注解配置时,需要用注解配置类
//    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

    @Autowired
    IAccountService service;


    @org.junit.Test
    public void test(){
        List<Account> accountList = service.findAllAccount();
        System.out.println(accountList);
    }

    @org.junit.Test
    public void test1(){
        Account account = new Account();
        account.setName("aping");
        account.setMoney(1000000.0);
        service.saveAccount(account);
    }
}

XML读取properties文件

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="staticConfig.properties" />
    <property name="fileEncoding" value="GBK"></property>
</bean>

<bean id="id" class="class">
	<property name="属性名" value="${properties中的key}"></property>
</bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值