day39-Spring 17-Spring的JDBC模板:完成增删改的操作

JdbcTemplate根DBUtils非常类似,你要是有非常多的Dao,你每一个Dao都需要写它

    /*在Dao层注入JDBC模板*/
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

而且你的每一个配置文件都得给它注入一下.

    <bean id="userDao" class="cn.itcast.spring3.demo2.UserDao">
       <property name="jdbcTemplate" ref="jdbcTemplate"></property>

    </bean>




JdbcDaoSupport的源码

/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.jdbc.core.support;

import java.sql.Connection;

import javax.sql.DataSource;

import org.springframework.dao.support.DaoSupport;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.jdbc.support.SQLExceptionTranslator;

/**
 * Convenient super class for JDBC-based data access objects.
 *
 * <p>Requires a {@link javax.sql.DataSource} to be set, providing a
 * {@link org.springframework.jdbc.core.JdbcTemplate} based on it to
 * subclasses through the {@link #getJdbcTemplate()} method.
 *
 * <p>This base class is mainly intended for JdbcTemplate usage but can
 * also be used when working with a Connection directly or when using
 * <code>org.springframework.jdbc.object</code> operation objects.
 *
 * @author Juergen Hoeller
 * @since 28.07.2003
 * @see #setDataSource
 * @see #getJdbcTemplate
 * @see org.springframework.jdbc.core.JdbcTemplate
 */
public abstract class JdbcDaoSupport extends DaoSupport {

    private JdbcTemplate jdbcTemplate;

    /**
     * Set the JDBC DataSource to be used by this DAO.
     */
    public final void setDataSource(DataSource dataSource) {
        if (this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) {
            this.jdbcTemplate = createJdbcTemplate(dataSource);
            initTemplateConfig();
        }
    }

    /**
     * Create a JdbcTemplate for the given DataSource.
     * Only invoked if populating the DAO with a DataSource reference!
     * <p>Can be overridden in subclasses to provide a JdbcTemplate instance
     * with different configuration, or a custom JdbcTemplate subclass.
     * @param dataSource the JDBC DataSource to create a JdbcTemplate for
     * @return the new JdbcTemplate instance
     * @see #setDataSource
     */
    protected JdbcTemplate createJdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * Return the JDBC DataSource used by this DAO.
     */
    public final DataSource getDataSource() {
        return (this.jdbcTemplate != null ? this.jdbcTemplate.getDataSource() : null);
    }

    /**
     * Set the JdbcTemplate for this DAO explicitly,
     * as an alternative to specifying a DataSource.
     */
    public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
        initTemplateConfig();
    }

    /**
     * Return the JdbcTemplate for this DAO,
     * pre-initialized with the DataSource or set explicitly.
     */
    public final JdbcTemplate getJdbcTemplate() {
      return this.jdbcTemplate;
    }

    /**
     * Initialize the template-based configuration of this DAO.
     * Called after a new JdbcTemplate has been set, either directly
     * or through a DataSource.
     * <p>This implementation is empty. Subclasses may override this
     * to configure further objects based on the JdbcTemplate.
     * @see #getJdbcTemplate()
     */
    protected void initTemplateConfig() {
    }

    @Override
    protected void checkDaoConfig() {
        if (this.jdbcTemplate == null) {
            throw new IllegalArgumentException("‘dataSource‘ or ‘jdbcTemplate‘ is required");
        }
    }

    /**
     * Return the SQLExceptionTranslator of this DAO‘s JdbcTemplate,
     * for translating SQLExceptions in custom JDBC access code.
     * @see org.springframework.jdbc.core.JdbcTemplate#getExceptionTranslator()
     */
    protected final SQLExceptionTranslator getExceptionTranslator() {
        return getJdbcTemplate().getExceptionTranslator();
    }

    /**
     * Get a JDBC Connection, either from the current transaction or a new one.
     * @return the JDBC Connection
     * @throws CannotGetJdbcConnectionException if the attempt to get a Connection failed
     * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection(javax.sql.DataSource)
     */
    protected final Connection getConnection() throws CannotGetJdbcConnectionException {
        return DataSourceUtils.getConnection(getDataSource());
    }

    /**
     * Close the given JDBC Connection, created via this DAO‘s DataSource,
     * if it isn‘t bound to the thread.
     * @param con Connection to close
     * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
     */
    protected final void releaseConnection(Connection con) {
        DataSourceUtils.releaseConnection(con, getDataSource());
    }

}

时间: 2024-11-05 20:45:05

day39-Spring 17-Spring的JDBC模板:完成增删改的操作的相关文章

eclipse控制台下实现jdbc简单的增删改查测试

1.现在MySQL中创建一个表 2.首先创建一个类 //导入的包 import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet; public class DBTil { } 3.然后实例化各个对象 1 //username和password是连接数据库的用户名和密码 2 private static String usern

JDBC学习笔记——增删改查

1.数据库准备  要用JDBC操作数据库,第一步当然是建立数据表: ? 1 2 3 4 5 6 CREATE TABLE `user` (   `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,   `name` varchar(45) DEFAULT NULL,   `birthday` date DEFAULT NULL,   `money` double DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET

struts spring hibernate 三大框架实现基本的增删改查技术

/*struts.xml配置*/ 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd">

如何用Spring框架的&lt;form:form&gt;标签实现REST风格的增删改查操作

1.首先创建两个bean类,Employee(职工)和Department(部门),一个部门可以有多个职工 Employee类(属性:职工ID:id:姓名:lastName:邮箱:email:性别:gender:所属部门:department) 1 package com.bwlu.bean; 2 public class Employee { 3 private Integer id; 4 private String lastName; 5 private String email; 6 /

简单的JDBC的增删改查操作-&gt;抽取了基类,附源码

1.主程序 1 package com.xyyz.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 8 import com.xyyz.utils.JDBCUtils; 9 10 public class JDBCDemo { 11 12 public static vo

JDBC编程 之 增删改查

JDBC编程之数据增加,更改,查询,删除 1 package com.good.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 import java.sql.Statement; 7 8 public class Main { 9 //1数据库连接的方法就独立出来了 10 public static Connection getConnec

简单的JDBC的增删改查操作,附源码

1 package com.xyyz.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 8 public class JDBCDemo { 9 10 public static void main(String[] args) throws Exception { 11

JAVA JDBC 简单的增删改查

package jdbc_util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcDemo { // 设置汉字编码 String jdbcUrl = "jdbc:mysql://localhost:3306/test?use

JSP + Servlet + JDBC + Mysql 实现增删改查 课程管理系统

1.项目目录结构 2.项目环境 Eclipse IDE  MYSQL  jdk tomcat jar包 3.数据库相关配置 先创建一个course的数据库名,然后在建立一个course的表 要注意将id字段 自动递增 4.源代码 1.Course.java package com.hjf.entity; public class Course { private int id; private String name; private String teacher; private String