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

[框架整合]spring+springmvc+mybatis(4)

程序员文章站 2022-06-13 20:50:11
...

在上篇博文中,我们已经将最简单的spring+springmvc+mybatis整合完了。
接下来就是进行简单的测试。


想要源代码的朋友点击这里下载哦~~


我在本地已经有数据库表,因为该项目是实现简单的秒杀系统。所有测试时简单的写一个查询接口进行测试。

创建package包
[框架整合]spring+springmvc+mybatis(4)

在dao包下创建类SeckillDao.java

package org.seckill.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.seckill.entity.Seckill;

/** * 秒杀 数据访问层 Dao * @author antgan * @datetime 2016/6/6 * */
public interface SeckillDao {
    /** * 查询全部秒杀商品 * @param offet * @param limit * @return */
    public List<Seckill> queryAll(@Param("offset") int offset, @Param("limit") int limit);
}

接下来在resource目录下的mapper文件夹下创建实现SeckillDao接口的配置文件。
(这里是我觉得mybatis最赞的地方。我再也不用我写实现类了)
如下图:
[框架整合]spring+springmvc+mybatis(4)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 目的:为DAO接口方法提供sql配置 -->
<!-- 具体的mybatis使用方法,这里不做介绍,有兴趣的朋友可以学习mybatis -->
<mapper namespace="org.seckill.dao.SeckillDao">
    <select id="queryAll" resultType="Seckill" >
        select seckill_id, name, number, start_time,end_time, create_time 
        from seckill 
        order by create_time desc 
        limit #{offset}, #{limit} 
    </select>
</mapper>

在service包下创建简单业务接口SeckillService.java
并在service.impl包下实现接口SeckillServiceImpl.java

[框架整合]spring+springmvc+mybatis(4)

1) SeckillService.java 接口

package org.seckill.service;

import java.util.List;
import org.seckill.entity.Seckill;

/** * 秒杀业务 * @author antgan * @datetime 2016/6/6 * */
public interface SeckillService {
    /** * 获取秒杀货品列表 * @return */
    public List<Seckill> getSeckillList();
}

2) SeckillServiceImpl.java 业务接口实现类

package org.seckill.service.impl;

import java.util.List;
import org.seckill.dao.SeckillDao;
import org.seckill.entity.Seckill;
import org.seckill.service.SeckillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/** * 秒杀 业务逻辑层 * * @author antgan * @datetime 2016/6/6 * */
@Service("seckillService")
public class SeckillServiceImpl implements SeckillService {
    // 日志
    private Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());
    //自动从SpringIOC容器中注入
    @Autowired
    private SeckillDao seckillDao;

    /** * 获取秒杀列表 */
    public List<Seckill> getSeckillList() {
        return seckillDao.queryAll(0, 4);
    }
}

3) Seckill.java 实体类

package org.seckill.entity;

import java.util.Date;

/** * 秒杀实体 * @author antgan */
public class Seckill {
    private long seckillId;
    private String name;
    private int number;
    private Date startTime;
    private Date endTime;
    private Date createTime;

    public Seckill() {
        // TODO Auto-generated constructor stub
    }

    public Seckill(long seckillId, String name, int number, Date startTime, Date endTime, Date createTime) {
        this.seckillId = seckillId;
        this.name = name;
        this.number = number;
        this.startTime = startTime;
        this.endTime = endTime;
        this.createTime = createTime;
    }
    public long getSeckillId() {
        return seckillId;
    }
    public void setSeckillId(long seckillId) {
        this.seckillId = seckillId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public Date getStartTime() {
        return startTime;
    }
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }
    public Date getEndTime() {
        return endTime;
    }
    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "Seckill [seckillId=" + seckillId + ", name=" + name + ", number=" + number + ", startTime=" + startTime
                + ", endTime=" + endTime + ", createTime=" + createTime + "]";
    }
}

最后,在web包下创建SeckillController.java

package org.seckill.web;

import java.util.List;
import org.seckill.entity.Seckill;
import org.seckill.service.SeckillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/** * controller * * @author antgan * @datetime 2016/6/6 * */
@Controller
@RequestMapping("/seckill")
public class SeckillController {
    @Autowired
    private SeckillService seckillService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) {
        List<Seckill> list = seckillService.getSeckillList();
        model.addAttribute("list", list);
        return "list";//返回jsp文件夹下的list.jsp
    }
}

最后测试,请求地址为 http://localhost:8080/seckill/seckill/list
[框架整合]spring+springmvc+mybatis(4)

完美收官!

在整合的过程中遇到什么问题,欢迎留言。

转载于:https://my.oschina.net/antgan/blog/697219