项目结构:
Controller层:
Controller层负责具体的业务模块流程的控制
BrandController:
package example_itliu.springboot_03_ssm.controller;
import example_itliu.springboot_03_ssm.domain.Brand;
import example_itliu.springboot_03_ssm.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
//标志为控制器类
("/brands")
public class BrandController {
private BrandService brandService;
public Result save( Brand brand) {
boolean flag = brandService.save(brand);
return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
}
public Result update( Brand brand) {
boolean flag = brandService.update(brand);
return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
}
("/{id}")
public Result delete( int id) {
boolean flag = brandService.delete(id);
return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
}
("/{id}")
public Result getById( int id) {
Brand brand = brandService.getById(id);
int code = brand != null ? Code.GET_OK : Code.GET_ERR;
String msg = brand != null ? " " : "数据查询失败,请重试";
return new Result(code, brand, msg);
}
public Result getAll() {
// int i = 1 / 0;
List<Brand> brandList = brandService.getAll();
int code = brandList != null ? Code.GET_OK : Code.GET_ERR;
String msg = brandList != null ? " " : "数据查询失败,请重试";
return new Result(code, brandList, msg);
}
}
code编码类,前后端交互信息:
package example_itliu.springboot_03_ssm.controller;
//异常代码
public class Code {
//测试成功返回编码
public static final int SAVE_OK = 20011;
public static final int DELETE_OK = 20021;
public static final int UPDATE_OK = 20031;
public static final int GET_OK = 20041;
//测试失败返回编码
public static final int SAVE_ERR = 20010;
public static final int DELETE_ERR = 20020;
public static final int UPDATE_ERR = 20030;
public static final int GET_ERR = 20040;
//开发过程异常码
public static final int SYSTEM_ERR = 50001;
public static final int BUSINESS_ERR = 50002;
public static final int TIME_OUT_ERR = 50003;
public static final int UNKNOWN_ERR = 59999;
}
异常处理:
ProjectExceptionAdvice
package example_itliu.springboot_03_ssm.controller;
//异常处理类
import example_itliu.springboot_03_ssm.exception.BusinessException;
import example_itliu.springboot_03_ssm.exception.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
//类注解,为rest风格开发的控制器类做增强
public class ProjectExceptionAdvice {
(BusinessException.class) //方法注解,拦截异常,也可以获得返回值
public Result BusinessException(BusinessException ex) {
//开发过程
//记录日志
//发送消息给运维
//发送邮件给开发人员
System.out.println("异常报错");
return new Result(ex.getCode(), null, ex.getMessage());
}
(SystemException.class) //方法注解,拦截异常,也可以获得返回值
public Result SystemException(SystemException ex) {
//开发过程
//记录日志
//发送消息给运维
//发送邮件给开发人员
System.out.println("异常报错");
return new Result(ex.getCode(), null, ex.getMessage());
}
(Exception.class) //方法注解,拦截异常,也可以获得返回值
public Result doException(Exception ex) {
System.out.println("异常报错");
return new Result(666, null, "异常报错");
}
}
该类为状态码交互类,表现层数据封装
Result:
//该类为状态码交互类,表现层数据封装
public class Result {
private Object data;
private int code;
private String msg;
public Result() {
}
public Result(int code, Object data) {
this.data = data;
this.code = code;
}
public Result(int 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 int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
dao层:数据库的增删改查操作
BrandDao接口:
package example_itliu.springboot_03_ssm.dao;
import example_itliu.springboot_03_ssm.domain.Brand;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface BrandDao {
("insert into tb_brand(id,brand_name,company_name,ordered,description,status) values(#{id},#{brand_name},#{company_name},#{ordered},#{description},#{status})")
int save(Brand brand); //这里的void变成int作为判断,因为数据库变动会生成影响行数,int接收之后就可以作为判断后台插入语句是否有错误
("update tb_brand set brand_name=#{brand_name},company_name=#{company_name},ordered=#{ordered},description=#{description},status=#{status} where id=#{id}")
int update(Brand brand);
("delete from tb_brand where id=#{id}")
int delete(int id);
("select * from tb_brand where id=#{id}")
Brand getById(int id);
("select * from tb_brand")
List<Brand> getAll();
}
domain层:javaBean存放目录
Brand
package example_itliu.springboot_03_ssm.domain;
public class Brand {
private int id;
private String brand_name;
private String company_name;
private int ordered;
private String description;
private int status;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBrand_name() {
return brand_name;
}
public void setBrand_name(String brand_name) {
this.brand_name = brand_name;
}
public String getCompany_name() {
return company_name;
}
public void setCompany_name(String company_name) {
this.company_name = company_name;
}
public int getOrdered() {
return ordered;
}
public void setOrdered(int ordered) {
this.ordered = ordered;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String toString() {
return "Brand{" +
"id=" + id +
", brand_name='" + brand_name + '\'' +
", company_name='" + company_name + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
exception层:异常处理层
BusinessException:
package example_itliu.springboot_03_ssm.exception;
//业务异常处理
public class BusinessException extends RuntimeException {
private final int code;
public int getCode() {
return code;
}
public BusinessException(int code, String message ) {
super(message);
this.code = code;
}
public BusinessException( int code,String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
SystemException:系统异常
package example_itliu.springboot_03_ssm.exception;
//系统异常处理
public class SystemException extends RuntimeException {
private final int code;
public int getCode() {
return code;
}
public SystemException(int code, String message) {
super(message);
this.code = code;
}
public SystemException(int code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
Service层:
Service层主要负责业务模块的逻辑。一般service层我们会先写一个interface,这个接口以service为后缀表示这是一个service接口,在这个类里定义好我们需要的方法,然后写实现类去实现这个接口里的方法,这样可以做到高度解耦合
BrandService:
package example_itliu.springboot_03_ssm.service;
import example_itliu.springboot_03_ssm.domain.Brand;
import java.util.List;
public interface BrandService {
boolean save(Brand brand); //保存
boolean update(Brand brand); //更新
boolean delete(int id); //删除
Brand getById(int id); //查询id
List<Brand> getAll(); //查询所有
}
BrandServiceImpl:
package example_itliu.springboot_03_ssm.service.impl;
import example_itliu.springboot_03_ssm.controller.Code;
import example_itliu.springboot_03_ssm.dao.BrandDao;
import example_itliu.springboot_03_ssm.domain.Brand;
import example_itliu.springboot_03_ssm.exception.BusinessException;
import example_itliu.springboot_03_ssm.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
public class BrandServiceImpl implements BrandService {
private BrandDao brandDao;
public boolean save(Brand brand) {
return brandDao.save(brand) > 0; //返回值变成数值表示数据库影响行级数是否大于0作为操作成功或者失败
}
public boolean update(Brand brand) {
return brandDao.update(brand) > 0;
}
public boolean delete(int id) {
return brandDao.delete(id) > 0;
}
public Brand getById(int id) {
if (id == 1) {
throw new BusinessException(Code.BUSINESS_ERR, "业务层异常");
}
// try {
// int i = 1 / 0;
// } catch (Exception e) {
// throw new SystemException(Code.SYSTEM_ERR, "系统异常", e); //抛出异常
// }
return brandDao.getById(id);
}
public List<Brand> getAll() {
return brandDao.getAll();
}
}
static存放静态资源:可以设置导航页,当端口为80时,导航地址为localhost
application.yml :数据库连接信息,服务器接口配置
pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>example_itliu</groupId>
<artifactId>springboot_03_ssm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_03_ssm</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--载入druid数据源,[优势](https://blog.csdn.net/mulinsen77/article/details/87778601)-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>