配置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. 深入解析SSD中MLC与SLC的性能差异

    固态硬盘(Solid State Disk或Solid State Drive),也称作电子硬盘或者固态电子盘,是由控制单元和固态存储单元(DRAM或FLASH芯片)组成的硬盘. 固态硬盘的接口规范和 ...

  2. Delphi中TWebBrowser中注入Js

    最近帮朋友做一个软件,其中要自动化某网页中的操作,最简的操作是调用自己写的代码. 代码如下: procedure TForm1.Button2Click(Sender: TObject);var  i ...

  3. 解析配置文件ConfigParser模块

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...

  4. 将外部准备好的sqlite导入到项目当中

    首先,将sqlite数据库文件放在Resource文件夹下,并且允许其编译到项目当中. 之后在AppDelegate当中执行一些代码,这里将代码封装了一个Helper: #import "R ...

  5. BeanUtils\DBUtils

    BeanUtil: 需要导入 beanutil包和logging日志包 用于给对象属性赋值. setProperty与copyProperty区别: 这个问题搁置,还不会. 将map数据拷贝到对象中, ...

  6. 2013年 ACM 有为杯 Problem I (DAG)

    有为杯  Problem I DAG  有向无环图 A direct acylic graph(DAG),is a directed graph with no directed cycles . T ...

  7. GridView边线Border设置

    1.黑色实线:(行列都有) <asp:GridViewID="GridView1"runat="server"CellPadding="3&qu ...

  8. NSURLConnect 的简单实用(iOS8淘汰)

    Demo_1 NSRULConnection NSRULConnection 苹果公司在ios8已经抛弃了,但是我还是要讲一下,因为这和后面的NSSession有着密切的联系 下面开始使用步骤: 1. ...

  9. android中ScrollView和GridView/ListView共存时,ScrollView不在顶部的解决方法

    listView.setFocusable(false); gridView.setFocusable(false); 这个必须在代码中写,xml文件中设置不起作用 原文:http://stackov ...

  10. Linux 安装g++

    g++ 它的全名不叫g++而是叫gcc-c++; 所以要安装它就可以用 yum install gcc-c++;