Home > Database > Mysql Tutorial > body text

spring mvc + mybatis + mysql 调整的一个简单的登录例子

WBOY
Release: 2016-06-07 16:24:54
Original
1512 people have browsed it

spring mvc + mybatis + mysql 整合的一个简单的登录例子 今天用spring跟mybatis整合写了一个简单的登录例子,第一次整合,给自己做个笔记,可能注释写的有点少,做的不足的地方谢谢指出,也分享给需要的朋友,下面给出登录失败和成功的效果图: 这个登录例子

spring mvc + mybatis + mysql 整合的一个简单的登录例子
今天用spring跟mybatis整合写了一个简单的登录例子,第一次整合,给自己做个笔记,可能注释写的有点少,做的不足的地方谢谢指出,也分享给需要的朋友,下面给出登录失败和成功的效果图:

 

     这个登录例子用的工具是myeclipse8.6+mysql5.1,使用到的技术有spring3.0+mybatis3.2.3+mybatis-spring-1.1.1(这个是spring跟mybatis整合的包),项目的整体结构如图:

 

    现在我们要做的就是在myeclipse工具里新建一个web项目,并且添加spring 支持,不懂的朋友可以查看http://blog.csdn.net/ooliuyunoo/article/details/19908661

    项目新建完之后我们就把项目分次序把项目新建起来:

   1: 新建vo类,代码如下:

     

package com.li.vo;

public class UserVO {
	private int id;
	private String name;
	private String pwd;
	public UserVO(){}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getPwd() {
		return pwd;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return id;
	}
}
Copy after login


 

    2:  新建UserDaoIMP接口:

package com.li.IMP;

import com.li.vo.UserVO;

public interface UserDaoIMP {
	public UserVO selectUser(UserVO uservo);
	public int insertUser(UserVO uservo);
	public int updaqteUser(UserVO uservo);
	public int deleteUserById(int user_id);
}
Copy after login


 

   3: 新建UserDaoIMP.xml配置文件:


			insert into user(name,pwd) values(#{name},#{pwd})
		
			update user set name=#{name} where id=#{id}
		
			delete from user where id=#{id}
		
Copy after login


 

    4:新建UserServiceIMP服务接口:

package com.li.service;

import com.li.vo.UserVO;

public interface UserServiceIMP {
	public UserVO selectUser(UserVO uservo);
	public int insertUser(UserVO uservo);
	public int updaqteUser(UserVO uservo);
	public int deleteUserById(int user_id);
}
Copy after login


 

     5:新建UserService继承服务接口:

package com.li.service;

import com.li.IMP.UserDaoIMP;
import com.li.vo.UserVO;

public class UserService implements UserServiceIMP {
	private UserDaoIMP userdao;
	public int deleteUserById(int userId) {
		// TODO Auto-generated method stub
		return this.deleteUserById(userId);
	}

	public int insertUser(UserVO uservo) {
		// TODO Auto-generated method stub
		return this.userdao.insertUser(uservo);
	}

	public UserVO selectUser(UserVO uservo) {
		// TODO Auto-generated method stub
		return this.userdao.selectUser(uservo);
	}

	public int updaqteUser(UserVO uservo) {
		// TODO Auto-generated method stub
		return this.userdao.updaqteUser(uservo);
	}

	public void setUserdaoIMP(UserDaoIMP userdaoIMP) {
		this.userdao = userdaoIMP;
	}

	public UserDaoIMP getUserdaoIMP() {
		return userdao;
	}

}
Copy after login


 

    6:新建一个登陆控制器:

package com.li.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.li.IMP.UserDaoIMP;
import com.li.service.UserService;
import com.li.vo.UserVO;

public class LoginController implements Controller {
	private UserService userService;
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String name=request.getParameter("userName");
		String password=request.getParameter("password");
		//System.out.println("name-----"+name+"----password-----"+password);
		UserVO uservo=new UserVO();
		uservo.setName(name);
		uservo.setPwd(password);
		Map model=new HashMap();
		if(userService.selectUser(uservo)!=null){
			uservo=userService.selectUser(uservo);
			System.out.println("能查到信息");
			model.put("uservo", uservo);
			return new ModelAndView("WEB-INF/Main.jsp",model);
		}else{
			System.out.println("查不到信息");
			model.put("error", "用户名或密码错误");
			return new ModelAndView("WEB-INF/Login.jsp",model);
		}
		
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public UserService getUserService() {
		return userService;
	}


}
Copy after login


 

    7: 新建一个mybatis-config.xml 配置文件:

Copy after login

 

 8:配置 applicationCotext.xml:

LoginController
Copy after login


 

     9: 修改index.jsp的body部份为:

 

  
    
Copy after login


 

    10:添加login.jsp:

 

  





  
    My JSP 'Login.jsp' starting page
Copy after login


${error}


 

11: 添加main.jsp:





  
    My JSP 'Main.jsp' starting page
    进入主页面
你的基本信息如下:
id:${uservo.id}
用户名:${uservo.name}
密码:${uservo.pwd}
Copy after login

 


 

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!