SpringJdbc 【springjdbc的使用方法】
1 什么是springjdbc
spring对jdbc的封装
2 使用SpringJdbc的编程步骤
2.1 导包
spring-jdbc : springjdbc的包
mysql : MySQL的驱动包
dbcp :数据库连接池
spring-webmvc : springmvc框架包
annotation :@resource需要用到的包,该包在Tomcat中有,如果是web项目而且运行环境是Tomcat的话就不需要导入这个包了
junit : 单元测试包
2.2 添加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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> </beans>
spring配置文件
2.3 添加含有数据库信息的的properties文件
driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@176.114.0.23:1521:orcl
username=jsd1608
password=jsd1608
maxactive=1
maxwait=3000
properties文件【oracle】
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/xiangxu
username=root
password=182838
maxActive=1
maxWait=3000
properties文件【MySQL】
2.4 在spring配置文件中配置 Jdbc Temple
2.4.1 配置properties的bean
2.4.2 配置数据库链接池
2.4.3 配置jdbcTemplate
2.5 在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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 读取config.properties文件 -->
<util:properties id="config"
location="classpath:config.properties"/> <!-- 配置连接池 -->
<bean id="ds"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="#{config.driverClassName}" />
<property name="url"
value="#{config.url}" />
<property name="username"
value="#{config.username}" />
<property name="password"
value="#{config.password}" />
</bean> <!-- 配置jdbcTemplate -->
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean> <!-- 组件扫描 -->
<context:component-scan base-package="dao"></context:component-scan> </beans>
配置好的spring配置文件
3 利用springjdbc中JdbcTemplate类中的方法去实现数据库操作
package dao; import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import javax.annotation.Resource; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository; import entity.Admin; @Repository("adminDao")
public class AdminDao implements Serializable {
/**
* JdbcTemplate提供了很多方法
* 这些方法对jdbc api做了封装,从而简化了代码;不再需要考虑获取连接,关闭连接。
* 另外,如果发生了异常,会自动封装成RuntimeException然后抛出
* @param emp
*/
@Resource(name="jt")
private JdbcTemplate jt; /**
* 向数据库插入数据
* @param
*/
public void insert(Admin admin) {
String sql = "INSERT INTO admin "
+ "(name, password, gender) "
+ "VALUES "
+ "(?,?,?) ";
Object [] args = {admin.getName(), admin.getPassword(), admin.getGender()};
jt.update(sql, args); // update() 这个方法可以完成:插入 删除 修改 都能完成
} /**
* 查询数据库中的所有数据
* @return
*/
public List<Admin> findAll() {
List<Admin> admins = new ArrayList<Admin>();
String sql = "SELECT * "
+ "FROM admin ";
admins = jt.query(sql, new AdminRowMapper()); // query() 方法可以实现 查询 功能
return admins;
} /**
* 查询满足条件的员工信息(bug版)
* @param id
* @return
*/
public Admin findById(Integer id) {
Admin admin = null;
String sql = "SELECT * "
+ "FROM admin "
+ "WHERE id = ? ";
Object [] args = {id};
admin = jt.queryForObject(sql, args, new AdminRowMapper());
return admin;
} // 查询满足条件的用户所有信息(加强版)
public Admin findById2(Integer id) {
Admin admin = null;
String sql = "SELECT * "
+ "FROM admin "
+ "WHERE id = ? ";
Object [] args = {id};
List<Admin> admins = jt.query(sql, args, new AdminRowMapper());
if(admins != null && admins.size() > 0) {
admin = admins.get(0);
}
return admin;
} // 修改满足指定条件的员工信息
public void modify(Admin admin){
String sql = "UPDATE admin "
+ "SET name = ?, gender = ? "
+ "WHERE id = ? ";
Object[] args = {admin.getName(), admin.getGender(), admin.getId()};
jt.update(sql, args);
} // 删除满足指定条件的员工信息
public void delete(Integer id) {
String sql = "DELETE FROM "
+ "admin "
+ "WHERE id = ? ";
Object [] args = {id};
jt.update(sql, args);
} // 编写一个内部类:需要实现RowMapper接口
// 该类的作用:告诉spring怎么将数据库中的记录变成对象
class AdminRowMapper implements RowMapper<Admin> {
// rs:查询到的结果集
// rowNum:记录的下标(从0开始)
public Admin mapRow(ResultSet rs, int rowNum) throws SQLException {
Admin admin = new Admin();
admin.setId(rs.getInt("id"));
admin.setName(rs.getString("name"));
admin.setPassword(rs.getString("password"));
admin.setGender(rs.getString("gender"));
return admin;
}
} }
springjdbc中JdbcTemplate类中的方法使用案例
该博客源代码:点击前往
SpringJdbc 【springjdbc的使用方法】的更多相关文章
- springJDBC的几种方法
1.简单粗暴,直接在类中创建连接池使用 package com.xiaostudy; import org.apache.commons.dbcp.BasicDataSource; import or ...
- SpringJDBC数据库的基本使用
SpringJDBC的基础使用部分内容 云笔记项目数据库部分采用的是Spring-MyBatis,前面学过了JDBC,SpringJDBC,Mybatis和Spring-MyBatis,有必要重新复习 ...
- springJDBC01 利用springJDBC操作数据库
1 什么是springJDBC spring通过抽象JDBC访问并一致的API来简化JDBC编程的工作量.我们只需要声明SQL.调用合适的SpringJDBC框架API.处理结果集即可.事务由Spri ...
- SpringJDBC解析4-query方法
重要步骤说明: 首先是从PersonServiceImpl方法进去,调用JdbcTemplate的query方法,然后执行一连串错中复杂的调用,而且里面有很多函数都是以回调形式处理, 1)JdbcTe ...
- SpringJDBC解析2-execute方法
大家都使用过JDBCTEMPLATE的execute方法,execute作为数据库操作的核心入口,将大多数数据库操作相同的步骤统一封装,而将个性化的操作使用参数PreparedStatementCal ...
- springJDBC实现查询方法二
无废话,看代码: @Override public List<Sites> queryAllSites(Pager pager) { String sql = "select * ...
- Spring DAO vs Spring ORM vs Spring JDBC
Pat 的疑惑 最近关注于 Spring 提供的数据访问技术,对于 Spring 相关的这几个项目有何不同我不是太明白: Spring-DAO (http://docs.spring.io/sprin ...
- Spring集成MyBatis01 【推荐使用】、springMVC中文乱码和json转换问题
1 导包 1.1 spring-webmvc : spring框架包(当然里面也包含springmvc) 1.2 mybatis : mybatis框架包 1.3 mybatis-spring : s ...
- SpringJDBC解析1-使用示例
JDBC(Java Data Base Connectivity,Java数据库连接)是一种用于执行SQL语句的JavaAPI,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组 ...
随机推荐
- uva 11088 暴力枚举子集/状压dp
https://vjudge.net/problem/UVA-11088 对于每一种子集的情况暴力枚举最后一个三人小组取最大的一种情况即可,我提前把三个人的子集情况给筛出来了. 即 f[S]=MAX{ ...
- OpenStack H版与 Ceph 整合的现状
转自:https://www.ustack.com/blog/openstack_and_ceph/ Contents 1 Ceph与Nova整合 2 Ceph与Cinder整合 3 相关Patch ...
- spring MVC multipart处理文件上传
在开发Web应用程序时比较常见的功能之一,就是允许用户利用multipart请求将本地文件上传到服务器,而这正是Grails的坚固基石——Spring MVC其中的一个优势.Spring通过对Serv ...
- POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)
A cellular automaton is a collection of cells on a grid of specified shape that evolves through a nu ...
- Java内存垃圾回收机制(转贴)
Java的堆是一个运行时数据区,类的实例(对象)从中分配空间.Java虚拟机(JVM)的堆中储存着正在运行的应用程序所建立的所有对象,这些对象通 过new.newarray.anewarray和mul ...
- c# Http请求之HttpClient
利用HttpClient进行Http请求,基于此,简单地封装了下: using System; using System.Collections.Generic; using System.Colle ...
- 基于JQ的多选/全选/反选及获取选中的值
<!-- author:青芒 --> <!DOCTYPE html> <html lang="en"> <head> <met ...
- Unity3D研究院之Assetbundle的实战(六十三)
http://www.xuanyusong.com/archives/2405 上一篇文章中我们相惜讨论了Assetbundle的原理,如果对原理还不太了解的朋友可以看这一篇文章:Unity3D研究院 ...
- 转载 VC轻松解析XML文件 - CMarkup类的使用方法
VC轻松解析XML文件 - CMarkup类的使用方法http://www.cctry.com/thread-3866-1-1.html VC解析XML文件的工具有很多,CMarkup, tinyXM ...
- Mesos问题汇总
1.Mesos的IP配置 我在虚拟机里面搭载了一个mesos,但是外主机无法通过http://ip:5050 我在虚拟机内部测试发现wget localhost:5050可以正常访问:但是wget i ...