English | 中文
A lightweight persistence framework that decouples code from sql, if you are tired of complex DSL, Sql4j
may be
another option for you.
The current mainstream persistence layer framework Mybatis
Jooq
and so on are combined with some DSL in use , in the
case of not running the program , running sql alone to test becomes very difficult , of course there are plug-ins can
output sql , but if you need to run on the server is more difficult . So I built this project
<dependency>
<groupId>io.github.youkale</groupId>
<artifactId>sql4j</artifactId>
<version>0.2.0</version>
</dependency>
-- :name com.github.youkale.sql4j.OrderMapper#getOrderIds :? :*
select id
from `order`
where id in (:v * :ids)
-- :name queryOrderIds :? :*
select id
from `order`
where id in (:v * :ids)
public interface OrderMapper {
List<Integer> getOrderIds(@Param("ids") List<Integer> ids).
@Alias("queryOrderIds")
Integer[] getOrderIdsByVec(@Param("ids") List<Integer> ids).
@Alias("queryOrderIds")
int[] getOrderIdsByVecPrim(@Param("ids") List<Integer> ids).
}
import java.util.List.
public class Main {
public static void main(String[] args) {
Sql4J sql4J = new Sql4J(dataSource, Dialect.mysql, "sql", true).
OrderMapper mapper = sql4J.lookup(OrderMapper.class).
List<Integer> ids = mapper.getOrderIds(List.of(1, 2, 3)).
}
}
HugSql Usage
:name
indicates the name of the mapped mapper:command
The command to be executed, divided into:execute
and:query
, which means execute and query, can be written in one line with:name
:result
result type, you can write a line with:name
- The corresponding
:command
for:raw
can be:execute
and:query
. :many
,:one
The corresponding:command
is:query
.:affected
corresponds to the:command
as:execute
.
- The corresponding
:doc
for document
- The
command
is:execute
.:execute
->:!
.:query
->:?
.
- `:result'.
:many
->:*
.:one
->:1
.:affected
->:n
.
- Multiple rows
-- :name create-characters-table -- :command :execute -- :result :raw -- :doc create-characters-table -- Auto-increment and current timestamp are -- for H2 databases (adjust to your database).
- Single row (query multiple rows)
-- :name queryOrders :? :*
- Single row (single row query)
-- :name queryOrders :? :!
-
:value
or:v
, Value type parameter, here:id
is{"id": 123}
. Doc--:name value-param :? :* select * from characters where id = :id --:name value-param-with-param-type :? :* select * from characters where id = :v:id
-
:value*
or:v*
, Value list parameter, wherenames
isList<String >
. Doc--:name-value-list-param :? :* select * from characters where name in (:v*:names)
-
:tuple
ort
, hereid-name
is[1 , "youkale"]
. Doc-- :name tuple-param -- :doc Tuple Param select * from test where (id, name) = :tuple:id-name
-
:tuple*
or:t*
, Tuple list parameter, herepeople
is `{"person": [[1, "Ed"], [2 "Al"], [3 "Bo"]]}``. Doc-- :name tuple-param-list -- :doc Tuple Param List insert into test (id, name) value :t*:person
-
:identifier
ori
, the quoted parameter, is replaced with the quoted field at runtime, for example mysql will be replaced with `table`. Doc--:name identifier-param :? :* select * from :i:table-name
-
:identifier*
ori*
, The quoted list parameter, which is replaced with the quoted field at runtime. For example, mysql replaces it with `columns`. Doc--:name identifier-list-param :? :* select :i*:column-names, count(*) as population From the example group by :i*:column-names order by :i*:column-names
- mapper
- Bind the defined mapper to the
db-fn*
of hugsql by using the dynamic proxy ofjava
, by default using the class name of the mapper + the fully qualified name of the method, if@Alias
is specified then it will use its defined if@Alias
is specified. - The returned paradigm type is not supported if it is not a
ParameterizedType
, for exampleinterface Foo<T> { T foo(); } class Bar { void call(){ Foo f = sql4j.lookup(Foo.class); } }
- Bind the defined mapper to the
- Calls parameter mapping (the following instructions are for taking values in sql)
- Map type can be accessed according to key, java bean will be accessed according to property name
orderNo
- Basic types and List and Set types need to be named with the
@Param
annotation to be accessed in thesql
file.
- Map type can be accessed according to key, java bean will be accessed according to property name
- Return parameter mapping
- Map returns directly
- List, Order[], Order, etc., will get the
writeMethod
of thePropertyDescriptor
of the bean to write. In case of inconsistency between databasecolumn
andproperty
, you need to configure the mapping relationship through the@Results
annotation.@Result
is the corresponding mapping relation for each. Containscolumn
andproperty
.
- JDK11
- Maven
Some IDEs may not compile the clojure
code, so it is recommended that you run the following command when debugging or
running
mvn -P dev compile -DskipTests=true
Translated with www.DeepL.com/Translator (free version)