方法的代码

该代码示例展示了如何利用MyBatis的注解处理数据库的查询、删除、登录和新增操作。在后台Controller中,实现了显示所有反馈、删除反馈、用户登录和添加新反馈的功能。前端JSP页面提供了查询和反馈列表展示。
摘要由CSDN通过智能技术生成

package com.hp.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import com.hp.domain.Back;
import com.hp.domain.User;

public interface BackMapper {

    //显示
    public List<Back> findAll(Back back);
    
    //查询
    @Select("select * from tab_user")
    public List<User> chaxun();
    
    //删除
    @Delete("delete from tab_feedback where id=#{id}")
    public void del(Integer id);
    
     //登录
    @Select("select * from tab_user where uname=#{uname} and pwd=#{pwd}")
    public User dl(User user);
    
  //新增
   @Insert("insert tab_feedback(author,commit_time,title,content,OPEN) values(#{author},#{commit_time},#{title},#{content},#{OPEN})")
   public void add(Back back);
}


<mapper namespace="com.hp.dao.BackMapper">
    <resultMap type="Back" id="backList"></resultMap>
    <select id="findAll" resultMap="backList">
    select tab_feedback.*,uname
    from tab_user,tab_feedback
    where tab_user.id=tab_feedback.author
     <!-- and uname = #{uname} -->
    <if test="title!=null and title!=''">
     and title like CONCAT ('%',#{title},'%') 
     </if>
     <if test="OPEN!=null">
         and OPEN=#{OPEN}
     </if>
    </select>
</mapper>

package com.hp.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hp.domain.Back;
import com.hp.domain.User;
import com.hp.service.BackService;

@Controller
public class BackController {

    
    @Autowired
    private BackService backService;
    
    //显示
    @RequestMapping("/findAll")
    public ModelAndView findAll(Back back)
    {
        ModelAndView mv=new ModelAndView();
        
        mv.addObject("backList", backService.findAll(back));
        //调用组合查询
        mv.addObject("zhcx", backService.chaxun());
        mv.setViewName("index");
        return mv;
    }
    //删除
    @RequestMapping("/del/{id}")
    public ModelAndView del(@PathVariable Integer id)
    {
        ModelAndView mv=new ModelAndView();
        backService.del(id);
        mv.setViewName("redirect:/findAll");
        return mv;
    }
    
    //登录
    @RequestMapping("/login")
    public ModelAndView dl(User user,HttpSession session)
    {
        ModelAndView mv=new ModelAndView();
        if(backService.dl(user)!=null)
        {
            //session.setAttribute("uname", user.getUname());
            mv.setViewName("redirect:/findAll");
        }
        else 
        {
            mv.setViewName("login");
        }
        return mv;
    }
    
    //新增页面
    @RequestMapping("/xzym")
    public ModelAndView xzym(HttpSession session)
    {
        ModelAndView mv=new ModelAndView();
        /*mv.addObject("id", (Integer)session.getAttribute("id"));
        mv.addObject("uname", (String)session.getAttribute("uname"));*/
        backService.chaxun();
        mv.setViewName("add");
        return mv;
    }
    
    //新增
    @RequestMapping("/add")
    public ModelAndView add(Back back)
    {
        ModelAndView mv=new ModelAndView();
        backService.add(back);
        mv.setViewName("redirect:/findAll");
        return mv;
    }
}
 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/findAll">
        <p>
            反馈标题<input type="text" name="title">
            公开状态
            <input type="radio" name="OPEN"value="0">不公开
            <input type="radio" name="OPEN"value="1">公开
            <input type="submit" value="查询">
            <a href="${pageContext.request.contextPath}/xzym">发表反馈</a>
        </p>
        <table border="2px">
                <tr>
                    <th>编号</th>
                    <th>反馈人</th>
                    <th>反馈时间</th>
                    <th>反馈标题</th>
                    <th>反馈内容</th>
                    <th>公开状态</th>
                    <th>操作</th>
                </tr>
                <c:forEach items="${backList}" var="b">
                <tr>
                    <td>${b.id}</td>
                    <td>${b.uname}</td>
                    <td>${b.commit_time}</td>
                    <td>${b.title}</td>
                    <td>${b.content}</td>
                    <td>${b.OPEN==0?'不公开':(b.OPEN==1?'2':'4')}</td>
                    <td>
                        <a href="" οnclick="return confirm('确定要删除吗')">${b.OPEN==0?'':'不公开'}</a>
                        <a href="${pageContext.request.contextPath}/del/${b.id}" οnclick="return confirm('确定要删除吗')">删除</a>
                    </td>
                </tr>
                </c:forEach>
        </table>
    </form>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值