Spring配置DataSource十分简单,同时也是非常常用,这里我们使用mysql数据库,在这里,我们配置数据库的驱动、url、用户名和密码信息,当然,配置项还有很多,这里我就不一一列举了,先附上beans.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"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.castiel"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
<property name="username" value="root"></property>
<property name="password" value="castiel"></property>
</bean>
</beans>
既然我们要使用mysql的数据库驱动,那么我们肯定要导入下面这几个包,这里我列举一下,顺便讲讲这几个包的作用:
mysql-connector-java.jar:MySQL官方退出的针对java的驱动包,用于连接MySQL数据库;
commons-dbcp:dbcp即DataBase Connection Pool(数据库连接池),是java数据库连接池的一种,通过数据库连接池,可以让程序自动管理数据库连接的释放和断开;
commons-pool:这个包是一个对象池组件, commons-pool实现思想非常简单,它主要的作用就是将"对象集合"池化,任何通过pool进行对象存取的操作,都会严格按照"pool配置"(比如池的大小)实时的创建对象/阻塞控制/销毁对象等.它在一定程度上,实现了对象集合的管理以及对象的分发;
commons-logging:顾名思义,这个包是用来实现程序运行时日志的记录;
接下来,我们在'UserDAOImpl'类中使用datasource,将对象持久化到数据库,贴上代码:
package com.castiel.dao.impl;
import java.sql.Connection;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.stereotype.Repository;
import com.castiel.dao.UserDAO;
import com.castiel.model.User;
@Repository("userDAO")
public class UserDAOImpl implements UserDAO {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
@Resource(name="dataSource")
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void save(User u) {
// TODO Auto-generated method stub
try {
Connection conn = dataSource.getConnection();
conn.createStatement().executeUpdate("insert into user values (null,'"+u.getUsername()+"','"+u.getPassword()+"')");
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("user saved!");
}
}
贴上测试类代码,测试能否持久化到数据库:
package com.castiel.service;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.castiel.model.User;
import com.castiel.service.UserService;
public class UserServiceTest {
/**测试类生成方式**/
/**
* 1.选中src下com.castiel.service包中的UserService
* 2.右键new jUnit Test Case
* 3.点击NEXT
* 4.选择需要测试的方法
* 5.点击Finish
* 6.把生成出来的*Test.java文件拖到test下com.castiel.service包中
* @throws Exception
*/
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
System.out.println("******************************");
UserService userService = (UserService)ctx.getBean("userService");
System.out.println(userService.getClass());
User user = new User();
user.setUsername("zhangsan");
user.setPassword("123456");
userService.add(user);
/**
* 在web框架中,一般自动管理生命周期,自动调用destroy方法,这里我们模拟一下
* 注意:ApplicationContext中没有destroy方法,所以我们要调用ApplicationContext的实现类
* ClassPathXmlApplicationContext中的destroy方法
*/
/**
* 如果我们设置bean的scope为'prototype',是不会执行destroy方法,spring无法自动管理多例的整个生命周期
*
* bean的构造函数执行完毕才会执行init方法
*/
ctx.destroy();
}
}
运行结果截图及数据库截图: