spring jdbcTemplate

spring框架提供了纯JDBC的实现方式可以在要求高性能和灵活性的程序中使用sql语句进行开发,Spring JDBC框架有4个部分组成,即core、datasource、object、support.

core包:提供了JDBC模板类,其中jdbcTemplate是Core包的核心类。

datasource包:提供了简化JDBC数据源的工具类并提供了一些DataSource简单实现类,从而使这些DataSource获取的链接能自动得到Spring事务管理的支持

object包:提供关系数据对象表示形式,如 MappingSQLQuery、SQLUpdate、SQLCall、SQLFunction等。

support包:提供将JDBC异常转换为DAO费检查异常的转换类和一些工具类。

下面讲解jdbcTemplate的基本实现步骤:

1.导入org.springframework.jdbc-*.jar,如图所示:

image

2.在applicationContext.xml配置文件中配置数据源dataSource

spring配置数据源的方式有三种:c3p0、dbcp和spring内置实现。

spring内置数据源配置:

<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${divername}"></property> <!--驱动类配置-->
        <property name="url" value="${utl}"></property><!--数据库连接url-->
        <property name="username" value="${username}"></property><!--数据库用户名-->
        <property name="password" value="${passworld}"></property><!--数据库密码-->
    </bean>

c3p0数据源配置:

c3p0的数据原配置需要引入一个jar包如图所示:

image

<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${divername}"></property> <!--驱动类配置-->
        <property name="jdbcUrl" value="${url}"></property><!--数据库连接url-->
        <property name="user" value="${username}"></property><!--数据库用户名-->
        <property name="password" value=""></property><!--数据库密码-->
    </bean>

dbcp数据原配置:

dbcp数据源的配置同样需要引入jar包如图所示:

image

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${divername}"></property> <!--驱动类配置-->
        <property name="url" value="${utl}"></property><!--数据库连接url-->
        <property name="username" value="${username}"></property><!--数据库用户名-->
        <property name="password" value="${passworld}"></property><!--数据库密码-->
    </bean>

3.在applicationContext.xml配置文件中配置jdbcTemplate,并为其注入dataSource。

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="datasource"/>
    </bean>

4.编写Dao接口即实现类并在其方法中使用jdbcTemplate API实现数据库操作。

JdbcTemplate API常用方法回顾:

image

使用JdbcTemplate实现存储过程调用

spring JdbcTemplate支持对存储过程的调用,JdbcTemplate类支持存储过程回调如下:

CallableStatementCreator:通过回调获取JdbTemplate提供的,由用户使用该Connection创建想关的CallableStatement。

CallableStatementCallback:通过回调获取JdbcTemplate提供的CallableStatement,用户可以在该CallableStatement执行任何操作。

String departmentName=(String)getJdbcTemplate().execute(
                new CallableStatementCreator() {
                    @Override
                    public CallableStatement createCallableStatement(Connection con)
                            throws SQLException {
                        String storedProc="{调用存储过程的sql语句}";
                        CallableStatement cs=con.prepareCall(storedProc);
                        cs.setString(1,"sdfd");//向存储过程中传入String类型的输入参数(第一个是参数的下标从1开始第二个是参数值)
                        cs.registerOutParameter(2,Types.VARCHAR);//向存储过程中传入输出参数
                        return cs;
                    }
                },new CallableStatementCallback() {

                    @Override
                    public Object doInCallableStatement(CallableStatement cs)
                            throws SQLException, DataAccessException {
                            cs.execute();
                        return cs.getString(2);
                    }
                }
                );
原文地址:https://www.cnblogs.com/wangzheand/p/5962981.html