直接上主要代码:
DataSource:数据库连接主要有三种方式:1) c3p0 2)dbcp 3)proxool
配置:
<?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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <context:annotation-config /> <context:component-scan base-package="com.bjsxt"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/spring" /> <property name="username" value="root" /> <property name="password" value="123" /> </bean> </beans>
实现:
package com.bjsxt.dao.impl; import java.sql.Connection; import java.sql.SQLException; import javax.annotation.Resource; import javax.sql.DataSource; import org.springframework.stereotype.Component; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; @Component("u") public class UserDAOImpl implements UserDAO { private DataSource dataSource; public DataSource getDataSource() { return dataSource; } @Resource public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void save(User user) { //Hibernate //JDBC //XML //NetWork try { Connection conn=dataSource.getConnection(); conn.createStatement().executeUpdate("insert into user values(null,'zhangsan')"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("user saved!"); } }
测试:
@Test public void testAdd() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService"); service.add(new User()); ctx.destroy(); }
第二种配置方式:
将数据源的配置直接配置在bean.xml中,然后写一个属性文件.properties
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:jdbc.properties</value> </property> </bean> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
属性文件:jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring jdbc.username=root jdbc.password=123
实现类:
package com.bjsxt.dao.impl; import java.sql.Connection; import java.sql.SQLException; import javax.annotation.Resource; import javax.sql.DataSource; import org.springframework.stereotype.Component; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; @Component("u") public class UserDAOImpl implements UserDAO { private DataSource dataSource; public DataSource getDataSource() { return dataSource; } @Resource public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void save(User user) { //Hibernate //JDBC //XML //NetWork try { Connection conn=dataSource.getConnection(); conn.createStatement().executeUpdate("insert into user values(null,'zcaijioj')"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("user saved!"); } }
测试类一样:
package com.bjsxt.service; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.model.User; //Dependency Injection //Inverse of Control public class UserServiceTest { @Test public void testAdd() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService"); service.add(new User()); ctx.destroy(); } }