yoqw/README.md
2020-07-01 15:22:07 +08:00

310 lines
6.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Corona
### IDEA 环境需求
由于项目依赖`org.projectlombok:lombok:1.18.2`,而`Lombok``1.18.4`版本做了[重设计](https://projectlombok.org/features/experimental/FieldNameConstants),不兼容之前版本,因此`IDEA``Lombok`插件必须是版本`lombok-plugin-0.23`,这里比较坑,如果升级到版本`1.18.4`以后插件还是得升级而且很多API都要改。
插件下载地址:[https://pan.caimi-inc.com/d/b038610394/](https://pan.caimi-inc.com/d/b038610394/)
下载对应版本插件,比如`IDEA 2018.2.x`下载`lombok-plugin-0.23-2018.2.zip`,卸载`IDEA`已安装`Lombok`插件,选择磁盘安装,如下图:
![安装Lombok插件](doc/images/install-lombok-plugin.png)
### 启动SpringBoot项目
建议先执行mvn编译命令比如编译模块`CMS`
```bash
mvn clean compile -am -pl cms -T 2C
```
### Tigon
#### Controller
如果加注了`@ResponseBody`或者`@RestController`,不做变动,依循`Spring MVC`方式`JSON`序列化返回数据,否则返回`Wacai`接口规范数据格式,如下例:
#### @ResponseBody
```java
@ResponseBody
@RequestMapping("/resp-body")
public String respBody() {
return "Response Body";
}
```
响应输出
```
Response Body
```
#### Wacai JSON API
```java
@RequestMapping("/resp-body")
public String respBody() {
return "Response Body";
}
```
响应输出
```
{
"code": 0,
"success": true,
"data": "Response Body"
}
```
简单来说就是,不加`@ResponseBody`注解,默认会以`Wacai`API规范响应数据结果。
参见以下样例:
I.
```java
@RequestMapping("/count")
public int count() {
return 1024;
}
```
响应输出
```
{
"code": 0,
"success": true,
"data": 1024
}
```
II.
```java
@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.
```java
@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
```java
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
```java
import com.wacai.tigon.mybatis.BaseMapper;
public interface CustomerMapper extends BaseMapper<Integer, Customer> {
}
```
##### Mapper.xml
```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
```java
@Autowired
private CustomerMapper customerMapper;
```
I. 插入
```java
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
```java
final Integer id = 1154;
final Customer customer = customerMapper.find(id);
```
Find by `Search`
```java
final Customer customer = customerMapper.find(
new Search(Customer.ACCOUNT, "donghuang")
.eq(Customer.ACTIVE, true));
```
List by `Search`
```java
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 another `Search`
- `asc` Order ASC
- `between` Between two values
- `contains` Value contains string
- `desc` Order DSC
- `endsWith` Value ends with string
- `eq` Eqauls
- `gt` Greater than
- `gte` Eqauls or greater than
- `in` In values
- `isNull` Value is null
- `like` Value like
- `limit` Return rows limit
- `lt` Less than
- `lte` Eqauls or less than
- `ne` Not equals
- `notIn` Not in values
- `notNull` Value is not null
- `offset` Return rows offset
- `or` Or another `Search`
- `orderBy` Order by
- `startsWith` Value starts with string
III. 更新
Update model
```java
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
```java
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
```java
// 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
```java
customerMapper.delete(1154);
```
Delete by `Search`
```java
customerMapper.delete(new Search(Customer.ID, 1154));
```