使用SSM框架完成后台管理项目开发——分页查询商品、增加、删除、修改商品(二)

使用SSM框架完成后台管理项目开发——分页查询商品、增加、删除、修改商品(二)

我的商品是游戏

创建商品表

create table game(
       g_id int primary key,--编号
       g_name varchar2(200),--游戏名字
       g_price varchar2(200),--价格
       g_date varchar2(200),--发行日期
);

编写商品实体类

封装商品属性、编写getter、setter,重写toString,无参、有参构造方法

	int gameId;//游戏id
    String gameName;//游戏名称
    String gamePrice;//游戏价格
    String gameDate;//发行日期
    int pageNum;//当前页数
    private int pageSize;//查询记录
    private int totalSize;//总条数

在mapper.xml文件下编写结果集映射和sql语句

	<!--编写映射结果集-->
    <resultMap id="GameMap" type="com.test.entity.Game">
        <id column="g_id" property="gameId"></id>
        <result column="g_name" property="gameName"></result>
        <result column="g_price" property="gamePrice"></result>
        <result column="g_date" property="gameDate"></result>
    </resultMap>

sql语句

	<!--分页查询-->
    <select id="selectByPage" resultMap="GameMap">
        select * from(
          select rownum rn,g.* from game g order by g_id
        ) where rn &lt; #{end} and rn > #{first}
    </select>
    <!--查询(获取商品总条数)-->
    <select id="selectTotalSize" resultType="int">
        SELECT count(*) FROM game
    </select>
    <!--根据商品id查询商品-->
    <select id="selectGameById" resultMap="GameMap">
        SELECT * FROM game WHERE g_id = #{gameId}
    </select>
    <!--添加商品-->
    <insert id="addGame">
        insert into game values(seq_game.nextval,#{gameName},#{gamePrice},#{gameDate})
    </insert>
    <!--修改商品-->
    <update id="updateGame">
        update game set g_name = #{gameName},g_price = #{gamePrice},g_date = #{gameDate} where g_id = #{gameId}
    </update>
    <!--删除商品-->
    <delete id="deleteGame">
       delete FROM game where g_id = #{gameId}
    </delete>

编写dao层、service层,serviceImpl层

因为代码基本相同,所以以分页显示所有商品为例

	//dao层
	//分页查询
    public List<Game> selectByPage(@Param("end") int end, @Param("first") int first);
    
	//service层
	//分页查询
    public List<Game> selectByPage(int pageIndex, int pageSize);

	//serviceImpl层
	/**
     * 分页查询
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public List<Game> selectByPage(int pageIndex, int pageSize) {
        int first = (pageIndex - 1) * pageSize;
        int end = pageIndex * pageSize + 1;
        return gameDao.selectByPage(end,first);
    }

编写Controller层

 	/**
     * 分页显示商品
     * @param model
     * @param index
     * @param size
     * @return
     */
    @RequestMapping("/selectByPage")
    public String selectByPage(Model model, @RequestParam(value = "index",defaultValue = "1")int index, @RequestParam(value = "size",defaultValue = "8") int size){
        //赋值当前页、页面大小
        int pageIndex = index;
        int pageSize = size;
        //调用service层方法
        List<Game> gameList = gameService.selectByPage(pageIndex, pageSize);
        int totalSize = gameService.selectTotalSize();
        //定义尾页
        int last = 0;
        //计算尾页
        if(totalSize % pageSize == 0){
            last = totalSize / pageSize;
        }else if(totalSize % pageSize != 0){
            last = totalSize / pageSize + 1;
        }
        //将数据存储到作用域中
        model.addAttribute("gameList",gameList);
        model.addAttribute("pageIndex",pageIndex);
        model.addAttribute("pageSize",pageSize);
        model.addAttribute("totalSize",totalSize);
        model.addAttribute("last",last);
        return "forward:/tables.jsp";
    }

tables.jsp是我的分页显示用户页面

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值