1. Trang chủ
  2. » Công Nghệ Thông Tin

Thực hành EJB (Enterprise Java Bean) Lab 11

7 201 1

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 7
Dung lượng 533,67 KB

Nội dung

Enterprise Application Development in Java EE Lab 11 Query and Criteria API Mục tiêu - Sử dụng Criteria Query API để viết truy vấn Phần I Bài tập step by step Bài 7.1 Xây dựng truy vấn tìm danh sách customer theo Customer Name ứng dụng EJB xây dựng 6.1 6.2 Step 1: Khai báo phương thức searchCustomer CRMApplication-ejb  CustomerBeanLocal.java  CustomerBeanLocal.java package beanpack; import entity.Address; import entity.Customer; import java.util.List; import javax.ejb.Local; /** * * @author Quang */ @Local public interface CustomerBeanLocal { public void createCustomer(Customer customer, List listAddress); public List searchCustomer(String custName); } IT Research Department @BKAP 2015 Page / Enterprise Application Development in Java EE Step 2: Thực thi phương thức searchCustomer CustomerBean.java Sử dụng Criteria query để truy vấn customer theo Customer Name  CustomerBean.java package beanpack; import entity.Address; import entity.Customer; import entity.Customer_; import java.util.List; import javax.ejb.Stateless; import javax.ejb.TransactionManagement; import javax.ejb.TransactionManagementType; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.ParameterExpression; import javax.persistence.criteria.Root; /** * * @author Quang */ @Stateless @TransactionManagement(TransactionManagementType.CONTAINER) public class CustomerBean implements CustomerBeanLocal { //Persistence @PersistenceContext(unitName = "CRMApplication-ejbPU") EntityManager em; @Override public void createCustomer(Customer customer, List listAddress) { customer.setListAddress(listAddress); for (Address addr : listAddress) { addr.setCustomer(customer); } em.persist(customer); } @Override public List searchCustomer(String custName) { List listCus = null; CriteriaBuilder cb = em.getCriteriaBuilder(); //Tao doi tuong CriteriaQuery tu cb CriteriaQuery cq = cb.createQuery(Customer.class); //Tao root interface tu criteria query Root cust = cq.from(Customer.class); // Tao ParameterExpression ParameterExpression p = cb.parameter(String.class); //dieu kien truy van cq.select(cust).where(cb.like(cust.get(Customer_.customerName), "%" + custName + "%")); IT Research Department @BKAP 2015 Page / Enterprise Application Development in Java EE //Tao loai truy van TypedQuery query = em.createQuery(cq); //Nhan ket qua listCus = query.getResultList(); return listCus; } } Step 3: Tạo form nhập thông tin Customer Name để search  CRMApplication-war  Web Pages  New  Other  Web  JSP  CustomerSearch.jsp Customer Search Page Customer Name: Step 4: Tạo Servlet CustomerSearchServlet thực việc search Customer theo Customer Name  CRMApplication-war  Source Packages  Servlet  New  Other  Web  Servlet IT Research Department @BKAP 2015 Page / Enterprise Application Development in Java EE  CustomerSearchServlet.java package servlet; import beanpack.CustomerBeanLocal; IT Research Department @BKAP 2015 Page / Enterprise Application Development in Java EE import entity.Address; import entity.Customer; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.ejb.EJB; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author Quang */ public class CustomerSearch extends HttpServlet { //Goi Enterprise Bean @EJB private CustomerBeanLocal customerBean; /** * Processes requests for both HTTP GET and POST * methods * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); //Nhan thong tin Customer Name nhap vao de search String custName = request.getParameter("customerName"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here You may use following sample code */ out.println(""); out.println(""); out.println(""); out.println("Servlet CustomerSearch"); out.println(""); out.println(""); //Goi phuong thuc searchCustomer List listCus = customerBean.searchCustomer(custName); //Hien thi danh sach Customer tim duoc out.println("Customer NameAddress"); for (Customer cust : listCus) { out.println(""+cust.getCustomerName()+""); for (Address addr : cust.getListAddress()) { out.println("Address Type - "+addr.getAddressType()); out.println("| Address Line - "+addr.getAddressLine() +""); } out.println(""); } out.println(""); IT Research Department @BKAP 2015 Page / Enterprise Application Development in Java EE out.println(""); out.println(""); } } // /** * Handles the HTTP GET method * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP POST method * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// }  Ứng dụng sau hoàn thành IT Research Department @BKAP 2015 Page / Enterprise Application Development in Java EE Step 5: Build and Run Application IT Research Department @BKAP 2015 Page / ... entity.Customer_; import java. util.List; import javax .ejb. Stateless; import javax .ejb. TransactionManagement; import javax .ejb. TransactionManagementType; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext;... import java. io.IOException; import java. io.PrintWriter; import java. util.List; import javax .ejb. EJB; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest;... javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.ParameterExpression;

Ngày đăng: 07/05/2018, 16:34

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN