JdbcTemplate在Dao中的使用以及No DataSource specified问题的出现和解决。

最近在学习Spring框架,学到JdbcTemplate在Dao中的使用时,出现了一个问题:Exception in thread "main" java.lang.IllegalArgumentException: No DataSource specified

现在贴出代码(JdbcTemplateDemo4是一个测试类):

package com.dahua.dao;

import java.util.List;

import com.dahua.domain.Account;

/**
 * @Version:1.0
 * @Description:账户的持久层接口
 * @author:秦雁回
 * @Date:2020年6月21日下午5:37:59
 */
public interface IAccountDao {
	
	/**
	 * @Description:根据id查询账户信息
	 * @param id
	 * @return
	 */
	Account findAccountById(Integer id);
	
	/**
	 * @Description:根据名称查询账户信息
	 * 	要求:账户名称必须唯一,如果不唯一,应该抛异常
	 * @param name
	 * @return
	 */
	Account findAccountByName(String name);
	
	/**
	 * @Description:保存账户
	 * @param account
	 */
	void saveAccount(Account account);
	
	/**
	 * @Description:更新账户信息
	 * @param account
	 */
	void updateAccount(Account account);
	
	/**
	 * @Description:删除账户
	 * @param account
	 */
	void deleteAccount(Account account);
	
	/**
	 * @Description:查询所有账户
	 * @return
	 */
	List<Account> findAllAccount();

}
package com.dahua.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.dahua.dao.IAccountDao;
import com.dahua.domain.Account;

/**
 * @Version:1.0
 * @Description:账户的持久层实现类
 * @author:秦雁回
 * @Date:2020年6月21日下午5:44:37
 */
public class AccountDaoImpl implements IAccountDao {
	
	private JdbcTemplate jdbcTemplate;

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

	@Override
	public Acco
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值