SSM整合(四):PersonDao.xml的编写与Controller的编写

这是接上一步的操作,首先是step4:


这里其他的先不说,已经产生了逻辑问题,恩,果然我还是有点菜,两个第四步。

mybatis部分

PersonDao.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.ssmlogin.dao.PersonDao">
 	<!-- 对应数据 -->
 	<resultMap type="Person" id="MyPerson">
 		<id column="id" property="id"/>
 		<result column="username" property="username"/>
 		<result column="pwd" property="password"/>
 	</resultMap>
 	
 	<select id="selectPerson" resultMap="MyPerson">
 		select * from LG_user 
 	</select>
 	
 	<select id="selectPersonByNAP" resultMap="MyPerson">
 		select * from LG_user where username=#{username} and pwd=#{password}
 	</select>
 	
 	<insert id="insertPerson" parameterType="com.ssmlogin.bean.Person">
 		insert into LG_user(username, pwd) values(#{username}, #{password})
 	</insert>
 	
 	<delete id="deletePersonById">
 		delete from LG_user where id=#{id}
 	</delete>
 </mapper>

这里还真有要注意的地方,语法我就不说了。

<!DOCTYPE mapper

这里,如果是mapper文件,文件头不能出现configuration,反之亦然。

namespace是Dao类的限定名。

这里捎带一下我理解的mybatis流程吧

服务器->mybatis.xml,这里应该就连好了数据库了,获得sqlSession.

->在别名路径下寻找到作为mapper的Dao

->在Mappers路径下找到对应的mapper的xml

->mapper.xml中如果namespace就是这个Dao类,则其中的id和方法名一样,自动匹配。

LG_user表结构

好,差不多Mybatis部分就这样了。

Controller部分

这里controller部分其实就一个LoginController.java

LoginController.java

package com.ssmlogin.controller;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;

import com.ssmlogin.bean.Person;
import com.ssmlogin.service.LoginService;

// 登录控制器

@Controller
public class LoginController {
	@Autowired
//	@Qualifier("MyService")
	private LoginService ls;
	
	public void setLs(LoginService ls) {
		this.ls = ls;
	}

	@RequestMapping("/login")
	public ModelAndView loginCheck(Person p, HttpServletRequest request)
	{
//		WebApplicationContext acc = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
//		ls = (LoginService) acc.getBean("MyService");
		System.out.println(p);
		ModelAndView mv = new ModelAndView();
		if(ls.Login(p))
		{
			request.getSession().setAttribute("username", p.getUesrname());
			mv.setViewName("redirect:/welcome");
		}
		else
		{
			mv.addObject("message", "登录失败!请检查登录账号密码");
			mv.setViewName("/index.jsp");
		}
		
		return mv;
	}
	
	@RequestMapping("/welcome")
	public String welcome()
//	public String welcome(HttpServletRequest request, HttpSession session)
	{
//		session.setAttribute("message", session.getAttribute("username"));
		// 这两种都是request的,可以直接取到
//		request.setAttribute("message", session.getAttribute("username"));
//		request.getSession().setAttribute("message", "欢迎您:"+request.getSession().getAttribute("username"));
		return "/welcome.jsp";
	}
	
	@RequestMapping("/buy")
	public String buy(HttpServletRequest request, HttpSession session)
	{
		request.setAttribute("message", session.getAttribute("username")+"来买东西了吗");
		return "/buy.jsp";
	}
	
	@RequestMapping("/logout")
	public String logout(HttpSession session)
	{
		session.removeAttribute("username");
		return "/logout.jsp";
	}
	
	@RequestMapping("/register")
	public ModelAndView register(Person p, HttpSession session) throws Exception
	{
		ModelAndView mv = new ModelAndView();
		if(ls.Register(p))
		{
			mv.setViewName("/regsuc.jsp");
		}
		else
		{
			mv.addObject("message", "注册失败");
			mv.setViewName("/login.jsp");
		}
		return mv;
	}
	
	@ExceptionHandler(Exception.class)
	public ModelAndView err(Exception ex)
	{
		ModelAndView mv = new ModelAndView();
		mv.addObject("msg", "发生错误了哟");
		mv.addObject("ex", ex.getMessage());
		mv.setViewName("/err.jsp");
		return mv;
	}
}

这里其实没啥好说的,最开始只有登录功能,即/login和/welcome,这里/welcome其实什么都没做,就是让url好看了点。。。

到这里的话,后台部分就完了,接下来是要写前台代码。

如果我的内容在哪里有问题,欢迎私信指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值