Speedment - ORM

IDE

起步,建立一個 Maven 項目,在 pom.xml 中配置以下java

<build>
    <plugins>
        <plugin>
            <groupId>com.speedment</groupId>
            <artifactId>speedment-maven-plugin</artifactId>
            <version>3.0.18</version>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>com.speedment</groupId>
        <artifactId>runtime</artifactId>
        <version>3.0.18</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.44</version>
        <scope>runtime</scope>
    </dependency>
</dependencies>

Speedment UI

Speedment 提供了一個 Maven 插件,你能夠用它鏈接數據庫,並經過配置,生成項目代碼。mysql

  1. 經過命令或者 IDE 運行插件sql

    mvn speedment:tool

  2. 加入社區數據庫

  3. 鏈接數據庫小程序

  4. 生成 Java 代碼app

  5. 完成maven

Hello Speedment

hello world 項目,寫一個小程序,將輸入的用戶姓名、年齡持久化到 MySql 中。ui

  1. 準備數據庫插件

    CREATE DATABASE hellospeedment;
    USE hellospeedment;
    
    CREATE TABLE IF NOT EXISTS `user` (
        `id` bigint(20) NOT NULL AUTO_INCREMENT,
        `name` varchar(32) NOT NULL,
        `age` int(5) NOT NULL,
        PRIMARY KEY (`id`),
        UNIQUE KEY `name` (`name`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
  2. 經過插件生成代碼日誌

    generated 包中爲自動生成的 Application相關。

    hellospeedment.hellospeedment.user 包爲對應數據庫、表生成的實體相關。

  3. 初始化 Speedment ,寫一個主類以下

    public class Main {
    
        public static void main(String[] args) {
    
            // 建立一個 application 實例,指定數據庫密碼及日誌
            HellospeedmentApplication app = new HellospeedmentApplicationBuilder()
                    .withPassword("123456")
                    .withLogging(PERSIST)
                    .build();
    
            // 建立 entity manager 實例
            UserManager users = app.getOrThrow(UserManager.class);
    
            // 建立控制檯 Scanner
            final Scanner scn = new Scanner(System.in);
    
            // 接收輸入參數
            System.out.print("What is your name? ");
            final String name = scn.nextLine();
            System.out.print("What is your age? ");
            final int age = scn.nextInt();
    
            try {
                User user = new UserImpl()
                        .setName(name)
                        .setAge(age);
    
                // 持久化到數據庫
                user = users.persist(user);
    
                System.out.print("Hello, " + user.getName() + "!");
            } catch (SpeedmentException se) {
                System.out.print("Why are you so persistent?");
                se.printStackTrace();
            }
        }
    
    }

    運行結果以下

    What is your name? Gnar
    What is your age? 3
    2017-11-18T08:18:18.760Z DEBUG [main] (#PERSIST) - INSERT INTO `hellospeedment`.`user` (`id`,`name`,`age`) VALUES (?,?,?), values:[0, Gnar, 3]
    Hello, Gnar!

    HellospeedmentApplication 是由 Speedment 自動生成的,從名稱上看,這個實例表明應用程序。它負責處理與數據庫的鏈接,並進行 全部的初始化操做。

    GeneratedUser 接口定義了實體的數據庫屬性,及 SetterGetter方法。

    GeneratedUserImpl 中實現了SetterGetter,至關因而一個 Java bean。

    UserManager 是實體的管理者,它實現了 Manager 接口,後者定義了一些 CRUD 的(默認)方法,以及 Stream 對象。

    UserImpl 是實體的最終實現,一些業務邏輯寫到這裏面。

相關文章
相關標籤/搜索