spring mvc配置 + dbcp数据源+jdbcTemplate

摘要:

把spring的jar包放到lib目录,jar可以根据你要用的功能来选择,如果懒或者不想以后用到功能再找就一起都放进去,注意不用放文档jar和源码jar

突然想起spring对环境的一些要求,也忘记说了,我jdk的版本是1.6,数据库会使用MySQL,应用服务器是Tomcat7.0

首先去spring官网下载完整的spring包,包含libs, docs和schema,spring的版本是3.2.4

我们来看一下spring的lib包都有那些内容:


 

上面图片中除红色框内的两个jar其它都是spring官方提供的jar包,红色框内的jar我们在配置事务的时候会用到,我们一会再说.我们仔细看一下spring提供的jar包可以看到每一个模块对应着3个jar包,sources包(源码),javadoc包(文档)和编译好的jar.

然后我们看看都有哪些模块,我们先看一下spring文档提供的一张overview图,看看这些jar是不是都是和它模块对应着的


 

第一个模块,数据存储/集成,它又包括JDBC,ORM(object-relational mapping ) ,OXM(object/xml mapping),JMS(Javamessaging service),Transactions(事务)

第二个模块,Web(MVC/Remoting),它又包含web层,web-servlet(包含spring mvc的实现),web-portlet,web-struts

第三个模块,AOP(aspect-orented programming)也就是我们通常说的面向方面编程

第四个模块,Aspects ,提供了和AspectJ的集成

第五个模块,Instrumentation,提供类仪表和类加载器实现的支持

第六个模块,CoreContainer(核心容器)它又包含Beans and Core(提供框架的基本部分,包括控制反转和依赖注入特性),Context,Expression Language(在运行时为查询和操纵对象图提供一个强大的表达式语言)

第七个模块,Test,提供了测试spring组件的功能,据说挺强大的,哈哈

经典介绍:

源码结构


 

看一下jar应该都对应了.我们继续,

在myeclipse中新建个web项目,创建一下相关的目录结构,如下图:


 

config文件夹是一个sources folder用来放置配置文件.

把spring的jar包放到lib目录,jar可以根据你要用的功能来选择,如果懒或者不想以后用到功能再找就一起都放进去,注意不用放文档jar和源码jar

突然想起spring对环境的一些要求,也忘记说了,我jdk的版本是1.6,数据库会使用MySQL,应用服务器是Tomcat7.0

好了,现在进行springmvc配置,我们都知道spring的配置文件叫applicationContext.xml而springmvc的配置文件会叫springmvc.xml其实这两个文件写成一个就可以,我们就就取名springmvc.xml

在config目录下创建一个springmvc.xml文件,我们先配置sprigmvc.xml文件,

配置spring文件头

Xml代码

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:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

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/aop

http://www.springframework.org/schema/aop/spring-aop-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/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd" default-lazy-init="true">

文件头主要是对sping的.xsd文件的引用,个人看法,不保证准确,你可以点击连接进去看看,例如: http://www.springframework.org/schema/mvc/

你可以看到spring提供的各个版本的spring-mvc*.xsd文件


 

然后你记住,用什么就把什么引用上,我都引用了,有aop,context,tx,mvc....

接下来添加注解支持:

Xml代码

自动扫描spring组件,可以配置到工程根目录,如com.xg.myspring,如果想了解更多请查看更详细的代码

Xml代码

配置对视图的解析,也就是后台到页面的跳转

默认页面跳转时,路径会从page/目录下找*.jsp的文件

Xml代码

再做一个异常处理配置:

程序发生异常后会跳转到指定的错误页面,增强程序的友好度.

这里做了一个通用配置,因为Exception是异常的父类,只要发生异常都会跳转到error目录下的error.jsp文件

Java代码

error/error

接下来配置数据库,我们使用dbcp类型数据源,在lib目录添加mysql数据库连接jar,common-dbcp.jar,记得把common-logging.jar也添加上,spring日志默认使用的是它.

将写好数据库配置文件放到config目录下

jdbc.properties文件内容如下:

Java代码

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/report

jdbc.username=root

jdbc.password=12345

下面是配置数据源的代码:

Xml代码

在配置一个jdbcTemplate

Java代码

springmvc.xml暂时就配置这么多,接下来配置一下web.xml文件

先配置spring对上下文的监听

Java代码

org.springframework.web.context.ContextLoaderListener

配置contextConfigLocation,spirngmvc.xml的路径

Java代码

contextConfigLocation

classpath:springmvc.xml

配置spring分发器

Java代码

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml

1

仔细看会发现配置了两遍contextConfigLocation,这里http://minglisoft.cn/technology有解释.

现在配置工作基本就完成了,接下来就是添加测试代码,我们就按照mvc模式添加一个control 一个service一个dao ,添加一个登陆页面,一个提示登陆成功的主页面

控制器SystemUserControl.java

Java代码

package com.xg.myspring.control;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.List;

import java.util.Properties;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.View;

import com.xg.myspring.entity.SystemUser;

import com.xg.myspring.service.SystemUserService;

@Controller

@RequestMapping("/systemuser")

public class SystemUserControl {

@Resource

private SystemUserService systemUserService;

@RequestMapping(value = "/login", method = RequestMethod.GET)

public ModelAndView login(SystemUser user) {

String message = "登陆成功!";

ModelAndView mv = new ModelAndView("index");

mv.addObject("user", user);

mv.addObject("message", message);

return mv;

}

@RequestMapping(value = "/login", method = RequestMethod.POST)

public String login2(HttpServletRequest request, HttpServletResponse response, SystemUser user) {

request.getSession().setAttribute("user", user);

request.getSession().setAttribute("message", "登陆成功!");

return "redirect:/page/index.jsp";

}

@RequestMapping("/queryList")

public String queryList(HttpServletRequest request) {

List list = null;

String sql = "select * from systemuserinfotable ";

list = systemUserService.queryUserList(sql);

request.setAttribute("list", list);

return "index";

}

}

SystemUserServiceImpl.java 接口就省略了

Java代码

package com.xg.myspring.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xg.myspring.dao.SystemUserDao;

import com.xg.myspring.entity.SystemUser;

import com.xg.myspring.service.SystemUserService;

@Service

public class SystemUserServiceImpl implements SystemUserService {

@Resource

private SystemUserDao systemUserDao;

public void addSystemUser(SystemUser systemUser) {

systemUserDao.addSystemUser(systemUser);

}

public void deleteSystemUser(String sql) {

systemUserDao.deleteSystemUser(sql);

}

public SystemUser getSystemUserById(String sql) {

return systemUserDao.getSystemUserById(sql);

}

public List queryUserList(String sql) {

systemUserDao.addSystemUser(new SystemUser());

return systemUserDao.queryUserList(sql);

}

}

SystemUserDaoImpl.java ,接口省略了

Java代码

package com.xg.myspring.dao.impl;

import java.util.Date;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Repository;

import com.xg.myspring.dao.SystemUserDao;

import com.xg.myspring.entity.SystemUser;

@Repository("SystemUserDao")

public class SystemUserDaoImpl implements SystemUserDao {

@Resource

private JdbcTemplate jdbcTemplate;

public void addSystemUser(SystemUser systemUser) {

Date date=new Date();

String sql="insert into systemuserinfotable values('000_"+date.getTime()+"','abc"+date.getTime()+"','abc','1','1','test')";

if(systemUser!=null){

jdbcTemplate.execute(sql);

}else{

throw new NullPointerException();

}

}

public void deleteSystemUser(String sql) {

}

public List queryUserList(String sql) {

List list = jdbcTemplate.query(sql, new BeanPropertyRowMapper(SystemUser.class));

return list;

}

public SystemUser getSystemUserById(String sql) {

return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(SystemUser.class));

}

}

现在运行一下,报java.lang.ClassNotFoundException: org.apache.commons.pool.KeyedObjectPoolFactory 异常,可知缺少common-pool.jar

一起把 jstl.jar 和servlet-api.jar也添加上,运行就没问题了

登陆


 

主页面

---------------------------------------------------------------------------------

完整的项目源码来源  欢迎大家一起学习研究相关技术,源码获取请加求求:2670716182

猜你喜欢

转载自www.cnblogs.com/didida/p/9145528.html
今日推荐