Git Product home page Git Product logo

latke's Introduction

Latke

一款以 JSON 为主的 Java Web 框架

简介

Latke('lɑ:tkə,土豆饼)是一个简单易用的 Java Web 应用开发框架,包含 MVC、IoC、事件通知、ORM、插件等组件。

在实体模型上使用 JSON 贯穿前后端,使应用开发更加快捷。这是 Latke 不同于其他框架的地方,比较适合小型应用的快速开发。

特性

  • 注解式、函数式路由
  • 依赖注入
  • 多种数据库 ORM
  • 多语言
  • 内存/Redis 缓存
  • 事件机制
  • 插件机制

案例

  • Demo:简单的 Latke 应用示例
  • Solo:一款小而美的 Java 博客系统
  • Symphony:一款用 Java 实现的现代化社区(论坛/BBS/社交网络/博客)平台

安装

<dependency>
    <groupId>org.b3log</groupId>
    <artifactId>latke-core</artifactId>
    <version>${latke.version}</version>
</dependency>

控制器层用法

注解声明式路由

@RequestProcessing("/")
public void index(final RequestContext context) {
    context.setRenderer(new SimpleFMRenderer("index.ftl"));
    final Map<String, Object> dataModel = context.getRenderer().getRenderDataModel();
    dataModel.put("greeting", "Hello, Latke!");
}

函数式路由

DispatcherServlet.post("/register", registerProcessor::register);
DispatcherServlet.mapping();

路径变量和查询字符串

@RequestProcessing("/var/{pathVar}")
public void paraPathVar(final RequestContext context) {
    final String paraVar = context.param("paraVar");
    final String pathVar = context.pathVar("pathVar");
    context.renderJSON(new JSONObject().put("paraVar", paraVar).put("pathVar", pathVar));
}

JSON 解析

final JSONObject requestJSON = context.requestJSON();

Servlet 封装

final String remoteAddr = context.remoteAddr();
final String requestURI = context.requestURI();
final Object att = context.attr("name");
final String method = context.method();
context.sendRedirect("https://b3log.org");
final HttpServletRequest request = context.getRequest();
final HttpServletResponse response = context.getResponse();

服务层用法

依赖注入、事务

@Service
public class UserService {

    private static final Logger LOGGER = Logger.getLogger(UserService.class);

    @Inject
    private UserRepository userRepository;

    @Transactional
    public void saveUser(final String name, final int age) {
        final JSONObject user = new JSONObject();
        user.put("name", name);
        user.put("age", age);

        String userId;

        try {
            userId = userRepository.add(user);
        } catch (final RepositoryException e) {
            LOGGER.log(Level.ERROR, "Saves user failed", e);

            // 抛出异常后框架将回滚事务
            throw new IllegalStateException("Saves user failed");
        }

        LOGGER.log(Level.INFO, "Saves a user successfully [userId={0}]", userId);
    }
}

持久层用法

构造 ORM

@Repository
public class UserRepository extends AbstractRepository {

    public UserRepository() {
        super("user");
    }
}

单表 CRUD

public interface Repository {
    String add(final JSONObject jsonObject) throws RepositoryException;
    void update(final String id, final JSONObject jsonObject) throws RepositoryException;
    void remove(final String id) throws RepositoryException;
    void remove(final Query query) throws RepositoryException;
    JSONObject get(final String id) throws RepositoryException;
    long count(final Query query) throws RepositoryException;
}

条件查询

public JSONObject getByName(final String name) throws RepositoryException {
    final List<JSONObject> records = getList(new Query().
            setFilter(new PropertyFilter("name", FilterOperator.EQUAL, name)));
    if (records.isEmpty()) {
        return null;
    }

    return records.get(0);
}

分页查询

new Query().setCurrentPageNum(1).setPageSize(50)

按字段排序

new Query().addSort("name", SortDirection.DESCENDING);

仅获取需要字段

new Query().addProjection("name", String.class);

原生 SQL

final List<JSONObject> records = select("SELECT * FROM `user` WHERE `name` = ?", name);

文档

社区

授权

Latke 使用 Apache License, Version 2.0 开源协议。

鸣谢

latke's People

Contributors

011011100010110101101111 avatar 88250 avatar armstrongcn avatar haorendashu avatar mainlove avatar vanessa219 avatar wangdx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.