1. 示例代码
CustomerDao.java ,dao接口
public interface CustomerDao { public void insertCustomer(Customer c); public void updateCustomer(Customer c); public List<Customer> findCustomerByName(String name); }
CustomerDaoImpl.java 接口实现
/** * CustomerDaoImpl */ public class CustomerDaoImpl implements CustomerDao { private JdbcTemplate jt ; public void setJt(JdbcTemplate jt) { this.jt = jt; } public List<Customer> findCustomerByName(String name) { String sql="select id,name,age from customers where name = ?"; return jt.query(sql, new Object[]{name}, new RowMapper(){ public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Customer c = new Customer(); c.setId(rs.getInt("id")); c.setName(rs.getString("name")); c.setAge(rs.getInt("age")); return c; }}); } /** * 插入 */ public void insertCustomer(Customer c) { String sql = "insert into customers(name,age) values(?,?)"; jt.update(sql, new Object[]{c.getName(),c.getAge()}); } public void updateCustomer(Customer c) { String sql = "update customers set name = ?,age = ? where id = ?"; jt.update(sql,new Object[]{c.getName(),c.getAge(),c.getId()}); } }
jdbc.properties 分散配置
jdbc.driverclass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring jdbc.username=root jdbc.password=root c3p0.pool.size.max=10 c3p0.pool.size.min=2 c3p0.pool.size.ini=3 c3p0.pool.size.increment=2
dao.xml 配置文件
<?xml version="1.0"?> <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" 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 "> <!-- 指定分散配置的文件的位置 --> <context:property-placeholder location="classpath:cn/itcast/spring/dao/jdbc.properties"/> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverclass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="maxPoolSize" value="${c3p0.pool.size.max}" /> <property name="minPoolSize" value="${c3p0.pool.size.min}" /> <property name="initialPoolSize" value="${c3p0.pool.size.ini}" /> <property name="acquireIncrement" value="${c3p0.pool.size.increment}" /> </bean> <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> <!-- customerDao --> <bean id="customerDao" class="cn.itcast.spring.dao.CustomerDaoImpl"> <property name="jt" ref="jt" /> </bean> <!-- *************************** daoSupport **************************** --> <bean id="customerDaoSuport" class="cn.itcast.spring.dao.CustomerDaoSuportImpl"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
App.java 测试代码
public class App { public static void main(String[] args) throws SQLException { ApplicationContext ac = new ClassPathXmlApplicationContext( "cn/itcast/spring/dao/dao.xml"); CustomerDao dao = (CustomerDao) ac.getBean("customerDao"); Customer c = new Customer(); c.setName("tom"); c.setAge(23); dao.insertCustomer(c); // c = new Customer(); c.setId(1); c.setName("jerry"); c.setAge(23); dao.updateCustomer(c); dao.findCustomerByName("tom"); } }
CustomerDaoSuportImpl.java 另外一种配置方法: 省略模板的配置
/** * CustomerDaoImpl,省略模板的配置 */ public class CustomerDaoSuportImpl extends JdbcDaoSupport implements CustomerDao { public List<Customer> findCustomerByName(String name) { String sql="select id,name,age from customers where name = ?"; return getJdbcTemplate().query(sql, new Object[]{name}, new RowMapper(){ public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Customer c = new Customer(); c.setId(rs.getInt("id")); c.setName(rs.getString("name")); c.setAge(rs.getInt("age")); return c; }}); } /** * 插入 */ public void insertCustomer(Customer c) { String sql = "insert into customers(name,age) values(?,?)"; getJdbcTemplate().update(sql, new Object[]{c.getName(),c.getAge()}); } public void updateCustomer(Customer c) { String sql = "update customers set name = ?,age = ? where id = ?"; getJdbcTemplate().update(sql,new Object[]{c.getName(),c.getAge(),c.getId()}); } }
AppDaoSupport.java 省略模板配置 测试代码
public class AppDaoSupport { public static void main(String[] args) throws SQLException { ApplicationContext ac = new ClassPathXmlApplicationContext( "cn/itcast/spring/dao/dao.xml"); CustomerDao dao = (CustomerDao) ac.getBean("customerDaoSuport"); Customer c = new Customer(); c.setName("tom"); c.setAge(23); dao.insertCustomer(c); } }