Spring:获取JDBCTemplate对象

1、JDBC与JDBCTemplate关系

怎么使用代码对数据库进行操作呢?这个时候就用到JDBC。

JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。

而template,就是模板,是Spring框架为我们提供的.所以JDBCTemplate就是Spring对JDBC的封装,通俗点说就是Spring对jdbc的封装的模板

2、JDBCTemplate对象的获取

JdbcTemplate是Spring框架自带的对JDBC操作的封装,目的是提供统一的模板方法使对数据库的操作更加方便、友好,效率也不错。但是功能还是不够强大(比如不支持级联属性),在实际应用中还需要和hibernate、mybaties等框架混合使用。

通常情况下,有三种种方式得到JDBCTemplate对象。

1、我们可以在自己定义的dao实现类中注入一个DataSource 引用来完成JdbcTemplate 的实例化。也就是它是从外部“注入” DataSource 到dao 中,然后 自己实例化JdbcTemplate,然后将DataSource 设置到JdbcTemplate 对象中。

xml配置:

<!--引入外部属性文件-->
<context:property-placeholder location="classpath:dbconfig.properties"/>

<!--配置C3p0的连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!--从dbconfig.properties配置文件中取值-->
    <property name="driverClass" value="${db.driverClass}"/>
    <property name="jdbcUrl" value="${db.url}"/>
    <property name="user" value="${db.username}"/>
    <property name="password" value="${db.password}"/>
</bean>

<!-- 在dao类注入数据源-->
<bean id="accountDao" class="com.hh.dao.AccountDaoImpl">
    <property name="dataSource" ref="dataSource"/>
</bean>

dao类:

public class AccountDaoImpl implements AccountDao {
    
    @Autowired
    private DataSource dataSource;
    
    //自己实例化JdbcTemplate
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    //通过setter方法注入DataSource
    public void setDataSource(ComboPooledDataSource dataSource) {
    }
}

2、 在 Spring 的 IoC 容器中配置一个 JdbcTemplate 的 bean,将 DataSource 注入进来,然后再把JdbcTemplate 注入到自定义dao中。

xml配置:

<!--引入外部属性文件-->
<context:property-placeholder location="classpath:dbconfig.properties"/>

<!--配置C3p0的连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
   <!--从dbconfig.properties配置文件中取值-->
   <property name="driverClass" value="${db.driverClass}"/>
   <property name="jdbcUrl" value="${db.url}"/>
   <property name="user" value="${db.username}"/>
   <property name="password" value="${db.password}"/>
</bean>

<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
   <!--在JdbcTemplate中通过setDataSource(DataSource dataSource)方法注入外部的数据源-->
   <property name="dataSource" ref="dataSource"/>
</bean>

<!--在dao层注入JdbcTemplate-->
<bean id="accountDao" class="com.hh.dao.AccountDaoImpl">
   <property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>

dao类:

public class AccountDaoImpl implements AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;
	//将JdbcTemplate通过setter方法注入到自定义dao中。
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    }
}

3、Spring 提供了org.springframework.jdbc.core.support.JdbcDaoSupport 类 ,这个类定义了 JdbcTemplate 属性,也定义了DataSource 属性。当设置DataSource 属性的时候,会创 建jdbcTemplate 的实例,所以我们自己编写的DAO 只需要继承JdbcDaoSupport 类, 然后注入DataSource 即可。

xml配置:

<!--引入外部属性文件-->
<context:property-placeholder location="classpath:dbconfig.properties"/>

<!--配置C3p0的连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!--从dbconfig.properties配置文件中取值-->
    <property name="driverClass" value="${db.driverClass}"/>
    <property name="jdbcUrl" value="${db.url}"/>
    <property name="user" value="${db.username}"/>
    <property name="password" value="${db.password}"/>
</bean>

<!-- 配置DAO类-->
<bean id="accountDao" class="com.hh.dao.AccountDaoImpl">
    <property name="dataSource" ref="dataSource"/>
</bean>

dao类:

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao  {
    
    @Override
    public void outMoney(String out, Double monry) {
        this.getJdbcTemplate().update("sql ");
    }
}
原创文章 723 获赞 153 访问量 18万+

猜你喜欢

转载自blog.csdn.net/qq_42764468/article/details/106036559