1 Star 0 Fork 41

Thomas / Mars-Java

forked from yuye / Martian 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT


声明式API编程 (DAP) 框架

使用示例

https://github.com/yuyenews/Mars-Example

项目特性

一、声明式API

只需要在你的service的父接口上加上一个注解,即可对外提供一个接口,并且我们还支持传统的Controller写法

@MarsApi(refBean="要引用的bean的name")// 这是应对多个实现类的情况
public interface TestService {

   返回类型 selectList(TestDTO testDTO);
}

二、单表增删改查无sql

// 根据主键查询一条数据
@MarsGet(tableName = "userinfo",primaryKey = "id")
public abstract 要返回的实体类 selectById(int id);

// 单表新增
@MarsUpdate(tableName = "userinfo",operType = OperType.INSERT)
public abstract int insert(实体对象参数);

// 单表根据主键删除
@MarsUpdate(tableName = "userinfo",operType = OperType.DELETE,primaryKey = "id")
public abstract int delete(int id);

// 单表根据主键修改
@MarsUpdate(tableName = "userinfo",operType = OperType.UPDATE,primaryKey = "id")
public abstract int update(实体对象参数);

三、参数校验只需一个注解

在API接口的参数对象里的字段上加上一个注解即可(VO的字段上加注解)

// 不可为空,且长度在2-3位
@MarsDataCheck(notNull = true,maxLength = 3L,minLength = 2L, msg = "id不可为空且长度必须在2-3位之间")
private Integer id;

// 正则校验
@MarsDataCheck(reg = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$",msg = "密码不可以为空且必须是6-12位数字字母组合")
private String password;

前端如何得到提示?

正常请求API就好了,如果校验不通过会得到这样一个json

{"error_code":1128,"error_info":"提示文字"}

四、异常监听器

通常我们在写代码的时候,需要给每个Controller的方法加上try{}catch(){},用来在异常的时候,能够正常的返回 json串

spring是有一个叫ExecptionHandler 来解决这个问题,而Mars-java也提供了对应的解决方案

解决方案就是什么都不用管,如果出了异常,会自动给前端返回如下json串

{"error_code":500,"error_info":"异常提示"}

五、一行注解,解决分布式锁

在要加锁的方法上添加RedisLock注解

@RedisLock(key = "自己定义一个key")
public int insert(){
  return 1;
}
The MIT License (MIT) Copyright 2019, Yuye Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

Mars-Java是一个简单的,开箱即用的java框架,可以帮助你很快的建立后端服务 展开 收起
Java
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/figole/Mars-Java.git
git@gitee.com:figole/Mars-Java.git
figole
Mars-Java
Mars-Java
master

搜索帮助