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的更多相关文章
随机推荐
- Android之CookieStore的持久化
CookieStore是一个对象,有的服务端 ,比如.net,保持登录状态不是用httpclient.addHeader(“cookie”,SessionId),而是用httppost.setCook ...
- Android 屏幕适配方式
适配:即当前应用在相同的手机上面显示相同的效果.适配前需要首先确定当前手机所属像素密度类型(如:xhdpi.hdpi.mdpi等) 像素密度:每英寸上分布的像素点个数,单位(dpi,ppi),利用勾股 ...
- [LeetCode][Python]18: 4Sum
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 18: 4Sumhttps://oj.leetcode.com/problem ...
- thanks使用注意事项;
router.get('/api/users/search/:key/:page', function(req, res) { if(_.isEmpty(req.params.key)) { res. ...
- js获取手机重力感应api
<html> <head> <title>DeviceOrientationEvent</title> <meta charset="U ...
- SSH整合方案2
[案例3]SSH整合_方案2 ** 案例描述 两个知识点的演示 其一,SSH整合的第二个方案 其二,Spring+JDBC+Struts2 参考代码 31) 使用工程spring4 32 ...
- Android中的数据存储
Android中的数据存储主要分为三种基本方法: 1.利用shared preferences存储一些轻量级的键值对数据. 2.传统文件系统. 3.利用SQLite的数据库管理系统. 对SharedP ...
- 关于Python网络爬虫实战笔记③
Python网络爬虫实战笔记③如何下载韩寒博客文章 Python网络爬虫实战笔记③如何下载韩寒博客文章 target:下载全部的文章 1. 博客列表页面规则 也就是, http://blog.sina ...
- 服务器是R710常见错误汇总:
报错: E1422 CPU 1 machine check error . power cycle AC 解决方案: 系统 BIOS 已报告机器检查错误.请断开系统的交流电源 10 秒,然后重新启动系 ...
- SMT贴片机抛料的成因和回流焊横向温差问题
SMT贴片机抛料的主要原因分析 在SMT生产过程中,怎么控制生产成本,提高生产效率,是企业老板及工程师们很关心的事情,而这些跟SMT贴片机的抛料率有很大的联系,以下就谈谈SMT贴片机的抛料问题. 所谓 ...