在数据库视图上显示记录

时间:2018-11-28 07:50:58

标签: java database spring list

首先,我是spring mvc的新手。我很困惑,无法显示数据库中与员工名字匹配的所有员工记录。

我尝试如下从SearchController的文本字段中获取员工的名字并将其发送到实体类AddEmployee以正确分配。在正确分配了firstname后,我将其发送到AddDao类的getallEmployee方法。

 package com.roshan.empl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.roshan.empl.dao.AddDao;
import com.roshan.empl.entity.AddEmployee;
import com.roshan.empl.service.EmployeeService;

@Controller
public class SearchController {

    @Autowired
    AddEmployee add;

    @Autowired
    AddDao adao;

     @RequestMapping("/search")
        public String search()
         {

            return "search.jsp";
        }

     @RequestMapping(value= "/searchEmployee" , method=RequestMethod.POST)
     public String searchEmployee(@RequestParam("firstname") String firstname)

     {
         add = new AddEmployee(firstname);
         adao.getAllEmployee(add);
         return "" ;

     }

在AddDao类中,getAllEmployee()返回与员工名字匹配的所有记录。

 package com.roshan.empl.dao;

import javax.transaction.Transactional;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.roshan.empl.entity.AddEmployee;

import antlr.collections.List;

@Component
public class AddDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Transactional
    public void addEmployee(AddEmployee ad) {

    System.out.println("in addemployee" +ad);   

    Session session = sessionFactory.getCurrentSession(); 
    session.save(ad);

    }

    //public AddEmployee getEmployee(String firstname) {

    //  return(AddEmployee) sessionFactory.getCurrentSession().get(AddEmployee.class,firstname);
    //}

    public List getAllEmployee(AddEmployee firstname) {

        String hql ="FROM EMPLOYEE E WHERE E.firstname = firstname";
        return (List) sessionFactory.getCurrentSession().createQuery("from Employee").list();

    }

}

但是我想知道如何将这些匹配的记录存储在SearchaController中(可能使用ArrayList,LinkedList),并将这些记录发送到视图serach.jsp的表的相应列中。

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="ISO-8859-1">
<title> Search Employee </title>
</head>
<body>

    <form action="/searchEmployee">

    <tr>
         <td>
         <input type="text" placeholder="Search by First Name" name="firstname" required>
         </td>

        <td colspan="2">
             <input type="submit"  value="Search Employee">
         </td>
     </tr>
     </form>



     <br>
     <br>

           <tr>
                <td></td>
                     <td><a href="index.jsp">Home</a>
                  </td>
           </tr>


<br>
<br>
<br>

<table border="1">
    <th>ID</th>
    <th>First name</th>
    <th>Last name</th>
    <th>Email</th>
    <th>Address</th>
    <th>Age</th>
    <th>Phone</th>
    <c:forEach items="$ " var=" ">
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td> </td>
            <td></td>
            <td> </td>
            <td></td>

        </tr>
    </c:forEach>
</table>

</body>
</html>

0 个答案:

没有答案