欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SSM框架模糊查询

程序员文章站 2024-01-15 23:52:58
...

SSM框架模糊查询
SSM框架模糊查询

User:
package com.zhongruan.bean;

public class User {
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 + '\'' +
            '}';
}

}

UserController:
package com.zhongruan.controller;

import com.github.pagehelper.PageInfo;
import com.zhongruan.bean.User;
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.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {
@Autowired
private IUserService userService;

@RequestMapping("/findAll.do")
public ModelAndView findAll(@RequestParam(defaultValue = "1") int pageNum,
                            @RequestParam(defaultValue = "5") int size,
                            String searchname, HttpSession session) {
    ModelAndView modelAndView=new ModelAndView();
    String username=(String) session.getAttribute("username");

    if(searchname!=null){
        session.setAttribute("searchname",searchname);
    }else {
        searchname=(String) session.getAttribute("searchname");
    }
    List<User> users=userService.findAll(pageNum,size,searchname);
    PageInfo<User> pageInfo=new PageInfo<>(users);

    modelAndView.setViewName("allUser.jsp");
    modelAndView.addObject("pageInfo",pageInfo);
    return modelAndView;

}
@RequestMapping("/login.do")
public String login(User user,HttpSession session){
    Boolean flag=userService.login(user.getUsername(),user.getPassword());
    if(flag){
        session.setAttribute("username",user.getUsername());
        return "redirect:/findAll.do";
    }else{
        return "faliure.jsp";
    }
}
@RequestMapping("/delete.do")
public String deleteById(int id){
    userService.deleteById(id);
    return "redirect:/findAll.do";
}

@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id){
    User user=userService.selectById(id);
    ModelAndView mv=new ModelAndView();
    mv.addObject("user",user);
    mv.setViewName("updateUser.jsp");

    return mv;
}
@RequestMapping("/addUser.do")
public String addById(User user) {
    userService.addById(user);
    return "redirect:/findAll.do";
}


@RequestMapping("/toupdate.do")
public ModelAndView toupdate(User user) {
    int id = user.getId();
    User users = userService.findById(id);
   ModelAndView mv = new ModelAndView();
    mv.setViewName("updateUser.jsp");
    mv.addObject("users", users);
    return mv;
}
@RequestMapping("update.do")
public String update(User user){
    userService.update(user);
    return "redirect:/findAll.do";

}

}

IUserDao:
package com.zhongruan.dao;

import com.zhongruan.bean.User;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface IUserDao {
List findAll(@Param(“searchname”) String searchname);
User selectByUserName(String name);
void deleteById(int id);
User selectById(int id);
void update(User user);

void  addById(User user);

}

LoginFilter:
package com.zhongruan.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request=(HttpServletRequest) servletRequest;
    HttpServletResponse response= (HttpServletResponse) servletResponse;
    HttpSession session=request.getSession();
    if(session.getAttribute("username")==null&&
    request.getRequestURI().indexOf("/login.do")==-1){
        response.sendRedirect("index.jsp");
    }else {
        filterChain.doFilter(request,response);
    }

}

@Override
public void destroy() {

}

}

UserService:
package com.zhongruan.service.iml;

import com.github.pagehelper.PageHelper;
import com.zhongruan.bean.User;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;

@Override
public List<User> findAll(int pageNum, int size,String searchname) {
    PageHelper.startPage(pageNum, size);
    return userDao.findAll(searchname);
}

@Override
public Boolean login(String username, String password) {
    User user = userDao.selectByUserName(username);
    if (user != null && password.equals(user.getPassword())) {
        return true;
    }
    return false;
}

@Override
public void deleteById(int id) {
    userDao.deleteById(id);
}

@Override
public void addById(User user) {
    userDao.addById(user);
}

@Override
public void updateById(User user) {
    userDao.update(user);
}

@Override
public User findById(int id) {
    return null;
}

@Override
public User selectById(int id) {
    return userDao.selectById(id);
}

@Override
public void update(User user) {
    userDao.update(user);

}

}

IUserService:
package com.zhongruan.service;

import com.zhongruan.bean.User;

import java.util.List;

public interface IUserService {
List findAll(int pageNum,int size,String searchname);
Boolean login(String username,String password);
void deleteById(int id);

void addById(User user);

void  updateById(User user);
User findById(int id);

User selectById(int id);
void update(User user);

}

SSM框架模糊查询

<?xml version="1.0" encoding="UTF-8"?>
<select id="findAll" resultType="com.zhongruan.bean.User" parameterType="String">
    select * from tb_user
    <where>
        <if test="searchname!=null">
            username like "%"#{searchname}"%"
        </if>
    </where>

</select>
<select id="selectByUserName" resultType="com.zhongruan.bean.User">
    select * from tb_user where username=#{username}
</select>

<select id="selectById" parameterType="int" resultType="user">
    select * from tb_user where id=#{id}
</select>
<update id="update" parameterType="user">
    update tb_user set username=#{username},password=#{password} where id=#{id}
</update>
<delete id="deleteById" parameterType="int">
    delete from tb_user where id=#{id}
</delete>
<insert id="addById" parameterType="com.zhongruan.bean.User">
    insert into tb_user(username,password) values (#{username},#{password})
</insert>
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191021135820269.png) <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 新增用户
<div class="row clearfix">
    <div class="col-md-12 column">
        <div class="page-header">
            <h1>
                <small>新增用户</small>
            </h1>
        </div>
    </div>
</div>
<form action="/addUser.do"
      method="post">
    用户姓名:<input type="text" name="username"><br><br><br>
    用户密码:<input type="text" name="password"><br><br><br>
    <input type="submit" value="添加" >
</form>
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019102113585016.png) <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: xcdn Date: 2019/9/26 Time: 10:09 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> user列表
<div class="row clearfix">
    <div class="col-md-12 column">
        <div class="page-header">
            <h1>
                <small>用户列表 —— 显示所有用户</small>
            </h1>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-4 column">
        <a class="btn btn-primary" href="addUser.jsp">新增</a>
    </div>

    <form action="/findAll.do" method="post">
        <input name="searchname" type="text">
        <input type="submit" value="搜索">
    </form>

</div>
<br/>

<br/>
<div class="row clearfix">
    <div class="col-md-12 column">
        <table class="table table-hover table-striped">
            <thead>
            <tr>
                <th>id</th>
                <th>用户名</th>
                <th>密码</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <c:forEach var="user" items="${pageInfo.list}">
                <tr>
                <td>${user.id}</td>
                <td>${user.username}</td>
                <td>${user.password}</td>
                <td><a href="/delete.do?id=${user.id}">删除</a>|
                    <a href="/toUpdate.do?id=${user.id}">修改</a></td>
                </tr>
            </c:forEach>
            </tbody>
        </table>
    </div>
</div>
<div>
    <a href="/findAll.do?pageNum=1&size=5">首页</a>


        <a href="/findAll.do?pageNum=${pageInfo.pageNum-1}&size=5">上一页</a>

    <c:forEach begin="1" end="${pageInfo.pages}" var="i">
        <a href="/findAll.do?pageNum=${i}&size=5">${i}</a>
    </c:forEach>


        <a href="/findAll.do?pageNum=${pageInfo.pageNum+1}&size=5">下一页</a>


    <a href="/findAll.do?pageNum=${pageInfo.pages}&size=5">尾页</a>
</div>
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191021135911947.png) <%-- Created by IntelliJ IDEA. User: 虫仔0621 Date: 2019/10/14 Time: 14:30 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> Title 失败了 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191021135935317.png) <%-- Created by IntelliJ IDEA. User: 虫仔0621 Date: 2019/10/12 Time: 10:32 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> Title hello hualili ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191021135957891.png) <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> <%-- Created by IntelliJ IDEA. User: 虫仔0621 Date: 2019/9/26 Time: 15:58 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> 后台登录 <%----%> <%----%>
<!-- //for-mobile-apps -->
<%--<link href="css/style.css" rel="stylesheet" type="text/css" media="all" />--%>
用户名: 密码:

SSM框架模糊查询
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8” isELIgnored=“false”%>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>

新增用户
<div class="row clearfix">
    <div class="col-md-12 column">
        <div class="page-header">
            <h1>
                <small>更新用户</small>
            </h1>
        </div>
    </div>
</div>
<form action="/update.do"
      method="post">
    <input type="hidden" name="id" value="${user.id}"><br><br><br>
    用户姓名:<input type="text" name="username" value="${user.username}"><br><br><br>
    用户密码:<input type="text" name="password" value="${user.password}"><br><br><br>
    <input type="submit" value="更新" >
</form>