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的更多相关文章
随机推荐
- 在VC6.0下如何调用Delphi5.0开发的进程内COM
因为本人的语言水平很差,考大学时150的总分,我考了个60分.外语也是,初中及格过一次,会考及格过一次.其它的时间好像从没有及格过.所以我不写文章,因我一百字的文章给我写,至少要出八九个错别字.哈哈… ...
- POJ 2686 Traveling by Stagecoach 壮压DP
大意是有一个人从某个城市要到另一个城市(点数<=30) 然后有n个马车票,相邻的两个城市走的话要消耗掉一个马车票. 花费的时间呢,是马车票上有个速率值,用边/速率就是花的时间. 问最后这个人花费 ...
- 【每天一个Linux命令】12. Linux中which命令的用法
which 用来查看可执行文件的位置. 1.命令格式: which 可执行文件名称 2.命令功能: which指令会在PATH变量指定的路径中,搜索某个系统命令的位置,并且返回第一个搜索结果. 3. ...
- oracle初始安装大小
oracle初始安装大小 /ruiy/ocr/DBSoftware/app/oracle/ruiy/ocr/DBSoftware/app/oraInventory/ruiy/ocr/DBData/or ...
- linux环境之监听端口配置
export JAVA_OPTS="-Dcom.sun.management.jmxremote.port=18950 -Dcom.sun.management.jmxremote.auth ...
- Android getReadableDatabase() 和 getWritableDatabase()
Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例.(getReadableDa ...
- 响应式(css_media)
开始研究响应式web设计,CSS3 Media Queries是入门. Media Queries,其作用就是允许添加表达式用以确定媒体的环境情况,以此来应用不同的样式表.换句话说,其允许我们在不改变 ...
- CSS用法简介
CSS(Cascading Style Sheets层叠样式表)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言. 1.基本使用语法 ...
- 创建Java项目报错处理
好久没用Eclipse编写Java程序了,今天创建一个Java项目的时候,老报错,错误信息如下: Implicit super constructor Object() is undefined fo ...
- Xposed出现 java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
Xposed出现 java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implem ...