java - Spring MVC/Hibernate/mySQL "read"始终为空

标签 java mysql hibernate spring-mvc

我已经阅读了一些帖子,但我无法弄清楚我在这里做错了什么。

我有一个简单的股票报价 Web 应用程序,可以从 mySQL 数据库读取数据。现在,我想要完成的只是从数据库中“搜索”(读取)符号。

我可以对符号进行硬编码,并且它按预期工作,但是当我在实际字段中键入符号时,它被读取为“null”并且没有任何反应。我对此很陌生,五月份才开始编码,所以我知道我很可能只是忽略了一些东西。

任何帮助将不胜感激,因为我已经为此工作了大约一周,但我只是不知道发生了什么。我要在网上上学,所以我没有很多人可以联系!谢谢!

Controller :

@Controller
@RequestMapping("/stockQuote")
public class StockQuoteController {

// inject StockService
@Autowired
private StockService stockQuoteService;

@RequestMapping("/list")
public String listStocks (Model theModel) throws IOException {

    // get the quotes form the dao
    List<StockQuote> theQuote = stockQuoteService.getStockQuotes();

    // add the customers to the model
    theModel.addAttribute("stockQuote", theQuote);
    return "list-stocks";

}


@GetMapping("/index")
public String setupForm(Map<String, Object> map){
    StockQuote stockQuote = new StockQuote();
    map.put("student", stockQuote);
    map.put("studentList", stockQuoteService.getStockQuotes());
    return "search";
}

@PostMapping("/doSearch")
public String doActions(@ModelAttribute StockQuote stockQuote, Model theModel, String symbol) throws IOException {

    List<StockQuote> theQuote = stockQuoteService.searchQuotes(symbol);

    theModel.addAttribute("stockQuote", theQuote);

    return "search";

}

}

StockQuoteDAOimpl:

@Repository
public class StockQuoteDAOimpl implements StockQuoteDAO {
//I need to inject the session factory
@Autowired
private SessionFactory sessionFactory;



@Override
public List<StockQuote> getStockQuote() {

    // I need to get the current hibernate session
    Session currentSession = sessionFactory.getCurrentSession();

    // create a query

    Query<StockQuote> theQuery = currentSession.createQuery("from StockQuote order by symbol", StockQuote.class);

    // execute query and get result list
    List<StockQuote> stockQuotes = theQuery.getResultList();

    // return list of stock quotes

    return stockQuotes;
}

@Override
public List<StockQuote> searchQuotes(String symbol) {


 Session currentSession = sessionFactory.getCurrentSession();
 sessionFactory.openSession();

 Query<StockQuote> theQuery = currentSession.createQuery("from StockQuote where symbol = '" + symbol + "'",
            StockQuote.class);

    List<StockQuote> stockQuotes = theQuery.getResultList();


    return stockQuotes;
}

}

StockQuoteServiceImpl:

@Service
@EnableTransactionManagement
public class StockQuoteServiceImpl implements StockService {

@Autowired // injecting our StockQuoteDAO
private StockQuoteDAO stockQuoteDAO;

@Override
@Transactional // handles DB transactions at the service level
public List<StockQuote> getStockQuotes() {
    return stockQuoteDAO.getStockQuote();
}

@Override
@Transactional
public List<StockQuote> searchQuotes(String symbol) {
    return stockQuoteDAO.searchQuotes(symbol);
}

}

StockQuote DAO: @实体 @索引 @启用事务管理 @Table(名称=“股票报价”) 公共(public)类 StockQuote 扩展 StockData {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@Column(name = "price", nullable = false, length = 20)
@org.hibernate.search.annotations.Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
private BigDecimal price;

@Column(name = "date", nullable = false, length = 20)
@org.hibernate.search.annotations.Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
private Date date;

@Column(name = "symbol", nullable = false, length = 20)
@org.hibernate.search.annotations.Field(index = Index.YES, analyze = Analyze.YES, store = Store.NO)
private String symbol;


public StockQuote() {}

public StockQuote(String symbol, BigDecimal price, Date date) {
    super();
    this.price = price;
    this.date = date;
    this.symbol = symbol;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public BigDecimal getPrice() {
    return price;
}

public void setPrice(BigDecimal price) {
    this.price = price;
}

public Date getDate() {
    return date;
}

public void setDate(Date date) {
    this.date = date;
}

public String getSymbol() {
    return symbol;
}

public void setSymbol(String symbol) {
    this.symbol = symbol;
}

}

搜索.jsp:

<!DOCTYPE html>
<html>
<head>
<title>Stock Quote Query</title>

<link type="text/css"
      rel="stylesheet"
      href="${pageContext.request.contextPath}/resources/css/style.css">

<link type="text/css"
      rel="stylesheet"
      href="${pageContext.request.contextPath}/resources/css/add-customer-style.css">

<div id="wrapper">
<div id="header">
    <h2>Easy Quote Query</h2>
</div>
</div>
<div id="container">
<html>
<body>
<h2>Search for Stock</h2>

  <form:form action="doSearch" method="post" 
 modelAttribute="stockQuote">

    Symbol: <input type="search" name="searchText"/><br/>

    <br><br>

    <input type="submit" name="action" value="search"/>



</form:form>

<br><br>

<h2>Found Stocks</h2>
<table border="1">
    <th>Symbol</th>
    <th>Price</th>
    <th>Date</th>
    <c:forEach items="${stockQuote}" var="stock">
        <tr>
            <td>${stock.symbol}</td>
            <td>${stock.price}</td>
            <td>${stock.date}</td>
        </tr>
    </c:forEach>
</table>



</body>
</html>
</div>

</body>
</html>

最佳答案

我自己解决了这个问题:

问题是我没有将符号映射到 View 。我添加了 @ModelAttribute("searchText") ,它映射到我 View 中的搜索字段 (name="searchText"),它现在可以工作:

 @PostMapping("/doSearch")
 public String doActions(@ModelAttribute StockQuote stockQuote, Model 
 theModel, @ModelAttribute("searchText") String symbol) throws 


IOException {

  List<StockQuote> theQuote = 
  stockQuoteService.searchQuotes(symbol);
  theModel.addAttribute("stockQuote", theQuote);

  return "search";

 }

关于java - Spring MVC/Hibernate/mySQL "read"始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47145500/

相关文章:

java - : Lcom/vungle/publisher/FullScreenAdActivity from inside codename one app 解析失败

java - Java-同一对象,不同变量,多个线程

mysql - mysql 数据库中用于在数据库中插入 CSV 文件的以下命令的等效命令是什么

java - 如何找到午夜过后最快的 Java 日期?

php - 在 PHP 中使用变量更新 MySQL

mysql - Order by 没有使用索引,奇怪的行为?

java - Spring 事务内的 Hibernate 实体生命周期和 session 生命周期

java - Netbeans + Derby + hibernate

mysql - Tomcat 8、HikariCP、Hibernate 和 MySQL

java - 从 Selenium 杀死远程浏览器进程