NebulaSQL
NebulaSQL 是一个用于 Nebula Framework 的 SQL 生成器,它可以根据 Nebula Framework 的领域模型生成 SQL 语句。
用法:
- 实现WhereGroups接口,用于定义查询条件(代码生成插件自动生成)。
java
public class UserWhereGroup extends WhereGroups {
public UserWhereGroup andIdIsNull() {
addCriterion("id", EnumOperator.IS_NULL, null);
return this;
}
public UserWhereGroup andIdIsNotNull() {
addCriterion("id", EnumOperator.IS_NOT_NULL, null);
return this;
}
public UserWhereGroup andIdEqualTo(Long value) {
addCriterion("id", EnumOperator.EQUAL, value);
return this;
}
public UserWhereGroup andIdNotEqualTo(Long value) {
addCriterion("id", EnumOperator.NOT_EQUAL, value);
return this;
}
public UserWhereGroup andIdBetween(Long value1, Long value2) {
addCriterion("id", EnumOperator.GREATER_EQUAL_THAN, value1);
addCriterion("id", EnumOperator.LESS_EQUAL_THAN, value2);
return this;
}
public UserWhereGroup andIdNotBetween(Long value1, Long value2) {
addCriterion("id", EnumOperator.LESS_THAN, value1);
addCriterion("id", EnumOperator.GREATER_THAN, value2);
return this;
}
public UserWhereGroup andIdLikeTo(Long value) {
addCriterion("id", EnumOperator.LIKE, true, value, true);
return this;
}
public UserWhereGroup andIdLikeTo(boolean prefix, Long value) {
addCriterion("id", EnumOperator.LIKE, prefix, value, false);
return this;
}
public UserWhereGroup andIdLikeTo(Long value, boolean suffix) {
addCriterion("id", EnumOperator.LIKE, false, value, suffix);
return this;
}
public UserWhereGroup andIdNotLikeTo(Long value) {
addCriterion("id", EnumOperator.NOT_LIKE, true, value, true);
return this;
}
public UserWhereGroup andIdNotLikeTo(boolean prefix, Long value) {
addCriterion("id", EnumOperator.NOT_LIKE, prefix, value, false);
return this;
}
public UserWhereGroup andIdNotLikeTo(Long value, boolean suffix) {
addCriterion("id", EnumOperator.NOT_LIKE, false, value, suffix);
return this;
}
public UserWhereGroup andIdIn(List<Long> list) {
addCriterion("id", EnumOperator.IN, list);
return this;
}
public UserWhereGroup andIdNotIn(List<Long> list) {
addCriterion("id", EnumOperator.NOT_IN, list);
return this;
}
// ……
}
- 通过建立
NebulaSQL
实例,来创建SQL语句对象。
java
NebulaSQL nebulaSQL = new NebulaSQL();
nebulaSQL.createWhereGroups(UserWhereGroup.class)
.andCreateTimeBetween(query.getStartCreateTime(),query.getEndCreateTime())
.andUpdateTimeBetween(query.getStartUpdateTime(),query.getEndUpdateTime())
.andCreateUserEqualTo(query.getCreateUser())
.andUpdateUserEqualTo(query.getUpdateUser())
.andNameLikeTo(query.getName())
.andPasswordLikeTo(query.getPassword())
.andDescriptionLikeTo(query.getDescription())
.andEmailLikeTo(query.getEmail())
.andMobilePhoneLikeTo(query.getMobilePhone())
.andEnabledEqualTo(query.getEnabled())
.andDeletedEqualTo(query.getDeleted());
- 通过实现Mybatis的
BaseMapper
接口,来实现自定义的SQL语句。
java
public interface UserMapper extends BaseMapper<User> {
List<User> selectByWhereGroups(NebulaSQL nebulaSQL);
}
- 通过代码生成插件,生成对应的
XXXMapper.xml
通用文件,来实现方法的调用和实现。
xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<!--
This file is auto-generated by nebula-framework-generator.
The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
Thanks to JetBrains for their outstanding contributions to the developer community.
The code generated by this tool is owned by the user of the tool.
The tool itself is copyrighted by https://www.neegix.com.
@author https://www.neegix.com
@version 1.0.0
@since 2024-12-03 11:47:28
-->
<mapper namespace="com.neegix.system.user.infrastructure.repository.mapper.UserMapper">
<!-- 用户管理-表和对象映射关系 -->
<resultMap id="UserResultMap" type="com.neegix.system.user.infrastructure.repository.dataobject.UserDO">
<id column="id" property="id" />
<id column="create_time" property="createTime" />
<id column="update_time" property="updateTime" />
<id column="create_user" property="createUser" />
<id column="update_user" property="updateUser" />
<id column="name" property="name" />
<id column="password" property="password" />
<id column="description" property="description" />
<id column="email" property="email" />
<id column="mobile_phone" property="mobilePhone" />
<id column="enabled" property="enabled" />
<id column="deleted" property="deleted" />
</resultMap>
<!-- 用户管理-查询条件 -->
<sql id="wheres">
<where>
<if test="whereClauses != null and whereClauses.size() > 0">
<foreach collection="whereClauses" item="group" separator=" OR ">
<if test="group.wheres != null and group.wheres.size() > 0">
<trim prefix="(" suffix=")" prefixOverrides="AND ">
<foreach collection="group.wheres" item="where" separator=" AND ">
<choose>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@EQUAL and where.value != null">
${where.column} = #{where.value}
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@NOT_EQUAL and where.value != null">
${where.column} != #{where.value}
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@GREATER_THAN and where.value != null">
${where.column} > #{where.value}
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@LESS_THAN and where.value != null">
${where.column} < #{where.value}
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@GREATER_EQUAL_THAN and where.value != null">
${where.column} >= #{where.value}
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@LESS_EQUAL_THAN and where.value != null">
${where.column} <= #{where.value}
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@LIKE and where.prefix and where.suffix and where.value != null">
${where.column} LIKE CONCAT('%', #{where.value}, '%')
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@LIKE and where.prefix and !where.suffix and where.value != null">
${where.column} LIKE CONCAT('%', #{where.value})
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@LIKE and !where.prefix and where.suffix and where.value != null">
${where.column} LIKE CONCAT(#{where.value}, '%')
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@LIKE and !where.prefix and !where.suffix and where.value != null">
${where.column} LIKE #{where.value}
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@NOT_LIKE and where.prefix and where.suffix and where.value != null">
${where.column} NOT LIKE CONCAT('%', #{where.value}, '%')
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@NOT_LIKE and where.prefix and !where.suffix and where.value != null">
${where.column} NOT LIKE CONCAT('%', #{where.value})
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@NOT_LIKE and !where.prefix and where.suffix and where.value != null">
${where.column} NOT LIKE CONCAT(#{where.value}, '%')
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@NOT_LIKE and !where.prefix and !where.suffix and where.value != null">
${where.column} NOT LIKE #{where.value}
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@IN and where.value != null and where.value.size() > 0">
${where.column} IN
<foreach collection="where.value" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@NOT_IN and where.value != null and where.value.size() > 0">
${where.column} NOT IN
<foreach collection="where.value" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@IS_NULL">
${where.column} IS NULL
</when>
<when test="where.enumOperator == @com.neegix.application.query.EnumOperator@IS_NOT_NULL">
${where.column} IS NOT NULL
</when>
<!-- 添加其他操作符的处理 -->
</choose>
</foreach>
</trim>
</if>
</foreach>
</if>
</where>
</sql>
<!-- 用户管理-字段列 -->
<sql id="columns">
id,create_time,update_time,create_user,update_user,name,password,description,email,mobile_phone,enabled,deleted
</sql>
<!-- 条件查询-用户管理-数量 -->
<select id="selectCount" resultType="java.lang.Long">
select count(1) from sys_user
<include refid="wheres" />
<if test="groupByClauses != null and groupByClauses.size() > 0">
GROUP BY
<foreach collection="groupByClauses" item="groupBy" separator=", ">
${groupBy}
</foreach>
</if>
<if test="havingClauses != null and havingClauses.size() > 0">
HAVING
<foreach collection="havingClauses" item="having" separator=" AND ">
${having}
</foreach>
</if>
</select>
<!-- 条件查询-用户管理-单条记录 -->
<select id="selectOne" resultMap="UserResultMap">
SELECT
<if test="distinct">DISTINCT</if>
<choose>
<when test="groupByClauses != null and groupByClauses.size() > 0">
<foreach collection="groupByClauses" item="groupBy" separator=", ">
${groupBy}
</foreach>
</when>
<otherwise>
<include refid="columns" />
</otherwise>
</choose>
FROM sys_user
<include refid="wheres" />
<if test="groupByClauses != null and groupByClauses.size() > 0">
GROUP BY
<foreach collection="groupByClauses" item="groupBy" separator=", ">
${groupBy}
</foreach>
</if>
<if test="havingClauses != null and havingClauses.size() > 0">
HAVING
<foreach collection="havingClauses" item="having" separator=" AND ">
${having}
</foreach>
</if>
<if test="orderByClauses != null and !orderByClauses.isEmpty()">
ORDER BY
<foreach collection="orderByClauses" item="orderBy" separator=", ">
${orderBy.column} ${orderBy.enumOrder}
</foreach>
</if>
<if test="pager != null">
LIMIT #{pager.offset}, #{pager.pageSize}
</if>
</select>
<!-- 条件查询-用户管理-记录 -->
<select id="selectList" resultMap="UserResultMap">
SELECT
<if test="distinct">DISTINCT</if>
<choose>
<when test="groupByClauses != null and groupByClauses.size() > 0">
<foreach collection="groupByClauses" item="groupBy" separator=", ">
${groupBy}
</foreach>
</when>
<otherwise>
<include refid="columns" />
</otherwise>
</choose>
FROM sys_user
<include refid="wheres" />
<if test="groupByClauses != null and groupByClauses.size() > 0">
GROUP BY
<foreach collection="groupByClauses" item="groupBy" separator=", ">
${groupBy}
</foreach>
</if>
<if test="havingClauses != null and havingClauses.size() > 0">
HAVING
<foreach collection="havingClauses" item="having" separator=" AND ">
${having}
</foreach>
</if>
<if test="orderByClauses != null and !orderByClauses.isEmpty()">
ORDER BY
<foreach collection="orderByClauses" item="orderBy" separator=", ">
${orderBy.column} ${orderBy.enumOrder}
</foreach>
</if>
<if test="pager != null">
LIMIT #{pager.offset}, #{pager.pageSize}
</if>
</select>
<!-- 根据Id查询-用户管理-记录 -->
<select id="selectByPrimaryKey" resultMap="UserResultMap">
select <include refid="columns" /> from sys_user where id=#{id,jdbcType=BIGINT}
</select>
<!--新增-用户管理-记录-->
<insert id="insert" parameterType="com.neegix.system.user.infrastructure.repository.dataobject.UserDO">
insert into sys_user
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="createTime != null">create_time,</if>
<if test="updateTime != null">update_time,</if>
<if test="createUser != null">create_user,</if>
<if test="updateUser != null">update_user,</if>
<if test="name != null">name,</if>
<if test="password != null">password,</if>
<if test="description != null">description,</if>
<if test="email != null">email,</if>
<if test="mobilePhone != null">mobile_phone,</if>
<if test="enabled != null">enabled,</if>
<if test="deleted != null">deleted,</if>
</trim>
<trim prefix="VALUES (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="createUser != null">#{createUser},</if>
<if test="updateUser != null">#{updateUser},</if>
<if test="name != null">#{name},</if>
<if test="password != null">#{password},</if>
<if test="description != null">#{description},</if>
<if test="email != null">#{email},</if>
<if test="mobilePhone != null">#{mobilePhone},</if>
<if test="enabled != null">#{enabled},</if>
<if test="deleted != null">#{deleted},</if>
</trim>
</insert>
<!--更新-用户管理-记录-->
<update id="updateByPrimaryKey" parameterType="com.neegix.system.user.infrastructure.repository.dataobject.UserDO">
update sys_user
<set>
<if test="createTime != null">
create_time = #{createTime},
</if>
<if test="updateTime != null">
update_time = #{updateTime},
</if>
<if test="createUser != null">
create_user = #{createUser},
</if>
<if test="updateUser != null">
update_user = #{updateUser},
</if>
<if test="name != null">
name = #{name},
</if>
<if test="password != null">
password = #{password},
</if>
<if test="description != null">
description = #{description},
</if>
<if test="email != null">
email = #{email},
</if>
<if test="mobilePhone != null">
mobile_phone = #{mobilePhone},
</if>
<if test="enabled != null">
enabled = #{enabled},
</if>
<if test="deleted != null">
deleted = #{deleted},
</if>
</set>
where id = #{id}
</update>
<!--批量删除-用户管理-记录-->
<delete id="deleteByPrimaryKeys">
delete from sys_user
<where>
id in
<foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
#{id, jdbcType=BIGINT}
</foreach>
</where>
</delete>
</mapper>