SpringBoot 基础增删改查
1.主要代码
1.controller层
package com. logan. wiki. controller ;
import com. logan. wiki. req. CategoryQueryReq ;
import com. logan. wiki. req. DocQueryReq ;
import com. logan. wiki. req. DocSaveReq ;
import com. logan. wiki. resp. CommonResp ;
import com. logan. wiki. resp. DocQueryResp ;
import com. logan. wiki. resp. PageResp ;
import com. logan. wiki. service. DocService ;
import org. springframework. web. bind. annotation. * ;
import javax. annotation. Resource ;
import javax. validation. Valid ;
import java. util. List ;
@RestController
@RequestMapping ( "/doc" )
public class DocController {
@Resource
DocService docService;
@GetMapping ( "/all" )
public CommonResp all ( ) {
CommonResp < List < DocQueryResp > > resp = new CommonResp < > ( ) ;
List < DocQueryResp > list = docService. all ( ) ;
resp. setContent ( list) ;
return resp;
}
@GetMapping ( "/list" )
public CommonResp list ( DocQueryReq req) {
CommonResp < PageResp < DocQueryResp > > resp = new CommonResp < > ( ) ;
PageResp < DocQueryResp > list = docService. list ( req) ;
resp. setContent ( list) ;
return resp;
}
@PostMapping ( "/save" )
public CommonResp save ( @Valid @RequestBody DocSaveReq req) {
CommonResp resp = new CommonResp ( ) ;
docService. save ( req) ;
return resp;
}
@DeleteMapping ( "/delete/{id}" )
public CommonResp delete ( @PathVariable long id) {
CommonResp resp = new CommonResp ( ) ;
docService. delete ( id) ;
return resp;
}
}
2.service层
package com. logan. wiki. service ;
import com. github. pagehelper. PageHelper ;
import com. github. pagehelper. PageInfo ;
import com. logan. wiki. domain. Doc ;
import com. logan. wiki. domain. DocExample ;
import com. logan. wiki. mapper. DocMapper ;
import com. logan. wiki. req. DocQueryReq ;
import com. logan. wiki. req. DocSaveReq ;
import com. logan. wiki. resp. DocQueryResp ;
import com. logan. wiki. resp. PageResp ;
import com. logan. wiki. utils. CopyUtil ;
import com. logan. wiki. utils. SnowFlake ;
import org. springframework. stereotype. Service ;
import org. springframework. util. ObjectUtils ;
import javax. annotation. Resource ;
import java. util. List ;
@Service
public class DocService {
@Resource
DocMapper docMapper;
@Resource
SnowFlake snowFlake;
public List < DocQueryResp > all ( ) {
DocExample docExample = new DocExample ( ) ;
docExample. setOrderByClause ( "sort asc" ) ;
List < Doc > docs = docMapper. selectByExample ( docExample) ;
List < DocQueryResp > respList = CopyUtil . copyList ( docs, DocQueryResp . class ) ;
return respList;
}
public PageResp < DocQueryResp > list ( DocQueryReq req) {
PageHelper . startPage ( req. getPage ( ) , req. getSize ( ) ) ;
DocExample docExample = new DocExample ( ) ;
DocExample. Criteria criteria = docExample. createCriteria ( ) ;
if ( ! ObjectUtils . isEmpty ( req. getName ( ) ) ) {
criteria. andNameLike ( "%" + req. getName ( ) + "%" ) ;
}
List < Doc > docList = docMapper. selectByExample ( docExample) ;
List < DocQueryResp > respList = CopyUtil . copyList ( docList, DocQueryResp . class ) ;
PageInfo < Doc > pageInfo = new PageInfo < > ( docList) ;
PageResp < DocQueryResp > pageResp = new PageResp ( ) ;
pageResp. setTotal ( pageInfo. getTotal ( ) ) ;
pageResp. setList ( respList) ;
return pageResp;
}
public void save ( DocSaveReq req) {
Doc doc = CopyUtil . copy ( req, Doc . class ) ;
if ( ObjectUtils . isEmpty ( doc. getId ( ) ) ) {
doc. setId ( snowFlake. nextId ( ) ) ;
docMapper. insert ( doc) ;
} else {
docMapper. updateByPrimaryKey ( doc) ;
}
}
public void delete ( Long id) {
docMapper. deleteByPrimaryKey ( id) ;
}
}
2.附属代码
1.测试
GET http: / / 127.0 .0 .1 : 8880 / doc/ list? page= 1 & size= 100
###
GET http: / / 127.0 .0 .1 : 8880 / doc/ all
###
POST http: / / 127.0 .0 .1 : 8880 / doc/ save
Content - Type : application/ json
{
"id" : "167475311271874560" ,
"ebookId" : 1 ,
"parent" : 5 ,
"name" : "测试1" ,
"sort" : 1 ,
"viewCount" : 0 ,
"voteCount" : 0
}
###
DELETE http: / / 127.0 .0 .1 : 8880 / doc/ delete/ 167475311271874560
###
2.输入
package com. logan. wiki. req ;
public class DocQueryReq extends PageReq {
private String name;
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
@Override
public String toString ( ) {
return "DocQueryReq{} " + super . toString ( ) ;
}
}
package com. logan. wiki. req ;
import com. fasterxml. jackson. annotation. JsonFormat ;
import javax. validation. constraints. NotNull ;
public class DocSaveReq {
private Long id;
@NotNull ( message = "【电子书】不能为空" )
private Long ebookId;
@NotNull ( message = "【父文档】不能为空" )
private Long parent;
@NotNull ( message = "【名称】不能为空" )
private String name;
@NotNull ( message = "【排序】不能为空" )
private Integer sort;
private Integer viewCount;
private Integer voteCount;
public Long getId ( ) {
return id;
}
public void setId ( Long id) {
this . id = id;
}
public Long getEbookId ( ) {
return ebookId;
}
public void setEbookId ( Long ebookId) {
this . ebookId = ebookId;
}
public Long getParent ( ) {
return parent;
}
public void setParent ( Long parent) {
this . parent = parent;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public Integer getSort ( ) {
return sort;
}
public void setSort ( Integer sort) {
this . sort = sort;
}
public Integer getViewCount ( ) {
return viewCount;
}
public void setViewCount ( Integer viewCount) {
this . viewCount = viewCount;
}
public Integer getVoteCount ( ) {
return voteCount;
}
public void setVoteCount ( Integer voteCount) {
this . voteCount = voteCount;
}
@Override
public String toString ( ) {
StringBuilder sb = new StringBuilder ( ) ;
sb. append ( getClass ( ) . getSimpleName ( ) ) ;
sb. append ( " [" ) ;
sb. append ( "Hash = " ) . append ( hashCode ( ) ) ;
sb. append ( ", id=" ) . append ( id) ;
sb. append ( ", ebookId=" ) . append ( ebookId) ;
sb. append ( ", parent=" ) . append ( parent) ;
sb. append ( ", name=" ) . append ( name) ;
sb. append ( ", sort=" ) . append ( sort) ;
sb. append ( ", viewCount=" ) . append ( viewCount) ;
sb. append ( ", voteCount=" ) . append ( voteCount) ;
sb. append ( "]" ) ;
return sb. toString ( ) ;
}
}
3.输出
package com. logan. wiki. resp ;
public class CommonResp < T > {
private boolean success = true ;
private String message;
private T content;
public boolean isSuccess ( ) {
return success;
}
public void setSuccess ( boolean success) {
this . success = success;
}
public String getMessage ( ) {
return message;
}
public void setMessage ( String message) {
this . message = message;
}
public T getContent ( ) {
return content;
}
public void setContent ( T content) {
this . content = content;
}
}
package com. logan. wiki. resp ;
import java. util. List ;
public class PageResp < T > {
private long total;
private List < T > list;
public long getTotal ( ) {
return total;
}
public void setTotal ( long total) {
this . total = total;
}
public List < T > getList ( ) {
return list;
}
public void setList ( List < T > list) {
this . list = list;
}
@Override
public String toString ( ) {
return "PageResp{" +
"total=" + total +
", list=" + list +
'}' ;
}
}
package com. logan. wiki. resp ;
import com. fasterxml. jackson. annotation. JsonFormat ;
public class DocQueryResp {
@JsonFormat ( shape = JsonFormat. Shape . STRING)
private Long id;
private Long ebookId;
private Long parent;
private String name;
private Integer sort;
private Integer viewCount;
private Integer voteCount;
public Long getId ( ) {
return id;
}
public void setId ( Long id) {
this . id = id;
}
public Long getEbookId ( ) {
return ebookId;
}
public void setEbookId ( Long ebookId) {
this . ebookId = ebookId;
}
public Long getParent ( ) {
return parent;
}
public void setParent ( Long parent) {
this . parent = parent;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public Integer getSort ( ) {
return sort;
}
public void setSort ( Integer sort) {
this . sort = sort;
}
public Integer getViewCount ( ) {
return viewCount;
}
public void setViewCount ( Integer viewCount) {
this . viewCount = viewCount;
}
public Integer getVoteCount ( ) {
return voteCount;
}
public void setVoteCount ( Integer voteCount) {
this . voteCount = voteCount;
}
@Override
public String toString ( ) {
StringBuilder sb = new StringBuilder ( ) ;
sb. append ( getClass ( ) . getSimpleName ( ) ) ;
sb. append ( " [" ) ;
sb. append ( "Hash = " ) . append ( hashCode ( ) ) ;
sb. append ( ", id=" ) . append ( id) ;
sb. append ( ", ebookId=" ) . append ( ebookId) ;
sb. append ( ", parent=" ) . append ( parent) ;
sb. append ( ", name=" ) . append ( name) ;
sb. append ( ", sort=" ) . append ( sort) ;
sb. append ( ", viewCount=" ) . append ( viewCount) ;
sb. append ( ", voteCount=" ) . append ( voteCount) ;
sb. append ( "]" ) ;
return sb. toString ( ) ;
}
}
4.工具
package com. logan. wiki. utils ;
import org. springframework. stereotype. Component ;
import java. text. ParseException ;
@Component
public class SnowFlake {
private final static long START_STMP = 1609459200000L ;
private final static long SEQUENCE_BIT = 12 ;
private final static long MACHINE_BIT = 5 ;
private final static long DATACENTER_BIT = 5 ;
private final static long MAX_DATACENTER_NUM = - 1L ^ ( - 1L << DATACENTER_BIT) ;
private final static long MAX_MACHINE_NUM = - 1L ^ ( - 1L << MACHINE_BIT) ;
private final static long MAX_SEQUENCE = - 1L ^ ( - 1L << SEQUENCE_BIT) ;
private final static long MACHINE_LEFT = SEQUENCE_BIT;
private final static long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
private final static long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT;
private long datacenterId = 1 ;
private long machineId = 1 ;
private long sequence = 0L ;
private long lastStmp = - 1L ;
public SnowFlake ( ) {
}
public SnowFlake ( long datacenterId, long machineId) {
if ( datacenterId > MAX_DATACENTER_NUM || datacenterId < 0 ) {
throw new IllegalArgumentException ( "datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0" ) ;
}
if ( machineId > MAX_MACHINE_NUM || machineId < 0 ) {
throw new IllegalArgumentException ( "machineId can't be greater than MAX_MACHINE_NUM or less than 0" ) ;
}
this . datacenterId = datacenterId;
this . machineId = machineId;
}
public synchronized long nextId ( ) {
long currStmp = getNewstmp ( ) ;
if ( currStmp < lastStmp) {
throw new RuntimeException ( "Clock moved backwards. Refusing to generate id" ) ;
}
if ( currStmp == lastStmp) {
sequence = ( sequence + 1 ) & MAX_SEQUENCE;
if ( sequence == 0L ) {
currStmp = getNextMill ( ) ;
}
} else {
sequence = 0L ;
}
lastStmp = currStmp;
return ( currStmp - START_STMP) << TIMESTMP_LEFT
| datacenterId << DATACENTER_LEFT
| machineId << MACHINE_LEFT
| sequence;
}
private long getNextMill ( ) {
long mill = getNewstmp ( ) ;
while ( mill <= lastStmp) {
mill = getNewstmp ( ) ;
}
return mill;
}
private long getNewstmp ( ) {
return System . currentTimeMillis ( ) ;
}
public static void main ( String [ ] args) throws ParseException {
SnowFlake snowFlake = new SnowFlake ( 1 , 1 ) ;
long start = System . currentTimeMillis ( ) ;
for ( int i = 0 ; i < 10 ; i++ ) {
System . out. println ( snowFlake. nextId ( ) ) ;
System . out. println ( System . currentTimeMillis ( ) - start) ;
}
}
}
package com. logan. wiki. utils ;
import org. springframework. beans. BeanUtils ;
import org. springframework. util. CollectionUtils ;
import java. util. ArrayList ;
import java. util. List ;
public class CopyUtil {
public static < T > T copy ( Object source, Class < T > clazz) {
if ( source == null ) {
return null ;
}
T obj = null ;
try {
obj = clazz. newInstance ( ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
return null ;
}
BeanUtils . copyProperties ( source, obj) ;
return obj;
}
public static < T > List < T > copyList ( List source, Class < T > clazz) {
List < T > target = new ArrayList < > ( ) ;
if ( ! CollectionUtils . isEmpty ( source) ) {
for ( Object c : source) {
T obj = copy ( c, clazz) ;
target. add ( obj) ;
}
}
return target;
}
}