转:.net/tonytfjing/article/details/39203121
目前主流的Web MVC框架,除了Struts这个主力 外,还有Spring MVC,主要是由于Spring MVC配置比较简单,使用起来也十分明了,非常灵活,与Spring 集成较好,对RESTful API的支持也比struts要好。
MyBatis是ibatis的升级版,作为hibernate的老对手,它 是一个可以自定义SQL、存储过程和高级映射的持久层框架。
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";
}
}
}