Spring系列学习 --- DAO学习

1、DAO 处理数据的 ---   Data Access Object

The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate, JPA or JDO in a consistent way. This allows one to switch between the aforementioned persistence technologies fairly easily and it also allows one to code without worrying about catching exceptions that are specific to each technology.

2、学习 JDBC 模板      要设置 xmlns : context

<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"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

3、配置数据源 dbcp

     

4、使用 DAO来进行处理 mysql需要使用依赖3个jar包,使用的是jdbcTemplate来处理mysql数据

DAO相关的包:

         commons-dbcp-1.4.jar

         commons-pool.jar

mysql连接用的驱动之类的jar包

        mysql-connector-java-8.0.12.jar

5、DAO 实现对应的bean.xml 文件 , for example:

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <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>
    <context:property-placeholder location="jdbc.properties"/>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"> </property>
        <!--这个类实现的是,可以调用里面的一些方法来实现 对数据库的一些读写插入删除等操作-->
    </bean>


    <bean id="studentdaoimpl" class="com.java1234.dao.StudentDAOImpl">
        <property name="jdbctemplate" ref="jdbcTemplate"> </property>
        <!--studentdaoimpl 的方法 jdbctemplate有 setter方法,才可以这样注入-->
    </bean>

</beans>

猜你喜欢

转载自blog.csdn.net/hwang4_12/article/details/82951050