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语言编写的类和接口组 ...
随机推荐
- 练习json读取中文
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Java企业微信开发_15_查询企业微信域名对应的所有ip
一.前言 二.方法 1.在线网站 百度搜索"域名查IP",可查到如下网站,输入域名即可查到所有IP: 站长工具 site.ip138.com tools.ipip.net 2.li ...
- LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- MySQL for Mac在Mac终端导入导出.sql文件
https://www.cnblogs.com/code4app/p/6222310.html 1.导入 打开终端输入:(前提是已经配置过MySQL环境变量) mysql -u root -p cre ...
- dilworth定理的通俗讲解
度娘定义:在数学理论中的序理论与组合数学中,Dilworth定理根据序列划分的最小数量的链描述了任何有限偏序集的宽度.其名称取自数学家Robert P. Dilworth. 反链是一种偏序集,其任意两 ...
- hdu-2673-shǎ崽 OrOrOrOrz(水题)
注意输出格式 #include <iostream> #include <algorithm> using namespace std; +]; int main() { in ...
- SQL使用指南(1)—— 数据定义语言(DDL)
1.使用create 语句创建表 CREATE TABLE table_name (column_name datatype[null|not null], column_name datatype[ ...
- 每天一个linux命令(15):head命令
版权声明更新:2017-05-19博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...
- 20181229模拟 T1 palindrome
20181229模拟 T1 palindrome 题意 : \(S\)是字符串\(s\)的子串可重集,求\(\sum\limits_{x\in S}\sum\limits_{y\in S}(|x|+| ...
- Linux动态gif图的录制
Linux动态gif图的录制 Linux动态gif图的录制 byzanz的安装与使用 recordmydesktop再convert成gif 参考资料 前几天写了两篇博客vim的配置和Vim的自动代码 ...