2017-07-14 8 views
1

私は、特定の場所/発注日に配置された発注のようなフィルタ条件をユーザが選択するJavaでWebアプリケーションを作成したいと思いますデータがデータベースから抽出される必要がある条件に基づいて、特定のサービスのために使用される。バックエンドJavaクラスファイルを作成してDBからデータを抽出することはできますが、フロントエンドJSP /サーブレットからフィルタ条件を渡す方法はわかりません。誰も私にこれを達成する方法の例を教えてもらえますか?ユーザフィルタ条件に基づいてDBからデータを抽出するJavaアプリケーション

+0

嫌な気持ちはありませんが、これはそのような質問のための適切な場所ではありません。 – Arvind

答えて

0

データベース接続で(この場合はREST)Webサービスを実装する最も簡単な方法は、おそらくSpring Bootとhibernate(これはSpringの標準のオブジェクトリレーショナルマッピングツールです)を使用することです。 @RestControllerの必須変数で要求を処理し、このようなトランザクションの@Serviceクラスで処理してください(この例では、クライアントから送信された変数にmedia type application/x-www-form-urlencodedを使用します) :

コントローラー: "本当の" サービスクラスは、スプリングによってautowiredすることができるように

@RestController 
public class Controller { 

    @Autowired 
    Service service; //Service needs to be an interface in order to be autowired 

    @RequestMapping(method = RequestMethod.GET, value = "/insertTheUrlYouWantToUse", 
      consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 
    public ResponseEntity<Response> yourMethod(@RequestParam String var1, 
      @RequestParam String var2){ 
     //Your code here 
     //Example code: 
     Response r = service.process(var1, var2); //Response would be your custom class. 
                //Of course you can use any other 
                //existing class if it fits your needs 
     return new ResponseEntity<Response>(r, HttpStatus.OK); 
    } 
} 

サービス・インターフェースが必要とされている:

public interface UpdateService { 

    Response process(String var1, String var2); 
} 

ServiceImplは、 "本当の" クラスになります。データベース接続を行うには、トランザクション処理が必要です。私は唯一のエンティティは次のようになり、これを完了するには

@Service 
@Repository 
@Transactional 
public class ServiceImpl implements Service{ 

    @PersistenceContext 
    private EntityManager em; //needed for all the database related stuff 

    public Response process(String var1, String var2){ 
     try{ 
      Entity entity = em 
      .createQuery("FROM Entity WHERE var1=:var1 AND var2=:var2", Entity.class) 
      .setParameter("var1", var1).setParameter("var2", var2).getSingleResult(); 
     }catch(NoResultException e){ 
      //whatever you want to do if no entity is available 
     } 
     return new Response(Entity); //return the result to the Controller, depending on 
            //you Response you can obviously add messages, links 
            //and other stuff if you like 
    } 
} 

(?どのように創造的な私は、右)「エンティティ」と呼ばれる1つのデータベース・エンティティで作業するつもりです:

@Entity 
@Table(name="TableNameInDatabase") 
public class Entity implements Serializable{ 

    @Id 
    @Column(name="ID", unique = true, nullable = false) 
    private String var1; 

    @Column(name="someValue") 
    private String var2; 

    //add getter, setter and constructor (too lazy for that) 
} 

あなたはすべきです"http://yourHostname:yourPort/insertTheUrlYouWantToUse?var1=someValue&var2=someOtherValue"を呼び出すことができます 私はクラスレスポンスをここに書くつもりはありません。コンストラクタがカスタムレスポンスでResponseEntityを返すと、レスポンスのすべての変数は、クライアントがレスポンスを受け取ったときにxmlに変換されます。 Springでクライアントを実装する場合は、同じReponseオブジェクトを期待し、クライアント側でそれを処理し続けることができます。私はあなたの質問に何らかの形で答えてくれることを願っています。 Springブートを使用したくない場合は、そこに相当するライブラリ/フレームワークがあり、いくらか似たような方法で動作すると確信しています。

+1

私はこれがJavascriptとしてマークされているのを見ましたが、ここでポイントを逃してしまったと思っています。 – Naeramarth

関連する問題