实体类:

 package cn.mepu.domain;

 import java.io.Serializable;

 /**
  * 账户实体类
  * @author shkstart
  * @create 2019-11-08 10:04
  */
 public class Account implements Serializable {
     private Integer id;
     private String name;
     private float money;

     public Account(Integer id, String name, float money) {
         this.id = id;
         this.name = name;
         this.money = money;
     }

     public Account() {
     }

     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 float getMoney() {
         return money;
     }

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

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

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 java.util.List;

 /**
  * @author shkstart
  * @create 2019-11-08 10:28
  */
 public class AccountDaoImp implements AccountDao {

     private QueryRunner runner;

     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 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 java.util.List;

 /**
  * @author shkstart
  * @create 2019-11-08 10:12
  */

 public class AccountServiceImp implements AccountService {

     private AccountDao dao;

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

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

     @Override
     public Account findAccountById(Integer accountId) {
         return dao.findAccountById(accountId);
     }

     @Override
     public void saveAccount(Account acc) {
         dao.saveAccount(acc);
     }

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

     @Override
     public void deleteAccount(Integer accountId) {
     dao.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名称空间的约束中-->
<!--    配置service-->
    <bean id="accountService" class="cn.mepu.service.imp.AccountServiceImp">
<!--        注入dao-->
        <property name="dao" ref="accountDao"></property>
    </bean>

<!--    配置dao-->
    <bean id="accountDao" class="cn.mepu.dao.imp.AccountDaoImp">
<!--        注入QueryRunner runner-->
        <property name="runner" ref="runner"></property>
    </bean>

<!--    配置注入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>

spring开发案例配合mysql的更多相关文章

  1. Spring开发案例1半注解开发

    dao层: package cn.mepu.dao.imp; import cn.mepu.dao.AccountDao; import cn.mepu.domain.Account; import ...

  2. SpringBoot开发案例之整合Activiti工作流引擎

    前言 JBPM是目前市场上主流开源工作引擎之一,在创建者Tom Baeyens离开JBoss后,JBPM的下一个版本jBPM5完全放弃了jBPM4的基础代码,基于Drools Flow重头来过,目前官 ...

  3. Spring进阶案例之注解和IoC案例

    Spring进阶案例之注解和IoC案例 一.常见的注解分类及其作用 从此前的基于xml的IoC开发案例和依赖注入案例中,我们可以将xml配置归纳为: <bean id="" ...

  4. 【译】Spring 4 + Hibernate 4 + Mysql + Maven集成例子(注解 + XML)

    前言 译文链接:http://websystique.com/spring/spring4-hibernate4-mysql-maven-integration-example-using-annot ...

  5. Spring Boot使用Spring Data Jpa对MySQL数据库进行CRUD操作

    只需两步!Eclipse+Maven快速构建第一个Spring Boot项目 构建了第一个Spring Boot项目. Spring Boot连接MySQL数据库 连接了MySQL数据库. 本文在之前 ...

  6. spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途

    Spring4 Jar包详解 SpringJava Spring AOP: Spring的面向切面编程,提供AOP(面向切面编程)的实现 Spring Aspects: Spring提供的对Aspec ...

  7. Spring Boot (五)Spring Data JPA 操作 MySQL 8

    一.Spring Data JPA 介绍 JPA(Java Persistence API)Java持久化API,是 Java 持久化的标准规范,Hibernate是持久化规范的技术实现,而Sprin ...

  8. 大数据技术之_09_Flume学习_Flume概述+Flume快速入门+Flume企业开发案例+Flume监控之Ganglia+Flume高级之自定义MySQLSource+Flume企业真实面试题(重点)

    第1章 Flume概述1.1 Flume定义1.2 Flume组成架构1.2.1 Agent1.2.2 Source1.2.3 Channel1.2.4 Sink1.2.5 Event1.3 Flum ...

  9. 《实战突击:PHP项目开发案例整合(第2版)(含DVD光盘1张)》

    <实战突击:PHP项目开发案例整合(第2版)(含DVD光盘1张)> 基本信息 作者: 徐康明    辛洪郁 出版社:电子工业出版社 ISBN:9787121221378 上架时间:2014 ...

随机推荐

  1. 使用pip安装python模块和包

    点击进入幕布视图浏览 https://mubu.com/doc/a8VGCUfqqw 五.使用pip安装python第三方库. pip的常用命令 方式一:在线安装 1.进入命令行 2.敲入pip命令: ...

  2. CSS3 动画Animation的8大属性

    animation复合属性.检索或设置对象所应用的动画特效. 如果有多个属性值时以","隔开,适用于所有元素,包含伪对象:after和:before 1.animation-nam ...

  3. Java中的session详解

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  4. javafx将数据库内容输出到tableview表格

    一 .创建Fxml文件,用Javafx Scene Builder 编辑页面,创建tableview(表格)和tablecolum(表格中的列),并为其设置fxid: 二.生成fxml文件的控制类: ...

  5. SharpZipLib 文件/文件夹 过滤

    这里就不说压缩/解压了.网上教程太多. 主要说一下,解压时,如何过滤某些文件/文件夹 参考地址:https://github.com/icsharpcode/SharpZipLib/wiki/Fast ...

  6. 取消SVN感叹号即去除版本库

    之前不小心直接将版本库 的内容检出 到桌面,后才发现桌面上的文件 都变成了问号,本来也以为没有多大问题,删除 .svn 即可,可是删除所有的.svn后,桌面上还是显示问号,刷新了很多次,还重启电脑 了 ...

  7. MATLAB GUI 设计要点 转

    https://www.cnblogs.com/wangh0802PositiveANDupward/p/4588512.html 从简单的例子说起吧. 创建Matlab GUI界面通常有两种方式: ...

  8. 利用jion阻塞主进程结束

    import time from multiprocessing import Process def run1(): for i in range(5): print("sunck is ...

  9. poj 2752 kmp的next数组

    题目大意: 求一个字符串中某一个既是前缀又是后缀的前缀的结尾下标: 基本思路: 从_next[len]开始找_next[_next[len]],再找_next[_next[_next[len]]],一 ...

  10. leetcode-第14周双周赛-1271-十六进制魔术数字

    自己的提交: class Solution: def toHexspeak(self, num: str) -> str: num = hex(int(num)) num = str(num)[ ...