从昨天开始一直在纠结数据库链接的问题,现在可以说才从库里面查出数据。这种感觉还是希望和大家分享一下

首先我们来看看我用ecplise创建项目的目录结构:

上面是我的目录结构,和数据库链接的都放在了applicantContent.xml这个配置文件里面了。在最开始学习SpringMVC的时候配置文件的位置真的是一个比较困扰的问题。而且如果你是自学的话找,往往在网上看一篇文章然后跟着做发现在自己本地就是不行,有的时候尽管从望山直接拉下一个别人布置好的SpringMVC你兴致冲冲的导入到ecplise中,可是还是不行,伴随着各种无奈,并不是说人家的项目不行,各种原因吧,可能你本地ecplise配置和人家的不一样都有。好了,不扯这些,我们首先来看看配置文件,首先我们在一开始创建web工程的时候自带的 是一个web.xml,所有的开始我们都从web.xml中进行,而且我还是注意的是web.xml你需要审批,配置什么,别看到网上的代码全部都复制过来。配置文件其实是你的代码里面需要读取的,你需要实现怎样的功能,就配置什么,然后引入相关的jar

那么我们这个地方需实现的是配置jdbc的配置,我们来看看web.xml应该怎样来进行配置:

<?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>Shopping</display-name><!-- 过滤器编码设置 --><filter><filter-name>CharacterEncodingFilter</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></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 信息转发器 --><servlet><servlet-name>springMVC</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springMVC</servlet-name><url-pattern>/</url-pattern></servlet-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext.xml</param-value></context-param><!-- 允许访问静态资源 --><servlet-mapping><servlet-name>default</servlet-name><url-pattern>*.html</url-pattern></servlet-mapping><!-- 欢迎页 --><welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list><!-- 配置log4j配置文件路径 --><context-param><param-name>log4jConfigLocation</param-name><param-value>log4j.properties</param-value></context-param><!-- 60s 检测日志配置 文件变化 --><context-param><param-name>log4jRefreshInterval</param-name><param-value>60000</param-value></context-param><!-- 配置Log4j监听器 --><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener>
</web-app>

上面是我的web.xml的配置,其中有一段忘记了注释,也是我们这地方比较重要的,那就是

 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:applicationContext.xml</param-value></context-param>

为什么说这一段重要,因为我们从在SpringMVC中许多的bean都是配置在applicantContent.xml中,当然链接数据库也是一样配置到了这个文件,其实这一段为加载配置文件applicantContent.xml只有在加载之后我们在java代码中才能找到这个文件,

从上面的目录中我们可以看到的是还有一个配置文件SpringMVC-Servlet.xml这个配置文件,其实这个是用来控制我们的访问,作为web项目接口不可避免的,为了很好的访问,我们用这个配置文件来进行控制访问的权限,或者对url进行转发。也就是我们在上面注释的信息转发器,那么我们拿出来这个SpringMVC-servlet.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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"><!-- 解决中文乱码 --><mvc:annotation-driven><mvc:message-converters register-defaults="true"><bean class="org.springframework.http.converter.StringHttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/plain;charset=UTF-8</value><value>text/html;charset=UTF-8</value><value>application/json;charset=UTF-8</value></list></property></bean></mvc:message-converters></mvc:annotation-driven><!-- 支持返回json(避免IE在ajax请求时,返回json出现下载 ) --><bean id="utf8Charset" class="java.nio.charset.Charset"factory-method="forName"><constructor-arg value="UTF-8" /></bean><beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"><property name="messageConverters"><list><beanclass="org.springframework.http.converter.StringHttpMessageConverter"><constructor-arg ref="utf8Charset" /></bean><bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/plain;charset=UTF-8</value><value>application/json;charset=UTF-8</value></list></property></bean></list></property></bean><bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" /><beanclass="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/html/" /></bean><!-- 静态资源处理 --><mvc:default-servlet-handler /><mvc:annotation-driven /><context:component-scan base-package="com.wdg.controller"></context:component-scan>
</beans>

对于这个文件的介绍我们就一一多说了,网上有很多详细的介绍,我们来继续看applicantContent.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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- <context:component-scan base-package="com.spring.controller" /> --><!--view --><!-- 获取配置文件 --><bean id="config"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath*:jdbc.properties</value></list></property></bean><!-- 获取数据源 --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/shopping?characterEncoding=utf8" /><property name="username" value="root" /><property name="password" value="11111" /></bean><bean id="userDao" class="com.wdg.dao.UserDao"><property name="dataSource" ref="dataSource"></property></bean><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/view/" /><property name="suffix" value=".html" /></bean>
</beans>

这个里面配置了数据源,当然在这个过程中会缺少包之类的ClassNotFound,

UserDao是你自己创建的类:

package com.wdg.dao;import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;public class UserDao extends JdbcDaoSupport {@SuppressWarnings("rawtypes")public  void getUserName(){String sql="select * from userinfo";List<Map> result = super.getJdbcTemplate().query(sql, new RowMapper<Map>() {  @Override  public Map<String,String> mapRow(ResultSet rs, int rowNum) throws SQLException {  Map<String,String> row = new HashMap<String, String>();  row.put("rowguid", rs.getString("rowguid"));  return row;  }}); System.out.println(result.toString());}public UserDao() {super();}}

希望对你有所帮助

SpringMVC中数据库链接配置相关推荐

  1. 多数据库应用加强,增加表枚举约定数据库链接配置

    对于多数据库应用,你仍可以使用以下的方法实例一个MAction MAction action  =   new  MAction(TableNames.Users, " CYQ " ...

  2. Spring 配置的项目中数据库链接信息加密(详细)

    传统JAVA_WEB项目如果是ssh.ssm之类的搭建的项目,其数据库链接信息大部分是交给Spring来作为管理 数据库的URL地址.账号.密码一般会写在配置文件中:dbconfig.properti ...

  3. Jmeter中JDBC链接配置 JDBC Connection Configuration

    如果在Jmeter 中想用到连接数据库的功能,必须下载jar包,常见的关系型数据库jar包见以下共享链接 链接:https://pan.baidu.com/s/1t-k9RW141lw0j_QSw53 ...

  4. [转]spring入门(六)【springMVC中各数据源配置】

    在使用spring进行javaWeb开发的过程中,需要和数据库进行数据交换,为此要经常获取数据库连接,使用JDBC的方式获取数据库连接,使用完毕之后再释放连接,这种过程对系统资源的消耗无疑是很大的,这 ...

  5. ejb3.0 中数据库的配置

    persistence.xml中的数据为: <?xml version="1.0" encoding="UTF-8"?> <persisten ...

  6. 【SpringMVC学习11】SpringMVC中的拦截器

    Springmvc的处理器拦截器类似于Servlet 开发中的过滤器Filter,用于对处理器进行预处理和后处理.本文主要总结一下springmvc中拦截器是如何定义的,以及测试拦截器的执行情况和使用 ...

  7. Django---Mysql数据库链接

    Django链接Mysql数据库: 第一步:创建应用 python manage.py startapp index 第二步:将应用添加到配置里面 settings INSTALLED_APPS = ...

  8. (转)SpringMVC学习(十二)——SpringMVC中的拦截器

    http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...

  9. springmvc中ajax,springmvc中ajax处理

    1.使用HttpServletResponse处理--不需要配置解析器 @Controller public class AjaxController { @RequestMapping(" ...

最新文章

  1. 购物价值观(values of shopping)
  2. 谷歌警告:安卓再现高危漏洞 华为小米等可能被黑客完全控制
  3. Spring boot 使用
  4. 小白学算法:买卖股票的最佳时机!
  5. 武汉大学c语言实验报告模板,武汉大学C语言程序设计第3讲(2012级).ppt
  6. [学习笔记] JavaScript 检测数组
  7. linux 域名对应ip 端口号,【原创】Linux基础之测试域名IP端口连通性
  8. 【数据预处理】TIMIT语料库WAV文件转换
  9. RabbitMQ之路由选择
  10. MFC更改环境目录可执行文件目录下继承的值
  11. 计算机知识提炼,2017计算机考研:操作系统复习策略及重要知识点提炼
  12. java解析MT940报文,swift MT报文解析处理
  13. 腾讯校招20道选择题含答案
  14. python 格林威治时间转换为标准时间格式
  15. Node.js 第一天
  16. 关于计算机知识脑筋急转弯,脑筋急转弯及知识竞答
  17. layui使用表格数据,json嵌套数据解决
  18. anchor机制讲解
  19. 静态重定位和动态重定位
  20. SCAN: learning to classify images without labels 阅读笔记

热门文章

  1. Eclipse下svn的创建分支/合并/切换使用
  2. SVN详解-linux+windows
  3. 块代码编程---开始使用块代码
  4. git patch操作
  5. 基于 JWT + Refresh Token 的用户认证实践
  6. 浅谈 UC 国际信息流推荐
  7. 阿里1688实时数据工程实践
  8. 程序员吐槽:不和同事一起吃午饭,被领导批了!网友戏称:以后拉屎也要和同事一起,打成一片!...
  9. 谁说程序员只能new对象?凭本事追的女神
  10. crt 运行时库dll跨模块传递crt对象,出现的崩溃问题