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配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
 5     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
 7     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 8     xsi:schemaLocation="
 9         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
11         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
12         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
13         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
14         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
15         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
16         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
17         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
18     
19     
20     
21 </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】
1 driverClassName=com.mysql.jdbc.Driver
2 url=jdbc:mysql://127.0.0.1:3306/xiangxu
3 username=root
4 password=182838
5 maxActive=1
6 maxWait=3000
properties文件【MySQL】

  2.4 在spring配置文件中配置 Jdbc Temple

    2.4.1 配置properties的bean

    2.4.2 配置数据库链接池

    2.4.3 配置jdbcTemplate

  2.5 在spring配置文件中配置注解扫描

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 4     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
 5     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
 6     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
 7     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 8     xsi:schemaLocation="
 9         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
11         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
12         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
13         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
14         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
15         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
16         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
17         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
18     
19     <!-- 读取config.properties文件 -->
20     <util:properties id="config" 
21     location="classpath:config.properties"/>
22     
23     <!-- 配置连接池 -->
24     <bean id="ds" 
25     class="org.apache.commons.dbcp.BasicDataSource"       
26        destroy-method="close">       
27       <property name="driverClassName" 
28       value="#{config.driverClassName}" />      
29       <property name="url" 
30       value="#{config.url}" />      
31       <property name="username" 
32       value="#{config.username}" />      
33       <property name="password" 
34       value="#{config.password}" />      
35     </bean>
36     
37     <!-- 配置jdbcTemplate -->
38     <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
39         <property name="dataSource" ref="ds"></property>
40     </bean>
41     
42     <!-- 组件扫描 -->
43     <context:component-scan base-package="dao"></context:component-scan>
44     
45 </beans>
配置好的spring配置文件

3 利用springjdbc中JdbcTemplate类中的方法去实现数据库操作

  1 package dao;
  2 
  3 import java.io.Serializable;
  4 import java.sql.ResultSet;
  5 import java.sql.SQLException;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 
  9 import javax.annotation.Resource;
 10 
 11 import org.springframework.jdbc.core.JdbcTemplate;
 12 import org.springframework.jdbc.core.RowMapper;
 13 import org.springframework.stereotype.Repository;
 14 
 15 import entity.Admin;
 16 
 17 @Repository("adminDao")
 18 public class AdminDao implements Serializable {
 19     /**
 20      * JdbcTemplate提供了很多方法
 21      *         这些方法对jdbc api做了封装,从而简化了代码;不再需要考虑获取连接,关闭连接。
 22      * 另外,如果发生了异常,会自动封装成RuntimeException然后抛出
 23      * @param emp
 24      */
 25     @Resource(name="jt")
 26     private JdbcTemplate jt;
 27     
 28     /**
 29      * 向数据库插入数据
 30      * @param 
 31      */
 32     public void insert(Admin admin) {
 33         String sql = "INSERT INTO admin "
 34                 + "(name, password, gender) "
 35                 + "VALUES "
 36                 + "(?,?,?) ";
 37         Object [] args = {admin.getName(), admin.getPassword(), admin.getGender()};
 38         jt.update(sql, args); // update() 这个方法可以完成:插入 删除 修改 都能完成
 39     } 
 40     
 41     /**
 42      * 查询数据库中的所有数据
 43      * @return
 44      */
 45     public List<Admin> findAll() {
 46         List<Admin> admins = new ArrayList<Admin>();
 47         String sql = "SELECT * "
 48                 + "FROM admin ";
 49         admins = jt.query(sql, new AdminRowMapper());  // query() 方法可以实现 查询 功能
 50         return admins;
 51     }
 52     
 53     /**
 54      * 查询满足条件的员工信息(bug版)
 55      * @param id
 56      * @return
 57      */
 58     public Admin findById(Integer id) {
 59         Admin admin = null;
 60         String sql = "SELECT * "
 61                 + "FROM admin "
 62                 + "WHERE id = ? ";
 63         Object [] args = {id};
 64         admin = jt.queryForObject(sql, args, new AdminRowMapper());
 65         return admin;
 66     }
 67     
 68 //    查询满足条件的用户所有信息(加强版)
 69     public Admin findById2(Integer id) {
 70         Admin admin = null;
 71         String sql = "SELECT * "
 72                 + "FROM admin "
 73                 + "WHERE id = ? ";
 74         Object [] args = {id};
 75         List<Admin> admins = jt.query(sql, args, new AdminRowMapper());
 76         if(admins != null && admins.size() > 0) {
 77             admin = admins.get(0);
 78         }
 79         return admin;
 80     }
 81     
 82 //    修改满足指定条件的员工信息
 83     public void modify(Admin admin){
 84         String sql = "UPDATE admin "
 85                 + "SET name = ?, gender = ? "
 86                 + "WHERE id = ? ";
 87         Object[] args = {admin.getName(), admin.getGender(), admin.getId()};
 88         jt.update(sql, args);
 89     }
 90     
 91 //    删除满足指定条件的员工信息
 92     public void delete(Integer id) {
 93         String sql = "DELETE FROM "
 94                 + "admin "
 95                 + "WHERE id = ? ";
 96         Object [] args = {id};
 97         jt.update(sql, args);
 98     }
 99     
100     
101     
102 //    编写一个内部类:需要实现RowMapper接口
103 //    该类的作用:告诉spring怎么将数据库中的记录变成对象
104     class AdminRowMapper implements RowMapper<Admin> {
105 //        rs:查询到的结果集
106 //        rowNum:记录的下标(从0开始)
107         public Admin mapRow(ResultSet rs, int rowNum) throws SQLException {
108             Admin admin = new Admin();
109             admin.setId(rs.getInt("id"));
110             admin.setName(rs.getString("name"));
111             admin.setPassword(rs.getString("password"));
112             admin.setGender(rs.getString("gender"));
113             return admin;
114         }
115     }
116     
117 }
springjdbc中JdbcTemplate类中的方法使用案例

该博客源代码:点击前往

原文地址:https://www.cnblogs.com/NeverCtrl-C/p/6850741.html