• Spring:在dao中使用JdbcTemplate


    方法一:让dao继承JdbcDaoSupport

    JdbcDaoSupport 是 spring 框架为我们提供的一个类,该类中定义了一个 JdbcTemplate 对象,我们可以 直接获取使用,但是要想创建该对象,需要为其提供一个数据源

    具体源码如下:

    由上图可以知道,JdbcDaoSupport 可以在xml中进行注入

    持久化类的接口

    package com.itheima.dao;
    import com.itheima.domain.Account;
    /**
     * 账户的持久层接口
     */
    public interface IAccountDao {
        /**
         * 根据Id查询账户
         * @param accountId
         * @return
         */
        Account findAccountById(Integer accountId);
        /**
         * 根据名称查询账户
         * @param accountName
         * @return
         */
        Account findAccountByName(String accountName);
        /**
         * 更新账户
         * @param account
         */
        void updateAccount(Account account);
    }
    View Code

    继承并且实现接口

    package com.itheima.dao.imp;
    import com.itheima.dao.IAccountDao;
    import com.itheima.domain.Account;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.support.JdbcDaoSupport;
    
    import java.util.List;
    
    public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
    
        public Account findAccountById(Integer accountId) {
            List<Account> accounts=super.getJdbcTemplate()
                    .query("select *from account where id=?"
                            ,new BeanPropertyRowMapper<Account>(Account.class),accountId);
            return accounts.isEmpty()?null:accounts.get(0);
        }
    
        public Account findAccountByName(String accountName) {
            List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName);
            if(accounts.isEmpty()){
                return null;
            }
            if(accounts.size()>1){
                throw new RuntimeException("结果集不唯一");
            }
            return accounts.get(0);
        }
    
        public void updateAccount(Account account) {
            super.getJdbcTemplate().update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId());
        }
    }
    View Code

    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
            http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!-- 配置账户的持久层-->
        <bean id="accountDao" class="com.itheima.dao.imp.AccountDaoImpl">
            <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="root"></property>
        </bean>
    </beans>
    View Code

    如上图所示是一种xml的注入方式;

    第二种方式在dao中定义JdbcTemplate

    直接在持久化类中定义了一个JdbcTemplate在这个时候就可以直接通过xml的方式往持久化类中注入数据源

    一纸高中万里风,寒窗读破华堂空。 莫道长安花看尽,由来枝叶几相同?
  • 相关阅读:
    背景透明,文字不透明
    判断数组类型
    前端工作流程自动化——Grunt/Gulp 自动化
    tools安装
    总结
    CSS Hack
    getBoundingClientRect()兼容性处理
    Math.random获得随机数
    spring RestTemplate 工程导入
    系统架构演变
  • 原文地址:https://www.cnblogs.com/byczyz/p/14449042.html
Copyright © 2020-2023  润新知