简体   繁体   English

使用现有数据库(模型)

[英]Using play with existing database ( models )

Can i use play framework linked to an existing database (Example SAP DBB) to only display:我可以使用链接到现有数据库(示例 SAP DBB)的播放框架来仅显示:

Dashboards (Queries)
Charts (Queries)

I developped an authentication page, suddenly, i did not tought about how to extract data from an existing database without declaring models!我开发了一个身份验证页面,突然间,我不知道如何在不声明模型的情况下从现有数据库中提取数据!

What is the good way to only extract with many queries data and display it on views scala (Play framework JAVA)?仅提取许多查询数据并将其显示在视图 scala(播放框架 JAVA)上的好方法是什么?

Thank you so much太感谢了

We also have many existing databases, without having a complete model of each table in Play applications.我们也有很多现有的数据库,在 Play 应用程序中没有每个表的完整 model。 I create a case class in the Play application for all necessary fields, also a subset of all columns.我在 Play 应用程序中为所有必要字段(也是所有列的子集)创建了一个案例 class。 It's Scala code, but also possible with Java, of course.这是 Scala 代码,当然也可以使用 Java。

  case class Positionstext(akt_abnr: Int
                   , akt_text_art: Int
                   , akt_text_pos: Int
                   , akt_text: String) {

} }

The carefully assembled SQL command retrieves rows from one or more tables.精心组装的 SQL 命令从一个或多个表中检索行。

  def positionstexte(x: Int) : List[Positionstext] = {

    DB.withConnection{ connection =>

    val select =
      """ select plr_ak_texte.akt_abnr
               , plr_ak_texte.akt_text_art
               , plr_ak_texte.akt_text_pos
               , plr_ak_texte.akt_text
            from plrv11.plr_ak_texte
               , plrv11.plr_auftr_status
           where plr_ak_texte.akt_abnr     =  plr_auftr_status.as_abnr
             and plr_ak_texte.akt_aend_ix  =  plr_auftr_status.as_aend_ix
             and plr_ak_texte.akt_abnr     =  ?
             and plr_ak_texte.akt_text_art =  7
             and plr_auftr_status.as_aend_ix <> 99
       """

    val prepareStatement = connection.prepareStatement(select)

    prepareStatement.setInt(1, x)

    val rs = prepareStatement.executeQuery

    var list: ListBuffer[Positionstext] = scala.collection.mutable.ListBuffer()

    while (rs.next) {
      list += new Positionstext(rs.getInt("akt_abnr")
                            , rs.getInt("akt_text_art")
                            , rs.getInt("akt_text_pos")
                            , rs.getString("akt_text"))
    }
    rs.close()
    prepareStatement.close()
    list.toList
  }

And that's it, The SQL command already does most of the work by using sub-queries.就是这样,SQL 命令已经通过使用子查询完成了大部分工作。 joins etc.加入等

All desired objects are in the list now and can display in the view.所有需要的对象现在都在列表中,并且可以显示在视图中。

Thank you for your return,谢谢你的回归,

This is what i did and it works pretty fine:这就是我所做的,它工作得很好:

package controllers;

import models.Sysuser;
import models.DataI;
import models.DataII;
import play.mvc.Controller;
import play.mvc.Result;
import play.mvc.Security;
import views.html.sitemap.index;

import javax.inject.*;
import java.util.concurrent.CompletionStage;
import play.libs.concurrent.HttpExecutionContext;
import static java.util.concurrent.CompletableFuture.supplyAsync;

import io.ebean.*;
import play.Logger;
import java.util.List;
import play.libs.Json;
import java.util.*;
import java.util.stream.*;

@Security.Authenticated(Secured.class)
public class SiteMap extends Controller {

    private final HttpExecutionContext httpExecutionContext;
    private static final Logger.ALogger logger = Logger.of(SiteMap.class);

    @Inject
    public SiteMap(HttpExecutionContext httpExecutionContext) {
        this.httpExecutionContext = httpExecutionContext;
    }   



    public CompletionStage<Result> index() {
      return supplyAsync(() -> {          
        return ok(views.html.sitemap.index.render(Sysuser.findByUserName(request().username()), QueryI(), QueryII() ));
        }, httpExecutionContext.current());
    }


    /**
     * Custom Query 1
     */ 
    public List<DataI> QueryI() {
                final String sql =  "SELECT sysuser_id, role_id "
                                   +"from sysuser_role "
                                   +"where sysuser_id = '1' "
                                   +"and role_id in ('1','2','3','4','5') ";
                final RawSql rawSql = RawSqlBuilder.parse(sql).create();                
                Query<DataI> query = Ebean.find(DataI.class);  
                query.setRawSql(rawSql);
                List<DataI> L = query.findList();
    return(L);
    }


    /**
     * Custom Query 2
     */ 
    public List<DataII> QueryII() {
                final String sql =  "SELECT sysuser.name, sysuser.active, department.description "
                                   +"from sysuser "
                                   +"left join department on department.id = sysuser.department_id "
                                   +"where sysuser.id = '2' ";
                final RawSql rawSql = RawSqlBuilder.parse(sql).create();                
                Query<DataII> query = Ebean.find(DataII.class);  
                query.setRawSql(rawSql);
                List<DataII> L = query.findList();
    return(L);
    }   





}

I am using Java instead of Scala, however, i don't think that there is a need of these codes such as: 1- DB.withConnection{ connection => 2- val prepareStatement = connection.prepareStatement(select)....and what else...我使用的是 Java 而不是 Scala,但是,我认为不需要这些代码,例如:1- DB.withConnection{ connection => 2- val prepareStatement = connection.prepareStatement(select)....还有什么...

What do you think about my code?你觉得我的代码怎么样? is it optimal?是最优的吗? I am going to use complexe queries to fill some dashboards in this template: https://adminlte.io/themes/v3/index.html我将使用复杂查询来填充此模板中的一些仪表板: https://adminlte.io/themes/v3/index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM