Spring-Boot 快速使用

Spring-Boot 介绍

架构
spring boot
MyBatis-Plus
MySql
start

实体映射表

1
2
3
4
5
6
7
8
9
10
11
12
13
@Data
@EqualsAndHashCode(callSuper = false)
#表名称 配置映射表
@TableName("t_cc_*")
public class Dao implements Serializable {
/**实现序列化,实现serializable接口的作用是就是可以把对象存到字节流,然后可以恢复。网络传输需要用到,分布式系统尤其重要*/
private static final long serialVersionUID = 1L;

//设置主键自增
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
private String tableName;
private String tableColumn;

Mapper层

1
2
3
4
5
6
@Mapper
public interface Mapper extends BaseMapper<Dao> {
//自定义sql
@Select("SELECT t.id,t.Key,t.name,t.version,t.createTime,t.mark FROM t_apk t where Key=#{Key}")
Dao selectByKey(@Param("Key") String Key);
}

service实现层

TO 继承了MyBatis-Plus的Pagination类有了分页属性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@Service
public class ServiceImpl implements Service {

private static AndroidPackageMapper mapper;

@Autowired
@SuppressWarnings("SpringJavaAutowiringInspection")
public AndroidPackageServiceImpl(AndroidPackageMapper mapper){
this.mapper=mapper;
}

@Override
public ActionResult<Page> List(TO){
EntityWrapper<DO> ew = new EntityWrapper<>();
ew.where("key={0}",key)
List<DO> DOList=mapper.selectPage(TO,ew);
Page<AndroidPackageVO> page=new Page(TO.getCurrent(),TO.getSize());
page.setRecords(DOList);
page.setTotal(TO.getTotal());
ActionResult<Page> ar=new ActionResult<>();
ar.setResult(page);
return ar;
}

}

service层

使用在启动类上使用@EnableTransactionManagement注解开启事务,在单元测试中的test函数上添加 @Transactional 注解配合
@Rollback 注解让每个单元测试都能在结束时回滚

1
2
3
4
5
//开启事务
@Transactional
public interface AndroidPackageService {
ActionResult<Page> List(TO)
}

controller层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController
@RequestMapping("/api/Controller")
public class Controller {
//参数层次较多时用@RequestBody注解映射
@RequestMapping("/find")
public ResponseEntity<ActionResult> findAPKList(@RequestBody PageTO pageTO){
ActionResult<Page> result=Service.List(pageTO);
return ResponseEntity.ok(result);
}

//参数层次单一时
@RequestMapping("/delete")
public ResponseEntity<ActionResult> delete(String key){
ActionResult<String> result=Service.delete(key);
return ResponseEntity.ok(result);
}
}