# 基于 SpringBoot 的 SSM 整合案例
- 代码演示
# controller 目录
# BookControoler
@SuppressWarnings("all") | |
@RestController | |
@RequestMapping("/books") | |
public class BookController { | |
@Autowired | |
private BookService bookService; | |
@GetMapping | |
public Result getAll(){ | |
List<Book> list = bookService.getAll(); | |
Integer code = list != null ? Code.SELECT_OK:Code.SELECT_ERR; | |
String msg = list != null ? "执行成功":"执行失败"; | |
return new Result(list,code,msg); | |
} | |
@PostMapping | |
public Result save(@RequestBody Book b){ | |
boolean f = bookService.save(b); | |
Integer code = f ? Code.SAVE_OK:Code.SAVE_ERR; | |
String msg = f ? "执行成功":"执行失败"; | |
return new Result(f,code,msg); | |
} | |
@PutMapping | |
public Result update(@RequestBody Book b){ | |
boolean f = bookService.update(b); | |
Integer code = f ? Code.UPDATE_OK:Code.UPDATE_ERR; | |
String msg = f ? "执行成功":"执行失败"; | |
return new Result(f,code,msg); | |
} | |
@DeleteMapping("/{id}") | |
public Result delete(@PathVariable Integer id){ | |
boolean f = bookService.delete(id); | |
Integer code = f ? Code.DELETE_OK:Code.DELETE_ERR; | |
String msg = f ? "执行成功":"执行失败"; | |
return new Result(f,code,msg); | |
} | |
@GetMapping("/{id}") | |
public Result getById(@PathVariable Integer id){ | |
Book book = bookService.getById(id); | |
Integer code = book != null ? Code.SELECT_OK:Code.SELECT_ERR; | |
String msg = book != null ? "执行成功":"执行失败"; | |
return new Result(book,code,msg); | |
} | |
} |
# Code
public class Code { | |
public static final Integer SAVE_OK = 20011; | |
public static final Integer DELETE_OK = 20011; | |
public static final Integer UPDATE_OK = 20011; | |
public static final Integer SELECT_OK = 20011; | |
public static final Integer SAVE_ERR = 20010; | |
public static final Integer DELETE_ERR = 20010; | |
public static final Integer UPDATE_ERR = 20010; | |
public static final Integer SELECT_ERR = 20010; | |
public static final Integer SYSTEM_ERR = 50011; | |
public static final Integer BUSINESS_ERR = 50012; | |
public static final Integer SYSTEM_TIMEOUT_ERR = 50013; | |
public static final Integer SYSTEM_UNKNO_ERR = 5999; | |
} |
# ProjectExceptionAdvice
@SuppressWarnings("all") | |
@RestControllerAdvice | |
public class ProjectExceptionAdvice { | |
@ExceptionHandler(SystemException.class) | |
public Result doSystemException(SystemException e){ | |
return new Result(null,e.getCode(),e.getMessage()); | |
} | |
} |
# Result
@SuppressWarnings("all") | |
public class Result { | |
private Object data; | |
private Integer code; | |
private String msg; | |
public Result(Object data, Integer code, String msg) { | |
this.data = data; | |
this.code = code; | |
this.msg = msg; | |
} | |
public Result(Object data, Integer code) { | |
this.data = data; | |
this.code = code; | |
} | |
public Result() { | |
} | |
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; | |
} | |
} |
# dao 目录
# BookMapper
@SuppressWarnings("all") | |
@Mapper | |
public interface BookMapper { | |
@Insert("insert into book (type,name,description) values (#{type},#{name},#{description})") | |
int save(Book b); | |
@Update("update book set (type=#{type},name=#{name},description=#{description} where id=#{id})") | |
int update(Book b); | |
@Delete("delete from book where id=#{id}") | |
int delete(Integer id); | |
@Select("select * from book where id=#{id}") | |
Book getById(Integer id); | |
@Select("select * from book") | |
List<Book> getAll(); | |
} |
# domain 目录结构
# Book
public class Book{ | |
private Integer id; | |
private String type; | |
private String name; | |
private String description; | |
public Book(Integer id,String type,String name,String description){ | |
this.id = id; | |
this.type = type; | |
this.name = name; | |
this.description = description; | |
} | |
public void setId(Integer id){ | |
this.id = id; | |
} | |
public Integer getId(){ | |
return id; | |
} | |
public void setType(String type){ | |
this.type = type; | |
} | |
public String getType(){ | |
return type; | |
} | |
public void setName(String name){ | |
this.name = name; | |
} | |
public String getName(){ | |
return name; | |
} | |
public void setDescription(String description){ | |
this.description = description; | |
} | |
public String getDescription(){ | |
return description; | |
} | |
@Override | |
public String toString() { | |
return "Book{" + | |
"id=" + id + | |
", type='" + type + '\'' + | |
", name='" + name + '\'' + | |
", description='" + description + '\'' + | |
'}'; | |
} | |
} |
# exception 目录
# SystemException
@SuppressWarnings("all") | |
public class SystemException extends RuntimeException{ | |
private Integer code; | |
public SystemException(Integer code) { | |
this.code = code; | |
} | |
public SystemException(String message, Integer code) { | |
super(message); | |
this.code = code; | |
} | |
public SystemException(String message, Throwable cause, Integer code) { | |
super(message, cause); | |
this.code = code; | |
} | |
public SystemException(Throwable cause, Integer code) { | |
super(cause); | |
this.code = code; | |
} | |
public SystemException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, Integer code) { | |
super(message, cause, enableSuppression, writableStackTrace); | |
this.code = code; | |
} | |
public Integer getCode() { | |
return code; | |
} | |
public void setCode(Integer code) { | |
this.code = code; | |
} | |
} |
# service 目录与 impl
# BookService
@SuppressWarnings("all") | |
public interface BookService { | |
/** | |
* 保存用户信息 | |
* @param b | |
* @return | |
*/ | |
boolean save(Book b); | |
/** | |
* 更新用户信息 | |
* @param b | |
* @return | |
*/ | |
boolean update(Book b); | |
/** | |
* 按 id 删除用户信息 | |
* @param id | |
* @return | |
*/ | |
boolean delete(Integer id); | |
/** | |
* 按用户 id 查询用户信息 | |
* @param id | |
* @return | |
*/ | |
Book getById(Integer id); | |
/** | |
* 查询所有用户信息 | |
* @return | |
*/ | |
List<Book> getAll(); | |
} |
# BookServiceimpl
@SuppressWarnings("all") | |
@Service | |
public class BookServiceImpl implements BookService { | |
@Autowired | |
private BookMapper bookMapper; | |
public boolean save(Book b){ | |
int i = bookMapper.save(b); | |
if(i > 0) | |
return true; | |
return false; | |
} | |
public boolean update(Book b){ | |
int i = bookMapper.update(b); | |
if(i > 0) | |
return true; | |
return false; | |
} | |
public boolean delete(Integer id){ | |
int i = bookMapper.delete(id); | |
if(i > 0) | |
return true; | |
return false; | |
} | |
public Book getById(Integer id){ | |
return bookMapper.getById(id); | |
} | |
public List<Book> getAll(){ | |
return bookMapper.getAll(); | |
} | |
} |
# 启动类
@SpringBootApplication | |
public class App { | |
public static void main( String[] args ) { | |
SpringApplication.run(App.class,args); | |
} | |
} |