• SpringBoot项目整合


    一、创建项目

    IDEA中采用spring initialzer...创建,jdk选择8,maven,jar。。。springboot版本2.5.0(稳定)

    项目依赖:

    二、项目结构:

    原始pom.xml文件

    1. "1.0" encoding="UTF-8"?>
    2. "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. 4.0.0
    5. org.springframework.boot
    6. spring-boot-starter-parent
    7. 2.5.0
    8. com.itheima
    9. springboot_09_ssm
    10. 0.0.1-SNAPSHOT
    11. springboot_09_ssm
    12. Demo project for Spring Boot
    13. 1.8
    14. org.springframework.boot
    15. spring-boot-starter-web
    16. org.mybatis.spring.boot
    17. mybatis-spring-boot-starter
    18. 2.2.0
    19. mysql
    20. mysql-connector-java
    21. runtime
    22. org.springframework.boot
    23. spring-boot-starter-test
    24. test
    25. com.alibaba
    26. druid
    27. 1.1.16
    28. org.springframework.boot
    29. spring-boot-maven-plugin

     原始application.yml

    1. spring:
    2. datasource:
    3. type: com.alibaba.druid.pool.DruidDataSource
    4. driver-class-name: com.mysql.cj.jdbc.Driver
    5. url: jdbc:mysql://localhost:3306/www #?servierTimezone=UTC
    6. username: root
    7. password: root
    8. server:
    9. port: 80

    domain层

    用于存储实体类

    1. public class Book {
    2. private Integer id;
    3. private String type;
    4. private String name;
    5. private String description;
    6. @Override
    7. public String toString() {
    8. return "Book{" +
    9. "id=" + id +
    10. ", type='" + type + '\'' +
    11. ", name='" + name + '\'' +
    12. ", description='" + description + '\'' +
    13. '}';
    14. }
    15. public Integer getId() {
    16. return id;
    17. }
    18. public void setId(Integer id) {
    19. this.id = id;
    20. }
    21. public String getType() {
    22. return type;
    23. }
    24. public void setType(String type) {
    25. this.type = type;
    26. }
    27. public String getName() {
    28. return name;
    29. }
    30. public void setName(String name) {
    31. this.name = name;
    32. }
    33. public String getDescription() {
    34. return description;
    35. }
    36. public void setDescription(String description) {
    37. this.description = description;
    38. }
    39. }

    dao层

    用于操作数据库 

    1. @Mapper //注意mapper
    2. public interface BookDao {
    3. @Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
    4. public int save(Book book);
    5. @Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
    6. public int update(Book book);
    7. @Delete("delete from tbl_book where id = #{id}")
    8. public int delete(Integer id);
    9. @Select("select * from tbl_book where id = #{id}")
    10. public Book getById(Integer id);
    11. @Select("select * from tbl_book")
    12. public List getAll();
    13. }

    service层

    用于业务实现

    接口:

    1. @Transactional
    2. public interface BookService {
    3. /**
    4. * 保存
    5. * @param book
    6. * @return
    7. */
    8. public boolean save(Book book);
    9. /**
    10. * 修改
    11. * @param book
    12. * @return
    13. */
    14. public boolean update(Book book);
    15. /**
    16. * 按id删除
    17. * @param id
    18. * @return
    19. */
    20. public boolean delete(Integer id);
    21. /**
    22. * 按id查询
    23. * @param id
    24. * @return
    25. */
    26. public Book getById(Integer id);
    27. /**
    28. * 查询全部
    29. * @return
    30. */
    31. public List getAll();
    32. }

    类:

    1. @Service
    2. public class BookServiceImpl implements BookService {
    3. @Autowired
    4. private BookDao bookDao;
    5. public boolean save(Book book) {
    6. return bookDao.save(book) > 0;
    7. }
    8. public boolean update(Book book) {
    9. return bookDao.update(book) > 0;
    10. }
    11. public boolean delete(Integer id) {
    12. return bookDao.delete(id) > 0;
    13. }
    14. public Book getById(Integer id) {
    15. return bookDao.getById(id);
    16. }
    17. public List getAll() {
    18. return bookDao.getAll();
    19. }
    20. }

    controller层

    用于控制业务流程

    1. @RestController
    2. @RequestMapping("/books")
    3. public class BookController {
    4. @Autowired
    5. private BookService bookService;
    6. @PostMapping
    7. public Result save(@RequestBody Book book) {
    8. boolean flag = bookService.save(book);
    9. return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    10. }
    11. @PutMapping
    12. public Result update(@RequestBody Book book) {
    13. boolean flag = bookService.update(book);
    14. return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    15. }
    16. @DeleteMapping("/{id}")
    17. public Result delete(@PathVariable Integer id) {
    18. boolean flag = bookService.delete(id);
    19. return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    20. }
    21. @GetMapping("/{id}")
    22. public Result getById(@PathVariable Integer id) {
    23. Book book = bookService.getById(id);
    24. Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
    25. String msg = book != null ? "" : "数据查询失败,请重试!";
    26. return new Result(code,book,msg);
    27. }
    28. @GetMapping
    29. public Result getAll() {
    30. List bookList = bookService.getAll();
    31. Integer code = bookList != null ? Code.GET_OK : Code.GET_ERR;
    32. String msg = bookList != null ? "" : "数据查询失败,请重试!";
    33. return new Result(code,bookList,msg);
    34. }
    35. }

    tip: Code 类和 Result 和ProjectExceptionAdvice在Controller层中

    code类用于规定result返回的编码类别

    1. public class Code {
    2. public static final Integer SAVE_OK = 20011;
    3. public static final Integer DELETE_OK = 20021;
    4. public static final Integer UPDATE_OK = 20031;
    5. public static final Integer GET_OK = 20041;
    6. public static final Integer SAVE_ERR = 20010;
    7. public static final Integer DELETE_ERR = 20020;
    8. public static final Integer UPDATE_ERR = 20030;
    9. public static final Integer GET_ERR = 20040;
    10. public static final Integer SYSTEM_ERR = 50001;
    11. public static final Integer SYSTEM_TIMEOUT_ERR = 50002;
    12. public static final Integer SYSTEM_UNKNOW_ERR = 59999;
    13. public static final Integer BUSINESS_ERR = 60002;
    14. }

    result用于封装返回的结果,让后台数据进行统一化处理

    1. public class Result {
    2. private Object data;
    3. private Integer code;
    4. private String msg;
    5. public Result() {
    6. }
    7. public Result(Integer code,Object data) {
    8. this.data = data;
    9. this.code = code;
    10. }
    11. public Result(Integer code, Object data, String msg) {
    12. this.data = data;
    13. this.code = code;
    14. this.msg = msg;
    15. }
    16. public Object getData() {
    17. return data;
    18. }
    19. public void setData(Object data) {
    20. this.data = data;
    21. }
    22. public Integer getCode() {
    23. return code;
    24. }
    25. public void setCode(Integer code) {
    26. this.code = code;
    27. }
    28. public String getMsg() {
    29. return msg;
    30. }
    31. public void setMsg(String msg) {
    32. this.msg = msg;
    33. }
    34. }

    ProjectExceptionAdvice类用于规定 系统异常都抛到业务控制层,也就是controller层

    1. @RestControllerAdvice
    2. public class ProjectExceptionAdvice {
    3. @ExceptionHandler(SystemException.class)
    4. public Result doSystemException(SystemException ex){
    5. //记录日志
    6. //发送消息给运维
    7. //发送邮件给开发人员,ex对象发送给开发人员
    8. return new Result(ex.getCode(),null,ex.getMessage());
    9. }
    10. @ExceptionHandler(BusinessException.class)
    11. public Result doBusinessException(BusinessException ex){
    12. return new Result(ex.getCode(),null,ex.getMessage());
    13. }
    14. @ExceptionHandler(Exception.class)
    15. public Result doOtherException(Exception ex){
    16. //记录日志
    17. //发送消息给运维
    18. //发送邮件给开发人员,ex对象发送给开发人员
    19. return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系统繁忙,请稍后再试!");
    20. }
    21. }

    exception层

    封装异常类

    BusinessException ,业务异常

    1. public class BusinessException extends RuntimeException{
    2. private Integer code;
    3. public Integer getCode() {
    4. return code;
    5. }
    6. public void setCode(Integer code) {
    7. this.code = code;
    8. }
    9. public BusinessException(Integer code, String message) {
    10. super(message);
    11. this.code = code;
    12. }
    13. public BusinessException(Integer code, String message, Throwable cause) {
    14. super(message, cause);
    15. this.code = code;
    16. }
    17. }

    SystemException  系统异常 

    1. public class SystemException extends RuntimeException{
    2. private Integer code;
    3. public Integer getCode() {
    4. return code;
    5. }
    6. public void setCode(Integer code) {
    7. this.code = code;
    8. }
    9. public SystemException(Integer code, String message) {
    10. super(message);
    11. this.code = code;
    12. }
    13. public SystemException(Integer code, String message, Throwable cause) {
    14. super(message, cause);
    15. this.code = code;
    16. }
    17. }

    测试:

    1. @SpringBootTest
    2. public class BookServiceTest {
    3. @Autowired
    4. private BookService bookService; //测试哪个 装配哪个
    5. // @Test
    6. // public void testGetById(){
    7. // Book book = bookService.getById(2);
    8. // System.out.println(book);
    9. // }
    10. @Test
    11. public void testGetAll(){
    12. List all = bookService.getAll();
    13. System.out.println(all);
    14. }
    15. }

  • 相关阅读:
    [极客大挑战 2019]EasySQL
    .NET Core Apollo 配置中心
    dns域名解析服务和bond网卡
    挂耳式蓝牙耳机性价比推荐,盘点五款性能高的耳机分享
    thinkphp5.1 单元测试phpunit使用和常见问题
    油罐清洗抽吸系统设计
    《C++ primer plus》精炼(OOP部分)——对象和类(6)
    C语言考试题库之单选题
    实现异步转同步的几种方式
    死锁的成因及避免
  • 原文地址:https://blog.csdn.net/m0_61395860/article/details/133700156