三種方法配置數據源
1.需要引入jar包:spring-jdbc-4.3.2.RELEASE.jar
<!-- spring內置,springJdbc,配置數據源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> <property name="username" value="123"></property> <property name="password" value="123"></property> </bean>
2.需要引入2個jar包:(1).spring-framework-3.0.2.RELEASE-dependencies\org.apache.commons\com.springsource.org.apache.commons.dbcp\1.2.2.osgi\com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
(2).spring-framework-3.0.2.RELEASE-dependencies\org.apache.commons\com.springsource.org.apache.commons.pool\1.5.3\com.springsource.org.apache.commons.pool-1.5.3.jar
<!-- dbcp,配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> <property name="username" value="123"></property> <property name="password" value="123"></property> </bean>
3.c3p0配置數據源,並引用了一個在src目錄下自定義創建的一個.properties文件,並使用${}設值
!需要引入jar包:spring-framework-3.0.2.RELEASE-dependencies\com.mchange.c3p0\com.springsource.com.mchange.v2.c3p0\0.9.1.2\com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
<!-- c3p0,配置數據源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.name}"></property> <property name="password" value="${jdbc.password}"></property> </bean>
Spring連接數據庫
1.創建dao
public interface SelectAllEmpDao { public void selectAll(); }
2.創建實現類
public class SelectAllEmpImpl extends JdbcDaoSupport implements SelectAllEmpDao { @Override public void selectAll() { String sql = "select * from emp"; List<Map<String, Object>> list = this.getJdbcTemplate().queryForList( sql); List<Emp> empList = new ArrayList<Emp>(); for (Map<String, Object> map : list) { Emp e = new Emp(); e.setId(((BigDecimal) map.get("id")).intValueExact()); e.setName(map.get("name").toString()); e.setAge(((BigDecimal) map.get("age")).intValueExact()); empList.add(e); } } }
3.配置xml
(1)配置數據源
<!-- spring內置,springJdbc,配置數據源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property> <property name="username" value="bdqn"></property> <property name="password" value="bdqn"></property> </bean>
(2)配置jdbcTemplate,並引用dataSource
<!-- 配置template,並為其注入dataSource --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean>
(3).注冊業務bean,並引用jdbcTemplate
<!-- 查詢emp所有記錄數 --> <bean id="selectAllEmpDao" class="cn.cnsdhzzl.dao.impl.SelectAllEmpImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean>
(4)測試
@Test // Spring使用jdbc查詢表中所有記錄 public void selectAll() { ApplicationContext ac = new ClassPathXmlApplicationContext( "applicationContext.xml"); SelectAllEmpDao selectAllEmpDao = (SelectAllEmpDao) ac .getBean("selectAllEmpDao"); selectAllEmpDao.selectAll(); }
注:由於底層進行了判斷所以可以省略引用jdbcTemplate,直接引用dateSource。
