mybatis study notes two mybatis combined with spring mvc implementation (user login, data query)

After coming last time, I made an example of user login

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

	}

}

Interface class 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>

 

 

 

 

 

The tag <select> means the query, the id corresponds to the method name defined by your interface. As for the resultType mentioned earlier in Configuration.xml (set by reducing the length of the class name ), this is done, the id is directly equal to the method name, if Otherwise, you have to:

 

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

 

Configuration.xml class:

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


User entity class

 

 

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

}

It's better to look at the demo page, it's relatively simple

 

Success page:


 

Guess you like

Origin blog.csdn.net/zhaoxiangpeng16/article/details/51063707