SpringMVC+MyBatis(最新)

目前主流的Web MVC框架,除了Struts这个主力 外,还有Spring MVC,主要是由于Spring MVC配置比较简单,使用起来也十分明了,非常灵活,与Spring 集成较好,对RESTful API的支持也比struts要好。
MyBatis是ibatis的升级版,作为hibernate的老对手,它 是一个可以自定义SQL、存储过程和高级映射的持久层框架。
与hibernate的主要区别就是mybatis是半自动化的,而hibernate是全自动的,所以当应用需求越来越复杂的时候,自动化的sql显得比较笨拙。
由于前段时间接了个项目要用springmvc做,所以我抱着练手的态度,又玩起了整合框架的游戏。经常搭框架的人应该都清楚,框架搭建的核心就是配置文件。所以我主要贴下几个配置文件的代码。还是那句话,我都是写好配置文件之后,运行报错再加jar。这里列一下我用的jar包(应该是最少的):

备注:上图有一些额外的jar,比如我用的数据库连接池是阿里巴巴的druid、日志框架式logback,所以引入了相关jar。关于这两个框架的使用和配置都是非常简单的,所以这里就不细说。
1.整合SpringMVC
springMybatis-servlet.xml:

[html] view plain copy





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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
































2.整合Mybatis

spring-dao.xml:

[html] view plain copy





xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">












1
5
1

60000

stat

60000

300000










3.web.xml整合SpringMVC和Mybatis

[html] view plain copy









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



4.logback.xml日志配置

[html] view plain copy









%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n















5.configuration.properties配置

[html] view plain copy




jdbc.url=jdbc\:mysql\://localhost\:3306/druid?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull
jdbc.username=root
jdbc.password=123456


6.测试搭建是否成功,后台代码

首先是登录,用了加密,可以去掉

[java] view plain copy




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";
}
}
}

关于service和model就不写了,写一下mybatis的mapper类映射

[html] view plain copy












源码来源: minglisoft.cn/technology

你可能感兴趣的:(SpringMVC+MyBatis(最新))