Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shigebeyond/jkmvc
Jkmvc is an elegant, powerful and lightweight MVC & ORM framework built using kotlin. It aims to be swift, secure, and small. It will turn java's heavy development into kotlin's simple pleasure. No spring.
https://github.com/shigebeyond/jkmvc
async-servlet kotlin mvc orm servlet3 web web-framework
Last synced: about 1 month ago
JSON representation
Jkmvc is an elegant, powerful and lightweight MVC & ORM framework built using kotlin. It aims to be swift, secure, and small. It will turn java's heavy development into kotlin's simple pleasure. No spring.
- Host: GitHub
- URL: https://github.com/shigebeyond/jkmvc
- Owner: shigebeyond
- Created: 2017-05-18T02:29:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-08-15T05:59:00.000Z (over 1 year ago)
- Last Synced: 2023-08-15T06:46:04.575Z (over 1 year ago)
- Topics: async-servlet, kotlin, mvc, orm, servlet3, web, web-framework
- Language: Kotlin
- Homepage:
- Size: 7.01 MB
- Stars: 89
- Watchers: 17
- Forks: 14
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[GitHub](https://github.com/shigebeyond/jkmvc) | [Gitee](https://gitee.com/shigebeyond/jkmvc)
# jkmvc
Jkmvc is an elegant, powerful and lightweight MVC web framework built using kotlin. It aims to be swift, secure, and small. It will turn java's heavy development into kotlin's simple pleasure.Inspired by 2 php frameworks: [kohana](https://github.com/kohana/kohana) and [skmvc](https://github.com/shigebeyond/skmvc)
[Document](#Document)
[中文文档](#中文文档)
[性能跟开发效率对比](https://github.com/shigebeyond/jkmvc-benchmark)
# Introduction - web
Take `jkmvc/jkmvc-example` for example.
## 1 Add jkmvc-http dependecies
1. gradle
```
dependencies{
compile "net.jkcode.jkmvc:jkmvc-http:1.9.0
// If you want to use embedded jetty
compile "net.jkcode.jkmvc:jkmvc-server-jetty:1.9.0
}
```2. maven
```net.jkcode.jkmvc
jkmvc-http
1.9.0net.jkcode.jkmvc
jkmvc-server-jetty
1.9.0```
## 2 Configure JkFilter in web.xml
JkFilter is a Filter for your web application
vim src/main/webapp/WEB-INF/web.xml
```
jkmvc
net.jkcode.jkmvc.http.JkFilter
jkmvc
/*
```
## 3 Create Controller
Controller handles request, and render data to response.
It has property `req` to represent request, `res` to represent response.
```
package net.jkcode.jkmvc.example.controllerimport net.jkcode.jkmvc.http.Controller
/**
* 主页
*/
class WelcomeController: Controller() {/**
* 主页
*/
public fun index() {
res.renderHtml("hello world");
}}
```## 4 Register Controller
configure controller classes's package paths
vim src/main/resources/http.yaml
```
# 是否调试
debug: true
# 静态文件的扩展名
# static file extension
staticFileExts: gif|jpg|jpeg|png|bmp|ico|svg|swf|js|css|eot|ttf|woff
# controller类所在的包路径
# controller classes's package paths
controllerPackages:
- net.jkcode.jkmvc.example.controller
```## 5 Gradle config jetty plugin
vim build.gradle
```
apply plugin: 'org.akhikhl.gretty'// 启动jetty
gretty{
// server 配置
servletContainer 'jetty9' // 'tomcat8'
httpPort 8080
managedClassReload true // 热部署
scanInterval 1 // 热部署的扫描间隔,当值为0时,不扫描新class,不热部署// 调试: gradle appRunDebug
debugPort 5006 // 运行jetty的jvm独立于运行gradle的jvm, 因此也使用独立的调试端口
debugSuspend true// webapp 配置
contextPath "/${project.name}"
inplaceMode "hard" // 资源目录 src/main/webapp
}
```## 5 Run web server
`gradle appRun -x test`
![](img/runserver.png)
## 6 Visit web page
visit http://localhost:8080/jkmvc-example/
![](img/webpage.png)
# Introduction - view
## 1 Render View in Controller
```
package net.jkcode.jkmvc.example.controllerimport net.jkcode.jkmvc.http.Controller
/**
* 主页
*/
class WelcomeController: Controller() {/**
* 显示jsp视图
* render jsp view
*/
public fun jsp(){
res.renderView(view("index" /* view file */, mapOf("name" to "shijianhang") /* view data */))
}}
```## 2 Create jsp view
I regret that we cannot write kotlin in jsp.
vim src/main/webapp/index.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>jkmvc
Hello <%= request.getAttribute("name") %>
```
## 3 Visit web page
visit http://localhost:8080/jkmvc-example/welcome/jsp
![](img/webview.png)
# Introduction - orm
Orm provides object-oriented way to mainpulate db data.
It has 2 concepts:
1 Orm meta data: include information as follows
1.1 mapping from object to table
1.2 mapping from object's property to table's column
1.3 mapping from object's property to other object
2 Orm object | Model
2.1 visit property
you can use operator `[]` to visit orm object's property, and also use property delegate `public var id:Int by property();` to visit it
2.2 method
`queryBuilder()` return a query builder to query data from table
`create()` create data
`update()` update data
`delete()` delete data
## 1 Add dependency
1. gradle
```
compile "net.jkcode.jkmvc:jkmvc-orm:1.9.0"
```2. maven
```net.jkcode.jkmvc
jkmvc-orm
1.9.0```
## 2 Create tables
user table
```
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户编号',
`name` varchar(50) NOT NULL DEFAULT '' COMMENT '用户名',
`age` tinyint(4) unsigned NOT NULL DEFAULT '0' COMMENT '年龄',
`avatar` varchar(250) DEFAULT NULL COMMENT '头像',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8 COMMENT='用户'
```address table
```
CREATE TABLE `address` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '地址编号',
`user_id` int(11) unsigned NOT NULL COMMENT '用户编号',
`addr` varchar(50) NOT NULL DEFAULT '' COMMENT '地址',
`tel` varchar(50) NOT NULL DEFAULT '' COMMENT '电话',
`name` varchar(50) NOT NULL DEFAULT '' COMMENT '用户名',
`is_home` tinyint(4) unsigned NOT NULL DEFAULT '0' COMMENT '是否是家庭住址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8 COMMENT='地址';
```## 3 Create Model
use model, extends Orm
```
package net.jkcode.jkmvc.example.modelimport net.jkcode.jkmvc.orm.OrmMeta
import net.jkcode.jkmvc.orm.Orm/**
* 用户模型
* User model
*/
class UserModel(id:Int? = null): Orm(id) {
// 伴随对象就是元数据
// company object is meta data for model
companion object m: OrmMeta(UserModel::class){
init {
// 添加标签 + 规则
// add label and rule for field
addRule("name", "姓名", "notEmpty");
addRule("age", "年龄", "between(1,120)");// 添加关联关系
// add relaction for other model
hasOne("home", AddressModel::class){ query ->
query.where("is_home", 1)
}
hasMany("addresses", AddressModel::class)
}
}// 代理属性读写
// delegate property
public var id:Int by property();public var name:String by property();
public var age:Int by property();
public var avatar:String? by property();
// 关联地址:一个用户有一个地址
// relate to AddressModel: user has a home address
public var home:AddressModel by property();// 关联地址:一个用户有多个地址
// relate to AddressModel: user has many addresses
public var addresses:List by property>();
}
```address model, extends Orm
```
package net.jkcode.jkmvc.example.modelimport net.jkcode.jkmvc.orm.OrmMeta
import net.jkcode.jkmvc.orm.Orm/**
* 地址模型
*/
class AddressModel(id:Int? = null): Orm(id) {
// 伴随对象就是元数据
// company object is meta data for model
companion object m: OrmMeta(AddressModel::class){
init {
// 添加标签 + 规则
// add label and rule for field
addRule("user_id", "用户", "notEmpty");
addRule("addr", "地址", "notEmpty");
addRule("tel", "电话", "notEmpty && digit");// 添加关联关系
// add relaction for other model
belongsTo("user", UserModel::class, "user_id")
}
}// 代理属性读写
// delegate property
public var id:Int by property();public var user_id:Int by property();
public var addr:String by property();
public var tel:String by property();
public var isHome:Int by property();
// 关联用户:一个地址从属于一个用户
public var user:UserModel by property()
}
```## 4 Use Model in Controller
```
package net.jkcode.jkmvc.example.controllerimport net.jkcode.jkutil.common.format
import net.jkcode.jkutil.common.httpLogger
import net.jkcode.jkmvc.example.model.UserModel
import net.jkcode.jkmvc.http.controller.Controller
import net.jkcode.jkmvc.http.fromRequest
import net.jkcode.jkmvc.http.isPost
import net.jkcode.jkmvc.http.isUpload
import net.jkcode.jkmvc.http.session.Auth
import net.jkcode.jkmvc.orm.OrmQueryBuilder
import net.jkcode.jkmvc.orm.isLoaded
import java.util.*/**
* 用户管理
* user manage
*/
class UserController: Controller()
{
/**
* action前置处理
*/
public override fun before() {
// 如检查权限
httpLogger.info("action前置处理")
}/**
* action后置处理
*/
public override fun after() {
// 如记录日志
httpLogger.info("action后置处理")
}/**
* 列表页
* list page
*/
public fun index()
{
val query: OrmQueryBuilder = UserModel.queryBuilder()
// 统计用户个数 | count users
val counter:OrmQueryBuilder = query.clone() as OrmQueryBuilder // 复制query builder
val count = counter.count()
// 查询所有用户 | find all users
val users = query.findModels()
// 渲染视图 | render view
res.renderView(view("user/index", mapOf("count" to count, "users" to users)))
}/**
* 详情页
* detail page
*/
public fun detail()
{
// 获得路由参数id: 2种写法 | 2 ways to get route parameter: "id"
// val id = req.getInt("id");
val id:Int? = req["id"] // req["xxx"]
// 查询单个用户 | find a user
//val user = UserModel.queryBuilder().where("id", id).findModel()
val user = UserModel(id)
if(!user.isLoaded()){
res.renderHtml("用户[$id]不存在")
return
}
// 渲染视图 | render view
val view = view("user/detail")
view["user"] = user; // 设置视图参数 | set view data
res.renderView(view)
}/**
* 新建页
* new page
*/
public fun new()
{
// 处理请求 | handle request
if(req.isPost){ // post请求:保存表单数据 | post request: save form data
// 创建空的用户 | create user model
val user = UserModel()
// 获得请求参数:3种写法 | 3 ways to get request parameter
/* // 1 req.getParameter("xxx");
user.name = req.getParameter("name")!!;
user.age = req.getInt("age", 0)!!; // 带默认值 | default value
*/
// 2 req["xxx"]
user.name = req["name"]!!;
user.age = req["age"]!!;// 3 Orm.fromRequest(req)
user.fromRequest(req)
user.create(); // create user
// 重定向到列表页 | redirect to list page
redirect("user/index");
}else{ // get请求: 渲染视图 | get request: render view
val view = view() // 默认视图为action名: user/new | default view's name = action: user/new
res.renderView(view)
}
}/**
* 编辑页
* edit page
*/
public fun edit()
{
// 查询单个用户 | find a user
val id: Int = req["id"]!!
val user = UserModel(id)
if(!user.isLoaded()){
res.renderHtml("用户[" + req["id"] + "]不存在")
return
}
// 处理请求 | handle request
if(req.isPost){ // post请求:保存表单数据 | post request: save form data
// 获得请求参数:3种写法 | 3 way to get request parameter
/* // 1 req.getParameter("xxx");
user.name = req.getParameter("name")!!;
user.age = req.getInt("age", 0)!!; // 带默认值 | default value
*/
/*// 2 req["xxx"]
user.name = req["name"]!!;
user.age = req["age"]!!;
*/
// 3 Orm.fromRequest(req)
user.fromRequest(req)
user.update() // update user
// 重定向到列表页 | redirect to list page
redirect("user/index");
}else{ // get请求: 渲染视图 | get request: render view
val view = view() // 默认视图为action名: user/edit | default view's name = action: user/edit
view["user"] = user; // 设置视图参数 | set view data
res.renderView(view)
}
}/**
* 删除
* delete action
*/
public fun delete()
{
val id:Int? = req["id"]
// 查询单个用户 | find a user
val user = UserModel(id)
if(!user.isLoaded()){
res.renderHtml("用户[$id]不存在")
return
}
// 删除 | delete user
user.delete();
// 重定向到列表页 | redirect to list page
redirect("user/index");
}/**
* 上传头像
* upload avatar
*/
public fun uploadAvatar()
{
// 查询单个用户 | find a user
val id: Int = req["id"]!!
val user = UserModel(id)
if(!user.isLoaded()){
res.renderHtml("用户[" + req["id"] + "]不存在")
return
}// 检查并处理上传文件 | check and handle upload request
if(req.isUpload){ // 检查上传请求 | check upload request
user.avatar = req.storePartFileAndGetRelativePath("avatar")!!
user.update()
}// 重定向到详情页 | redirect to detail page
redirect("user/detail/$id");
}/**
* 登录
*/
public fun login(){
if(req.isPost){ // post请求
val user = Auth.instance().login(req["username"]!!, req["password"]!!);
if(user == null)
res.renderHtml("登录失败")
else
redirect("user/login")
}else{ // get请求
res.renderView(view())
}
}/**
* 登录
*/
public fun logout(){
Auth.instance().logout()
redirect("user/login")
}
}
```# demo
download source and run web server
```
git clone https://github.com/shigebeyond/jkmvc.git
cd jkmvc
gradle :jkmvc-example:appRun
```visit url
http://localhost:8080/jkmvc-example/user/index
![](img/actionindex.png)
http://localhost:8080/jkmvc-example/user/detail
![](img/actiondetail.png)
http://localhost:8080/jkmvc-example/user/new
![](img/actionnew.png)
http://localhost:8080/jkmvc-example/user/edit
![](img/actionedit.png)
# gradle command for build
```
gradle :jkmvc-orm:build -x test
gradle :jkmvc-http:build -x test
gradle :jkmvc-example:build -x test
```# Document
## http module
1. [getting started](doc/http/getting_started.md)
2. [controller](doc/http/controller.md)
3. [request](doc/http/request.md)
4. [response](doc/http/response.md)
5. [route](doc/http/route.md)
6. [upload](doc/http/upload.md)
7. [request handling flow](doc/http/flow.md)
8. [cookie](doc/http/cookie.md)
9. [session](doc/http/session.md)## db module
10. [getting started](doc/db/getting_started.md)
11. [query](doc/db/query.md)
12. [query builder](doc/db/query_builder.md)
13. [db expression](doc/db/expr.md)## orm module
14. [getting_started](doc/orm/getting_started.md)
15. [model](doc/orm/model.md)
16. [relation](doc/orm/relation.md)
17. [validation](doc/orm/validation.md)
18. [using](doc/orm/using.md)
19. [relation using callback](doc/orm/cbrelation.md)## es module(ElasticSearch client)
20. [getting started](doc/es/getting_started.md)
21. [Entity - mapping es document](doc/es/Entity.md)
22. [EsManager - manage es index](doc/es/EsManager.md)
23. [EsDocRepository - as entity repository](doc/es/EsDocRepository.md)
24. [EsQueryBuilder - query builder](doc/es/EsQueryBuilder.md)## other
25. [deploy](doc/deploy.md)# 中文文档
## http模块
1. [快速开始](doc/http/getting_started.cn.md)
2. [控制器](doc/http/controller.cn.md)
3. [请求](doc/http/request.cn.md)
4. [响应](doc/http/response.cn.md)
5. [路由](doc/http/route.cn.md)
6. [上传](doc/http/upload.cn.md)
7. [请求处理流程](doc/http/flow.cn.md)
8. [cookie](doc/http/cookie.cn.md)
9. [会话](doc/http/session.cn.md)
10. [jetty server](doc/http/jetty_server.cn.md)
11. [生成api文档](doc/http/api_doc.cn.md)
12. [整合jphp-支持php来写db/orm代码](doc/orm/jphp.cn.md)
13. [整合jphp-支持php来写控制器代码](doc/http/jphp.cn.md)## db模块
14. [快速开始](doc/db/getting_started.cn.md)
15. [查询](doc/db/query.cn.md)
16. [sql构建器](doc/db/query_builder.cn.md)
17. [db表达式](doc/db/expr.cn.md)## orm模块
18. [快速开始](doc/orm/getting_started.cn.md)
19. [模型](doc/orm/model.cn.md)
20. [关联关系](doc/orm/relation.cn.md)
21. [校验](doc/orm/validation.cn.md)
22. [使用](doc/orm/using.cn.md)
23. [基于回调的关联关系](doc/orm/cbrelation.cn.md)## es模块(ElasticSearch client)
24. [快速开始](doc/es/getting_started.md)
25. [实体类 - 映射文档](doc/es/Entity.md)
26. [EsManager - 管理es索引](doc/es/EsManager.md)
27. [EsDocRepository - 实体仓库类](doc/es/EsDocRepository.md)
28. [EsQueryBuilder - 查询构建器](doc/es/EsQueryBuilder.md)## 其他
29. [部署](doc/deploy.cn.md)
30. [changelog](doc/changelog.md)## jkorm/mybatis性能测试与对比
31. [测试场景](doc/orm/benchmark/sence.md)
32. [model代码](doc/orm/benchmark/code_model.md)
33. [add场景代码](doc/orm/benchmark/code_add.md)
34. [update场景代码](doc/orm/benchmark/code_update.md)
35. [delete场景代码](doc/orm/benchmark/code_delete.md)
36. [getDepWithEmps场景代码](doc/orm/benchmark/code_getDepWithEmps.md)
37. [getEmpsByConditionIf场景代码](doc/orm/benchmark/code_getEmpsByConditionIf.md)
38. [updateEmpOnDynFields场景代码](doc/orm/benchmark/code_updateEmpOnDynFields.md)
39. [getEmpsByIds场景代码](doc/orm/benchmark/code_getEmpsByIds.md)
40. [第1轮测试结果](doc/orm/benchmark/result_round1.md)
41. [第2轮测试结果](doc/orm/benchmark/result_round2.md)# 其他
[变更历史](doc/changelog.md)