Skip to content

go-juicedev/juice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

d3462c3 · Feb 21, 2025
Feb 12, 2025
Feb 10, 2025
Jan 23, 2025
Jan 21, 2025
Oct 30, 2024
Dec 29, 2024
Oct 10, 2022
Oct 7, 2022
Jan 21, 2025
May 11, 2024
Feb 11, 2025
Dec 29, 2024
Feb 12, 2025
Dec 17, 2024
Jan 15, 2025
Jan 21, 2025
Feb 11, 2025
Jan 17, 2025
Jan 21, 2025
Feb 12, 2025
Jan 16, 2025
Jan 8, 2025
Feb 7, 2025
Dec 31, 2024
Jan 22, 2025
Jan 16, 2025
Jan 21, 2025
May 27, 2024
Jan 21, 2025
Jan 16, 2025
Jan 17, 2025
Feb 7, 2025
Feb 21, 2025
Dec 31, 2024
Dec 31, 2024
Jan 21, 2025
Oct 30, 2024
Feb 11, 2025
Jan 21, 2025
Dec 17, 2024
Feb 7, 2025
May 11, 2024
Jan 21, 2025
Jan 20, 2025

Repository files navigation

Juice SQL Mapper Framework For Golang

Go Doc Release Go Report Card License

Juice is a SQL mapper framework for Golang, inspired by MyBatis. It is simple, lightweight, and easy to use and extend. This document provides a brief introduction to Juice and its usage.

Installation

To install Juice, use the following command:

go get github.com/go-juicedev/juice

Example

touch config.xml

add the following content to config.xml

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

<configuration>
    <environments default="prod">
        <environment id="prod">
            <dataSource>root:qwe123@tcp(localhost:3306)/database</dataSource>
            <driver>mysql</driver>
        </environment>
    </environments>

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

add the following content to mappers.xml

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

<mapper namespace="main.Repository">
    <select id="HelloWorld">
        <if test="1 == 1">  <!-- always be true -->
            select "hello world"
        </if>
    </select>
</mapper>
touch main.go

add the following content to main.go

package main

import (
	"context"
	"fmt"
	"github.com/go-juicedev/juice"
	_ "github.com/go-sql-driver/mysql"
)

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

type RepositoryImpl struct{}

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

func main() {
	cfg, err := juice.NewXMLConfiguration("config.xml")
	if err != nil {
		panic(err)
	}
	
	engine, err := juice.Default(cfg)
	if err != nil {
		panic(err)
	}
	defer engine.Close()
	
	ctx := juice.ContextWithManager(context.Background(), engine)
	repo := RepositoryImpl{}
	result, err := repo.HelloWorld(ctx)
	fmt.Println(result, err) // hello world <nil>
}
go run main.go

API Documentation

English 简体中文

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