JAVA框架学习文章索引点这里
使用JdbcTemplate:
1,选用c3p0连接池作为数据源:
Dao层:
package com.i_c3p0;
import org.springframework.jdbc.core.JdbcTemplate;
public class PersonDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int getNum() {
String sql = "select count(id) from person";
return jdbcTemplate.queryForObject(sql, Integer.class);
}
}
配置文件:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 创建数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!-- 将数据源注入到模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="personDao" class="com.i_c3p0.PersonDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
</beans>
测试类:
package com.i_c3p0;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo {
@Test
public void test1() {
String xmlPath = "com/i_c3p0/beans.xml";
ApplicationContext appcont = new ClassPathXmlApplicationContext(xmlPath);
PersonDao personDao = (PersonDao) appcont.getBean("personDao");
System.out.println(personDao.getNum());
}
}
结果:
2
2,使用JdbcDaoSupport
package com.i_c3p0_autotemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class PersonDao extends JdbcDaoSupport{
public int getNum() {
String sql = "select count(id) from person";
return this.getJdbcTemplate().queryForObject(sql, Integer.class);
}
}
<!-- 创建数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<!--将数据源注册到模板-->
<bean id="personDao" class="com.i_c3p0_autotemplate.PersonDao">
<property name="dataSource" ref="dataSource"></property>
</bean>
测试类及结果同上
加载properties配置文件
properties配置文件
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/test
user=root
password=123456
xml配置文件
<!-- 加载文件,classpath代表src下 -->
<context:property-placeholder location="classpath:com/i_c3p0_properties/data"/>
<!-- 创建数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!--将数据源注册到模板-->
<bean id="personDao" class="com.i_c3p0_properties.PersonDao">
<property name="dataSource" ref="dataSource"></property>
</bean>