微前端架构如何改变企业的开发模式与效率提升
718
2022-11-03
Jkmvc 使用kotlin构建的优雅,强大和轻量级的MVC Web框架
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 and skmvc
Document
中文文档
Introduction - web
Take jkmvc/jkmvc-example for example.
1 Gradle add jkmvc dependecies
dependencies{ compile "net.jkcode.jkmvc:jkmvc-http:1.8.0" compile "net.jkcode.jkmvc:jkmvc-server-jetty:1.8.0"}
2 Configure JkFilter in web.xml
JkFilter is a Filter for your web application
vim src/main/webapp/WEB-INF/web.xml
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 indexAction() { res.renderString("hello world"); }}
4 Register Controller
configure controller classes's package paths
vim src/main/resources/http.yaml
# controller类所在的包路径# controller classes's package pathscontrollerPackages: - net.jkcode.jkmvc.example.controller
5 Gradle config jetty plugin
vim build.gradle
apply plugin: 'org.akhikhl.gretty'// 启动jettygretty{ // 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
6 Visit web page
visit http://localhost:8080/jkmvc-example/
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 jspAction(){ res.renderView(view("index" /* view file */, mutableMapOf("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"%>
3 Visit web page
visit http://localhost:8080/jkmvc-example/welcome/jsp
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
2.2 method
queryBuilder() return a query builder to query data from table
create() create data
update() update data
delete() delete data
1 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 '用户名', PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=29 DEFAULT CHARSET=utf8 COMMENT='地址';
2 Create Model
use model, extends Orm
package net.jkcode.jkmvc.example.modelimport net.jkcode.jkmvc.orm.OrmMetaimport 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("address", AddressModel::class) hasMany("addresses", AddressModel::class) } } // 代理属性读写 // delegate property public var id:Int by property>();}
address model, extends Orm
package net.jkcode.jkmvc.example.modelimport net.jkcode.jkmvc.orm.OrmMetaimport 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
3 Use Model in Controller
package net.jkcode.jkmvc.example.controllerimport net.jkcode.jkmvc.common.formatimport net.jkcode.jkmvc.common.httpLoggerimport net.jkcode.jkmvc.example.model.UserModelimport net.jkcode.jkmvc.http.controller.Controllerimport net.jkcode.jkmvc.http.fromRequestimport net.jkcode.jkmvc.http.isPostimport net.jkcode.jkmvc.http.isUploadimport net.jkcode.jkmvc.http.session.Authimport net.jkcode.jkmvc.orm.OrmQueryBuilderimport net.jkcode.jkmvc.orm.isLoadedimport 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 indexAction() { 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.findAllModels
demo
download source and run web server
git clone https://github.com/shigebeyond/jkmvc.gitcd jkmvcgradle :jkmvc-example:appRun
visit url
http://localhost:8080/jkmvc-example/user/index
http://localhost:8080/jkmvc-example/user/detail
http://localhost:8080/jkmvc-example/user/new
http://localhost:8080/jkmvc-example/user/edit
gradle command for build
gradle :jkmvc-common:build -x testgradle :jkmvc-orm:build -x testgradle :jkmvc-http:build -x testgradle :jkmvc-example:build -x test
Document
http module
getting startedcontrollerrequestresponserouteuploadrequest handling flow
db module
getting startedqueryquery builderdb expression
orm module
getting_startedmodelrelationvalidationusing
common module
deployconfigvalidation_expressionvalidation
中文文档
http模块
快速开始控制器请求响应路由上传请求处理流程
db模块
快速开始查询sql构建器db表达式
orm模块
快速开始模型关联关系校验使用
公共
部署配置缓存锁序列化校验器校验表达式id生成器
其他
changelog
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~