Spring企业课三,spring+jdbc实现dao层

8 篇文章 0 订阅
7 篇文章 0 订阅

下面,我们将使用spring,完成的实现jdbc的dao层。在这里,我们的connection将不使用connectionfactory获得,而是使用spring中的标签实现。通过标签获得datasource,在dao接口的实现类中注入这个依赖,再在实现类中通过datasource获得connection。我们完成整个需要一个bean,dao接口,dao实现类,xml配置文件。

bean:

public class Account {
	private int id;
	private String name;
	private String code;
	private double balance;
	private Date openDate;
	public Account() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public Account(String name, String code, double balance, Date openDate) {
		super();
		this.name = name;
		this.code = code;
		this.balance = balance;
		this.openDate = openDate;
	}

	public Account(int id, String name, String code, double balance, Date openDate) {
		super();
		this.id = id;
		this.name = name;
		this.code = code;
		this.balance = balance;
		this.openDate = openDate;
	}
	@Override
	public String toString() {
		return "Account [id=" + id + ", name=" + name + ", code=" + code + ", balance=" + balance + ", openDate="
				+ openDate + "]";
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public double getBalance() {
		return balance;
	}
	public void setBalance(double balance) {
		this.balance = balance;
	}
	public Date getOpenDate() {
		return openDate;
	}
	public void setOpenDate(Date openDate) {
		this.openDate = openDate;
	}
	
}

dao接口:

public interface IAccountDao {
	public void saveAccount(Account account);
	public List<Account> findAllAccount();

}

接口实现类:

public class AccountDaoImpl implements IAccountDao{
	private DataSource data;

	@Override
	public void saveAccount(Account account) {
		// TODO Auto-generated method stub
		Connection connection=null;
		PreparedStatement pst;
		try {
			connection=data.getConnection();
			System.out.println(connection);
			String sql="insert into tbl_account values(?,?,?,?,?)";
			pst=connection.prepareStatement(sql);
			pst.setInt(1, account.getId());
			pst.setString(2, account.getName());
			pst.setString(3, account.getCode());
			pst.setDouble(4, account.getBalance());
			pst.setDate(5, new Date(account.getOpenDate().getTime()));
			int i=pst.executeUpdate();
			if (i==1) {
				System.out.println("成功插入");
			}else {
				System.out.println("插入失败");
			}
			pst.close();
			connection.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}

	public DataSource getData() {
		return data;
	}

	public void setData(DataSource data) {
		this.data = data;
	}

	@Override
	public List<Account> findAllAccount() {
		List<Account> list=new ArrayList<Account>();
	try {
		Connection connection=data.getConnection();
		PreparedStatement pst=connection.prepareStatement("select * from tbl_account");
		ResultSet rst=pst.executeQuery();
		Account account=null;
		while (rst.next()) {
			account=new Account(rst.getInt(1),rst.getString(2),rst.getString(3),rst.getDouble(4),new java.util.Date(rst.getDate(5).getTime()));
			list.add(account);
		}
		
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		return list;
	}

}

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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  
   
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
     <property name="driverClassName" value="${driver}">
    
      
     </property>
     <property name="url" value="${url}"></property>
     <property name="username" value="${username}"></property>
     <property name="password" value="${password}"></property>
     
   
   </bean>
   <context:property-placeholder location="classpath:sql.properties"/>
   <bean id="accountDao" class="com.briup.db.jdbc.AccountDaoImpl">
   <property name="data" ref="dataSource"></property>
   </bean>
</beans>

到这里,我们的工作完成了。那么,让我们回顾一下。

dao接口实现类中有没有datasource,加粗的部分可以看到是有的。但是,我们并没有找到他在哪里被声明了。让我们看看xml文件,可以发现其中加粗部分声明了datasource,并在dao接口实现类的声明中被依赖了

  <property name="data" ref="dataSource"></property>
由此我们可以这么理解,datasource不需要我们使用java代码实现。我们在xml文件中声明出来,配置相应参数。同时将其注入进dao接口的实现类中。在实现类中声明datasource并写出其set方法。这样,我们在dao接口实现类中可以直接使用datasource获得connection。
其余的都是一些基础jdbc技术,没什么可讲的。

#特别注释:在测试时,不能因为xml文件中我们使用的是dao接口实现类,获取dao对象时使用实现类。这个做法是错的,我们应该获取的是其接口。解释如下:如果只是单纯注入是可以用实现类接收注入对象的,但是往往开发中会对实现类做增强,如事务,日志等,实现增强的AOP技术是通过动态代理实现的,而spring默认是JDK动态代理,对实现类对象做增强得到的增强类与实现类是兄弟关系,所以不能用实现类接收增强类对象,只能用接口接收。解释链接:点击打开链接(此为他人博客,如需删除请联系本人)。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值