栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

逐步学习SSM框架整合:Spring + SpringMVC + MyBatis(下)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

逐步学习SSM框架整合:Spring + SpringMVC + MyBatis(下)

上篇主要介绍了ssm框架整合的配置文件,接下来我们介绍具体的应用实例。应用项目借鉴了该博主的案例,但其项目时间比较早,我们采用idea并且运用当下常用的技术,制作一个简单的图书预约系统。

 项目已上传至GitHub:GitHub - caiyucheng00/ssm

逐步学习SSM框架整合:Spring + SpringMVC + MyBatis(上)

目录

1.数据库的构建

 2.创建枚举类、异常类、过程类

3.service层的创建 

4.controller层的创建

1.数据库的构建

主要有两张表,book表如下:三个字段是id,name和数量

appointment表如下:三个字段是学生id,图书id和预约时间

并且采用上篇提到的mybatis逆向工程创建bean对象和对应的mapper接口和xml文件。 

测试一下mapper类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring-config.xml"})
public class MapperTest {

    @Autowired
    BookMapper bookMapper;

    @Test
    public void testQuery(){

        List bookList = bookMapper.selectByExample(null);
        for(Book book:bookList){
            System.out.println(book);
        }
    }
}

结果如下,说明配置文件都正确,无异常:

  

 2.创建枚举类、异常类、过程类

枚举类代表了图书的预约状态,如下:

public enum AppointStateEnum {
    SUCCESS(1,"预约成功"),NO_NUMBER(0,"库存不足"),REPEAT_APPOINT(-1,"重复预约"),INNER_ERROR(-2,"系统异常");
    private int state;
    private String info;

    AppointStateEnum(int state, String info) {
        this.state = state;
        this.info = info;
    }

    public int getState() {
        return state;
    }

    public String getInfo() {
        return info;
    }
}

异常类比如预约图书时出现了没有库存的异常:

public class NoNumberException extends RuntimeException{
    public NoNumberException(String message) {
        super(message);
    }

    public NoNumberException(String message, Throwable cause) {
        super(message, cause);
    }
}

以及存储执行预约操作的返回结果,保存了图书编号,图书状态和预约信息:

public class AppointExecution {
    private Integer bookId;
    private int state;
    private String info;
    private Appointment appointment;

    public AppointExecution() {
    }

    // 预约成功
    public AppointExecution(Integer bookId, AppointStateEnum stateEnum, Appointment appointment) {
        this.bookId = bookId;
        this.state = stateEnum.getState();
        this.info = stateEnum.getInfo();
        this.appointment = appointment;
    }

    public Integer getBookId() {
        return bookId;
    }

    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public Appointment getAppointment() {
        return appointment;
    }

    public void setAppointment(Appointment appointment) {
        this.appointment = appointment;
    }

    @Override
    public String toString() {
        return "AppointExecution{" +
                "bookId=" + bookId +
                ", state=" + state +
                ", info='" + info + ''' +
                ", appointment=" + appointment +
                '}';
    }
}

3.service层的创建 

service层业务逻辑主要有两个接口与两个实现类。对于book表来说实现了查询功能和预约功能,代码如下:

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private AppointmentMapper appointmentMapper;


    @Override
    public Book getById(Integer bookId) {
        return bookMapper.selectByPrimaryKey(bookId);
    }

    @Override
    public List getAll() {
        return bookMapper.selectByExample(null);
    }

    @Override
    @Transactional
    public AppointExecution appoint(Integer bookId, long studentId) {
        try {

            // 减少库存
            Book book = bookMapper.selectByPrimaryKey(bookId);
            Integer bookCount = book.getBookCount();

            if (bookCount <= 0) {
                throw new NoNumberException("库存不足");
            } else {
                book.setBookCount(bookCount - 1);
                bookMapper.updateByPrimaryKeySelective(book);

                appointmentMapper.insert(new Appointment(studentId, bookId, new Date(new java.util.Date().getTime())));
                AppointmentExample appointmentExample = new AppointmentExample();
                appointmentExample.createCriteria().andBookIdEqualTo(bookId).andStudentIdEqualTo(studentId);
                List appointmentList = appointmentMapper.selectByExample(appointmentExample);
                return new AppointExecution(bookId, AppointStateEnum.SUCCESS, appointmentList.get(0));
            }

        } catch (NoNumberException e) {
            e.getMessage();
        } catch (Exception e) {
            throw new AppointException("other error:" + e.getMessage());
        }
        return null;
    }
}

appointment表目前只做一个查询功能:

@Service
public class AppointServiceImpl implements AppointService {
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Override
    public List getAll() {
        return appointmentMapper.selectByExample(null);
    }
}

对service层进行测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring-config.xml"})
public class ServiceTest {
    @Autowired
    private BookService bookService;
    
    @Test
    public void test(){
        AppointExecution appointExecution = bookService.appoint(5, 11118403L);
        System.out.println(appointExecution);

    }
}

结果如下:

4.controller层的创建 

最后一步来到了controller层,也就是和前端的交互,用到了@RequestMapping注解。

已/book/..为例,查找book与添加预约信息用到了RESTFul风格的url,GET方法是查找,PUT方法是新建信息。

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    private String list(Model model){
        List bookList = bookService.getAll();
        model.addAttribute("bookList",bookList);
        return "bookList";
    }


    @RequestMapping(value = "/list",method = RequestMethod.POST)
    private String add(@Param("studentId") String studentId,@Param("bookId") String bookId){
        int bookID = Integer.parseInt(bookId);
        long studentID = Long.parseLong(studentId);
        bookService.appoint(bookID,studentID);
        return "redirect:/book/list";
    }

简单的bookList.html界面如下:

 添加预定后界面重定向回bookList.html

 可以看到book_id=7的图书数量减一

 预约信息均可以在appointmentList.html看到

OK,SSM框架的搭建与应用实例到这里就结束了。其实还是比较麻烦的,所以下一步就要进行springboot的学习,在框架上再封装一次框架。 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/851686.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号