Spring -- spring 中使用jdbc, c3p0连接池, dao概念
来源:互联网 发布:周琦比赛数据 编辑:程序博客网 时间:2024/05/23 11:52
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.Driverjdbc.url=jdbc:mysql://localhost:3306/springjdbc.username=rootjdbc.password=rootc3p0.pool.size.max=10c3p0.pool.size.min=2c3p0.pool.size.ini=3c3p0.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);}}
0 0
- Spring -- spring 中使用jdbc, c3p0连接池, dao概念
- spring中使用c3p0连接池
- Spring通过XML配置c3p0连接池和dao层注解注入使用 jdbcTemplate
- Spring C3P0 连接池
- spring中配置c3p0连接池
- spring中配置c3p0连接池
- Spring和C3P0数据连接池使用
- java c3p0连接池 不用spring单独使用c3p0
- Spring中使用JDBC,C3P0,DBCP的区别
- Spring配C3P0连接池
- Spring配C3P0连接池
- Spring c3p0连接池配置
- spring + c3p0 连接池配置
- spring配置c3p0连接池
- 在JDBC、Spring或者Hibernate中使用proxool连接池
- 在Red5中使用Spring-JDBC配置MySql连接池
- 在JDBC和Spring中使用proxool连接池
- Spring学习-24:Spring中的JDBC Template(JDBC模板):默认连接池、DBCP连接池、C3P0池的配置
- Android IOS SQLite 数据库 数据类型
- go语言web项目翻页实现
- eclipse,svn安装
- 【Objective-C】01-Objective-C概述
- getcwd
- Spring -- spring 中使用jdbc, c3p0连接池, dao概念
- Erlang环境搭建 for mac os
- 字符串倒序(面试题)
- Android Binder机制的Native应用
- CxImage
- 电子政务数据库安全保护的路径创建
- Servlet 单例多线程
- lua utf-8编码的汉字
- getClassLoader().getResourceAsStream()去哪里找资源文件