2.整合Mybatis
spring-dao.xml:
1
5
1
60000
stat
60000
300000
3.web.xml整合SpringMVC和Mybatis
default
/js/*
/css/*
/images/*
/fonts/*
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/*
springMybatis
org.springframework.web.servlet.DispatcherServlet
1
springMybatis
/
contextConfigLocation
/WEB-INF/config/spring-*.xml
org.springframework.web.context.ContextLoaderListener
DruidStatView
com.alibaba.druid.support.http.StatViewServlet
DruidStatView
/druid/*
404
/error/404.jsp
500
/error/500.jsp
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
jdbc.url=jdbc\:mysql\://localhost\:3306/druid?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull
jdbc.username=root
jdbc.password=123456
6.测试搭建是否成功,后台代码
首先是登录,用了加密,可以去掉
package com.alibaba.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.alibaba.model.User;
import com.alibaba.service.UserService;
import com.alibaba.util.RequestUtil;
/**
* @author tfj
* 2014-7-26
*/
@Controller
public class SystemController {
private final Logger log = LoggerFactory.getLogger(SystemController.class);
@Resource
private UserService userService;
@RequestMapping(value = "/",method = RequestMethod.GET)
public String home() {
log.info("返回首页!");
return "index";
}
@RequestMapping(value = "/test/hello",method = RequestMethod.GET)
public String testHello() {
log.info("执行了testHello方法!");
return "testHello";
}
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String testLogin(HttpServletRequest request,@RequestParam String username, @RequestParam String password) {
log.info("执行了testLogin方法!");
User user = userService.findUserByName(username);
if(user!=null){
if(user.getPassword().equals(DigestUtils.md5Hex(password))){
request.getSession().setAttribute("userId", user.getId());
request.getSession().setAttribute("user", username);
return "redirect:" + RequestUtil.retrieveSavedRequest();//跳转至访问页面
}else{
log.info("密码错误");
request.getSession().setAttribute("message", "用户名密码错误,请重新登录");
return "login";
}
}else{
log.info("用户名不存在");
request.getSession().setAttribute("message", "用户名不存在,请重新登录");
return "login";
}
}
}
7.前台jsp主要是登录和登录成功的页面,就不写了
贴一下截图:
到此,springmvc+mybatis整合成功。后续复杂的功能待添加
1.框架中关于druid和logback的配置都是从官网上copy下来的,所以都是最基本的,读者可以忽略,也可以换成读者熟悉的数据库组件和日志框架,如c3p0和log4j。
2.代码里加入了权限管理,即访问前需登录,登录后跳转至待访问页面,关于springmvc的权限管理请看:http://blog.csdn.net/tonytfjing/article/details/39207551
3.本文是从我的测试代码里剥离出来的最简单的也是最基本的代码,有些没剥离干净的地方还请见谅