Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

net.jkcode.jkmvc:jkmvc-orm

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

net.jkcode.jkmvc:jkmvc-orm

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.

  • 1.9.0
  • Source
  • Maven
  • Socket score

Version published
Maintainers
1
Source

GitHub | Gitee

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 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
}
  1. maven
<dependency>
    <groupId>net.jkcode.jkmvc</groupId>
    <artifactId>jkmvc-http</artifactId>
    <version>1.9.0</version>
</dependency>
<!-- If you want to use embedded jetty -->
<dependency>
    <groupId>net.jkcode.jkmvc</groupId>
    <artifactId>jkmvc-server-jetty</artifactId>
    <version>1.9.0</version>
</dependency>

2 Configure JkFilter in web.xml

JkFilter is a Filter for your web application

vim src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<filter>
		<filter-name>jkmvc</filter-name>
		<filter-class>net.jkcode.jkmvc.http.JkFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>jkmvc</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

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.controller

import 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

6 Visit web page

visit http://localhost:8080/jkmvc-example/

Introduction - view

1 Render View in Controller

package net.jkcode.jkmvc.example.controller

import 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"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jkmvc</title>
</head>
<body>
Hello <%= request.getAttribute("name") %><br/>
</body>
</html>

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<Int>(); 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"
  1. maven
<dependency>
    <groupId>net.jkcode.jkmvc</groupId>
    <artifactId>jkmvc-orm</artifactId>
    <version>1.9.0</version>
</dependency>

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.model

import 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<Int>();

    public var name:String by property<String>();

    public var age:Int by property<Int>();

    public var avatar:String? by property<String?>();

    // 关联地址:一个用户有一个地址
    // relate to AddressModel: user has a home address
    public var home:AddressModel by property<AddressModel>();

    // 关联地址:一个用户有多个地址
    // relate to AddressModel: user has many addresses
    public var addresses:List<AddressModel> by property<List<AddressModel>>();
}

address model, extends Orm

package net.jkcode.jkmvc.example.model

import 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<Int>();

    public var user_id:Int by property<Int>();

    public var addr:String by property<String>();

    public var tel:String by property<String>();

    public var isHome:Int by property();

    // 关联用户:一个地址从属于一个用户
    public var user:UserModel by property<UserModel>()
}

4 Use Model in Controller

package net.jkcode.jkmvc.example.controller

import 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<UserModel>()
        // 渲染视图 | 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<UserModel>()
        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

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-orm:build -x test
gradle :jkmvc-http:build -x test
gradle :jkmvc-example:build -x test

Document

http module

  1. getting started
  2. controller
  3. request
  4. response
  5. route
  6. upload
  7. request handling flow
  8. cookie
  9. session

db module

  1. getting started
  2. query
  3. query builder
  4. db expression

orm module

  1. getting_started
  2. model
  3. relation
  4. validation
  5. using
  6. relation using callback

es module(ElasticSearch client)

  1. getting started
  2. Entity - mapping es document
  3. EsManager - manage es index
  4. EsDocRepository - as entity repository
  5. EsQueryBuilder - query builder

other

  1. deploy

中文文档

http模块

  1. 快速开始
  2. 控制器
  3. 请求
  4. 响应
  5. 路由
  6. 上传
  7. 请求处理流程
  8. cookie
  9. 会话
  10. jetty server
  11. 生成api文档
  12. 整合jphp-支持php来写db/orm代码
  13. 整合jphp-支持php来写控制器代码

db模块

  1. 快速开始
  2. 查询
  3. sql构建器
  4. db表达式

orm模块

  1. 快速开始
  2. 模型
  3. 关联关系
  4. 校验
  5. 使用
  6. 基于回调的关联关系

es模块(ElasticSearch client)

  1. 快速开始
  2. 实体类 - 映射文档
  3. EsManager - 管理es索引
  4. EsDocRepository - 实体仓库类
  5. EsQueryBuilder - 查询构建器

其他

  1. 部署
  2. changelog

jkorm/mybatis性能测试与对比

  1. 测试场景
  2. model代码
  3. add场景代码
  4. update场景代码
  5. delete场景代码
  6. getDepWithEmps场景代码
  7. getEmpsByConditionIf场景代码
  8. updateEmpOnDynFields场景代码
  9. getEmpsByIds场景代码
  10. 第1轮测试结果
  11. 第2轮测试结果

其他

变更历史

FAQs

Package last updated on 20 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc