文章目录

  • JdbcTemplate是什么?
  • Spring整合JdbcTemplate
  • Spring实现转账案例

JdbcTemplate是什么?

  1. JdbcTemplate是spring框架中提供的一个模板对象,是对原始繁琐的Jdbc API对象的简单封装。

    核心对象
    JdbcTemplate jdbcTemplate = new JdbcTemplate(DataSource dataSource);核心方法
    int update(); 执行增、删、改语句List<T> query(); 查询多个T queryForObject(); 查询一个new BeanPropertyRowMapper<>(); 实现ORM映射封装
    
  2. 查询数据库所有账号信息到Account实体中

    public class JdbcTemplateTest {@Testpublic void testFindAll() throws Exception {// 创建核心对象JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());// 编写sqlString sql = "select * from account";// 执行sqlList<Account> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Account.class));}
    }
    

Spring整合JdbcTemplate

  1. 基于Spring的xml配置实现账户的CRUD案例

  2. AccountDao接口和实现类

    package com.code.dao;import com.code.entity.Account;import java.util.List;public interface AccountDao {public List<Account> findAll();public Account findById(Integer id);public void save(Account account);public void update(Account account);public void delete(Integer id);
    }
    
    package com.code.dao.impl;import com.code.dao.AccountDao;
    import com.code.entity.Account;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Repository;import java.util.List;
    @Repository
    public class AccountDaoImpl implements AccountDao {@Autowiredprivate JdbcTemplate jdbcTemplate;/*查询所有账户*/@Overridepublic List<Account> findAll() {// 需要用到jdbcTemplateString sql = "select * from account";List<Account> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Account>(Account.class));return list;}/*根据ID查询账户*/@Overridepublic Account findById(Integer id) {String sql = "select * from account where id = ?";Account account = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class), id);return account;}/*添加账户*/@Overridepublic void save(Account account) {String sql = "insert into account values(null,?,?)";jdbcTemplate.update(sql,account.getName(),account.getMoney());}/*更新账户*/@Overridepublic void update(Account account) {String sql = "update account set money = ? where name = ?";jdbcTemplate.update(sql,account.getMoney(),account.getName());}/*根据ID删除账户*/@Overridepublic void delete(Integer id) {String sql = "delete from account where id = ?";jdbcTemplate.update(sql,id);}
    }
    
  3. AccountService接口和实现类

    package com.code.service;import com.code.entity.Account;import java.util.List;public interface AccountService {/*查询所有账户*/public List<Account> findAll();/*根据ID查询账户*/public Account findById(Integer id);/*添加账户*/public void save(Account account);/*更新账户信息*/public void update(Account account);/*根据ID删除账户*/public void delete(Integer id);}
    
    package com.code.service.impl;import com.code.dao.AccountDao;
    import com.code.entity.Account;
    import com.code.service.AccountService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;import java.util.List;@Service
    public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;@Overridepublic List<Account> findAll() {List<Account> all = accountDao.findAll();return all;}@Overridepublic Account findById(Integer id) {Account account = accountDao.findById(id);return account;}@Overridepublic void save(Account account) {accountDao.save(account);}@Overridepublic void update(Account account) {accountDao.update(account);}@Overridepublic void delete(Integer id) {accountDao.delete(id);}
    }
    
  4. spring核心配置文件

    <?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--IOC注解扫描--><context:component-scan base-package="com.code"/><!--引入properties--><context:property-placeholder location="classpath:jdbc.properties"/><!--dataSource--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!--jdbcTemplate--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><constructor-arg name="dataSource" ref="dataSource"/></bean></beans>
    

Spring实现转账案例

  1. AccountDao接口和实现类

    package com.code.dao;public interface AccountDao {/*减钱:转出操作*/public void out(String outUser,Double money);/*加钱:转入操作*/public void in(String inUser,Double money);
    }
    
    package com.code.dao.impl;import com.code.dao.AccountDao;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Repository;@Repository
    public class AccountDaoImpl implements AccountDao {@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic void out(String outUser, Double money) {String sql = "update account set money = money - ? where name = ?";jdbcTemplate.update(sql, money, outUser);}@Overridepublic void in(String inUser, Double money) {String sql = "update account set money = money + ? where name = ?";jdbcTemplate.update(sql, money, inUser);}
    }
    
  2. AccountService接口和实现类

    package com.code.service;public interface AccountSerivce {/*转账方法*/public void transfer(String outUser,String inUser,Double money);}
    
    package com.code.service.impl;import com.code.dao.AccountDao;
    import com.code.service.AccountSerivce;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Isolation;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;@Service
    @Transactional
    public class AccountServiceImpl implements AccountSerivce {@Autowiredprivate AccountDao accountDao;@Override@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,timeout = -1,readOnly = false)public void transfer(String outUser, String inUser, Double money) {//调用dao的out及in方法accountDao.out(outUser,money);//        int i = 1/0;accountDao.in(inUser,money);}
    }
    
  3. spring核心配置文件

    <?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--IOC注解扫描--><context:component-scan base-package="com.lagou"/><!--加载jdbc配置文件--><context:property-placeholder location="classpath:jdbc.properties"/><!--把数据库连接池交给IOC容器--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"></property><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--把JdbcTemplate交给IOC容器--><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><constructor-arg name="dataSource" ref="dataSource"></constructor-arg></bean>
    </beans>
    

Spring JDBCTemplate相关推荐

  1. Spring JdbcTemplate方法详解

    2019独角兽企业重金招聘Python工程师标准>>> Spring JdbcTemplate方法详解 标签: springhsqldbjava存储数据库相关sql 2012-07- ...

  2. Spring JdbcTemplate的queryForList(String sql , ClassT elementType)易错使用--转载

    原文地址: http://blog.csdn.net/will_awoke/article/details/12617383 一直用ORM,今天用JdbcTemplate再次抑郁了一次. 首先看下这个 ...

  3. 转载:为什么使用ibatis而不用spring jdbcTemplate

    第一:ibatis仅仅是对jdbc薄薄的一层封装,完全不丧失sql的灵活性 第二:ibatis所有的sql都可以放在配置文件中,这样有利于sql的集中管理,特别是在sql tuning是很容易把得到所 ...

  4. Spring JdbcTemplate的queryForList(String sql , Class<T> elementType)返回非映射实体类的解决方法

    Spring JdbcTemplate的queryForList(String sql , Class elementType)返回非映射实体类的解决方法 参考文章: (1)Spring JdbcTe ...

  5. 【SSM框架系列】Spring - JdbcTemplate声明式事务

    JdbcTemplate概述 以往使用jdbc时,每次都需要自己获取PreparedStatement,执行sql语句,关闭连接等操作.操作麻烦冗余,影响编码的效率. Spring把对数据库的操作在j ...

  6. spring jdbctemplate调用存储过程,返回list对象

    注:本文来源于< spring jdbctemplate调用存储过程,返回list对象 > spring jdbctemplate调用存储过程,返回list对象 方法: /*** 调用存储 ...

  7. SpringJdbc持久层封装,Spring jdbcTemplate封装,springJdbc泛型Dao,Spring baseDao封装

    SpringJdbc持久层封装,Spring jdbcTemplate封装,springJdbc泛型Dao,Spring baseDao封装 >>>>>>>& ...

  8. spring JdbcTemplate数据库查询实例

    使用JdbcTemplate查询数据库的例子 配置等可以看前一篇文章: Spring JdbcTemplate实例 创建数据库 可以使用下面的SQL create table A( `id` INT ...

  9. Spring JdbcTemplate实例

    简介 Spring JdbcTemplate类是Spring提供的简化数据库操作的一个类,这个类使用了模板方法模式,可以减少一些重复代码.这里主要演示一下 JdbcTemplate 的使用. 完整的代 ...

  10. Spring JdbcTemplate示例

    Spring JdbcTemplate示例 Spring JdbcTemplate是Spring JDBC包中最重要的类. 目录[ 隐藏 ] 1 Spring JdbcTemplate 1.1 Spr ...

最新文章

  1. kotlin + springboot启用elasticsearch搜索
  2. java有main却说找不到_小说:女子雨夜找扳指,男子却说你找不到的:我没丢怎么也找到...
  3. 2015.1.3 让CombBox自动弹出下拉框
  4. ES6的 super 关键字
  5. Python+opencv 机器视觉 - 基于霍夫圈变换算法检测图像中的圆形实例演示
  6. Linux中 set、env、declare、export显示shell变量的区别
  7. Linux环境部署jenkins
  8. python续行_python基础(变量,续行符,is,round,if,字符串,日期,数学,参数)...
  9. java统计行列和字数的函数_JAVA使用POI获取Excel的列数与行数
  10. css调用方式的方法
  11. 全网最细之super讲解
  12. 校友聊---Sprint计划会议总结
  13. python和java学哪个好-Python和Java学哪个好?大家是怎么选的
  14. 数据库E-R图——图书管理系统
  15. Java中级软件工程师应该具备的知识点
  16. WWW15年:改变世界的15个网站
  17. 2021计算机一级选择题必背知识点,2021年度全国计算机等级考试MSOffice选择题必考.doc...
  18. centos linux 修改系统默认语言设置,centos怎么更改语言设置为中文
  19. git tag 打标签(我看过最透彻的文章)
  20. FPGA Verilog视频笔记

热门文章

  1. Win10安装mysql详细步骤
  2. javacv实现直播流
  3. IOS 大众点评 搜索界面实现
  4. 解决git提交每次都要输入密码
  5. 疯壳AI开源无人机GPIO(LED航情灯、信号灯控制)
  6. 高房价与多核分布式计算
  7. 如何将excel的数据直接导入数据库
  8. nginx 端口禁止浏览器访问
  9. 基于微信小程序的小型企业人力资源管理小程序-计算机毕业设计
  10. 前后端联调和跨域处理