创建数据库和编写实体类

create table account(
id int primary key auto_increment,
name varchar(40),
money float
)character set utf8 collate utf8_general_ci;
insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);.
public class Account implements Serializable {
        private Integer id;
        private String name;
        private Float money;
    }

编写持久层代码

 public interface IAccountDao {
        /** * 保存 * @param account */
        void save(Account account);
        /** * 更新 * @param account */
        void update(Account account);
        /** * 删除 * @param accountId */
        void delete(Integer accountId);
        /** * 根据 id 查询 * @param accountId * @return */
        Account findById(Integer accountId);
        /** * 查询所有 * @return */
        List<Account> findAll();
    }
public class AccountDaoImpl implements IAccountDao {
    private DBAssit dbAssit;
    public void setDbAssit(DBAssit dbAssit) {
        this.dbAssit = dbAssit;
    }
    @Override
    public void save(Account account) {
        dbAssit.update("insert into
                account(name,money)values(?,?)",account.getName(),account.getMoney());
    }
    @Override
    public void update(Account account) {
        dbAssit.update("update account set name=?,money=? where
                id=?",account.getName(),account.getMoney(),account.getId());
    }
    @Override
    public void delete(Integer accountId) {
        dbAssit.update("delete from account where id=?",accountId);
    }
    @Override
    public Account findById(Integer accountId) {
        return dbAssit.query("select * from account where id=?",new
                BeanHandler<Account>(Account.class),accountId);
    }
    @Override
    public List<Account> findAll() {
        return dbAssit.query("select * from account where id=?",new
                BeanListHandler<Account>(Account.class));
    }
}

编写业务层代码

 public interface IAccountService {
        /** * 保存账户 * @param account */
        void saveAccount(Account account);
        /** * 更新账户 * @param account */
        void updateAccount(Account account);
        /** * 删除账户 * @param account */
        void deleteAccount(Integer accountId);
        /** * 根据 id 查询账户 * @param accountId * @return */
        Account findAccountById(Integer accountId);
        /** * 查询所有账户 * @return */
        List<Account> findAllAccount();
    }
   public class AccountServiceImpl implements IAccountService {
        private IAccountDao accountDao;
        public void setAccountDao(IAccountDao accountDao) {
            this.accountDao = accountDao;
        }
        @Override
        public void saveAccount(Account account) {
            accountDao.save(account);
        }
        @Override
        public void updateAccount(Account account) {
            accountDao.update(account);
        }
        @Override
        public void deleteAccount(Integer accountId) {
            accountDao.delete(accountId);
        }
        @Override
        public Account findAccountById(Integer accountId) {
            return accountDao.findById(accountId);
        }
        @Override
        public List<Account> findAllAccount() {
            return accountDao.findAll();
        }
    }

创建并编写配置文件

<?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">
</beans>

配置对象

<?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">
<!-- 配置 service -->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置 dao -->
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
<property name="dbAssit" ref="dbAssit"></property>
</bean>
<!-- 配置 dbAssit 此处我们只注入了数据源,表明每条语句独立事务-->
<bean id="dbAssit" class="com.itheima.dbassit.DBAssit">
<property name="dataSource" ref="dataSource"></property>
</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:///spring_day02"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean>
</beans>

测试类代码

 public class AccountServiceTest {
        /** * 测试保存 */
        @Test
        public void testSaveAccount() {
            Account account = new Account();
            account.setName("黑马程序员");
            account.setMoney(100000f);
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            IAccountService as = ac.getBean("accountService", IAccountService.class);
            as.saveAccount(account);
        }

        /** * 测试查询一个 */
        @Test
        public void testFindAccountById() {
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            IAccountService as = ac.getBean("accountService", IAccountService.class);
            Account account = as.findAccountById(1);
            System.out.println(account);
        }

        /** * 测试更新 */
        @Test
        public void testUpdateAccount() {
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            IAccountService as = ac.getBean("accountService", IAccountService.class);
            Account account = as.findAccountById(1);
            account.setMoney(20301050f);
            as.updateAccount(account);
        }

        /** * 测试删除 */
        @Test
        public void testDeleteAccount() {
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            IAccountService as = ac.getBean("accountService", IAccountService.class);
            as.deleteAccount(1);
        }

        /** * 测试查询所有 */
        @Test
        public void testFindAllAccount() {
            ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
            IAccountService as = ac.getBean("accountService", IAccountService.class);
            List<Account> list = as.findAllAccount();
            for (Account account : list) {
                System.out.println(account);
            }
        }

    }