Spring - JdbcTemplate(二) 在dao层的应用

一、概述

JdbcTemplate用在Dao也是有两种方法

  1. 第一种:使用xml配置文件
  2. 第二种:使用注解

案例的文件结构
在这里插入图片描述
需要的依赖

  <dependencies>
<!--        spring的依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
<!--        支持jdbc的依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
<!--        支持事务的依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
<!--        连接mysql的依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
    </dependencies>

基础代码
IAccountDao

public interface IAccountDao {
    /**
     * 根据id查询用户
     */
    Account findAccountById(Integer accountId);
    /**
     * 根据名称
     */
    Account findAccountByName(String name);
    /**
     * 更新
     */
    void updateAccount(Account account);

}

Account

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 +
                '}';
    }
}

第一种: 使用xml配置文件

配置文件bean.xml,记得将url 密码 用户名改为自己的哦

<?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.impl.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/*******"></property>
        <property name="username" value="****"></property>
        <property name="password" value="****"></property>
    </bean>
</beans>

AccountDaoImpl

public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
    /**
     * 根据id查询用户
     */
    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 name){
        List<Account> accounts = super.getJdbcTemplate().query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),name);
        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());
    }
}

知识点:
这里我们AccountDaoImpl中并没有以下代码

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

而且在bean中我们也没有注入JdbcTemplate。这是因为我们在持久层实现类中我们继承了一个父类JdbcDaoSupport。
继承该类的原因:
当我们开发中出现多个dao的文件时,每个*****DaoImpl文件中都会有以上的代码,这就会造成代码的重复。因此spring框架为我们编写了JdbcDaoSupport来解决这个问题。减少了代码的重复出现。

第二种:使用注解

在bean.xml中引入约束,加入要扫描的包,并且把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"
       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">
    <!--配置要扫描的包-->                        
    <context:component-scan base-package="com.itheima.dao"></context:component-scan>
    <!--配置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/*****"></property>
        <property name="username" value="***"></property>
        <property name="password" value="****"></property>
    </bean>
</beans>

AccountdaoImpl

import java.util.List;
@Repository("accountDao")
public class AccountDaoImpl2 implements IAccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    /**
     * 根据id查询用户
     */
    public Account findAccountById(Integer accountId){
        List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId);
        return accounts.isEmpty()?null:accounts.get(0);
    }
    /**
     * 根据名称
     */
    public Account findAccountByName(String name){
        List<Account> accounts =jdbcTemplate.query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),name);
        if(accounts.isEmpty()){
            return null;
        }
        if (accounts.size()>1){
            throw new RuntimeException("结果不唯一");
        }
        return accounts.get(0);
    }
    /**
     * 更新
     */
    public void updateAccount(Account account){
        jdbcTemplate.update("update account set name=?,money=? where id =?",account.getName(),account.getMoney(),account.getId());
    }
}

两者的区别

使用注解的话可以根据自己的需要更改jdcbTemplate,
但是使用的xml因为是继承了父类所以不能更改jdbcTemplate他是只读属性的

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值