java框架SSM学习——JdbcTemplate使用

其实JdbcTemplate有点类似于我们之前使用的jdbcutils工具,在JdbcTemplate中,操作数据库的功能Spring都封装好了,我们只要利用IoC操作注入JdbcTemplate对象即可使用。先用一个小案例来演示用法。

账户实体类

package com.domain;

import java.io.Serializable;

//账户的实体类
public class Account implements Serializable {
    private Integer id;
    private String name;
    private Float money;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

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

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

IAccountDao接口类

package com.dao;

import com.domain.Account;

//账户的持久层接口
public interface IAccountDao {
    Account findAccountById(int id);

    Account findAccountByname(String name);

    void updateAccount(Account account);
}

IAccountDao实现类

package com.dao.impl;

import com.dao.IAccountDao;
import com.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

//账户的持久层实现类
public class AccountDaoImpl implements IAccountDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public Account findAccountById(int id) {
        List<Account> accountList =  jdbcTemplate.query("select * from account where id = ?" , new BeanPropertyRowMapper<Account>(Account.class) , id);
        return accountList.isEmpty()? null : accountList.get(0);
    }

    public Account findAccountByname(String name) {
        List<Account> accountList = jdbcTemplate.query("select * from account where name = ? " , new BeanPropertyRowMapper<Account>(Account.class) , name);
        if(accountList.isEmpty()){
            return null;
        }
        if(accountList.size() > 1){
            throw new RuntimeException("结果集不唯一");
        }
        return accountList.get(0);
    }

    public void updateAccount(Account account) {
        jdbcTemplate.update("update account set name = ? , money = ? where id = ?" , account.getName() , account.getMoney() , account.getId());
    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置AccountDao-->
    <bean id="accountDao" class="com.itheima.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://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value=""></property>
    </bean>
</beans>

测试类

package jdbctemplate;

import com.dao.IAccountDao;
import com.domain.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//JdbcTemplate的最基本用法
public class JdbcTemplateDemo4 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
        IAccountDao iAccountDao = applicationContext.getBean("accountDao" , IAccountDao.class);
        Account account = iAccountDao.findAccountById(1);
        System.out.println(account);
    }
}

测试结果

在这里插入图片描述

查看测试结果就可以看到,我们使用了JdbcTemplate工具去进行根据id查询账户。那么注意以下几点:

①增删改都可以使用jdbcTemplate中封装的update方法,因为增删改的操作,其中参数只需要要执行的SQL语句以及传入的参数。
②查询使用query方法,其中参数为:要执行的SQL语句、封装返回的结果集以及在SQL语句中需要传入的参数
③返回结果集可以使用JdbcTemplate封装的BeanPropertyRowMapper,泛型选择你要返回的对象类型,参数选择返回值类型对象的实体类字节码。也可以自己编写返回值对象结果集,如下所示:

//定义Account的封装策略
class AccountRoleMapper implements RowMapper<Account>{
    //把结果集中的数据封装到Account中,然后由spring把每个Account加到集合中
    public Account mapRow(ResultSet resultSet, int i) throws SQLException {
        Account account = new Account();
        account.setId(resultSet.getInt("id"));
        account.setName(resultSet.getString("name"));
        account.setMoney(resultSet.getFloat("money"));
        return account;
    }

这样将返回的结果集修改一下即可:

List<Account> accounts = jdbcTemplate.query("select * from account where money > ?" , new AccountRoleMapper(),"900f");
要了解为什么使用的这么简单,其中我们运用到了IoC的操作,将JdbcTemplate通过set方法注入到账户持久层实现类中,然后我们就能使用JdbcTemplate去操作数据库的相关操作了。JdbcTemplate将连接的具体步骤,以及操作数据库的步骤,甚至结果集的封装全部封装到了底层,而我们只需要告诉Spring,连接源是什么?具体的操作数据库的操作是什么,就可以实现对数据库的操作。其余的只需要交给Spring替我们完成就可以了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值