spring配置数据源,连接数据库,实现spring的jdbc操作

本文记录了使用Spring框架配置数据源并连接数据库的过程。在解决配置错误问题时,对比了继承JdbcTemplate对象与不继承的区别。测试类和XML配置文件的展示提供了具体实现细节。
摘要由CSDN通过智能技术生成

今天用spring容器来连接数据库,一直报错。

这是以前的java代码

public class TuserDao extends JdbcTemplate{
	public void insert() {
        this.update(" insert into STUDENT (stu_id,STUNAME) values (?,?) ", new Object[]{5,"aaa"});
    }
}

这是修改之后的代码:

public class TuserDao {
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    public void insert() {
        this.jdbcTemplate.update(" insert into STUDENT (stu_id,STUNAME) values (?,?) ", new Object[]{4,"aaa"});
    }
}

前后有什么区别呢?
以前是继承了JdbcTemplate对象,后者是没有继承。

测试类:

@Test
    public void aa() {
        ApplicationContext context = new
                ClassPathXmlApplicationContext("com/etc/spring/jdbc.xml");
        TuserService ts = (TuserService) context.getBean("tuserService");
        ts.getTuserDao().insert();
    }

下面是spring 的 xml配置文件

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--    将TuserService类添加到容器中-->
    <bean class="com.etc.spring.service.TuserService" id="tuserService">
        <property name="tuserDao" ref="tuserDao"></property>
    </bean>
    <!--    将TuserDao添加到容器中-->
    <bean class="com.etc.spring.dao.TuserDao" id="tuserDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <!--    数据库数据源的配置-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.121.132:1521:orcl"/>
        <property name="driverClass" value="oracle.jdbc.OracleDriver"/>
        <property name="user" value="aa"/>
        <property name="password" value="root"/>
        <property name="initialPoolSize" value="3"/>
        <property name="maxPoolSize" value="10"/>
        <property name="minPoolSize" value="1"/>
        <property name="acquireIncrement" value="3"/>
        <property name="maxIdleTime" value="60"/>
    </bean>
    <!--    配置数据源 jdbcTemplate-->
    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

如果用继承的方式,那xml的写法和这个是不一样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值