dao层:

 package cn.mepu.dao.imp;

 import cn.mepu.dao.AccountDao;
 import cn.mepu.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;

 /**
  * @author shkstart
  * @create 2019-11-08 10:28
  */
 @Repository("accountDao")
 public class AccountDaoImp implements AccountDao {

     @Autowired
     private QueryRunner 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 acc) {
         try {
             runner.update("insert into account(name,money) values(?,?)" , acc.getName(),acc.getMoney());
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }

     @Override
     public void updateAccount(Account acc) {
         try {
             runner.update("update account set name=? , money=? where id = ? " , acc.getName(),acc.getMoney(),acc.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);
         }
     }
 }

service层:

 package cn.mepu.service.imp;

 import cn.mepu.dao.AccountDao;
 import cn.mepu.domain.Account;
 import cn.mepu.service.AccountService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;

 import java.util.List;

 /**
  * @author shkstart
  * @create 2019-11-08 10:12
  */
 @Service("accountService")
 public class AccountServiceImp implements AccountService {
     @Autowired
     private 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 acc) {
         accountDao.saveAccount(acc);
     }

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

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

servlet层:

 package cn.mepu.service;

 import cn.mepu.domain.Account;
 import org.junit.Test;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;

 import java.util.List;

 /**
  * @author shkstart
  * @create 2019-11-08 10:45
  */
 public class AccountServiceTest {
     @Test
     public void testFindAll(){
     //1.获取容器
         ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
     //2.得到业务层对象
         AccountService service = (AccountService) ac.getBean("accountService");
     //3.执行方法
         List<Account> accounts = service.findAllAccount();
         for (Account account : accounts) {
             System.out.println("account = " + account);
         }

     }

     @Test
     public void testFindOne(){
         //1.获取容器
         ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
         //2.得到业务层对象
         AccountService service = (AccountService) ac.getBean("accountService");
         //3.执行方法
         Account account = service.findAccountById(1);
         System.out.println(account);
     }

     @Test
     public void testSave(){
         //1.获取容器
         ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
         //2.得到业务层对象
         AccountService service = (AccountService) ac.getBean("accountService");
         //3.执行方法
         service.saveAccount(new Account(1,"DDD",1234));
     }

     @Test
     public void testUpdate(){
         //1.获取容器
         ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
         //2.得到业务层对象
         AccountService service = (AccountService) ac.getBean("accountService");
         //3.执行方法
         service.updateAccount(new Account(1,"DDD",2345));
     }

     @Test
     public void testDelete(){
         //1.获取容器
         ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
         //2.得到业务层对象
         AccountService service = (AccountService) ac.getBean("accountService");
         //3.执行方法
         service.deleteAccount(4);
     }
 }

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"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
     <!--告知spring加载容器时扫描要扫描的包配置所需要的约束不在beans中,而是称为context名称空间的约束中-->
     <context:component-scan base-package="cn.mepu"></context:component-scan>
 <!--    配置注入QueryRunner scope保证线程安全-->
     <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/javaee"></property>
         <property name="user" value="root"></property>
         <property name="password" value="root"></property>
     </bean>

 </beans>

最新文章

  1. EasyUI使用JSON保存数据
  2. 使用Wireshark 查看查找未被过滤端口
  3. Android java传递int类型数据给C
  4. javascript原型Prototype【转】
  5. CSS 有关margin padding
  6. jquery 常用基础方法
  7. 练习一_使用Git进行代码管理的心得
  8. ural 1247. Check a Sequence
  9. 你不知道的JavaScript-- 事件流与事件处理
  10. WinDBG使用之线程
  11. poj 1300 Door Man 欧拉回路
  12. Hbase学习记录(1)|伪分布式安装
  13. Spring MVC与easyui国际化
  14. Web APP 随笔
  15. URAL 1009 K-based numbers(DP递推)
  16. VS2010中水晶报表应用及实例
  17. jquery于form正在使用submit问题,未解决
  18. 面向对象的SOLID原则白话篇
  19. Integration Services 服务连接失败,拒绝访问以及无法检索数据报错问题
  20. 理解Spring中的IOC和AOP

热门文章

  1. java部署:CentOS 7下Tomcat安装与配置教程(Tomcat开机启动)
  2. Android组件内核之间组件间通信方案(四)下篇
  3. 一、最新Kafka单节点部署+测试 完整
  4. 2018-8-10-win10-UWP-圆形等待
  5. shell 脚本文件类型.sh ,变量
  6. 头文件 &lt;sys/un.h&gt;
  7. 视频专家之路【三】:Vs开发环境的搭建
  8. C++ 空类,默认产生哪些成员函数
  9. java中文乱码转换
  10. Vue学习笔记【9】——Vue指令之v-for和key属性