Spring-Aop的学习总结——3——spring与操作jdbc

每个框架肯定都得对数据库进行操作,来进行数据的操作:
首先我们先来自己写一个自己的模板:
首先思考:与数据库操作肯定需要一个DataSource,需要我们的数据库配置信息。需要sql语句。
数据库的配置信息我们可以在xml文件中配置,sql语句可以在curd方法中编写,那这个数据库连接池在哪设置呢,就是在我们的模板中创建。
看一下模板的代码:

public class MyTemplate {
    private DataSource dataSource;
    public MyTemplate() {
    }
    public MyTemplate(DataSource dataSource) {
        this.dataSource = dataSource;
    }
    public DataSource getDataSource() {
        return dataSource;
    }
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }


    public void insert(String sql) {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = this.getDataSource().getConnection();
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                stmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
}

创建PersonDao类:

public class PersonDao  extends MyTemplate{
    public void savePerson() {
        String sql  = "insert into person(name,description) values('yss','帅')";
        this.insert(sql);
    }
}

这里要实现我们的模板类:从而可以调用我们的inert方法。
applicationContext.xml:

<bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>
    <bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
        <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 = "personDao" class="com.my.spring.myjdbc.PersonDao">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>

首先我们引入我们配置的我们自己的数据库连接的信息,
然后创建我们的DataSource的实例, class为

org.apache.commons.dbcp.BasicDataSource

并为里面的属性通过jdbc的配置文件jdbc.properties赋值
接下来,为PersonDao创建对象,由于继承了我们的模板类,所以拥有dataSource属性,我们给他赋值。

测试类:

public class PersonDaoText {
    public void textPerson() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        PersonDao personDao = (PersonDao) applicationContext.getBean("personDao");
        personDao.savePerson();
    }

    public static void main(String[] args) {
        PersonDaoText text = new PersonDaoText();
        text.textPerson();
    }
}

以上使我们自己写的spring操作jdbc的一个小模板,下面我们来看一下spring内部的是如何实现与jdbc实现联系的。
spring内部和jdbc联系的主要有三个接口,包括JdbcDaoSupprot、JdbcAccessor和JdbcTemplate。
下面我们来说明一下这三个类之间的关系。
首先我们要明确,执行数据库的操作的是JdbcTemplate。而我们需要做的就是创建出jdbcTemplate;
那么如何创建出JdbcTemplate呢?
首先我们先来看最简单的JdbcTemplate接口:
这个接口里面的构造方法包括了DataSource这个参数,也就是说我们要通过DataSouce来创建出dataSource。
这里写图片描述
另一种方法是getJdbcSupport注入dataSource或者jdbcTemplate.
这里写图片描述
下面我们总结一下:
1、执行数据的操作的是JdbcTemplate
2、最根本的步骤就是要把dataSource注入到JdbcTemplate
3、通过给JdbcTemplate注入dataSource
(1)、采用构造器的形式注入
(2)、采用setter方法进行注入
4、可以给JdbcDaoSupport注入dataSource
5、可以给JdbcDaoSupport注入JdbcTemplate

猜你喜欢

转载自blog.csdn.net/qq_39411208/article/details/81666353