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>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值