#JdbcTemplate、dao#在dao中使用Jdbc Template@FDDLC

本文展示了如何使用 Spring 的 JdbcTemplate 模块连接 MySQL 数据库并执行 CRUD 操作。通过配置 bean.xml 文件设置数据源和模板,然后在 AccountDaoImpl 类中实现具体的数据操作方法。

项目结构:

 

pom.xml的内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>P034_dao_JdbcTemplate</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.16</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <!--注意:spring-jdbc和spring-context有一部分依赖相同,易导致冲突,因此要让二者版本相同-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
    </dependencies>

</project>

 

MySQL数据库:

database:test        port:3306        table:account

 

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 class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test?characterEncoding=UTF8&amp;serverTimezone=Asia/Shanghai"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

    <bean class="org.springframework.jdbc.core.JdbcTemplate" id="template">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean class="cn.liuxingchang.dao.impl.AccountDaoImpl" id="accountDao">
        <property name="jdbcTemplate" ref="template"></property>
    </bean>
</beans>

 

Account.java的内容:

package cn.liuxingchang.dao;

import cn.liuxingchang.domain.Account;

public interface AccountDao {
    Account findAccountById(Integer id);
    Account findAccountByName(String name);
    void update(Account account);
}

 

AccountDao.java的内容:

package cn.liuxingchang.dao;

import cn.liuxingchang.domain.Account;

public interface AccountDao {
    Account findAccountById(Integer id);
    Account findAccountByName(String name);
    void update(Account account);
}

 

AccountDaoImpl.java的内容:

package cn.liuxingchang.dao.impl;

import cn.liuxingchang.dao.AccountDao;
import cn.liuxingchang.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class AccountDaoImpl implements AccountDao {
    private JdbcTemplate template;
    public void setJdbcTemplate(JdbcTemplate template) {
        this.template = template;
    }

    public Account findAccountById(Integer id) {
        List<Account> accounts = template.query("select * from account where id = ?",
                new BeanPropertyRowMapper<Account>(Account.class), id);
        if(accounts.isEmpty()) {
            return null;
        } else {
            return accounts.get(0);
        }
    }

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

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

 

Main.java的内容:

package cn.liuxingchang;

import cn.liuxingchang.dao.AccountDao;
import cn.liuxingchang.domain.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        AccountDao accountDao = context.getBean("accountDao", AccountDao.class);
        //System.out.println(accountDao.findAccountById(1));
        //System.out.println(accountDao.findAccountByName("Zhao"));
        Account account = accountDao.findAccountById(1);
        account.setMoney(111.0);
        accountDao.update(account);
    }
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值