日期:2014-05-16  浏览次数:20591 次

Java 学习笔记07:使用Spring的Dao连接数据库
  • 关于DAO
    DAO组件是整个java应用的吃就成访问的重要组件,Dao模式的主要内容为:应用中所有对数据库的访问都通过Dao组件完成,Dao组件封装了对数据库的增、删、改、查等操作。
  • 具体实现
  1. 创建一个新的项目地址,名称为Spring
  2. 既然是数据库操作,肯定要有数据库配置,创建一个jdbc.properties的文件,放置在src/目录下,里面包含数据库连接的重要信息。

    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url = jdbc:mysql://127.0.0.1:3306/test
    jdbc.username = root
    jdbc.password = root


  3. 创建一个bean.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.0.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:annotation-config/>
        <aop:aspectj-autoproxy/>
        
        <bean id="jdbc" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        	<property name="locations" value="jdbc.properties"/>    
        </bean>
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">        
         <property name="driverClassName" value="${jdbc.driverClassName}" />       
         <property name="url" value="${jdbc.url}" />       
         <property name="username" value="${jdbc.username}" />       
         <property name="password" value="${jdbc.password}" />       
    	</bean> 
    	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
    	
    	<bean id="UserImpl" class="com.spring.test.dao.UserImpl" ></bean>
    	<bean id="User" class="com.spring.test.service.User" ></bean>
    	<bean id="UserService" class="com.spring.test.service.UserService" ></bean>
    </beans>

    这个xml中需要在beans头部添加:http://www.springframework.org/schema/tx 地址以及,http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-2.5.xsd。
    <bean id="jdbc" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
        	<property name="locations" value="jdbc.properties"/>    
        </bean>
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">        
         <property name="driverClassName" value="${jdbc.driverClassName}" />       
         <property name="url" value="${jdbc.url}" />       
         <property name="username" value="${jdbc.username}" />       
         <property name="password" value="${jdbc.password}" />       
    	</bean>