当前位置:   article > 正文

spring学习整合C3p0小案例_private queryrunner runner;

private queryrunner runner;

目录

准备代码

 C3p0 xml配置文件

获取连接池的工具类 

使用junit测试

整合spring部分代码更改:

自己写的类代码块的依赖

注意不能忽略调用包类的依赖(这部分需要看包的源代码)


准备代码

业务层机持久层的分离并且整合C3p0,

导入jar包:

bean类

cn.pro.domain.CustormerBean

  1. package cn.pro.domain;
  2. import java.util.Date;
  3. public class CustormerBean {
  4. private int custormer_id;
  5. private String custormer_name;
  6. private String custormer_add;
  7. private Date custormer_birth;
  8. private int custormer_tel;
  9. private String custormer_sex;
  10. public int getCustormer_id() {
  11. return custormer_id;
  12. }
  13. @Override
  14. public String toString() {
  15. return "CustormerBean [custormer_id=" + custormer_id
  16. + ", custormer_name=" + custormer_name + ", custormer_add="
  17. + custormer_add + ", custormer_birth=" + custormer_birth
  18. + ", custormer_tel=" + custormer_tel + ", custormer_sex="
  19. + custormer_sex + "]";
  20. }
  21. public void setCustormer_id(int custormer_id) {
  22. this.custormer_id = custormer_id;
  23. }
  24. public String getCustormer_name() {
  25. return custormer_name;
  26. }
  27. public void setCustormer_name(String custormer_name) {
  28. this.custormer_name = custormer_name;
  29. }
  30. public String getCustormer_add() {
  31. return custormer_add;
  32. }
  33. public void setCustormer_add(String custormer_add) {
  34. this.custormer_add = custormer_add;
  35. }
  36. public Date getCustormer_birth() {
  37. return custormer_birth;
  38. }
  39. public void setCustormer_birth(Date custormer_birth) {
  40. this.custormer_birth = custormer_birth;
  41. }
  42. public int getCustormer_tel() {
  43. return custormer_tel;
  44. }
  45. public void setCustormer_tel(int custormer_tel) {
  46. this.custormer_tel = custormer_tel;
  47. }
  48. public String getCustormer_sex() {
  49. return custormer_sex;
  50. }
  51. public void setCustormer_sex(String custormer_sex) {
  52. this.custormer_sex = custormer_sex;
  53. }
  54. }

cn.pro.service.ICustormerService

  1. package cn.pro.service;
  2. import java.util.List;
  3. import cn.pro.domain.CustormerBean;
  4. public interface ICustormerService {
  5. void addcustormer(CustormerBean custormer);
  6. void deletecustormer(int custormer_id);
  7. public CustormerBean getcustormer(String custormer_name);
  8. void updatecustormer(CustormerBean custormer);
  9. public List<CustormerBean> getall();
  10. }

cn.pro.service.impl.ICustormerServiceImpl

  1. package cn.pro.service.impl;
  2. import java.util.List;
  3. import cn.pro.dao.CustormerDao;
  4. import cn.pro.dao.CustormerDaoImpl;
  5. import cn.pro.domain.CustormerBean;
  6. import cn.pro.service.ICustormerService;
  7. public class ICustormerServiceImpl implements ICustormerService {
  8. private CustormerDao custormerdao = new CustormerDaoImpl();
  9. @Override
  10. public void addcustormer(CustormerBean custormer) {
  11. // TODO Auto-generated method stub
  12. custormerdao.addcustormer(custormer);
  13. }
  14. @Override
  15. public void deletecustormer(int custormer_id) {
  16. // TODO Auto-generated method stub
  17. custormerdao.deletecustormer(custormer_id);
  18. }
  19. @Override
  20. public CustormerBean getcustormer(String custormer_name) {
  21. // TODO Auto-generated method stub
  22. return custormerdao.getcustormer(custormer_name);
  23. }
  24. @Override
  25. public void updatecustormer(CustormerBean custormer) {
  26. // TODO Auto-generated method stub
  27. custormerdao.updatecustormer(custormer);
  28. }
  29. @Override
  30. public List<CustormerBean> getall() {
  31. // TODO Auto-generated method stub
  32. return custormerdao.getall();
  33. }
  34. }

业务层调用持久层:

cn.pro.dao.CustormerDao

  1. package cn.pro.dao;
  2. import java.util.List;
  3. import cn.pro.domain.CustormerBean;
  4. //客户的持久层接口
  5. public interface CustormerDao {
  6. List<CustormerBean> getall();
  7. void addcustormer(CustormerBean custormer);
  8. void deletecustormer(int custormer_id);
  9. CustormerBean getcustormer(String custormer_name);
  10. void updatecustormer(CustormerBean custormer);
  11. }

cn.pro.dao.CustormerDaoImpl

  1. package cn.pro.dao;
  2. import java.sql.SQLException;
  3. import java.util.List;
  4. import org.apache.commons.dbutils.QueryRunner;
  5. import org.apache.commons.dbutils.handlers.BeanHandler;
  6. import org.apache.commons.dbutils.handlers.BeanListHandler;
  7. import com.mchange.v2.c3p0.ComboPooledDataSource;
  8. import cn.pro.domain.CustormerBean;
  9. import cn.pro.utils.C3p0Util;
  10. //客户的持久层实现类
  11. public class CustormerDaoImpl implements CustormerDao {
  12. ComboPooledDataSource ds = C3p0Util.getds();
  13. private QueryRunner runner=new QueryRunner(ds);
  14. @Override
  15. public List<CustormerBean> getall() {
  16. // TODO Auto-generated method stub
  17. try {
  18. return runner.query("select * from custormer", new BeanListHandler<CustormerBean>(CustormerBean.class));
  19. } catch (SQLException e) {
  20. // TODO Auto-generated catch block
  21. throw new RuntimeException(e);
  22. }
  23. }
  24. @Override
  25. public void addcustormer(CustormerBean custormer) {
  26. // TODO Auto-generated method stub
  27. try {
  28. runner.update("insert into custormer values('custormer_id'=?,'custormer_name'=?,'custormer_add'=?,'custormer_tel'=?,'custormer_sex'=?,'custormer_birth'=?)",
  29. custormer.getCustormer_id(),
  30. custormer.getCustormer_name(),
  31. custormer.getCustormer_add(),
  32. custormer.getCustormer_tel(),
  33. custormer.getCustormer_sex(),
  34. custormer.getCustormer_birth());
  35. } catch (SQLException e) {
  36. // TODO Auto-generated catch block
  37. throw new RuntimeException(e);
  38. }
  39. }
  40. @Override
  41. public void deletecustormer(int custormer_id) {
  42. // TODO Auto-generated method stub
  43. try {
  44. runner.update("delete custormer where custormer_id=?",custormer_id);
  45. } catch (SQLException e) {
  46. // TODO Auto-generated catch block
  47. throw new RuntimeException(e);
  48. }
  49. }
  50. @Override
  51. public CustormerBean getcustormer(String custormer_name) {
  52. try {
  53. return runner.query("select * from custormer where custormer_name=?", new BeanHandler<CustormerBean>(CustormerBean.class),custormer_name);
  54. } catch (SQLException e) {
  55. // TODO Auto-generated catch block
  56. throw new RuntimeException(e);
  57. }
  58. }
  59. @Override
  60. public void updatecustormer(CustormerBean custormer) {
  61. // TODO Auto-generated method stub
  62. try {
  63. runner.update("update custormer set custormer_name=?,custormer_add=?,custormer_tel=?,custormer_sex=?,custormer_birth=? whrer custormer_id=?",
  64. custormer.getCustormer_name(),
  65. custormer.getCustormer_add(),
  66. custormer.getCustormer_tel(),
  67. custormer.getCustormer_sex(),
  68. custormer.getCustormer_birth(),
  69. custormer.getCustormer_id());
  70. } catch (SQLException e) {
  71. // TODO Auto-generated catch block
  72. throw new RuntimeException(e);
  73. }
  74. }
  75. }

 C3p0 xml配置文件

/src/c3p0-config.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <c3p0-config>
  3. <default-config>
  4. <property name="driverClass">com.mysql.jdbc.Driver</property>
  5. <property name="jdbcUrl">jdbc:mysql://localhost:3306/springtest</property>
  6. <property name="user">root</property>
  7. <property name="password">123456</property>
  8. <property name="initialPoolSize">10</property>
  9. <property name="maxIdleTime">30</property>
  10. <property name="maxPoolSize">100</property>
  11. <property name="minPoolSize">10</property>
  12. </default-config>
  13. </c3p0-config>

C3p0 properties配置文件。

/springtest226/src/c3p0.properties

  1. driverClass=com.mysql.jdbc.Driver
  2. jdbcUrl=jdbc:mysql://localhost:3306/springtest
  3. user=root
  4. password=123456

获取连接池的工具类 

cn.pro.utils.C3p0Util

  1. package cn.pro.utils;
  2. import java.beans.PropertyVetoException;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.util.ResourceBundle;
  6. import org.omg.CORBA.PUBLIC_MEMBER;
  7. import com.mchange.v2.c3p0.ComboPooledDataSource;
  8. import com.mysql.jdbc.Connection;
  9. import com.mysql.jdbc.Statement;
  10. public class C3p0Util {
  11. static ComboPooledDataSource ds =new ComboPooledDataSource();//自动加载配置文件类容
  12. //获取properties文件中配置
  13. /* static{
  14. ResourceBundle rb = ResourceBundle.getBundle("c3p0");
  15. try {
  16. ds.setDriverClass(rb.getString("driverClass"));
  17. ds.setJdbcUrl(rb.getString("jdbcUrl"));
  18. ds.setUser(rb.getString("user"));
  19. ds.setPassword(rb.getString("password"));
  20. } catch (PropertyVetoException e) {
  21. // TODO Auto-generated catch block
  22. throw new ExceptionInInitializerError("初始化失败"+e);
  23. }
  24. }*/
  25. public static ComboPooledDataSource getds() {
  26. return ds;
  27. }
  28. public static Connection getConection(){
  29. try {
  30. return (Connection) ds.getConnection();
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33. throw new RuntimeException();
  34. }
  35. }
  36. public static void close(ResultSet rs,Statement state,Connection conn){
  37. if(rs != null){
  38. try {
  39. rs.close();
  40. } catch (SQLException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. if(state != null){
  45. try {
  46. state.close();
  47. } catch (SQLException e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. if(conn != null){
  52. try {
  53. conn.close();
  54. } catch (SQLException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }
  59. }

使用junit测试

  1. package cn.pro.junit;
  2. import static org.junit.Assert.*;
  3. import java.util.List;
  4. import java.util.ResourceBundle;
  5. import org.junit.Test;
  6. import cn.pro.domain.CustormerBean;
  7. import cn.pro.service.ICustormerService;
  8. import cn.pro.service.impl.ICustormerServiceImpl;
  9. public class ICustormerServiceImplTest {
  10. @Test
  11. public void test() {
  12. ICustormerService custormer = new ICustormerServiceImpl();
  13. List<CustormerBean> list = custormer.getall();
  14. System.out.println(list);
  15. }
  16. @Test
  17. public void test2(){
  18. ResourceBundle rb = ResourceBundle.getBundle("c3p0");
  19. System.out.println(rb.getString("driverClass"));
  20. }
  21. }

把数据库中类容排列成数组打印出来。

整合spring部分代码更改:

自己写的类代码块的依赖

1.明确依赖关系。

2.使用set方法注入依赖

注意不能忽略调用包类的依赖(这部分需要看包的源代码)

1.有构造函数 

例如:

<bean id="runner" class="org.apache.commons.dbutils.QueryRunner">
    <constructor-arg name="ds" ref="datasource"></constructor-arg>
    </bean>

2.有set方法:

例如:

    <!-- 配置C3p0连接池  ComboPooledDataSource有set方法,通过set逐日依赖   -->
    <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/springtest"></property>
    <property name="user" value="root"></property>
    <property name="password" value="123456"></property>
    </bean>

 

ICustormerServiceImpl依赖CustormerDao接口来调用持久层,生成set方法;

cn.pro.service.impl.ICustormerServiceImpl

  1. package cn.pro.service.impl;
  2. import java.util.List;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import cn.pro.dao.CustormerDao;
  6. import cn.pro.dao.CustormerDaoImpl;
  7. import cn.pro.domain.CustormerBean;
  8. import cn.pro.service.ICustormerService;
  9. public class ICustormerServiceImpl implements ICustormerService {
  10. //ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  11. private CustormerDao custormerdao;
  12. public void setCustormerdao(CustormerDao custormerdao) {
  13. this.custormerdao = custormerdao;
  14. }
  15. //private CustormerDao custormerdao = new CustormerDaoImpl();
  16. @Override
  17. public void addcustormer(CustormerBean custormer) {
  18. // TODO Auto-generated method stub
  19. custormerdao.addcustormer(custormer);
  20. }
  21. @Override
  22. public void deletecustormer(int custormer_id) {
  23. // TODO Auto-generated method stub
  24. custormerdao.deletecustormer(custormer_id);
  25. }
  26. @Override
  27. public CustormerBean getcustormer(String custormer_name) {
  28. // TODO Auto-generated method stub
  29. return custormerdao.getcustormer(custormer_name);
  30. }
  31. @Override
  32. public void updatecustormer(CustormerBean custormer) {
  33. // TODO Auto-generated method stub
  34. custormerdao.updatecustormer(custormer);
  35. }
  36. @Override
  37. public List<CustormerBean> getall() {
  38. // TODO Auto-generated method stub
  39. return custormerdao.getall();
  40. }
  41. }

CustormerDaoImpl  依赖runner生成set方法:

cn.pro.dao.CustormerDaoImpl

  1. package cn.pro.dao;
  2. import java.sql.SQLException;
  3. import java.util.List;
  4. import org.apache.commons.dbutils.QueryRunner;
  5. import org.apache.commons.dbutils.handlers.BeanHandler;
  6. import org.apache.commons.dbutils.handlers.BeanListHandler;
  7. import com.mchange.v2.c3p0.ComboPooledDataSource;
  8. import cn.pro.domain.CustormerBean;
  9. import cn.pro.utils.C3p0Util;
  10. //客户的持久层实现类
  11. public class CustormerDaoImpl implements CustormerDao {
  12. private QueryRunner runner;
  13. public void setRunner(QueryRunner runner) {
  14. this.runner = runner;
  15. }
  16. //ComboPooledDataSource ds = C3p0Util.getds();
  17. //private QueryRunner runner=new QueryRunner(ds);
  18. @Override
  19. public List<CustormerBean> getall() {
  20. // TODO Auto-generated method stub
  21. try {
  22. return runner.query("select * from custormer", new BeanListHandler<CustormerBean>(CustormerBean.class));
  23. } catch (SQLException e) {
  24. // TODO Auto-generated catch block
  25. throw new RuntimeException(e);
  26. }
  27. }
  28. @Override
  29. public void addcustormer(CustormerBean custormer) {
  30. // TODO Auto-generated method stub
  31. try {
  32. runner.update("insert into custormer values('custormer_id'=?,'custormer_name'=?,'custormer_add'=?,'custormer_tel'=?,'custormer_sex'=?,'custormer_birth'=?)",
  33. custormer.getCustormer_id(),
  34. custormer.getCustormer_name(),
  35. custormer.getCustormer_add(),
  36. custormer.getCustormer_tel(),
  37. custormer.getCustormer_sex(),
  38. custormer.getCustormer_birth());
  39. } catch (SQLException e) {
  40. // TODO Auto-generated catch block
  41. throw new RuntimeException(e);
  42. }
  43. }
  44. @Override
  45. public void deletecustormer(int custormer_id) {
  46. // TODO Auto-generated method stub
  47. try {
  48. runner.update("delete custormer where custormer_id=?",custormer_id);
  49. } catch (SQLException e) {
  50. // TODO Auto-generated catch block
  51. throw new RuntimeException(e);
  52. }
  53. }
  54. @Override
  55. public CustormerBean getcustormer(String custormer_name) {
  56. try {
  57. return runner.query("select * from custormer where custormer_name=?", new BeanHandler<CustormerBean>(CustormerBean.class),custormer_name);
  58. } catch (SQLException e) {
  59. // TODO Auto-generated catch block
  60. throw new RuntimeException(e);
  61. }
  62. }
  63. @Override
  64. public void updatecustormer(CustormerBean custormer) {
  65. // TODO Auto-generated method stub
  66. try {
  67. runner.update("update custormer set custormer_name=?,custormer_add=?,custormer_tel=?,custormer_sex=?,custormer_birth=? whrer custormer_id=?",
  68. custormer.getCustormer_name(),
  69. custormer.getCustormer_add(),
  70. custormer.getCustormer_tel(),
  71. custormer.getCustormer_sex(),
  72. custormer.getCustormer_birth(),
  73. custormer.getCustormer_id());
  74. } catch (SQLException e) {
  75. // TODO Auto-generated catch block
  76. throw new RuntimeException(e);
  77. }
  78. }
  79. }

依赖关系:cn.pro.service.impl.ICustormerServiceImpl   ------>cn.pro.dao.CustormerDaoImpl------->org.apache.commons.dbutils.QueryRunner------>com.mchange.v2.c3p0.ComboPooledDataSource

bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!-- bean definitions here -->
  7. <bean id="custormerservice" class="cn.pro.service.impl.ICustormerServiceImpl">
  8. <property name="custormerdao" ref="custormerdao"></property>
  9. </bean>
  10. <bean id="custormerdao" class="cn.pro.dao.CustormerDaoImpl">
  11. <property name="runner" ref="runner"></property>
  12. </bean>
  13. <!-- 配置runner runner有构造函数可通过arg配置 -->
  14. <bean id="runner" class="org.apache.commons.dbutils.QueryRunner">
  15. <constructor-arg name="ds" ref="datasource"></constructor-arg>
  16. </bean>
  17. <!-- 配置C3p0连接池 ComboPooledDataSource有set方法,通过set逐日依赖 -->
  18. <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  19. <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
  20. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springtest"></property>
  21. <property name="user" value="root"></property>
  22. <property name="password" value="123456"></property>
  23. </bean>
  24. </beans>

junit测试

@Test
    public void test3(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        ICustormerService custormer =  (ICustormerService) ac.getBean("custormerservice");
        List<CustormerBean> list = custormer.getall();
        System.out.println(list);

这样就解决了程序的耦合。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/68251
推荐阅读
相关标签
  

闽ICP备14008679号