Skip to content
项目

关于

Like Mybatis but for golang.

数据与分析

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

Juice Logo

Juice: A SQL Mapper for Go Inspired by MyBatis

Go Doc Release Go Report Card License JetBrains Marketplace JetBrains Marketplace Downloads

Juice is a SQL mapper for Go that keeps SQL explicit while adding XML mappers, dynamic SQL, typed binding, middleware, and transaction helpers.

Why Juice

  • Keep SQL readable and explicit instead of hiding it behind a heavy ORM
  • Organize queries with XML mappers inspired by MyBatis
  • Build dynamic SQL with nodes like if, where, set, and foreach
  • Bind query results into typed Go values with generics
  • Extend execution with middleware, transaction helpers, and datasource switching

Quick Start

Create a minimal configuration:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//juice.org//DTD Config 1.0//EN"
        "https://raw.githubusercontent.com/go-juicedev/juice/refs/heads/main/config.dtd">

<configuration>
    <environments default="prod">
        <environment id="prod">
            <dataSource>sqlite.db</dataSource>
            <driver>sqlite3</driver>
        </environment>
    </environments>

    <mappers>
        <mapper resource="mappers.xml"/>
    </mappers>
</configuration>

Define a mapper:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//juice.org//DTD Config 1.0//EN"
        "https://raw.githubusercontent.com/go-juicedev/juice/refs/heads/main/mapper.dtd">

<mapper namespace="main.Repository">
    <select id="HelloWorld">
        <if test="1 == 1">
            select "hello world"
        </if>
    </select>
</mapper>

Call it from Go:

package main

import (
	"context"
	"fmt"

	"github.com/go-juicedev/juice"
	_ "github.com/mattn/go-sqlite3"
)

type Repository interface {
	HelloWorld(ctx context.Context) (string, error)
}

type RepositoryImpl struct {
	manager juice.Manager
}

func (r RepositoryImpl) HelloWorld(ctx context.Context) (string, error) {
	executor := juice.NewGenericManager(r.manager).Object(Repository(r).HelloWorld)
	return executor.QueryContext[string](ctx, nil)
}

func main() {
	cfg, err := juice.NewXMLConfiguration("config.xml")
	if err != nil {
		panic(err)
	}

	engine, err := juice.New(cfg)
	if err != nil {
		panic(err)
	}
	defer engine.Close()

	repo := RepositoryImpl{manager: engine}
	result, err := repo.HelloWorld(context.Background())
	fmt.Println(result, err)
}

Run it:

CGO_ENABLED=1 go run main.go

Expected output:

hello world <nil>

Installation

Install Juice:

go get github.com/go-juicedev/juice

Install a database driver for the backend you want to use. For example, the quick start above uses SQLite:

import _ "github.com/mattn/go-sqlite3"

If you use the SQLite example above, CGO_ENABLED=1 is required.

When To Use

Juice is a good fit when:

  • you want explicit SQL instead of a code-first ORM
  • you like MyBatis-style mapper organization
  • you need dynamic SQL without manually concatenating strings
  • you want a thin abstraction over database/sql with stronger structure

Juice may not be the right fit when:

  • you want a code-first query builder or ORM model layer
  • you do not want XML-based mapping at all
  • you expect built-in migration or schema management features

Documentation

License

Juice is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Support Me

If you like my work, please consider supporting me by buying me a coffee.

Buy Me A Coffee

探索相关项目

加载中…