Kite的学习历程之JdbcTemplate

Kite学习框架的第十一天

1. Spring中的 JdbcTemplate

1.1 JdbcTemplate 概述

它是 spring 框架中提供的一个对象,是对原始 Jdbc API 对象的简单封装。spring 框架为我们提供了很多 的操作模板类。
操作关系型数据的: JdbcTemplate HibernateTemplate
操作 nosql 数据库的: RedisTemplate
操作消息队列的: JmsTemplate
我们今天的主角在 spring-jdbc-5.0.2.RELEASE.jar 中(注意如果是5.0.3则不行,我一开始导包就是5.0.3),我们在导包的时候,除了要导入这个 jar 包 外,还需要导入一个 spring-tx-5.0.2.RELEASE.jar(它是和事务相关的)。

1.2 实现JdbcTemplate的数据库操作

我是对我数据库中的account 表进行操作,

1.2.1 首先创建Account 的实体类
package cn.kitey.domain;

public class Account {

    private Integer id;
    private Integer uid;
    private double money;

    public Account() {
    }

    public Account(Integer id, Integer uid, double money) {
        this.id = id;
        this.uid = uid;
        this.money = money;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", uid=" + uid +
                ", money=" + money +
                '}';
    }
}


1.2.2 创建account 的接口,和接口的实现类

创建接口

package cn.kitey.dao;

import cn.kitey.domain.Account;

import java.util.List;

/**
 * 账户的持久层接口
 */
public interface AccountDao {

    List<Account> findAccountById(Integer id);

    void InsertAccount(Account account);

    List<Account> findAll();
}


创建接口实现类


package cn.kitey.dao.impl;

import cn.kitey.dao.AccountDao;
import cn.kitey.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {



    public List<Account> findAccountById(Integer id) {
        //从父类获取方法
        return  super.getJdbcTemplate().query("select * from account where id =?" ,
                new BeanPropertyRowMapper<Account>(Account.class),id);
    }

    public void InsertAccount(Account account) {
        getJdbcTemplate().update("insert into account(id, uid, money) values (?,?,?)",account.getId(),account.getUid(),account.getMoney());
    }

    public List<Account> findAll() {
        return getJdbcTemplate().query("select * from account" ,
                new BeanPropertyRowMapper<Account>(Account.class));
    }
}

这里我出去了daoimpl中以后会重复使用的代码,则jdbcTemplate 对象的创建,将它独自创建了一个类,让接口的实现类进行继承,然后调用父类方法,获取jdbctemplate对象

package cn.kitey.dao.impl;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 这个用于抽取dao代码中的重复代码块
 */
public class JdbcSupport {
    private JdbcTemplate jdbcTemplate;


    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    //使用spring数据注入的方式
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}


1.2.3 在bean.xml中配配置accountDao,以及注入jdbcTemplate对象数据
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--配置持久层-->
    <bean id = "accountDao" class="cn.kitey.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <!--配置jdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>

    </bean>

    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql:///eesy?characterEncoding=utf8"></property>
        <property name="username" value="root"></property>
        <property name="password" value="25002500"></property>
    </bean>
</beans>

1.2.4 创建测试类进行测试

在test中创建测试类进行测试,我这里只测试了查询所用的方法,

package cn.kitey.test;

import cn.kitey.dao.impl.AccountDaoImpl;
import cn.kitey.domain.Account;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Demo1Test {

    /**
     * 测试dao层查询数据所用
     */
    @Test
    public void testFindAll(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        AccountDaoImpl accountDao1 = context.getBean("accountDao", AccountDaoImpl.class);

        List<Account> all = accountDao1.findAll();
        for (Account account : all) {
            System.out.println(account);
        }

    }
}


运行结果截图:
在这里插入图片描述

上面解释基于xml的方式的JdbcTemplate的使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值