SSM学习日记一,实现登录查询功能

SSM学习日记一,实现登录查询功能

UserMapper.xml

<mapper namespace="com.zhongruan.dao.UserDao" >
   <select id="findUserByUserName" parameterType="String" resultType="user">
        select * from db_user where username=#{username}
   </select>
    <select id="findAll" resultType="user">
        select * from db_user
   </select>
</mapper>

bean

package com.zhongruan.bean;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class User {

    public User() {
    }

    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    private int id;
    private String username;
    private String password;

    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 getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

controller

package com.zhongruan.controller;


import com.zhongruan.bean.User;
import com.zhongruan.dao.UserDao;
import com.zhongruan.service.IUserService;
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 java.util.List;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/login.do")
    public ModelAndView login(User user){
        boolean flag = userService.login(user.getUsername(), user.getPassword());
        ModelAndView modelAndView=new ModelAndView();
        List<User> users = userService.findAll();
        if(flag){
            modelAndView.addObject("users", users);
            modelAndView.setViewName("../success");
        }else {
            modelAndView.setViewName("../failure");
        }
        return modelAndView;

    }
}

dao

package com.zhongruan.dao;

import com.zhongruan.bean.User;

import java.util.List;

public interface UserDao {
    User findUserByUserName(String username);
    List<User> findAll();
}

service

package com.zhongruan.service.impl;

import com.zhongruan.bean.User;
import com.zhongruan.dao.UserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements IUserService {

    @Autowired
    private UserDao userDao;

    @Override
    public boolean login(String username, String password) {
        User user = userDao.findUserByUserName(username);
        List<User> users = userDao.findAll();
        if (user!=null && user.getPassword().equals(password)){
            return true;
        }
        return false;
    }
    public List<User> findAll() {
        List<User> users = userDao.findAll();
        return users;
    }
}

package com.zhongruan.service;

import com.zhongruan.bean.User;

import java.util.List;

public interface IUserService {
    boolean login(String username,String password);
    List<User> findAll();
}

index.jsp

<body>
<form action="/user/login.do" method="post">
    username:<input name="username" type="text">
    password:<input name="password" type="password">
    <input type="submit" value="login">
</form>
</body>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <th>id</th>
        <th>username</th>
        <th>password</th>
    </tr>
    <c:forEach var="users" items="${users}">
    <tr>
        <td>${users.id}</td>
        <td>${users.username}</td>
        <td>${users.password}</td>
    </tr>
    </c:forEach>
</table>
</body>
</html>

效果图在这里插入图片描述
在这里插入图片描述

展开阅读全文
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值