Springmvc3+Spring3+MyBatis3

阅读更多

 最近写的一个三层框架:Springmvc3+Spring3+MyBatis3,后来,在Web层和Service层之间加了一个门面层,前台模块跟Web层的Controller类是对应的,Controller与门面层的类是一一对应的,在门面类中可以添加所需要的Server层所需的接口。

 

1. 项目的目录


Springmvc3+Spring3+MyBatis3_第1张图片
 
2. 所用jar文件
Springmvc3+Spring3+MyBatis3_第2张图片

 

3. 在MyEclipse创建Web工程

 

4. 修改/WebRoot/WEB-INF/Web.xml文件



 
	
	
		org.springframework.web.context.ContextLoaderListener
	

 	
 		contextConfigLocation
 		classpath*:applicationContext.xml
 	
	
	
	
		spring
			org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation 
			/WEB-INF/spring-servlet.xml
		
		1
	

	
		spring
		/
	
	
	
	
		characterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
	

	
		characterEncodingFilter
		/*
	

	
		index.jsp
	

 
 5. 创建 Spring MVC配置文件 /WebRoot/WEB-INF/spring-servlet.xml



      
     
     

     
     

     
     
     	
     	
     
     
     
     
     	 
     	 
      

 
 6. 创建页面:/WebRoot/login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    Login
	
	
	    
	
	
  
  
  
   		
UserName:
Password:
  

 

 

7.  创建页面:/WEB-INF/view/main.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
		Home
		
		
  
  
    	你好:<%=((com.smartcom.esp.pojo.User)request.getSession().getAttribute("loginUser")).getUsername() %>,现在时间是<%= new Date() %>       



 

8. 创建Spring配置文件 /src/applicationContext.xml




	
	
		
		
		
		
	

	
	
		
		
	

	
	
		
	

	
	
		
	
	

	
	
		
		
	
	
	
	
		
	
	
	
	
		
	

 

9. 创建MyBatis的配置文件:/src/mybatis-cfg.xml

 
 
  
	
	
		
	

	
	
		
	

 

10.  创建实体类:com.smartcom.esp.pojo.User.java

package com.smartcom.esp.pojo;

public class User {
	private Integer id;
	private String username;
	private String password;
	private Integer age;
	private Integer status;
	private String name;
	
	public final static Integer USER_LOCK=-1; 	//锁定
	public final static Integer USER_NORMAL=1;	//正常
	public final static Integer USER_NOEXISTS=0;//不存在
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	 
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public User() {
		super();
	}
	
	public User(Integer id, String username, String password, Integer age,Integer status,String name) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.age = age;
		this.status = status;
		this.name = name;
	}
}

 

 

11.    创建Dao层:com.smartcom.esp.dao.mapper (勿需实现Dao层的接口)

11.1  创建Dao层接口文件:com.smartcom.esp.dao.mapper.IUserDao.java

package com.smartcom.esp.dao.mapper;

import com.smartcom.esp.pojo.User;

public interface IUserDao {
	public User findByName(String username);
}

 

11.2  创建Dao层映射文件:com.smartcom.esp.dao.mapper.IUserDao.xml







	
	
		
		
		
		
		
		
	

	

	
		insert into
		user(name,phone,birthday)
		values(#{name},#{phone},#{birthday})
	

	
		update user set
		name=#{name},phone=#{phone},birthday=#{birthday}
		where id=#{id}
	

	
		delete from user
		where id=#{id}
	

	

	
	
 

 

12.    创建Service层:com.smartcom.esp.service---->该层依赖Dao层(com.smartcom.esp.dao).  

12.1  创建Service层基本接口文件,所有Server中的接口都需继承该接口:com.smartcom.esp.service.IBaseService.java

package com.smartcom.esp.service;
public interface IBaseService {
        //无任何方法   
}

12.2  创建Service层接口文件:com.smartcom.esp.service.IUserService.java

package com.smartcom.esp.service;

import com.smartcom.esp.pojo.User;

public interface IUserService extends IBaseService{
	public User checkPWD(String username);
}

12.3  创建Service层实现文件:com.smartcom.esp.service.impl.UserServiceImpl.java

package com.smartcom.esp.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.smartcom.esp.dao.mapper.IUserDao;
import com.smartcom.esp.pojo.User;
import com.smartcom.esp.service.IUserService;

@Transactional  
public class UserServiceImpl implements IUserService {

	@Autowired
	private IUserDao userDao;
	

	public IUserDao getUserDao() {
		return userDao;
	}


	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}


	@Override
	public User checkPWD(String username) {
		return userDao.findByName(username);
	} 
}

 

13.    创建门面层:com.smartcom.esp.manage---->该层依赖Service层(com.smartcom.esp.service).  

13.1  创建门面层接口文件:com.smartcom.esp.manage.IBizManage.java

package com.smartcom.esp.manage;
/**
 * 门面层
 * @author Administrator
 *
 */
public interface IBizManage {
	//返回Service层对象
	public T getService();
}

 13.2  创建门面层实现文件:com.smartcom.esp.manage.LoginManage.java

package com.smartcom.esp.manage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smartcom.esp.service.IBaseService;
import com.smartcom.esp.service.IUserService;
/**
 * 登录门面
 * @author Administrator
 *
 */
@Service
public class LoginManage implements IBizManage {
	
	@Autowired
	private IUserService userService;
	

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	@Override
	public IBaseService getService() {
		if(this.userService instanceof IBaseService){
			return userService;
		}else{
			return null;
		}
	}
}

 14. 创建Web层:com.smartcom.esp.web [放置所有Controller] ---->该层依赖门面层(com.smartcom.esp.manage).  

com.smartcom.esp.web.LoginController.java

package com.smartcom.esp.web;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
 
import com.smartcom.esp.manage.IBizManage;
import com.smartcom.esp.pojo.User;
import com.smartcom.esp.service.IUserService;

/**
 * 登录
 * 
 * @author Administrator
 * 
 */
@Controller
@RequestMapping("/login")
public class LoginController extends  BaseController { 
	 
	private String formView ;
	private String succView ; 
	
	@Autowired 
	public IBizManage login; 
	
	public IBizManage getLogin() {
		return login;
	}

	public void setLogin(IBizManage login) {
		this.login = login;
	}
 
	public String getSuccView() {
		return succView;
	}

	public void setSuccView(String succView) {
		this.succView = succView;
	}

	public String getFormView() {
		return formView;
	}

	public void setFormView(String formView) {
		this.formView = formView;
	}

	public LoginController() {
		
	}
 
	// 用户登录
	@RequestMapping("login")
	public ModelAndView login(HttpServletRequest request, User user) {
		//由用户名取用户信息
		User dbUser = login.getService().checkPWD(user.getUsername());
		ModelAndView mav = new ModelAndView();
		mav.setViewName("forward:login");
		
		if (dbUser == null) {
			mav.addObject("errorMsg", "用户名不存在");
		} else if (!dbUser.getPassword().equals(user.getPassword())) {
			mav.addObject("errorMsg", "用户密码不正确");
		} else if (dbUser.getStatus() == User.USER_LOCK) {
			mav.addObject("errorMsg", "用户已经被锁定,不能登录");
		} else {
			mav.addObject("Msg", "登录成功");
			setSessionUser(request, dbUser);
			String toUrl = (String) request.getSession().getAttribute(LOGIN_TO_URL);
			
			// 如果当前会话中没有保存登录之前的请求URL,则直接跳转到主页
			if (StringUtils.isEmpty(toUrl)) {
				toUrl = "esp/main";			//前台首页
				//toUrl = "esptool/main"; 	//后台首页
			}
			System.out.println("++============>>:"+request.getAttribute("loginUser"));
			mav.setViewName(getSuccView());
		}
		return mav;
	}

	// 登录注销
	@RequestMapping("/doLogout")
	public String logout(HttpSession session) {
		session.removeAttribute(USER_CONTEXT);
		return "forward:/login";
	}
}

 

 15. 测试:http://localhost:8080/esp/login.jsp 

 

 

结果:      你好:lis,现在时间是Tue Nov 27 15:02:17 CST 2012 
 

 

  • Springmvc3+Spring3+MyBatis3_第3张图片
  • 大小: 34.8 KB
  • Springmvc3+Spring3+MyBatis3_第4张图片
  • 大小: 68 KB
  • Springmvc3+Spring3+MyBatis3_第5张图片
  • 大小: 7.1 KB
  • esp.zip (7.2 MB)
  • 下载次数: 699
  • 查看图片附件

你可能感兴趣的:(Springmvc3+Spring3+MyBatis3)