Spring+jdbc使用示例

myeclipse10使用spring框架结合jdbc操作数据库

步骤:

1、引入必要的jar包,使用到了如下的jar包

spring.jar

aspectjrt.jar

aspectjweaver.jar

cglib-nodep-2.1.3.jar

common-annotations.jar

common-logging.jar

common-dbcp.jar

common-pool.jar

mysql-connector-java-5.1.13.jar

2、配置命名空间

<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:<span style="color:#ff0000;">tx="http://www.springframework.org/schema/tx</span>"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
          <span style="color:#ff0000;"> http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd</span>">

3、配置数据源

使用的是从properties读取属性,下面蓝色部分是引入属性文件必要的代码,其中

jdbc.properties是属性文件的名称。

<span style="color:#000066;"><context:property-placeholder location="classpath:jdbc.properties"/></span><!-- 属性占位,提取配置文件中的值 -->   
		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		    <property name="driverClassName" value="${driverClassName}"/>
		    <property name="url" value="${url}"/>
		    <property name="username" value="${username}"/>
		    <property name="password" value="${password}"/>
		     <!-- 连接池启动时的初始值 -->
			 <property name="initialSize" value="${initialSize}"/>
			 <!-- 连接池的最大值 -->
			 <property name="maxActive" value="${maxActive}"/>
			 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
			 <property name="maxIdle" value="${maxIdle}"/>
			 <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
			 <property name="minIdle" value="${minIdle}"/>
	    </bean>

下面是主要的文件的源代码:

beans.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/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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:property-placeholder location="classpath:jdbc.properties"/><!-- 属性占位,提取配置文件中的值 -->   
		<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		    <property name="driverClassName" value="${driverClassName}"/>
		    <property name="url" value="${url}"/>
		    <property name="username" value="${username}"/>
		    <property name="password" value="${password}"/>
		     <!-- 连接池启动时的初始值 -->
			 <property name="initialSize" value="${initialSize}"/>
			 <!-- 连接池的最大值 -->
			 <property name="maxActive" value="${maxActive}"/>
			 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
			 <property name="maxIdle" value="${maxIdle}"/>
			 <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
			 <property name="minIdle" value="${minIdle}"/>
	    </bean>
	     <!-- 配置事务管理器 -->
	     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	     	<property name="dataSource" ref="dataSource"></property>
	     </bean>
	     <!-- 采用@Transactional注解方式使用事务 -->
	     <tx:annotation-driven transaction-manager="txManager"/>
	     
	     <bean id="personService" class="com.gdhdcy.service.impl.PersonServiceBean">
	     	<property name="dataSource" ref="dataSource"></property>
	     </bean>
	     
</beans>

jdbc.properties文件

driverClassName=org.gjt.mm.mysql.Driver
url=jdbc\:mysql\://localhost\:3306/person?useUnicode\=true&characterEncoding\=UTF-8
username=root
password=root
initialSize=1
maxActive=500
maxIdle=2
minIdle=1

数据库实现增删查改的java文件

package com.gdhdcy.service.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.gdhdcy.bean.Person;
import com.gdhdcy.service.PersonService;

//声明事务,这样就会自动的打开事务和关闭事务
@Transactional
public class PersonServiceBean implements PersonService {
	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public void delete(Integer personid) {
		jdbcTemplate.update("delete from person where id=?", new Object[]{personid},
				new int[]{java.sql.Types.INTEGER});
	}

	public Person getPerson(Integer personid) {		
		return (Person)jdbcTemplate.queryForObject("select * from person where id=?", new Object[]{personid}, 
				new int[]{java.sql.Types.INTEGER}, new PersonRowMapper());
	}

	@SuppressWarnings("unchecked")
	public List<Person> getPersons() {
		return (List<Person>)jdbcTemplate.query("select * from person", new PersonRowMapper());
	}

	public void save(Person person) {
		
		jdbcTemplate.update("insert into person(name) values(?)", new Object[]{person.getName()},
				new int[]{java.sql.Types.VARCHAR});
		System.out.println("保存成功");
	}

	public void update(Person person) {
		jdbcTemplate.update("update person set name=? where id=?", new Object[]{person.getName(), person.getId()},
				new int[]{java.sql.Types.VARCHAR, java.sql.Types.INTEGER});
	}
}

下面是完整源代码下载链接

http://pan.baidu.com/s/1ntG6T3z

下载文件直接导入到您的myeclipse,然后建立mysql的数据库(数据库文件也在代码文件夹中)。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Happy编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值