當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
web service方法进行全文检索_SpringMVC(Web应用)配置教程终章项目实战
生活随笔
收集整理的這篇文章主要介紹了
web service方法进行全文检索_SpringMVC(Web应用)配置教程终章项目实战
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
建立數(shù)據(jù)源本章是SpringMVC(Web應(yīng)用)配置教程系列的最后一章了,感謝大家一路陪伴,一起成長(zhǎng)。這篇文章較長(zhǎng),建議先收藏再看,這一章將鏈接之前所有的部分,之前沒有看過(guò)的也可以在這篇中學(xué)習(xí)。項(xiàng)目實(shí)戰(zhàn)將實(shí)現(xiàn)一個(gè)簡(jiǎn)單的查詢操作,然后把查詢到的用戶數(shù)據(jù)渲染到頁(yè)面上。前提是已經(jīng)創(chuàng)建好數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)中創(chuàng)建了test_info的表,字段有user_id,user_name,值分別為1和binarystudio,然后在applicationContext配置文件中添加該數(shù)據(jù)源:?<property?name="driverClassName"><value>com.mysql.jdbc.Drivervalue>property><property?name="url">?<value>你的數(shù)據(jù)庫(kù)路徑value>?property>?<property?name="username"><value>你的數(shù)據(jù)庫(kù)用戶名(通常默認(rèn)為root)value>property>?<property?name="password">?<value>你的數(shù)據(jù)庫(kù)密碼value>?property>
今日推薦文章
數(shù)據(jù)源回顧
點(diǎn)擊下方圖片即可閱讀
接著在pojo中為項(xiàng)目創(chuàng)建該數(shù)據(jù)源類,pojo在命名最好和我們數(shù)據(jù)庫(kù)建的表相同,使用類似于JPA的結(jié)構(gòu)是最好的:package com.SpringMVC.pojo;public class test_info {?private?int?user_id;?private?String?user_name;?public?int?getUser_id()?{???????return?user_id;????}?public?void?setUser_id(int?usr_id)?{???????this.user_id?=?usr_id;????}?public?String?getUser_name()?{???????return?user_name;????}?public?void?setUser_name(String?user_name)?{???????this.user_name?=?user_name; }}POJO回顧點(diǎn)擊下方圖片即可閱讀我們還需要在applicationContext配置文件中將pojo與數(shù)據(jù)源綁定:
"sqlSession" ?"typeAliasesPackage"???"dataSource"???"mapperLocations"? 關(guān)聯(lián)數(shù)據(jù)源與Mapper首先為test_info這個(gè)表創(chuàng)建mapper接口:
package com.SpringMVC.mapper;public?interface?TestInfoMapper?{}?在該接口中創(chuàng)建業(yè)務(wù)方法,查詢?cè)摫碇兴袛?shù)據(jù),返回的類型是List:package com.SpringMVC.mapper;import com.SpringMVC.pojo.test_info;import?java.util.List;public interface TestInfoMapper {List?selectALLTestInfo();}接著在mapper.xml配置文件中添加sql語(yǔ)句,我們是做查詢操作,就使用select語(yǔ)句,通過(guò)id綁定我們創(chuàng)建的業(yè)務(wù)方法,返回的參數(shù)類型為test_info:<?xml version="1.0" encoding="UTF-8"?>/span>?PUBLIC?"-//mybatis.org//DTD?Mapper?3.0//EN"?"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.SpringMVC.mapper.TestInfoMapper"> <select id="selectALLTestInfo" resultType="test_info"> select * from test_info select>mapper>mapper回顧點(diǎn)擊下方圖片即可閱讀實(shí)現(xiàn)Service接口創(chuàng)建我們的Service接口并添加業(yè)務(wù)方法,因?yàn)橐邮誱apper數(shù)據(jù)源的數(shù)據(jù),所以返回的類型與mapper接口中的一致,方法名也和mapper中一樣:package com.SpringMVC.Service;import com.SpringMVC.pojo.test_info;import?java.util.List;public?interface?TestService?{ ListselectAllTestInfo();}接著創(chuàng)建ServiceImpl類繼承Service接口,重寫接口中的方法:package com.SpringMVC.Service.Impl;import?com.SpringMVC.Service.TestService;import com.SpringMVC.pojo.test_info;import org.springframework.stereotype.Service;import?java.util.List;@Servicepublic?class?TestServiceImpl?implements?TestService?{????public?List?selectAllTestInfo()?{ return null; }}注入我們的數(shù)據(jù)源Mapper,將其傳遞給ServiceImpl:
package com.SpringMVC.Service.Impl;import?com.SpringMVC.Service.TestService;import com.SpringMVC.mapper.TestInfoMapper;import com.SpringMVC.pojo.test_info;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import?java.util.List;@Servicepublic class TestServiceImpl implements TestService { @Autowired TestInfoMapper testInfoMapper; ????public?List?selectAllTestInfo()?{ return testInfoMapper.selectALLTestInfo(); }}service回顧點(diǎn)擊下方圖片即可閱讀創(chuàng)建業(yè)務(wù)控制器創(chuàng)建好業(yè)務(wù)控制器,指定HTTP請(qǐng)求路徑為handleTestInfo:package com.SpringMVC.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@RequestMapping("handleTestInfo")@Controllerpublic?class?TestController?{?}注入Service,創(chuàng)建一個(gè)方法用來(lái)處理ModelAndView:package com.SpringMVC.controller;import com.SpringMVC.Service.TestService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;@RequestMapping("handleTestInfo")@Controllerpublic class TestController { @Autowired TestService testService; @RequestMapping("selectAllTestInfo") private ModelAndView selectAllTestInfo()?????{???????}}控制器回顧點(diǎn)擊下方圖片即可閱讀接著我們創(chuàng)建ModelAndView的實(shí)例,建立List數(shù)據(jù)模型接收Service返回的數(shù)據(jù):
package com.SpringMVC.controller;import?com.SpringMVC.Service.TestService;import com.SpringMVC.pojo.test_info;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import?java.util.List;@RequestMapping("handleTestInfo")@Controllerpublic class TestController { @Autowired TestService testService; @RequestMapping("selectAllTestInfo")??????public?ModelAndView?selectAllTestInfo() { ModelAndView mav = new ModelAndView();?????????List?test_infoList?=?testService.selectAllTestInfo();????????}}ModelAndView回顧點(diǎn)擊下方圖片即可閱讀前端數(shù)據(jù)渲染為ModelAndView添加模型數(shù)據(jù),并返回給視圖:
package com.SpringMVC.controller;import?com.SpringMVC.Service.TestService;import com.SpringMVC.pojo.test_info;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import?java.util.List;@RequestMapping("handleTestInfo")@Controllerpublic class TestController { @Autowired TestService testService; @RequestMapping("selectAllTestInfo")??????public?ModelAndView?selectAllTestInfo() { ModelAndView mav = new ModelAndView(); List test_infoList = testService.selectAllTestInfo(); mav.addObject("test_info",test_infoList); mav.setViewName("index"); return mav;?????}}接著在jsp視圖中添加jstl表達(dá)式,用來(lái)渲染ModelAndView中的數(shù)據(jù):
"text/html;charset=UTF-8" language= import="java.util.*"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><html><head> <title>Titletitle>head><body>body>html>此外,還需要在視圖解析類中添加jstl屬性:
class=???????"viewClass"?????????value="org.springframework.web.servlet.view.JstlView"?/>??????"prefix"???????"suffix"??在JSP中使用jstl表達(dá)式中的foreach標(biāo)簽遍歷從控制器傳來(lái)的List數(shù)據(jù)模型:
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><html><head> <title>Titletitle>head><body><c:forEach items="${test}" var="c"> <a> ${c.user_id}a> ${c.user_name}c:forEach>body>html>運(yùn)行測(cè)試所有配置已經(jīng)完成,業(yè)務(wù)層、數(shù)據(jù)源都已經(jīng)寫完了,接下來(lái)就讓全部的代碼跑起來(lái)吧,在瀏覽器中輸入http://localhost:8080/handleTestInfo/selectAllTestInfo即可看到運(yùn)行結(jié)果:今日推薦文章
輕觸下方探索BinaryStudio
讓我知道你在看
總結(jié)
以上是生活随笔為你收集整理的web service方法进行全文检索_SpringMVC(Web应用)配置教程终章项目实战的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python爬取百度百科表格_第一个py
- 下一篇: 百度首页html简单代码_百度站长平台为