6.2 KiB
6.2 KiB
Yo
运行环境需求
IDEA 开发环境需求
由于项目依赖org.projectlombok:lombok:1.18.2
,而Lombok
在1.18.4
版本做了重设计,不兼容之前版本,因此IDEA
的Lombok
插件必须是版本lombok-plugin-0.23
,这里比较坑,如果升级到版本1.18.4
以后,插件还是得升级,而且很多API都要改。
插件下载地址:https://pan.caimi-inc.com/d/b038610394/
下载对应版本插件,比如IDEA 2018.2.x
下载lombok-plugin-0.23-2018.2.zip
,卸载IDEA
已安装Lombok
插件,选择磁盘安装,如下图:
启动SpringBoot项目
建议先执行mvn编译命令,比如编译模块CMS
mvn clean compile -am -pl cms -T 2C
Tigon
Controller
如果加注了@ResponseBody
或者@RestController
,不做变动,依循Spring MVC
方式JSON
序列化返回数据,否则返回Wacai
接口规范数据格式,如下例:
@ResponseBody
@ResponseBody
@RequestMapping("/resp-body")
public String respBody() {
return "Response Body";
}
响应输出
Response Body
Wacai JSON API
@RequestMapping("/resp-body")
public String respBody() {
return "Response Body";
}
响应输出
{
"code": 0,
"success": true,
"data": "Response Body"
}
简单来说就是,不加@ResponseBody
注解,默认会以Wacai
API规范响应数据结果。
参见以下样例:
I.
@RequestMapping("/count")
public int count() {
return 1024;
}
响应输出
{
"code": 0,
"success": true,
"data": 1024
}
II.
@RequestMapping("/user-info")
public Map<String, Object> userInfo() {
final Map<String, Object> user = new HashMap<>(4);
user.put("name", "Uncle Donghuang");
user.put("gender", "MALE");
user.put("mobile", "17161787481");
user.put("active", true);
return user;
}
响应输出
{
"code": 0,
"success": true,
"data": {
"name": "Uncle Donghuang",
"gender": "MALE",
"mobile": "17161787481",
"active": true,
}
}
III.
@RequestMapping("/error-caused")
public Map<String, Object> errorCaused() {
throw new RuntimeException("Oops, some error caused");
}
响应输出
{
"code": 5000,
"success": false,
"message": "Oops, some error caused"
}
MyBatis
MyBatis
做了启动期增强,实例Mapper
继承了BaseMapper
之后,会继承获得相关的增删改查等方法,如下例:
Model
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
import com.wacai.tigon.model.M0;
import com.wacai.tigon.mybatis.Table;
import com.wacai.tigon.mybatis.NotUpdate;
import lombok.experimental.FieldNameConstants;
@Getter
@Setter
@Table("yo_customer")
@FieldNameConstants(prefix = "")
public class Customer extends M0<Integer> {
private String name;
private String account;
private String password;
private String note;
@NotUpdate
protected Date createdTime;
protected Date updatedTime;
@NotUpdate
protected String createdBy;
protected String updatedBy;
protected boolean active;
}
Mapper
import com.wacai.tigon.mybatis.BaseMapper;
public interface CustomerMapper extends BaseMapper<Integer, Customer> {
}
Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wacai.loan.yo.mapper.CustomerMapper">
</mapper>
Usage
@Autowired
private CustomerMapper customerMapper;
I. 插入
final Customer customer = new Customer();
customer.setName("Uncle Donghuang");
customer.setAccount("donghuang");
customer.setPassword("qR$#FzM!z9W*");
customer.setCreatedBy("SYS");
customer.setCreatedTime(new Date());
customer.setActive(true);
customerMapper.insert(customer);
II. 查询
Find by ID
final Integer id = 1154;
final Customer customer = customerMapper.find(id);
Find by Search
final Customer customer = customerMapper.find(
new Search(Customer.ACCOUNT, "donghuang")
.eq(Customer.ACTIVE, true));
List by Search
final List<Customer> customers = customerMapper.list(
new Search(Customer.ACTIVE, true)
.between(Customer.ID, 1, 1154)
.asc(Customer.ACCOUNT)
.limit(42));
Search
API
and
And anotherSearch
asc
Order ASCbetween
Between two valuescontains
Value contains stringdesc
Order DSCendsWith
Value ends with stringeq
Eqaulsgt
Greater thangte
Eqauls or greater thanin
In valuesisNull
Value is nulllike
Value likelimit
Return rows limitlt
Less thanlte
Eqauls or less thanne
Not equalsnotIn
Not in valuesnotNull
Value is not nulloffset
Return rows offsetor
Or anotherSearch
orderBy
Order bystartsWith
Value starts with string
III. 更新
Update model
Customer customer = customerMapper.find(
new Search(Customer.ACCOUNT, "donghuang")
.eq(Customer.ACTIVE, true));
customer.setPassword("g!5KpWdXEB!^");
customer.setUpdatedBy("SYS");
customer.setUpdatedTime(new Date());
customerMapper.update(customer);
Update with map
final Map<String, Object> update = new HashMap<>(4);
update.put(Customer.PASSWORD, "g!5KpWdXEB!^");
update.put(Customer.UPDATED_BY, "SYS");
update.put(Customer.UPDATED_TIME, new Date());
customerMapper.update(update, 1154);
// OR
// customerMapper.update(update, new Search(Customer.ID, 1154));
Set null
// Update note to null of customer 1154
customerMapper.setNull(Customer.NOTE, 1154);
// Update note to null of customer 1154
customerMapper.setNull(Customer.NOTE, new Search(Customer.ID, 1154));
// Update note to null of all
customerMapper.setNull(Customer.NOTE, new Search());
IV. 删除
Delete by ID
customerMapper.delete(1154);
Delete by Search
customerMapper.delete(new Search(Customer.ID, 1154));