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>

Spring开发案例1半注解开发的更多相关文章

  1. Spring学习04(使用注解开发)

    7.使用注解开发 说明:在spring4之后,想要使用注解形式,必须得要引入aop的包. 在配置文件当中,还得要引入一个context约束 <?xml version="1.0&quo ...

  2. Java开发学习(十三)----基于注解开发定义第三方bean及注解开发总结

    在前面的博客中定义bean的时候都是在自己开发的类上面写个注解就完成了,但如果是第三方的类,这些类都是在jar包中,我们没有办法在类上面添加注解,这个时候该怎么办? 遇到上述问题,我们就需要有一种更加 ...

  3. Java开发学习(十)----基于注解开发定义bean 已完成

    一.环境准备 先来准备下环境: 创建一个Maven项目 pom.xml添加Spring的依赖 <dependencies>    <dependency>        < ...

  4. Java开发学习(十一)----基于注解开发bean作用范围与生命周期管理

    一.注解开发bean作用范围与生命周期管理 前面使用注解已经完成了bean的管理,接下来将通过配置实现的内容都换成对应的注解实现,包含两部分内容:bean作用范围和bean生命周期. 1.1 环境准备 ...

  5. spring boot整合mybatis基于注解开发以及动态sql的使用

    让我们回忆一下上篇博客中mybatis是怎样发挥它的作用的,主要是三类文件,第一mapper接口,第二xml文件,第三全局配置文件(application.properties),而今天我们就是来简化 ...

  6. 阶段3 1.Mybatis_02.Mybatis入门案例_3.mybatis注解开发和编写dao实现类的方式

    注解的用法 直接创建一个新的项目 下一步直接next 然后finish即可 把之前项目01里面的代码直接复制过来 复制到我们02的注解的工程中 把01项目导入的依赖也都粘贴过来 再把测试类复制过去 复 ...

  7. Spring _day02_IoC注解开发入门

    1.Spring IoC注解开发入门 1.1 注解开发案例: 创建项目所需要的jar,四个基本的包(beans core context expression ),以及两个日志记录的包,还要AOP的包 ...

  8. Spring注解开发系列专栏

    这个系列主要是讲Spring注解的使用,可以为后面SpringBoot的学习带来一定的帮助.我觉得从Spring直接过度到SpringBoot还是有点快,还是得需要一个演变的过程.从Spring开发, ...

  9. Spring (二)SpringIoC和DI注解开发

    1.Spring配置数据源 1.1 数据源(连接池)的作用 数据源(连接池)是提高程序性能出现的 事先实例化数据源,初始化部分连接资源 使用连接资源时从数据源中获取 使用完毕后将连接资源归还给数据源 ...

随机推荐

  1. jdk紧急漏洞,XMLDecoder反序列化攻击

    昨天在公司发现了一个jdk中的XMLDecoder反序列化的漏洞,看起来很危险!下面通过两个示例来看看这个漏洞的危害! 示例1:利用XmlDecoder删除本地文件 首先来看这个xmldecoder. ...

  2. 什么是索引?Mysql目前主要的几种索引类型

    一.索引 MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度. 打个比方,如果合理的设计且使用索引的MySQL是一辆兰博基尼的话,那么没有设计和使用索引的My ...

  3. python_面向对象,类名称空间,对象名称空间,组合

    创建一个类就会创建一个类的名称空间,用来存储类中定义的所有名字,这些名字称为类的属性 而类有两种属性:静态属性和动态属性 静态属性就是直接在类中定义的变量(字段) 动态属性就是定义在类中的方法 其中类 ...

  4. 43-python基础-python3-字符串-常用字符串方法(一)-upper()-lower()-isupper()-islower()

    请注意, 这些方法没有改变字符串本身,而是返回一个新字符串. 如果你希望改变原来的字符串,就必须在该字符串上调用 upper()或 lower(),然后将这个新字符串赋给保存原来字符串的变量.   1 ...

  5. pytest---参数化

    import pytest @pytest.mark.parametrize('test_input,expected',[('3+5',8), ('2-1',1),('7*5',30)])def t ...

  6. 使用Nuget重新安装packages.config中的组件的方法

    Update-Package -ProjectName 'Ko.app.web' -Reinstall 该语句作用:按照packages.config中给出的程序组件,重新下载安装一遍.

  7. android中使用MediaPlayer和SurfaceView播放视频

    package com.test.video; import java.io.IOException; import android.media.AudioManager; import androi ...

  8. bat批处理----copy和xcopy区别

    copy和xcopy区别:两者都可以赋值文件 1.copy不能在有子目录存在的文件中拷贝文件的同时重命名此文件名 2.copy命令能合并两个文件,而xcopy不能

  9. 为什么要用webpack!

    为什么要用webpack?   现今的很多网页其实可以看做是功能丰富的应用,它们拥有着复杂的JavaScript代码和一大堆依赖包. 模块化,让我们可以把复杂的程序细化为小的文件;   类似于Type ...

  10. Codeforces 1105E 最大独立集 状态DP 中途相遇法

    题意:你有一个字符串, 有两种操作,一种是改变字符串,一种是某个用户询问这个字符串,如果一个用户每次查询字符串的时候都是他的用户名,他就会高兴.问最多有多少个用户会高兴? 题意:容易发现,在两个1操作 ...