spring系列:九、spring jdbc template

简单使用

  • 使用spring自带的连接池
<!--声明数据库连接池-->
<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/spring"/>
    <property name="username" value="root"/>
    <property name="password" value="1234"/>
</bean>
@Test
public void test01() {
    // 创建数据库连接池
    DriverManagerDataSource dataSource = new DriverManagerDataSource();

    // 设置数据库连接池的属性
    dataSource.setDriverClassName("driver");
    dataSource.setUrl("url");
    dataSource.setUsername("username");
    dataSource.setPassword("password");

    // 创建spring jdbc template
    JdbcTemplate template = new JdbcTemplate();
    template.setDataSource(dataSource);

    // 执行数据库操作
    template.execute("update user set username = '张三' where id = 1 ");
}

配置spring内置连接池

<!--声明数据库连接池-->
<bean id="driverManagerDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/spring"/>
    <property name="username" value="root"/>
    <property name="password" value="1234"/>
</bean>

<!--声明JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="driverManagerDataSource"/>
</bean>
@Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate template;

@Test
public void test02() {
    template.execute("update user set username = '李四' where id = 2 ");
}

配置c3p0连接池

<!--配置自定义的c3p0数据库连接池-->
<bean id="c3poDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="dataSourceName" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"/>
    <property name="user" value="root"/>
    <property name="password" value="1234"/>
</bean>

<!--声明JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="c3p0DataSource"/>
</bean>

引入外部配置文件

jdbc.driverClass=driver
jdbc.jdbcUrl=url
jdbc.username=username
jdbc.password=password
<!--引入外部属性文件-->
<context:property-placeholder location="classpath:db.properties"/>

<!--通过引入外部属性文件来配置c3p0数据库连接池-->
<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driverClass}"/>
    <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
    <property name="user" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

<!--声明JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="c3p0DataSource"/>
</bean>

JdbcTemplate API

Spring JdbcTemplate方法详解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值