mybatis学习笔记二mybatis结合spring mvc实现(用户登录,数据查询)

接着上次的来,我做了一个用户登录的例子

UserController:

package com.yihaomen.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.yihaomen.inter.IUserOperation;
import com.yihaomen.model.Article;
import com.yihaomen.model.User;

@Controller
@RequestMapping("/article")
public class UserController {
	@Autowired
	public  IUserOperation userMapper;

	@RequestMapping("/list")
	public ModelAndView listall(User user, HttpServletRequest request,HttpServletResponse response){
		
	/*	User user=new  User();
		user.setUserName("殊途同归");
		user.setUserAge("23");
		user.setUserAddress("广州天河");
		userMapper.addUser(user);*/

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name",user.getUserName());
		map.put("age",user.getUserAge());
                System.out.println(user.getUserName());
		List<User> listuser=userMapper.mapUser(map);
		
	        /*List<Article> articles=userMapper.getUserArticles(1); */
		ModelAndView mav=new ModelAndView("list");
		mav.addObject("articles",listuser);
		return mav;
	}
	
	@RequestMapping({"/login"})
	public ModelAndView  login(){
		ModelAndView mv=new ModelAndView("login");
		return mv;
		
	}
	
	@RequestMapping({"/doLogin"})
	public ModelAndView  login(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> map = new HashMap<String, Object>();
		String name=request.getParameter("name");
		String password=request.getParameter("password");
		map.put("name",name);
		map.put("password", password);
	    User user=userMapper.login(map);
	    if(user!=null){
	     return listall(user,request, response);
	    } else {	
	    request.getSession().setAttribute("msg","失败了!");
	    return new ModelAndView("login");
		}

	}

}
接口类IUserOperation

package com.yihaomen.inter;

import java.util.List;
import java.util.Map;

import com.yihaomen.model.Article;
import com.yihaomen.model.User;

public interface IUserOperation {
    

	public User login(Map<String, Object> map);
	public List<User> mapUser(Map<String, Object> map);	

	
}

User.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.yihaomen.inter.IUserOperation">//利用xml配置Mapper的方式来定义接口</span>

	<select id="selectUserByID" parameterType="int" resultType="User">
	    select * from `user` where id = #{id} //以id为查询条件
	</select>

	<select id="listUser"  resultType="User">
	    select * from `user` //以id为查询条件

	</select>
	
	<select id="mapUser"  parameterType="map" resultType="User">
	 select * from `user`  where userName= #{name} and userAge= #{age}
	</select>

    <select id="login"  parameterType="map"  resultType="User">
          select * from `user` where userName=#{name} and password=#{password}//登录
    </select>
</mapper>


标签<select>查询的意思,id对应就是你接口定义的方法名,关于resultType前面讲到Configuration.xml(减少类名的长度来设置的),这个做了一个处理,id直接等于方法名,倘若不这样的处理的话,就得:

<select id="mapUser"  parameterType="map" resultType="com.yihaomen.model.User"></span>

Configuration.xml类:

<configuration>  
    <typeAliases>   
        <typeAlias alias="User" type="com.yihaomen.model.User"/>  
    </typeAliases>   
</configuration>  

User实体类

package com.yihaomen.model;

public class User {
	
	private int id;
	private String userName;
	private String passWord;
	private String userAge;
	private String userAddress;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserAge() {
		return userAge;
	}
	public void setUserAge(String userAge) {
		this.userAge = userAge;
	}
	
	public String getPassWord() {
		return  passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	
	public String getUserAddress() {
		return userAddress;
	}
	public void setUserAddress(String userAddress) {
		this.userAddress = userAddress;
	}

}
最好看一下dome页面,比较简单

mybatis学习笔记二mybatis结合spring mvc实现(用户登录,数据查询)_第1张图片

成功页面:




你可能感兴趣的:(mybatis学习笔记二mybatis结合spring mvc实现(用户登录,数据查询))