zoukankan      html  css  js  c++  java
  • Java框架spring 学习笔记(十六):c3p0连接池的配置以及dao使用jdbcTemplate

    连接池可以大大提高数据库的性能和连接速度,将那些已连接的数据库连接存放在一个连接池里,以后别人要连接数据库的时候,将不会重新建立数据库连接,直接从连接池中取出可用的连接,用户使用完毕后,会释放连接重新放到连接池中。

    下载必要jar包mchange-commons-java-0.2.14.jar和c3p0-0.9.2.1.jar

    快捷键ctrl+alt+shift+s导入jar包

    编写UserDao.java

     1 package com.jdbc.c3p0;
     2 import org.springframework.jdbc.core.JdbcTemplate;
     3 
     4 public class UserDao {
     5     private JdbcTemplate jdbcTemplate;
     6     public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
     7         this.jdbcTemplate = jdbcTemplate;
     8     }
     9 
    10     public void add(){
    11         //创建jdbcTemplate对象
    12         //JdbcTemplate jdbcTemplate = new JdbcTemplate();
    13         String sql = "insert into user values(?,?)";
    14         jdbcTemplate.update(sql,"Lilei","bbbbbb");
    15     }
    16 }

    编写UserService.java

     1 package com.jdbc.c3p0;
     2 
     3 public class UserService {
     4     private UserDao userDao;
     5     public void setUserDao(UserDao userDao){
     6         this.userDao = userDao;
     7     }
     8 
     9     public void add(){
    10         userDao.add();
    11     }
    12 }

    编写配置文件bean.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:tx="http://www.springframework.org/schema/tx"
     4        xmlns:aop="http://www.springframework.org/schema/aop"
     5        xmlns:context="http://www.springframework.org/schema/context"
     6        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     7        xsi:schemaLocation="
     8             http://www.springframework.org/schema/beans
     9             http://www.springframework.org/schema/beans/spring-beans.xsd
    10             http://www.springframework.org/schema/context
    11             http://www.springframework.org/schema/context/spring-context.xsd
    12             http://www.springframework.org/schema/tx
    13             http://www.springframework.org/schema/tx/spring-tx.xsd
    14             http://www.springframework.org/schema/aop
    15             http://www.springframework.org/schema/aop/spring-aop.xsd ">
    16 
    17     <!-- 配置c3p0连接池 -->
    18     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    19         <!-- 注入dao对象 -->
    20         <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    21         <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
    22         <property name="user" value="root"></property>
    23         <property name="password" value="xxxxx"></property>
    24     </bean>
    25 
    26     <bean id="userService" class="com.jdbc.c3p0.UserService">
    27         <property name="userDao" ref="userDao"></property>
    28     </bean>
    29     <bean id="userDao" class="com.jdbc.c3p0.UserDao">
    30         <!-- 注入jdbcTemplate对象-->
    31         <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    32     </bean>
    33 
    34     <!-- 创建jdbcTemplate对象 -->
    35     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    36         <!-- 把dataSource传递到模板对象中-->
    37         <property name="dataSource" ref="dataSource"></property>
    38     </bean>
    39 </beans>

    编写测试文件TestService.java

     1 package com.jdbc.c3p0;
     2 
     3 import org.junit.Test;
     4 import org.springframework.context.ApplicationContext;
     5 import org.springframework.context.support.ClassPathXmlApplicationContext;
     6 
     7 public class TestService {
     8     @Test
     9     public void testDemo(){
    10         ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    11         UserService userService = (UserService) context.getBean("userService");
    12         userService.add();
    13     }
    14 }

    运行可见添加一条记录

  • 相关阅读:
    android Scroller类的理解
    android 小方法
    android 小知识点
    【转】Android TouchEvent事件传递机制
    android 自定义控件二之仿QQ长按删除
    android ExpandAbleListView控件
    android AChartEngine图标引擎
    2018.11.07-1117-无序字母对 character
    2018.11.07-4031-reverse
    2018.11.07-1015-幸运字符串查询 (lucky)
  • 原文地址:https://www.cnblogs.com/zylq-blog/p/7814569.html
Copyright © 2011-2022 走看看