配置applicationContext.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/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!--自动装箱-->
<context:component-scan base-package="com.donghua.jdbc"></context:component-scan>

<!--引入外部配置文件-->
<context:property-placeholder location="classpath:com/donghua/jdbc/jdbc.properties"/>
<bean id="userDao" class="com.donghua.jdbc.UserDao"></bean>

<!-- 一、配置数据库连接池 ComboPooledDataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<!-- Connection properties -->
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
<!-- Pool properties-->
<property name="minPoolSize" value="2" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="50" />
<property name="idleConnectionTestPeriod" value="3000" />
<property name="loginTimeout" value="300" />

</bean>

<!-- 二、配置JdbcTemplate,引入模板数据源 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>

User.java

public class User {
private int age;
private String name;

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

package com.donghua.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class UserDao {
//注入模板
@Resource
private JdbcTemplate jdbcTemplate;

public void save(final User u){
jdbcTemplate.execute(new ConnectionCallback() {
@Override
public Object doInConnection(Connection conn) throws SQLException,
DataAccessException {
String sql ="insert into t_user(age,userName) values(?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, u.getAge());
ps.setString(2, u.getName());
ps.execute();
ps.close();
return null;
}
});
}

/* (non-Javadoc)
* @see com.donghua.jdbc.UserInterface#update()
*/
public void update(){}
}

测试

public class UserDaoTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml", getClass());

private UserDao userDao1 = (UserDao) ac.getBean("userDao");

@Test
public void testSave() {
User user = new User();
user.setName("李四");
userDao1.save(user);

}

@Test
public void testUpdate() {

}

}

Spring_database_Template的更多相关文章

随机推荐

  1. 柯南君:看大数据时代下的IT架构(2)消息队列之RabbitMQ-基础概念详细介绍

    一.基础概念详细介绍 1.引言 你是否遇到过两个(多个)系统间需要通过定时任务来同步某些数据?你是否在为异构系统的不同进程间相互调用.通讯的问题而苦恼.挣扎?如果是,那么恭喜你,消息服务让你可以很轻松 ...

  2. JAVA 中的RMI是什么

    RMI的概念 RMI(Remote Method Invocation)远程方法调用是一种计算机之间利用远程对象互相调用实现双方通讯的一种通讯机制.使用这种机制,某一台计算机上的对象可以调用另外 一台 ...

  3. 如何阅读mysql源码

    在微博上问mysql高手,如何阅读mysql 源码大致给了下面的一些建议: step 1,知道代码的组织结构(官方文档http://t.cn/z8LoLgh: Step2: 尝试大致了解一条sql涉及 ...

  4. deque双向队列(转)

    deque双向队列是一种双向开口的连续线性空间,可以高效的在头尾两端插入和删除元素,deque在接口上和vector非常相似,下面列出deque的常用成员函数:   deque的实现比较复杂,内部会维 ...

  5. JAVA里的String、Timestamp、Date相互转换

    Timestamp转化为String: SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义 ...

  6. C# 想要程序文件移动 而数据保持相对位置

    如果用的数据库是access数据库 可以把数据库文件放到bin\debug下面,引用相对位置就可以了 如果程序中有上载文件,而程序需要使用到该文件,那么我们最好也是引用相对文件,我们只需要在数据表中的 ...

  7. Memcached基础

    1.实例化 MemcachedClient client = new XMemcachedClient(); public XMemcachedClient() public XMemcachedCl ...

  8. SQL中存储过程和自定义函数的区别(转载)

    存储过程:     存储过程可以使得对数据库的管理.以及显示关于数据库及其用户信息的工作容易得多.存储过程是 SQL 语句和可选控制流语句的预编译集合,以一个名称存储并作为一个单元处理.存储过程存储在 ...

  9. JDK源码学习--String篇(三) 存储篇

    在进一步解读String类时,先了解下内存分配和数据存储的. 数据存储 1.寄存器:最快的存储区,位于处理器的内部.由于寄存器的数量有限,所以寄存器是按需分配. 2.堆栈:位于RAM中,但是通过堆栈指 ...

  10. Mybatis设置自增主键

    useGeneratedKeys="true" keyProperty="id" 方法1: <insert id="insert" p ...