三种方法配置数据源
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-dependenciesorg.apache.commonscom.springsource.org.apache.commons.dbcp1.2.2.osgicom.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
(2).spring-framework-3.0.2.RELEASE-dependenciesorg.apache.commonscom.springsource.org.apache.commons.pool1.5.3com.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-dependenciescom.mchange.c3p0com.springsource.com.mchange.v2.c3p0 .9.1.2com.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。