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的數據庫(數據庫文件也在代碼文件夾中)。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章