Bootstrap

SpringBoot整合ssm——图书管理系统

1.数据库建立:

在本机mysql中建立一个名为:ssm_db 数据库:

数据库建表:tbl_book

初始一下测试用的数据: 

 

2.导入起步依赖: 

新建项目: 

选择导入SpringWeb的起步依赖: 

选择导入:Mysql和Mybatis的起步依赖: 

3.项目构建: 

 

3.1在类路径下新建包:

controller控制器层:负责处理后台请求

service

业务层:向控制器层提供业务接口
dao数据层:向业务层提供对数据操作的方法
domain实体类:与数据库中的数据一一对应
exception异常类:解决异常处理

3.2在pom中导入阿里巴巴的druid数据源:

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>

</dependency>

 

3.3进行mybatis以及端口的配置编写

:在类路径下的resources包新建application.yml文件:

#端口配置
server:
  port: 80

#mybatis相关配置
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/ssm_db
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: 你数据库的用户
    password: 你数据库的密码

 

 

4.实体类编写 

 

在domain包下编写Book类,其属性与数据库中的book_tbl的属性一一对应,代码如下: 

package com.example.springboot_ssm.domain;

public class Book {
    private Integer id;
    private String type;
    private String name;
    private String description;

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", type='" + type + '\'' +
                ", name='" + name + '\'' +
                ", description='" + description + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

 

 

5.数据层编写

 

在dao包下编写BookDao类,其提供对数据库直接进行操作的方法,代码如下:

package com.example.springboot_ssm.dao;



import com.example.springboot_ssm.domain.Book;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookDao {

//    @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
    @Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
    public void save(Book book);

    @Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
    public void update(Book book);

    @Delete("delete from tbl_book where id = #{id}")
    public void delete(Integer id);

    @Select("select * from tbl_book where id = #{id}")
    public Book getById(Integer id);

    @Select("select * from tbl_book")
    public List<Book> getAll();
}

 

6.业务层编写

6.1编写业务层接口

 

在service包下编写接口:BookService内容如下:

package com.example.springboot_ssm.service;

import com.example.springboot_ssm.domain.Book;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
public interface BookService {

    /**
     * 保存
     *
     * @return
     */
    public boolean save(Book book);

    /**
     * 修改
     * @param book
     * @return
     */
    public boolean update(Book book);

    /**
     * 按id删除
     * @param id
     * @return
     */
    public boolean delete(Integer id);

    /**
     * 按id查询
     * @param id
     * @return
     */
    public Book getById(Integer id);

    /**
     * 查询全部
     * @return
     */
    public List<Book> getAll();
}

6.2编写业务层实现类  

        在service包下新建impl包,并在其中新建BookServiceImpl类:其负责调用数据层的方法以及向控制器层提供调用的业务处理方法:

package com.example.springboot_ssm.service.impl;


import com.example.springboot_ssm.dao.BookDao;
import com.example.springboot_ssm.domain.Book;
import com.example.springboot_ssm.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    public boolean save(Book book) {
        bookDao.save(book);
        return true;
    }

    public boolean update(Book book) {
        bookDao.update(book);
        return true;
    }

    public boolean delete(Integer id) {
        bookDao.delete(id);
        return true;
    }

    public Book getById(Integer id) {
        return bookDao.getById(id);
    }

    public List<Book> getAll() {
        return bookDao.getAll();
    }

}

 

 

7.控制器层编写 

 

在controller包下新建:

BookController负责处理http请求,并调用业务层方法
Code定义了一些请求处理后附加信息的返回值
ProjectExceptionAdvice做异常处理
Result封装后台返回的结果,与前端json数据做对应

 BookController代码如下:

package com.example.springboot_ssm.controller;


import com.example.springboot_ssm.domain.Book;
import com.example.springboot_ssm.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @PostMapping
    public Result save(@RequestBody Book book) {
        boolean flag = bookService.save(book);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    @PutMapping
    public Result update(@RequestBody Book book) {
        boolean flag = bookService.update(book);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = bookService.delete(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Book book = bookService.getById(id);
        Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
        String msg = book != null ? "" : "数据查询失败,请重试!";
        return new Result(code,book,msg);
    }

    @GetMapping
    public Result getAll() {
        List<Book> bookList = bookService.getAll();
        Integer code = bookList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = bookList != null ? "" : "数据查询失败,请重试!";
        return new Result(code,bookList,msg);
    }
}

 Code代码如下:

package com.example.springboot_ssm.controller;

public class Code {
    public static final Integer SAVE_OK = 20011;
    public static final Integer DELETE_OK = 20021;
    public static final Integer UPDATE_OK = 20031;
    public static final Integer GET_OK = 20041;

    public static final Integer SAVE_ERR = 20010;
    public static final Integer DELETE_ERR = 20020;
    public static final Integer UPDATE_ERR = 20030;
    public static final Integer GET_ERR = 20040;

    public static final Integer SYSTEM_ERR = 50001;
    public static final Integer SYSTEM_TIMEOUT_ERR = 50002;
    public static final Integer SYSTEM_UNKNOW_ERR = 59999;

    public static final Integer BUSINESS_ERR = 60002;

}

 

 ProjectExceptionAdvice代码如下:

package com.example.springboot_ssm.controller;

import com.example.springboot_ssm.exception.BusinessException;
import com.example.springboot_ssm.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ProjectExceptionAdvice {
    @ExceptionHandler(SystemException.class)
    public Result doSystemException(SystemException ex){
        //记录日志
        //发送消息给运维
        //发送邮件给开发人员,ex对象发送给开发人员
        return new Result(ex.getCode(),null,ex.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public Result doBusinessException(BusinessException ex){
        return new Result(ex.getCode(),null,ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result doOtherException(Exception ex){
        //记录日志
        //发送消息给运维
        //发送邮件给开发人员,ex对象发送给开发人员
        return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系统繁忙,请稍后再试!");
    }
}

 Result代码如下:

package com.example.springboot_ssm.controller;

public class Result {
    private Object data;
    private Integer code;
    private String msg;

    public Result() {
    }

    public Result(Integer code,Object data) {
        this.data = data;
        this.code = code;
    }

    public Result(Integer code, Object data, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

8.使用junit进行业务层测试 

 

在项目的test文件夹下新建service.ServiceTest测试类,对业务层做一下业务测试:

代码如下:记得对测试类使用@SpringBootTest注解,对测试方法使用@Test注解

package com.example.springboot_ssm.service;

import com.example.springboot_ssm.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class ServiceTest {
    @Autowired
    BookService bookService;
    @Test
    public void test(){
        Book book = bookService.getById(1);
        System.out.println(book);
    }
}

测试:运行测试方法

 

结果正确!

9.导入前端静态资源 

将前端的界面复制到resources下的static文件夹下面:(静态资源在本文章绑定资源的压缩包中!)

10.启动程序:

右键运行springboot程序的启动类:

服务器成功运行:上浏览器查看页面

http://localhost/pages/books.html

10.1增 

 

 

10.2删 

 

10.3查 

 

 

10.4改 

 

 

 

;