SSM框架实现简单增删改查

        学习ssm框架有一段时间了,现在能做到简单的信息增删改查。以酒店信息举例,实现酒店信息的增删改查

下面是运行效果:

添加

 修改删除

代码如下:

实体类:

public class Hotel {

    private int hid; //酒店编号
    private String hname; //酒店名称
    private int price; //酒店价格
    private String introduce; //酒店介绍
    private int collectcount; //酒店收藏数量

    //getter 和 setter 方法
}

HotelMapper

    //酒店管理
public interface HotelMapper{

    List<Hotel> findAllHotel();

    Hotel findOneHotel(int hid);

    void insertHotel(Hotel hotel);

    void updateHotel(Hotel hotel);

    void deleteHotel(int hid);
}

HotelService

    //酒店管理
public interface HotelService{

    List<Hotel> findAllHotel();

    Hotel findOneHotel(int hid);

    void insertHotel(Hotel Hotel);

    void updateHotel(Hotel Hotel);

    void deleteHotel(int hid);
}

HotelServiceImpl

@Service
public class HotelServiceImpl implements HotelService {

    @Autowired
    private HotelMapper hotelMapper;

    @Override
    public List<Hotel> findAllHotel() {
        return hotelMapper.findAllHotel();
    }

    @Override
    public Hotel findOneHotel(int hid) {
        return hotelMapper.findOneHotel(hid);
    }

    @Override
    public void insertHotel(Hotel hotel) {
        hotelMapper.insertHotel(hotel);
    }

    @Override
    public void updateHotel(Hotel hotel) {
        hotelMapper.updateHotel(hotel);
    }

    @Override
    public void deleteHotel(int hid) {
        hotelMapper.deleteHotel(hid);
    }
}

HotelController

@Controller
@RequestMapping("/hotel/")
public class HotelController {
    @Autowired
    private HotelService hotelService;        

    @RequestMapping(value = "findAllHotel")
    public String FindAllHotel(@RequestParam(value = "pn", defaultValue = "1")Integer pn,Model model){

        PageHelper.startPage(pn,5);
        List<Hotel> allHotel = hotelService.findAllHotel();
        model.addAttribute("allHotels",allHotel);

        PageInfo page = new PageInfo(allHotel,5);
        model.addAttribute("pageInfo",page);

        return "hotel";
    }

    @RequestMapping(value = "insertHotel")
    public String insertHotel(Hotel hotel,Model model){
        try {
            hotelService.insertHotel(hotel);
            model.addAttribute("msg","保存用户信息成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:findAllHotel";
    }

    @ResponseBody
    @RequestMapping(value = "findOneHotel")
    public Hotel findOneHotel(@RequestBody Hotel hotel){
        Hotel oneHotel = hotelService.findOneHotel(hotel.getHid());
        if(oneHotel != null){
            return oneHotel;
        }else {
            return null;
        }
    }

    @RequestMapping(value = "updateHotel")
    public String updateHotel(Hotel hotel){
        hotelService.updateHotel(hotel);
        return "redirect:findAllHotel";
    }

    @RequestMapping(value = "deleteHotel")
    public String deleteHotel(int hid){
        try {
            hotelService.deleteHotel(hid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:findAllHotel";
    }
}

HotelMapper.xml

    <!--查询所有酒店信息-->
    <select id="findAllHotel" parameterType="com.system.domain.Hotel" resultType="Hotel">
        select * from tab_hotel
    </select>

    <!--添加酒店信息-->
    <insert id="insertHotel" parameterType="Hotel">
        insert into tab_hotel(hname,price,introduce,collectcount)
        values (#{hname},#{price},#{introduce},#{collectcount})
    </insert>

    <!--查询单个酒店信息-->
    <select id="findOneHotel" resultType="Hotel" parameterType="int">
        select * from tab_hotel where hid = #{hid}
    </select>

    <!--修改酒店信息-->
    <update id="updateHotel" parameterType="Hotel">
        update tab_hotel set
            hname = #{hname},
            price = #{price},
            introduce = #{introduce},
            collectcount = #{collectcount}
            where hid = #{hid}
    </update>

    <!--删除酒店信息-->
    <delete id="deleteHotel" parameterType="int">
        delete from tab_hotel where hid = #{hid}
    </delete>

hotel.jsp

        这里使用了bootstrap框架的模态框,能够减少jsp文件的数量

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path;
%>
<html lang="en">
<head>
    <title>酒店管理</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.2/dist/js/bootstrap.bundle.min.js"></script>

    <style type="text/css">
        td, th {
            text-align: center;
            writing-mode: lr-bt;
        }
    </style>


    <script>

        //编辑模态框
        function update_user(hid) {
            if (!hid) {
                alert("错误")
            } else {
                $.ajax({
                    url: '<%=basePath%>/hotel/findOneHotel',
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json;charset=UTF-8',
                    data: JSON.stringify({
                        hid: hid
                    }),
                    success: function (data) {
                        console.log(data);
                        $("#hid").val(data.hid);
                        $("#hname2").val(data.hname);
                        $("#price2").val(data.price);
                        $("#introduce2").val(data.introduce);
                        $("#myModal1").modal('show');
                    },
                    error: function () {
                        alert("错误");
                    }
                });
            }

        }

        //删除模态框
        function delete_user(hid) {
            if (!hid) {
                alert("error");
            } else {
                $(".delSure").click(function () {
                    $.ajax({
                        url: '<%=basePath%>/hotel/deleteHotel?hid=' + hid,
                        type: 'POST',
                        success: function (data) {
                            $("body").html(data);
                        }
                    });
                });
            }
        }

        //增加模态框
        $("#addUser").click(function () {
            $("#myModal").modal({
                backdrop: "static"
            })
        })
    </script>
</head>
<body>

<div class="box">
    <h1>酒店信息</h1>
    <!-- 添加酒店模态的按钮 -->
    <button type="button" id="addUser" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#myModal">
        添加酒店信息
    </button>
    <table class="table table-bordered table-hover" style="white-space: normal">
        <thead>
        <tr>
            <th>编号</th>
            <th>酒店名称</th>
            <th>价格</th>
            <th>酒店介绍</th>
            <th>操作</th>

        </tr>
        </thead>
        <tbody>
        <tr>
            <c:forEach items="${requestScope.allHotels}" var="h">
                <tr>
                    <td>${h.hid}</td>
                    <td>${h.hname}</td>
                    <td>${h.price}</td>
                    <td>${h.introduce}</td>
                    <td>
                        <a id="updateUser" href="#" onclick="return update_user(${h.hid})" class="btn btn-default btn-sm" data-bs-toggle="modal" data-bs-target="#myModal1"  >修改</a>
                        <a id="deleteUser" href="#" onclick="return delete_user(${h.hid})" class="btn btn-default btn-sm" data-bs-toggle="modal" data-bs-target="#deleteUser2" >删除</a>
                    </td>

                </tr>
            </c:forEach>
        </tr>
        </tbody>
    </table>

    <!-- 显示分页信息 -->
    <div class="row">
        <!--分页文字信息  -->
        <div class="col-md-6">当前 ${pageInfo.pageNum }页,总${pageInfo.pages }
            页,总 ${pageInfo.total } 个酒店</div>
        <!-- 分页条信息 -->
        <div class="col-md-6">
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    <li><a href="<%=basePath%>/hotel/findAllHotel?pn=1"><h4>首页</h4></a></li>
                    <c:if test="${pageInfo.hasPreviousPage }">
                        <li><a href="<%=basePath%>/hotel/findAllHotel?pn=${pageInfo.pageNum-1}"
                               aria-label="Previous"><h4><span aria-hidden="true">&laquo;</span></h4>
                        </a></li>
                    </c:if>

                    <c:forEach items="${pageInfo.navigatepageNums }" var="page_Num">
                        <c:if test="${page_Num == pageInfo.pageNum }">
                            <h4><li class="active"><a href="#">${page_Num }</a></li></h4>
                        </c:if>
                        <c:if test="${page_Num != pageInfo.pageNum }">
                            <h4><li><a href="<%=basePath%>/hotel/findAllHotel?pn=${page_Num }">${page_Num }</a></li></h4>
                        </c:if>

                    </c:forEach>
                    <c:if test="${pageInfo.hasNextPage }">
                        <li>
                            <a href="<%=basePath%>/hotel/findAllHotel?pn=${pageInfo.pageNum+1 }"
                               aria-label="Next"> <h4><span aria-hidden="true">&raquo;</span></h4>
                            </a>
                        </li>
                    </c:if>
                    <li><a href="<%=basePath%>/hotel/findAllHotel?pn=${pageInfo.pages}"><h4>末页</h4></a></li>
                </ul>
            </nav>
        </div>
    </div>






    <!-- 添加旅游分类模态 -->
    <div class="modal" id="myModal">
        <div class="modal-dialog">
            <div class="modal-content">

                <!-- 模态标题 -->
                <div class="modal-header">
                    <h4 class="modal-title">添加酒店信息</h4>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>

                <!-- 模态主体 -->
                <form action="<%=basePath%>/hotel/insertHotel" method="post">
                    <div class="modal-body">

                        <div class="form-group">
                            <label>酒店名称:</label>
                            <input type="text" class="form-control" id="hname" name="hname" placeholder="请输入酒店名称">
                        </div>

                        <div class="form-group">
                            <label>酒店价格:</label>
                            <input type="text" class="form-control" id="price" name="price" placeholder="请输入酒店价格">
                        </div>

                        <div class="form-group">
                            <label>酒店介绍:</label>
                            <input type="text" class="form-control" id="introduce" name="introduce" placeholder="请输入酒店介绍">
                        </div>


                    </div>
                <!-- 模态页脚 -->
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary" id="emp_save_btn">保存</button>
                    <button type="button" class="btn btn-danger" data-bs-dismiss="modal">关闭</button>
                </div>
                </form>

            </div>
        </div>
    </div>
</div>



    <!-- 修改用户模态 -->
    <div class="modal" id="myModal1">
        <div class="modal-dialog">
            <div class="modal-content">

                <!-- 模态标题 -->
                <div class="modal-header">
                    <h4 class="modal-title">修改酒店信息</h4>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>

                <!-- 模态主体 -->
                <form action="<%=basePath%>/hotel/updateHotel" method="post">

                    <div class="modal-body">
                            <input type="hidden" id="hid" name="hid">
                            <div class="form-group">
                                <label>酒店名称:</label>
                                <input type="text" class="form-control" id="hname2" name="hname" placeholder="请输入酒店名称">
                            </div>

                            <div class="form-group">
                                <label>酒店价格:</label>
                                <input type="text" class="form-control" id="price2" name="price" placeholder="请输入酒店价格">
                            </div>

                            <div class="form-group">
                                <label>酒店介绍:</label>
                                <input type="text" class="form-control" id="introduce2" name="introduce" placeholder="请输入酒店介绍">
                            </div>

                    </div>

                    <!-- 模态页脚 -->
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary" id="emp_save_btn2">保存</button>
                        <button type="button" class="btn btn-danger" data-bs-dismiss="modal">关闭</button>
                    </div>
                </form>

            </div>
        </div>
    </div>


    <!-- 删除模态 -->
    <div class="modal" id="deleteUser2">
        <div class="modal-dialog">
            <div class="modal-content">

                <!-- 模态标题 -->
                <div class="modal-header">
                    <h4 class="modal-title">删除</h4>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>

                <!-- 模态主体 -->
                <div class="modal-body">
                    <strong>你确定要删除吗?</strong>
                </div>

                <!-- 模态页脚 -->
                <div class="modal-footer">
                    <button type="button" class="delSure btn btn-info" data-dismiss="modal">确定</button>
                    <button type="button" class="btn btn-danger" data-bs-dismiss="modal">取消</button>
                </div>

            </div>
        </div>
    </div>

</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值