一、概述
libapi是一个专门创建app接口的库,支持无配置controller、model、dao
二、项目构建流程
1、下载template.zip
2、解压缩
3、打开ApiMain,以TestCase的方式运行
三、Controller
1、创建包com.damai.api.controllers
创建文件TestController
@Controller(key = "test")
@Action(value = WebJsonActionFactory.class)
public class TestController {
public Boolean test(){
return true;
}
}
框架将自动加载controllers包下,并用@Controller标注的类作为Controller
重启,并浏览器打开访问
http://127.0.0.1:8098/api/test/test
三、Dao层
1、打开配置database-local.xml
改成已有数据库连接
<item
driver="mysql"
user="myuser"
password="mypassword"
host="192.168.1.238"
db="test" port="3306"></item>
表结构如上
新建包:com.damai.api.entities
新建类
package com.damai.api.entities;
import com.citywithincity.db.annotation.AutoGenerate;
import com.citywithincity.db.annotation.Column;
import com.citywithincity.db.annotation.Table;
@Table(name = "test")
public class TestVo {
@AutoGenerate
private Integer id;
private String name;
@Column("TEST_CLASS")
private String cls;
private String myId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCls() {
return cls;
}
public void setCls(String cls) {
this.cls = cls;
}
public String getMyId() {
return myId;
}
public void setMyId(String myId) {
this.myId = myId;
}
}
注意这里的字段隐射规则:
框架将自动解析出字段前缀为TEST_ ,规则为本表中使用最多的前缀。
并且将下划线方式的命名规则,转为驼峰式
在controller增加方法
package com.damai.api.controllers;
import com.damai.action.factories.WebJsonActionFactory;
import com.damai.annotation.Action;
import com.damai.annotation.Controller;
import com.damai.annotation.ReturnString;
import com.damai.api.entities.TestVo;
@Controller(key = "test")
@Action(value = WebJsonActionFactory.class)
public class TestController {
public Boolean test(){
return true;
}
public TestVo testInsert(){
TestVo vo = new TestVo();
vo.setCls("一班");
vo.setName("小子");
vo.setMyId("123");
return vo;
}
}
运行一下
http://127.0.0.1:8098/api/test/testInsert
四、数据模型层
Controll中使用@Model标注的字段会自动初始化(目前只支持启动创建,即为scope为Application)
@Model
private TestModel model;
···
package com.damai.api.models;
import com.citywithincity.db.Db;
import com.damai.api.entities.TestVo;
public class TestModel {
public TestVo query(Object id){
return Db.ar(TestVo.class).get(id);
}
}
···
如需要使用自定义配置加载,则需要定义bean-local.xml
<bean id="test" class="xxx"></bean>
class指定需要加载的类,并在@Model中指定id
@Model(config="test")
private TestModel model2;