1.项目搭建

<1>数据库表account对应的账户实体类

package domain;

import java.io.Serializable;

/**
* 账户实体类
*/
public class Account implements Serializable {
private Integer id;
private String name;
private Float money; 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 +
'}';
}
}

<2>账户的持久层接口及其实现类

接口类 IAccountDao

package dao;
import domain.Account; /**
* 账户的持久层接口
*/
public interface IAccountDao { /**
* 根据Id查询账户
* @param accountId
* @return
*/
Account findAccountById(Integer accountId); /**
* 根据名称查询账户
* @param accountName
* @return
*/
Account findAccountByName(String accountName); /**
* 更新账户
* @param account
*/
void updateAccount(Account account);
}

AccountDaoImpl.java

package dao.impl;
import dao.IAccountDao;
import domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport; import java.util.List; /**
* 账户的持久层实现类
*/
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao { public Account findAccountById(Integer accountId) {
List<Account> accounts = super.getJdbcTemplate().query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
return accounts.isEmpty()?null:accounts.get(0);
} public Account findAccountByName(String accountName) {
List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);
if(accounts.isEmpty()){
return null;
}
if(accounts.size()>1){
throw new RuntimeException("结果集不唯一");
}
return accounts.get(0);
} public void updateAccount(Account account) {
super.getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
}
}

2.JdbcTemplate的使用

<1>JdbcTemplate最基本用法

package jdbcTemplate;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource; /**
* JdbcTemplate最基本用法
*/
public class JdbcTemplateTest01 {
public static void main(String[] args) {
//1.准备数据源,Spring的内置数据源
DriverManagerDataSource ds=new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/spring");
ds.setUsername("root");
ds.setPassword("plj824"); //2.创建jdbcTemplate对象
JdbcTemplate jdbcTemplate=new JdbcTemplate();
//3.给jdbcTemplate对象设置数据源
jdbcTemplate.setDataSource(ds);
//2.执行操作
jdbcTemplate.execute("insert into account(name,money)values ('ccc',1000)");
}
}

<2>JdbcTemplate 用IOC进行依赖注入

package jdbcTemplate;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource; /**
* JdbcTemplate 用IOC进行依赖注入
*/
public class JdbcTemplateTest02 {
public static void main(String[] args) {
//1.获取容器
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
//2.获取对象
JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); //3.执行操作
jdbcTemplate.execute("insert into account(name,money)values ('eee',1250)");
}
}

<3>JdbcTemplate的CRUD操作

package jdbcTemplate;

import domain.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List; /**
* JdbcTemplate的CRUD操作
*/
public class JdbcTemplateTest03 {
public static void main(String[] args) {
//1.获取容器
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
//2.获取对象
JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class); //3.执行操作
//3.1保存
//jdbcTemplate.update("insert into account(name,money)values ('eee',1250)"); //3.2 更新
//jdbcTemplate.update("update account set name=?,money=? where id=?","test",4567,6); //3.3 删除
//jdbcTemplate.update("delete from account where id=? ",4); //3.4 查询所有
// List<Account> accounts = jdbcTemplate.query("select * from account where money > ?", new AccountRowMapper(), 1000f);
// for (Account account : accounts) {
// System.out.println(account);
// } // //查询一个
// List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),1);
// System.out.println(accounts.isEmpty()?"没有内容":accounts.get(0)); //查询返回一行一列(使用聚合函数,但不加group by子句)
Long count = jdbcTemplate.queryForObject("select count(*) from account where money > ?",Long.class,1000f);
System.out.println(count); } } /**
* 定义Account的封装策略
*/
class AccountRowMapper implements RowMapper<Account> {
/**
* 把结果集中的数据封装到Account中,然后由spring把每个Account加到集合中
*
* @param rs
* @param rowNum
* @return
* @throws SQLException
*/
public Account mapRow(ResultSet rs, int rowNum) throws SQLException {
Account account = new Account();
account.setId(rs.getInt("id"));
account.setName(rs.getString("name"));
account.setMoney(rs.getFloat("money"));
return account;
}
}

<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置账户的持久层-->
<bean id="accountDao" class="dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--配置jdbcTemplate,注意:JdbcTemplate这个类是在jar包中,无法使用setter方法进行注入,只能用xml配置-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="plj824"></property>
</bean>
</beans>

12 Spring JdbcTemplate的使用的更多相关文章

  1. Spring JdbcTemplate 的使用与学习(转)

    紧接上一篇 (JdbcTemplate是线程安全的,因此可以配置一个简单的JdbcTemplate实例,将这个共享的实例注入到多个DAO类中.辅助的文档) Spring DAO支持 http://ww ...

  2. 使用Spring JDBCTemplate简化JDBC的操作

    使用Spring JDBCTemplate简化JDBC的操作 接触过JAVA WEB开发的朋友肯定都知道Hibernate框架,虽然不否定它的强大之处,但个人对它一直无感,总感觉不够灵活,太过臃肿了. ...

  3. Spring JDBCTemplate 简单使用

    Spring JDBCTemplate applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8&q ...

  4. 【Spring】Spring的数据库开发 - 2、Spring JdbcTemplate的常用方法(execute、update、query)

    Spring JdbcTemplate的常用方法 文章目录 Spring JdbcTemplate的常用方法 execute() update() query() 简单记录-Java EE企业级应用开 ...

  5. 【Spring】Spring的数据库开发 - 1、Spring JDBC的配置和Spring JdbcTemplate的解析

    Spring JDBC 文章目录 Spring JDBC Spring JdbcTemplate的解析 Spring JDBC的配置 简单记录-Java EE企业级应用开发教程(Spring+Spri ...

  6. (转)Spring JdbcTemplate 方法详解

    Spring JdbcTemplate方法详解 文章来源:http://blog.csdn.net/dyllove98/article/details/7772463 JdbcTemplate主要提供 ...

  7. [转]Spring JdbcTemplate 查询分页

    原文:http://blog.csdn.net/xiaofanku/article/details/4280128 现在进行的项目由于数据库的遗留原因(设计的不堪入目)不能用hibernate.所以用 ...

  8. spring jdbcTemplate query

    1. spring jdbcTemplate query需要实现mapRow方法 package com.cdv.apolloagent.jdbc.dao.impl; import java.sql. ...

  9. Spring JdbcTemplate的queryForList(String sql , Class<T> elementType)易错使用--转载

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

随机推荐

  1. invoke和begininvoke 区别

    一直对invoke和begininvoke的使用和概念比较混乱,这两天看了些资料,对这两个的用法和原理有了些新的认识和理解. 首先说下,invoke和begininvoke的使用有两种情况: 1. c ...

  2. LeetCode 1105. Filling Bookcase Shelves

    原题链接在这里:https://leetcode.com/problems/filling-bookcase-shelves/ 题目: We have a sequence of books: the ...

  3. 使用readthedocs 发布 sphinx doc文档

    readthedocs 是由社区驱动的开源sphinx doc 托管服务,我们可以用来方便的构建以及发布文档 这是一个简单的demo 项目,使用了用的比较多的sphinx_rtd_theme 主题,主 ...

  4. Linux下搭建iSCSI共享存储的方法 Linux-IO Target 方式CentOS7-1810下实现

    iSCSI(internet SCSI)技术由IBM公司研究开发,是一个供硬件设备使用的.可以在IP协议的上层运行的SCSI指令集,这种指令集合可以实现在IP网络上运行SCSI协议,使其能够在诸如高速 ...

  5. 70: libreoj #2424 区间dp

    $des$ $sol$ $f_{i, j, k} => a => [1, i], b => [1, j], a_i = b_j | a_i != b_j , a_i => 0 ...

  6. gulp开发工具之postcss

    参考文章:http://www.cnblogs.com/givebest/p/4771154.html package.json { "name": "postcss&q ...

  7. GoCN每日新闻(2019-10-17)

    GoCN每日新闻(2019-10-17) 通过go module管理go tool https://marcofranssen.nl/manage-go-tools-via-go-modules/ 使 ...

  8. mysql 创建时间字段

    alter table table1 add order_date datetime null; mysql> select * from table1; +----------+------- ...

  9. 百度地图中如何获取到发布的SHA1

    百度地图中如何获取到发布的SHA1 下面介绍的是一种通过命令的方式获取到发布版SHA1的方法: 打开Android的命令行Terminal: 1.首先进入到.android文件所在的目录,我的是如下图 ...

  10. debian源配置实例

    debian9 官方源: deb http://ftp.debian.org/debian stretch main contribdeb http://ftp.debian.org/debian s ...