Spring 事务控制 —— JdbcTemplate在DAO中的使用

package com.fy.dao;

import com.fy.domain.Account;

/**
 * 账户持久层接口
 */
public interface AccountDao{
    /**
     * 根据Id的查询
     * @param accountId
     * @return
     */
    Account findAccountById(Integer accountId);

    /**
     * 根据name查找
     * @param accountName
     * @return
     */
    Account findAccountByName(String accountName);

    /**
     * 更改账户
     * @param account
     */
    void updateAccount(Account account);
}

package com.fy.dao.impl;

import com.fy.dao.AccountDao;
import com.fy.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 jdbcTemplate;

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

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

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

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

<?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.fy.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <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/account?serverTimezone=UTC"></property>
        <property name="username" value="root"></property>
        <property name="password" value="991206"></property>
    </bean>
</beans>
package com.fy.jdbctemplate;

import com.fy.dao.AccountDao;
import com.fy.domain.Account;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

/**
 * Jabctemplate得最基础用法
 */
public class JDBCTemplateDemo2 {
    public static void main(String[] args) {
        //1.获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("JDBCBean.xml");
        //2.获取对象
        AccountDao accountDao = ac.getBean("accountDao",AccountDao.class);
        //3.执行操作
        Account account = accountDao.findAccountById(2);
        System.out.println(account);

        account.setMoney(5555f);
        accountDao.updateAccount(account);

    }
}

发布了25 篇原创文章 · 获赞 70 · 访问量 3221

猜你喜欢

转载自blog.csdn.net/qq_44706044/article/details/104129466