Spring_database_Template
配置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的更多相关文章
随机推荐
- WinSetupFromUSB – Install Windows XP from USB Flash Drive
http://myeeeguides.wordpress.com/2008/11/15/winsetupfromusb-install-windows-xp-from-usb-flash-drive/ ...
- CSV 客座文章系列:KGroup 通过 Windows Azure 将 Qoob 内容管理发布到云中
编辑人员注释: 今天这篇文章由 KGroup 首席软件架构师兼研发部主管 Jody Donetti 与 KGroup 技术总监 Simone Procopio 共同撰写,介绍了 KGroup 如何使用 ...
- svn 问题汇总
1.当删除了原来的仓库时,再次新建,更新版本时会出现这个问题:
- 在OC项目工程中混编Swift
1.创建一个OC项目工程,然后在Build Settings中找到如下字段,修改. 2.然后在项目中创建swift文件,如果系统提示是否需要创建桥接文件的时候,点击确定. 然后在Build Setti ...
- 《UML和模式应用》重点之思想篇
本书是帮助开发人员和学生学习面向对象分析和设计(OOA/D)的核心技能的重要工具. UML不是OOA/D.也不是方法,仅仅是图形表示法,假设没有真正掌握怎样创建优秀的面向对象设计,或者怎样评估和改进现 ...
- Android开发学习之TypedArray类
在学习Android的开发中,学习Gallery视图显示图片的过程中,在设置图片适配器的时候,用到了此TypedArray类型,这次根据android SDK,一块把此类型弄清楚! android.c ...
- printf("%d, %d\n", i++, ++i)的输出结果是确定的吗???
1. 问题描述 以下代码的输出结果是什么? 题目1: ; printf("%d, %d\n", i++, ++i); 题目2: ; printf("%d, %d, %d, ...
- node.js的一些知识
什么是node.js node.js是构建于chrome浏览器v8引擎上的一个js运行环境 可以解析和执行js代码 可以当做另一种上下文,脱离浏览器环境(后端)运行js代码,而代码解析就是基于V8引擎 ...
- JavaScript之arguments.callee
arguments.callee 在哪一个函数中运行,它就代表哪个函数. 一般用在匿名函数中. 在匿名函数中有时会需要自己调用自己,但是由于是匿名函数,没有名子,无名可调. 这时就可以用argumen ...
- 部署WSP出现错误—已在此服务器场中安装ID为XXXXX的功能
stsadm -o deploysolution -name ***.wsp -immediate -allowGacDeployment -url http://*** -force