0
点赞
收藏
分享

微信扫一扫

【spring mvc(二)】spring mvc使用属性文件配置c3p0和dbcp数据源

独兜曲 2022-11-11 阅读 96


spring mvc如果要用到数据库,就要为项目配置数据源,目前有两个比较常见的数据源:c3p0与dbcp。这两个都是连接池技术,连接池的概念大概是,维护若干个数据库的连接,程序需要使用的时候直接返回给它,这样做的用意是降低数据库连接,关闭的开销,如果每次程序请求数据库都进行连接操作,那么代价会很大,取而代之的做法是先准备好若干个连接,用的时候返回即可。

1.web.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 应用上下文配置文件 -->
<param-value>/WEB-INF/config/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置spring核心servlet -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

web.xml的listener主要是配置spring 容器,通过<context-param>来指定spring配置文件的位置,默认是web-inf下的applicatinContext.xml

2.属性文件jdbc.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=

这个properties文件本质上是一个map,等号左边是key,右边是value。

url项的格式是固定的,必须有jdbc:mysql才行,后面跟数据库的url,后面加上参数是一个好的习惯,参数的意义在于如果项目编码是utf-8而底层数据库是gbk,那么存数据的时候先把utf-8数据解码,转成gbk再存,取数据过程相反。

至于指定这个文件的位置在后面。

3.spring配置文件spring.xml:

这个是spring容器的配置文件,里面是spring要管理的bean,当然有关数据源的信息就可以配在这里面,有的时候为了更好的维护或者安全考虑,会把数据库的信息另外存放在文件中,比如上面的jdbc.properties。那么需要配置propertyConfiger,指定配置文件的位置信息。

值得一说的是c3p0和dbcp数据源的配置信息是不同的,查表有两点,要格外注意,否则数据源连接不上。

   (1),datasource的class不同,这个比较明显,不说了。

   (2),<property>名字不同,比如dbcp使用driverClassName而c3p0使用driverClass,dbcp用username而c3p0用user,这个property name属性是定义死的,是连接池实现类的属性,如果名字错了,spring无法注入属性,因此要格外注意。


下面是dbcp的配置信息:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!--引入jdbc配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/config/jdbc.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

<!-- dataSource 配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>

</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
lazy-init="false" autowire="default" >
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>


</beans>

下面是c3p0的配置信息:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!--引入jdbc配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/config/jdbc.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

<!-- dataSource 配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${jdbc.driver}</value>
</property>
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>

</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"
lazy-init="false" autowire="default" >
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>

</beans>

配置好以后,再配置一个jdbctemplate即可。

4.spring mvc的配置文件,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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- 启用spring mvc 注解 -->
<context:annotation-config />
<!-- 自动扫描的包名 -->
<context:component-scan base-package="com.test"/>

<!-- 默认的注解映射的支持,自动注册DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<mvc:annotation-driven />

<!-- 视图解释类 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>

<!-- 对静态资源文件的访问-->
<!-- <mvc:resources mapping="/images/**" location="/WEB-INF/images/" cache-period="31556926"/> -->
<!-- <mvc:resources mapping="/js/**" location="/WEB-INF/js/" cache-period="31556926"/> -->
<!-- <mvc:resources mapping="/css/**" location="/WEB-INF/css/" cache-period="31556926"/> -->

</beans>

主要是加了注解功能和视图解析。


5.controller:

package com.test;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

@Autowired
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@RequestMapping(value="/index.htm")
public String test(){
String sql = "SELECT * FROM user";
List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).get("first"));
}
return "index";
}
}

我的项目设置了web context root 为"/",直接http://localhost:8080/index.htm,就可以访问。如果没设置web context root ,可能访问方式不一样。



源码下载:

​​http://pan.baidu.com/s/1pKqZWvh​​



举报

相关推荐

0 条评论