• (四)Alian 的 Spring Cloud Ebean自动生成Query Bean


    一、简介

       Query Bean 是可选的,但它们提供了一种使用编写查询的方法,它们还易于使用和学习属性和表达式。 Query Bean 是使用 Java annotation processing (APT) 或 Kotlin annotation processing (KAPT)生成的。

      对于每个实体,将生成一个具有相同名称,但以前缀Q开头的对象,比如对于名为 Order 的实体 Bean,会生成一个名为 QOrder 的查询 Bean。

      当实体 Bean 模型更改时, Query Bean 也要重新生成,如果我们的查询对模型不再有效,则会收到编译时错误。本次我们主要使用maven插件生成。

      使用Ebean一个是工作中用到,另外一个是确实是非常的方便,当然你们使用jpa或者mybatis都没有关系,反正这个就是创建一个实体的工程。只不过如果你是跟着我去做这个SpringCloud,我建议你还是使用Ebean,毕竟它还是一个非常牛逼的技术。

    二、准备工作

    2.1、Ebean插件安装

      我这里的插件版本是 Ebean10.x Enhancement ,你们可以根据各自的环境需要安装。
    在这里插入图片描述

    三、maven依赖

    3.1、pom.xml

    pom.xml

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        
        <parent>
            <groupId>cn.alian.microservicegroupId>
            <artifactId>parentartifactId>
            <version>1.0.0-SNAPSHOTversion>
        parent>
    
        <groupId>cn.alian.domaingroupId>
        <artifactId>domain-orderartifactId>
        <version>1.0.0-SNAPSHOTversion>
        <name>ordername>
        <description>订单domaindescription>
    
        <dependencies>
            <dependency>
                <groupId>io.github.hexagonframework.bootgroupId>
                <artifactId>spring-boot-starter-data-ebeanartifactId>
            dependency>
            <dependency>
                <groupId>io.ebeangroupId>
                <artifactId>ebeanartifactId>
            dependency>
            <dependency>
                <groupId>io.ebeangroupId>
                <artifactId>ebean-querybeanartifactId>
            dependency>
            <dependency>
                <groupId>io.ebeangroupId>
                <artifactId>querybean-generatorartifactId>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <inherited>falseinherited>
                    <executions>
                        <execution>
                            <id>spring-boot-repackageid>
                            <phase>nonephase>
                        execution>
                    executions>
                plugin>
    
    			
                <plugin>
                    <groupId>io.repaint.mavengroupId>
                    <artifactId>tiles-maven-pluginartifactId>
                    <version>2.26version>
                    <extensions>trueextensions>
                    <configuration>
                        <tiles>
                            <tile>org.avaje.tile:java-compile:1.1tile>
                            <tile>io.ebean.tile:enhancement:12.2.6tile>
                        tiles>
                    configuration>
                plugin>
    
            plugins>
        build>
    
        <repositories>
            <repository>
                <id>nexus-reposid>
                <name>Team Nexus Repositoryname>
                <url>http://192.168.0.210:8081/nexus/content/groups/publicurl>
            repository>
            <repository>
                <id>nexus-repos-snapshotsid>
                <name>Team Nexus Repository Snapshotsname>
                <url>http://192.168.0.210:8081/nexus/content/groups/public-snapshotsurl>
            repository>
            <repository>
                <id>aliyun-maven-repoid>
                <url>https://maven.aliyun.com/repository/publicurl>
            repository>
        repositories>
    
        <pluginRepositories>
            <pluginRepository>
                <id>nexus-reposid>
                <name>Team Nexus Repositoryname>
                <url>http://192.168.0.210:8081/nexus/content/groups/publicurl>
            pluginRepository>
            <pluginRepository>
                <id>nexus-repos-snapshotsid>
                <name>Team Nexus Repository Snapshotsname>
                <url>http://192.168.0.210:8081/nexus/content/groups/public-snapshotsurl>
            pluginRepository>
        pluginRepositories>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

      这里具体的依赖的版本号,可以查看我之前的文章,并且不要盲目的使用高版本的Ebean,而想要打包时自动生成 Query Bean 的关键在于下面这个插件

    <plugin>
        <groupId>io.repaint.mavengroupId>
        <artifactId>tiles-maven-pluginartifactId>
        <version>2.26version>
        <extensions>trueextensions>
        <configuration>
            <tiles>
                <tile>org.avaje.tile:java-compile:1.1tile>
                <tile>io.ebean.tile:enhancement:12.2.6tile>
            tiles>
        configuration>
    plugin>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    四、domain

    4.1、实体

    Order.java

    package cn.alian.domain.order.domain;
    
    import io.ebean.annotation.DbEnumValue;
    import lombok.Getter;
    import lombok.Setter;
    
    import javax.persistence.*;
    import java.io.Serializable;
    import java.time.LocalDateTime;
    
    @Setter
    @Getter
    @Entity
    @Table(name = "tb_order")
    public class Order implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        /**
         * 主键
         */
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "id", insertable = false, nullable = false)
        private Integer id;
    
        /**
         * 用户id
         */
        @Column(name = "user_id", nullable = false)
        private int userId;
    
        /**
         * 商品id
         */
        @Column(name = "goods_id", nullable = false)
        private int goodsId;
    
        /**
         * 单价
         */
        @Column(name = "price", nullable = false)
        private Integer price = 0;
    
        /**
         * 数量
         */
        @Column(name = "num", nullable = false)
        private Integer num = 1;
    
        /**
         * 订单标题
         */
        @Column(name = "title", nullable = false)
        private String title = "";
    
        /**
         * 订单状态
         */
        @Column(name = "order_status", nullable = false)
        private String orderStatus = "01";
    
        /**
         * 创建时间
         */
        @Column(name = "create_time", nullable = false, insertable = false, updatable = false)
        private LocalDateTime createTime;
    
        /**
         * 更新时间
         */
        @Column(name = "update_time", nullable = false, insertable = false, updatable = false)
        private LocalDateTime updateTime;
    
        public enum StatusEnum {
    
            SUCCESS("00", "成功"),
            HANDING("01", "处理中"),
            FAIL("02", "失败");
    
            private final String code;
    
            private final String desc;
    
            StatusEnum(String code, String desc) {
                this.code = code;
                this.desc = desc;
            }
    
            @DbEnumValue
            public String getCode() {
                return code;
            }
    
            public String getDesc() {
                return desc;
            }
    
            public static Order.StatusEnum get(String code) {
                Order.StatusEnum[] values = Order.StatusEnum.values();
                for (Order.StatusEnum e : values) {
                    if (e.code.equals(code)) {
                        return e;
                    }
                }
                return null;
            }
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110

    4.2、持久层

    OrderRepository.java

    package cn.alian.domain.order.repository;
    
    import cn.alian.domain.order.domain.Order;
    import org.springframework.data.ebean.repository.EbeanRepository;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface OrderRepository extends EbeanRepository<Order, Integer> {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

      啥也不用做了,打包就行了,以后我们的实体都按照上面的方式写,然后去打包,它会为我们生成很多的静态查询方法,后面我会对使用进行简单举例。

    4.3、脚本

    deploy.bat

    cd %~dp0
    cd..
    
    call mvn clean source:jar deploy -Dmaven.test.skip=true
    
    cd bin
    pause
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    package.bat

    cd %~dp0
    cd..
    
    call mvn clean package -Ppack -X -Dmaven.test.skip=true
    
    cd bin
    pause
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4.4、打包效果

    最终的结果:
    在这里插入图片描述

    4.5、使用举例

      使用方法简单举例:

    //查询订单id为123订单信息
    int id=123;
    Order order = new QOrder()._id().eq(id).findOne();
    
    • 1
    • 2
    • 3
    //查询用户id为456的所有订单
    int userId=456;
    List<Order> orderList = new QOrder()._userId().eq(userId).findList();
    
    • 1
    • 2
    • 3
    //查询用户id为456的已完成的订单(已完成状态为00)
    int userId=456;
    String orderStatus="00";
    List<Order> orderList = new QOrder()._userId().eq(userId).and()._orderStatus().eq(orderStatus).findList();
    
    • 1
    • 2
    • 3
    • 4

    4.6、官方文档

      更多的关于使用参考官网

    https://ebean.io/docs/query/

  • 相关阅读:
    产品需求分析师的基本职责(合集)
    Jsp基础了解(二)
    阿里云的CIPU
    Appium自动化测试基础 — ADB常用命令(二)
    Power BI 如何使用Tooltip创建悬浮报表页 (自定义工具提示)
    【每日一题】避免洪水泛滥
    Golang sync.Pool
    虚拟补丁备忘单
    python使用字典暴力解析wifi密码
    docsify搭建个人博客——简单公共知识库
  • 原文地址:https://blog.csdn.net/Alian_1223/article/details/124025894