Spring JDBC开发

@(Spring)[spring jdbc]

  • Spring JDBC开发

    • Spring的JDBC模板的概述

      • 什么是JDBC的模板
    • Spring的JDBC模板入门
      • 创建web项目引入jar包
      • 创建表
      • 编写测试
    • Spring管理连接池和模板
      • Spring管理内置连接池
      • Spring管理Spring JDBC模板
      • Spring管理DBCP连接池
      • Spring管理C3P0连接池
      • 将连接数据库信息提取到属性文件中
    • Spring的JDBC的模板的API完成CRUD
      • 插入更新删除的方法
      • 查询的方法
    • 案例

Spring的JDBC模板的概述

Spring是一站式框架有EE开发的每一层的解决方案,像持久层Spring提供了JDBC的模板和ORM模块用于整合其他的持久层框架。

什么是JDBC的模板

JDBC的模板是Spring提供的用于简化JDBC开发的一个技术。类似于DBUtils。

PS:在Spring4中已经移除对IBatis的支持,如果需要使用,请使用3版本的orm包

Spring的JDBC模板入门

创建web项目,引入jar包

创建表

create table account(id int primary key AUTO_INCREMENT,name varchar(20),money double
);

编写测试

package com.pc;import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;/*** JDBC测试* * @author Switch* @data 2016年11月25日* @version V1.0*/
public class JDBCTest {/*** 测试Spring JDBC模板*/@Testpublic void testSpringJDBC() {// 创建连接池DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/test");dataSource.setUsername("root");dataSource.setPassword("123456");// 创建Spring JDBC模板JdbcTemplate jdbcTemplate = new JdbcTemplate();jdbcTemplate.setDataSource(dataSource);jdbcTemplate.update("insert into account(id, name, money) values(?,?,?)", null, "Switch", 50000d);}
}

Spring管理连接池和模板

Spring管理内置连接池

<!-- Spring配置内置连接池 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/test"/><property name="username" value="root" /><property name="password" value="123456"/>
</bean>

Spring管理Spring JDBC模板

<!-- Spring配置Spring JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource" />
</bean>

Spring管理DBCP连接池

  • 引入DBCP连接池jar包
  • 在Spring中配置DBCP的连接池
<!-- Spring配置dbcp连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/test" /><property name="username" value="root" /><property name="password" value="123456" />
</bean>

Spring管理C3P0连接池

  • 引入c3p0连接池的jar包
  • 在Spring中配置C3P0连接池
<!-- spring配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver" /><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" /><property name="user" value="root" /><property name="password" value="123456" />
</bean>

将连接数据库信息提取到属性文件中

  • 提供属性文件db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456
  • 在Spring的配置文件中引入外部属性文件

    • 通过<context>标签引入

      • <!-- 引入外部属性文件 -->
        <context:property-placeholder location="classpath:db.properties"/>
    • 通过<bean>标签引入
      • <!-- 引入外部属性文件 -->
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:db.properties"/>
        </bean>
  • 使用属性文件中的key配置连接池

<!-- spring配置c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" />
</bean>

Spring的JDBC的模板的API(完成CRUD)

插入、更新、删除的方法

  • int update(String sql,Object… args);
package com.pc;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/*** JDBC测试* * @author Switch* @data 2016年11月25日* @version V1.0*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JDBCTest {@Resource(name = "jdbcTemplate")private JdbcTemplate jdbcTemplate;@Test// insert语句public void insert() {jdbcTemplate.update("insert into account(id, name, money) values(?,?,?)", null, "Kity", 10000d);}@Test// update语句public void update() {jdbcTemplate.update("update account set name = ?, money = ? where id = ?", "Tom", 20000d, 2);}@Test// delete语句public void delete() {jdbcTemplate.update("delete from account where id = ?", 2);}
}

查询的方法

  • T queryForObject(String sql,Class<T> c,Object… args);
  • T queryForObject(String sql,RowMapper<T> rowMapper,Object… args);
  • List<T> query(String sql,RowMapper rowMapper,Object… args);
package com.pc;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/*** JDBC测试* * @author Switch* @data 2016年11月25日* @version V1.0*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JDBCTest {@Resource(name = "jdbcTemplate")private JdbcTemplate jdbcTemplate;@Test// 查询单行单列public void queryForObject1() {String name = jdbcTemplate.queryForObject("select name from account where id = ?", String.class, 1);System.out.println(name);}@Test// 查询一行,封装到对应的对象public void queryForObject2() {Account account = jdbcTemplate.queryForObject("select * from account where id = ?", new AccountRowMapper(), 1);System.out.println(account);}@Test// 查询多行,封装到对应对象集合中public void queryForList1() {List<Account> accounts = jdbcTemplate.query("select * from account", new AccountRowMapper());System.out.println(accounts);}
}
package com.pc;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
/*** 账号类行映射* * @author Switch* @data 2016年11月25日* @version V1.0*/
public class AccountRowMapper implements RowMapper<Account> {@Overridepublic 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.getDouble("money"));return account;}
}

案例

GitHub:SpringDataTest

Spring JDBC开发相关推荐

  1. 【spring】spring JDBC开发 、 将创建表生成sql语句的方法

    将navicate中已存在表的创建转化成sql语句的方法 1.右击表,选择对象信息 2.点击DDL jar包引入 1.spring-starter-jdbc 代码实现: <dependency& ...

  2. Spring整合JDBC开发

    背景 在JDBC开发中,充斥这大量重复的代码,可能只是换了个SQL语句,其他代码是完全不用变的.Spring的jar包里,提供了一个叫JDBCTemplate的模板,在保持操作灵活方便的情况下,将代码 ...

  3. (转)Spring+JDBC组合开发

    http://blog.csdn.net/yerenyuan_pku/article/details/52882435 搭建和配置Spring与JDBC整合的环境 使用Spring+JDBC集成步骤如 ...

  4. Spring JDBC,JDBCTemplate对象简化JDBC的开发

    Spring JDBC,JDBCTemplate对象简化JDBC的开发 本篇文章中使用到了Druid数据库连接池,和自己创建的一个配合数据库连接池的工具类,本篇文章对于这些没有进行详细说明,对于这个, ...

  5. Spring + JDBC + Struts联合开发(实现单表的CRUD)

    这里使用Spring + JDBC +Struts,完成新闻表的添加,修改,删除和查询功能. 建立数据库: [sql] view plain copy CREATE TABLE news_type ( ...

  6. Spring JDBC详解

    <Spring JDBC详解> 本文旨在讲述Spring JDBC模块的用法.Spring JDBC模块是Spring框架的基础模块之一. 一.概述 在Spring JDBC模块中,所有的 ...

  7. Spring Boot——开发新一代Spring Java应用

    2019独角兽企业重金招聘Python工程师标准>>> Spring官方网站本身使用Spring框架开发,随着功能以及业务逻辑的日益复杂,应用伴随着大量的XML配置文件以及复杂的Be ...

  8. Spring JDBC-使用Spring JDBC访问数据库

    概述 使用Spring JDBC 基本的数据操作 更改数据 返回数据库表的自增主键值 批量更改数据 查询数据 使用RowCallbackHandler处理结果集 使用RowMapperT处理结果集 R ...

  9. Spring-Spring MVC + Spring JDBC + Spring Transaction + Maven 构建web登录模块

    概述 功能简介 环境准备 构建工具Maven 数据库脚本Oracle 建立工程 类包及Spring配置文件规划 持久层 建立领域对象 用户领域对象 登录日志领域对象 UserDao LoginLogD ...

最新文章

  1. python使用matplotlib可视化、移除可视化图像X轴坐标轴的刻度线和标签( remove the default axis ticks and labels of x axis)
  2. NSArray,NSSet,NSDictionary总结 (转)
  3. 清理多个varnish服务器缓存的脚本
  4. DEV express 对Gridview某行的元素赋值
  5. unity打包模型存在的一个问题
  6. perl 面向对象demo
  7. spark基础之spark streaming的checkpoint机制
  8. AndroidStudio debug
  9. ajax 关闭弹窗并跳转到url_Python 爬虫 | Ajax数据爬取
  10. [目标跟踪] 论文笔记:Parallel Tracking and Verifying(PTAV-Update)
  11. 【ntp时间校准配置】
  12. CNN卷积神经网络(图解CNN)
  13. (一)线段树入门--补充与其他模板
  14. android 适配俄语添加,【技术贴】怎么在手机上添加俄语输入法?,怎样在电脑上打俄语重音?...
  15. 整数解(韦达定理解法)
  16. Mybatis批量update修改实例
  17. 获取微信用户在微信小店的订单
  18. skycons.js 基于canvas的天气动态图标小插件
  19. C++实现前向欧拉法Forward Euler解决偏微分方程
  20. Java调用aliyun OCR图文识别

热门文章

  1. 亲测使用 swagger 动态修改后台默认访问地址 swagger-ui.html
  2. leetcode 寻找两个有序数组的中位数
  3. 浅谈 TypeScript【下】-- TypeScript 语言规范与基本应用
  4. C#LeetCode刷题之#700-二叉搜索树中的搜索(Search in a Binary Search Tree)
  5. C#LeetCode刷题之#840-矩阵中的幻方(Magic Squares In Grid)
  6. centos root密码_如何在CentOS中恢复丢失的root密码
  7. dell加装固态硬盘_技术丨如何进行笔记本硬盘拆装?
  8. mysql 连接失败的日志(或者输出)结果分析
  9. freeswitch 安装 fail2ban 动态拦截IP攻击
  10. 使用 Python 实现鼠标键盘自动化