C3P0连接池初始化设置

本文介绍如何在Web应用中使用C3P0连接池进行数据库连接管理,并通过Spring MVC实现请求处理与响应。内容包括配置web.xml、创建监听器、定义数据库配置文件、配置Spring MVC以及相关依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:配置web.xml文件,将初始化文件config_db.properties放进ServletContext, 监听器将从ServletContext调用初始化文件,对SQL进行初始化,这个过程随服务器开启而开启,随服务器停用而销毁。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>pay-v2</display-name>
  <!-- 初始化数据库池 -->
  <context-param>
  	<param-name>c3p0filePath</param-name>
  	<param-value>/WEB-INF/classes/config_db.properties</param-value>
  </context-param>
  <listener>
  	<listener-class>com.syunke.payv2.listener.MyC3p0PoolInitListener</listener-class>
  </listener>
  
  <!-- SpringMVC 配置  -->
  <!-- 注册DispatcherServlet -->
  <servlet>
  	<servlet-name>springMVC</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 	<init-param>
 		<param-name>contextConfigLocation</param-name>
 	<!-- 	<param-value>/WEB-INF/classes/spring-mvc.xml</param-value> -->
 		<param-value>classpath:spring-mvc.xml</param-value>
 	</init-param>
 	<load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 配置所有的URL都给SpringMVC来处理 -->
  <servlet-mapping>
  	<servlet-name>springMVC</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 设置统一编码 -->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
		<param-name>forceEncoding</param-name>
		<param-value>true</param-value>
	</init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  <!-- <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list> -->
</web-app>

2:新建Listener类,继承 implements ServletContextListener父类,重写contextInitialized()方法

package com.syunke.payv2.listener;

import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.syunke.payv2.utils.JDBCUtil;

public class MyC3p0PoolInitListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void contextInitialized(ServletContextEvent event) {
		System.out.println("开始初始化C3P0连接池");
		
		ServletContext context = event.getServletContext();
		String filePath = context.getInitParameter("c3p0filePath");
		if(null==filePath || "".equals(filePath.trim())) {
			throw new RuntimeException("初始化C3P0连接池配置文件找不到");
		}
		InputStream inputStream = context.getResourceAsStream(filePath);
		
		if(null==inputStream) {
			throw new RuntimeException("初始化C3P0连接池,找不到文件:"+filePath);
		}
		Properties properties = new Properties();
		try {
			properties.load(inputStream);
			JDBCUtil.initDataSource(properties);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e.getMessage());
		}
		

	}

}

3:config_db.properties文件配置

#驱动
driver=com.mysql.jdbc.Driver
#连接地址
url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
#用户名
user=root
#密码
password=888888

4:Spring-MVC.xml文件配置

<?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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    
       <!-- 增加注解扫描 -->
       <context:component-scan base-package="com.syunke.payv2.controller"/>
       <!-- 增加Spring MVC注解支持 -->
       <!--  <mvc:annotation-driven/> -->
       <!-- 增加Spring MVC注解支持 -->
       <mvc:annotation-driven >
  			<mvc:message-converters register-defaults="true">
		  		<!-- 解决@ResponseBody中文乱码   -->
		  		<!--  或者在每个请求映射的注解(@RequestMapping)中增加 produces="text/html;charset=UTF-8" -->
			    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
			       <!-- <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" /> -->
			       <property name="supportedMediaTypes">
						<list>
							<value>text/html; charset=UTF-8</value>
							<value>application/json;charset=UTF-8</value>
						</list>
					</property>
			    </bean>
		    </mvc:message-converters>
  		</mvc:annotation-driven>
       
       
	   <!-- 使用web服务器的DefaultServlet来处理js、css等静态资源 -->
	   <mvc:default-servlet-handler/>
	    <!-- jsp视图映射 -->
	   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/page/"></property>
			<property name="suffix" value=".jsp"></property>
			<!-- 配置解析器优先级 -->
			<property name="order" value="1"></property>
	   </bean>
       
       <!-- 返回json配置 需要导入 jackson-annotations.jar,jackson-core.jar,jackson-databind.jar-->
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
		<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
			<property name="messageConverters">
				<list>
					<bean class="org.springframework.http.converter.StringHttpMessageConverter">
						<property name="supportedMediaTypes">
							<list>
								<value>text/html; charset=UTF-8</value>
								<value>application/json;charset=UTF-8</value>
							</list>
						</property>
					</bean>
					<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
						<property name="supportedMediaTypes">
							<list>
								<value>text/html; charset=UTF-8</value>
								<value>application/json;charset=UTF-8</value>
							</list>
						</property>
					</bean>
				</list>
			</property>
		</bean>
</beans>

5:Spring-MVC和C3P0相关Jar包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值