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类中的方法使用案例

该博客源代码:点击前往

05-11 13:56