1. 程式人生 > >SSM 實訓筆記 -11- 使用 Spring MVC + JDBC Template 實現篩選、檢索功能(maven)

SSM 實訓筆記 -11- 使用 Spring MVC + JDBC Template 實現篩選、檢索功能(maven)

SSM 實訓筆記 -11- 使用 Spring MVC + JDBC Template 實現篩選、檢索功能(maven)

本篇是新建的一個數據庫,新建的一個完整專案。

本篇內容:
(1)使用 Spring MVC + JDBC Template 實現資料庫查詢

(2)實現對資料庫資訊的篩選、檢索功能

(3)分類查詢的功能

效果演示:

在這裡插入圖片描述

一、建立資料庫

(1)如果沒有 Navicat 可以自己手動在命令列建庫。
分享一篇文章:Navicat for MySQL 最新版安裝與破解 + 報錯解決辦法

(2)建立 hrdb 資料庫,建立 employee 表:

在這裡插入圖片描述

(3)建立 post 表(職位表):

在這裡插入圖片描述

二、建立和配置 Maven Web 專案、

(1)如果不會建立專案請參考:

(!特別提醒,建議專案名及包名和下面的本篇的專案檔名一致)

SSM 實訓筆記 -05- 建立 Maven Web 專案 + Tomcat 及目錄結構配置

(2)完整專案目錄結構:

(3)先建立專案,標記資料夾(java、resources)

(4)使用 Maven 載入依賴包:

  <dependencies>
    <dependency>
      <groupId>junit</
groupId
>
<artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.1.3.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.3.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.54</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies>

(5)開啟配置 web.xml 檔案:

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

(6)在 web.xml 檔案的目錄下,建立 springmvc.xml (建議名稱一致):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="com.xpwi.controller"/>

    <!--配置 json 訊息轉換器-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean>
            </list>
        </property>
    </bean>

</beans>

(7)同樣在 web.xml 檔案的目錄下,建立 applicationContext.xml (建議名稱一致):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="com.xpwi.dao,com.xpwi.service"/>
    <!--    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations" value="classpath*:jdbcConfig.properties">
            </property>
        </bean>-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/hrdb?serverTimezone=UTC"></property>
        <property name="username" value="root"></property>
        <property name="password" value="xiaopengwei"></property>
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
    </bean>
    <bean id="jdbcTemplage" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

(8)在 resources/jdbc.properties(手動建立的目錄,沒有直接建立此類的檔案的方式,直接新建 file,自己寫字尾名即可,內容:

(填寫自己建立的資料庫的資訊)

url=jdbc:mysql://localhost:3306/hrdb?serverTimezone=UTC
username=root
password=xiaopengwei
driverClassName=com.mysql.cj.jdbc.Driver

**(9)在 webapp/node 目錄下,安裝 jQuery 和 Bootstrap3 **:

步驟:

  • 安裝 node.js
  • 在 cmd 進入上述目錄
  • 使用:
npm install jquery
npm install bootstrap3

三、專案原始碼

(1)index.html 檔案:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查詢使用者</title>
    <link rel="stylesheet" href="node/node_modules/bootstrap3/dist/css/bootstrap.min.css">
    <script type="text/javascript" src="node/node_modules/jquery/dist/jquery.js"></script>
    <script type="text/javascript" src="node/node_modules/bootstrap3/dist/js/bootstrap.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                url:"findAllPost.do",
                //async:false,
                method:"get",
                dataType:"json",
                success:function (data) {
                    //alert(data);
                    var str="<option value='0'>未選擇</option>";
                    //var jsonObj = JSON.parse(data);
                    $.each(data,function (index,row,rs) {
                        str+="<option value='"+row.id+"'>"+row.post_name+"</option>";
                    });
                    $("#input_post").html(str);
                },
                error:function () {
                    alert("請求失敗");
                }
            });
        });
        function doSelect() {

            var input_name = $("#input_name").val();
            var input_post = $("#input_post").val();

            $.ajax({
                // url:"http://10.2.21.34:8080/StudentSystem/userServletForAjax",
                url:"doSelect.do",
                method:"get",
                data:{
                  "emp_name":input_name,
                  "post_type":input_post
                },
                dataType:"json",
                success:function (data) {
                    //alert("成功");
                    var str = "";
                    $.each(data,function (i,item) {
                        //對資料庫資料進行轉換
                        if (item.post_type == 1) {
                            item.post_type = "行政助理";
                        }else if(item.post_type == 2) {
                            item.post_type = "業務經理";
                        }else{
                            item.post_type = "總經理";
                        };
                        if (item.emp_sex == 1) {
                            item.emp_sex = "男";
                        }else{
                            item.emp_sex = "女";
                        };
                        str+="<tr><td><input type='checkbox' value='"
                            +item.id+"'></td><td>"
                            +item.id+"</td><td>"
                            +item.post_type+"</td><td>"
                            +item.emp_name+"</td><td>"
                            +item.emp_sex+"</td><td>"
                            +item.emp_age+"</td><td>"
                            +item.emp_depart+"</td><td>"
                            +item.emp_year+"</td><td>"
                            +"<a href='javascript:deleteUserById(\""+data.id+"\")' title='刪除' onclick='return confirm(\"是否真的刪除記錄?\")'><span class='glyphicon glyphicon-remove'></span>刪除</a></td></tr>";
                    });
                    $("#alluser").html(str);

                },
                error:function () {
                    alert("伺服器請求失敗")
                }
            })
        }
    </script>
    <!--資料展示部分 js -->
    <script>
        function addUserInfo() {
            $.post("deleteUserByIdServlet",{"uname":$("#uname").val(),"role":$("#role").val()},function (data) {
                if (data == "true"){
                    alert("新增成功!");
                    findAllUser();
                } else {
                    alert("新增失敗!");

                }

            });
        }

        function showAddModal() {
            $("#myModal").modal("show");
        }

        //刪除
        function deleteUserById(id) {
            $.ajax({
                url:"deleteUserByIdServlet",
                data:{"id":id},
                success:function (result) {
                    if (result=="true"){
                        findAllUser();
                    } else {
                        alert("刪除記錄失敗!");
                    }

                },error:function () {
                    alert("訪問伺服器失敗")
                }
            })

        }

        //查詢
        function findAllUser() {
            $.ajax({
                // url:"http://10.2.21.34:8080/StudentSystem/userServletForAjax",
                url:"doFindAllUser.do",
                method:"get",
                dataType:"json",
                success:function (data) {
                    //alert("成功");
                    var str = "";
                    $.each(data,function (i,item) {
                        //對資料庫資料進行轉換
                        if (item.post_type == 1) {
                            item.post_type = "行政助理";
                        }else if(item.post_type == 2) {
                            item.post_type = "部門經理";
                        }else{
                            item.post_type = "總經理";
                        };
                        if (item.emp_sex == 1) {
                            item.emp_sex = "男";
                        }else{
                            item.emp_sex = "女";
                        };
                        str+="<tr><td><input type='checkbox' value='"
                            +item.id+"'></td><td>"
                            +item.id+"</td><td>"
                            +item.post_type+"</td><td>"
                            +item.emp_name+"</td><td>"
                            +item.emp_sex+"</td><td>"
                            +item.emp_age+"</td><td>"
                            +item.emp_depart+"</td><td>"
                            +item.emp_year+"</td><td>"
                            +"<a href='javascript:deleteUserById(\""+data.id+"\")' title='刪除' onclick='return confirm(\"是否真的刪除記錄?\")'><span class='glyphicon glyphicon-remove'></span>刪除</a></td></tr>";
                    });
                    $("#alluser").html(str);

                },
                error:function () {
                    alert("伺服器請求失敗")
                }
            })

        }
        findAllUser();
    </script>

            
           

相關推薦

SSM 筆記 -11- 使用 Spring MVC + JDBC Template 實現篩選檢索功能maven

SSM 實訓筆記 -11- 使用 Spring MVC + JDBC Template 實現篩選、檢索功能(maven) 本篇是新建的一個數據庫,新建的一個完整專案。 本篇內容: (1)使用 Spring MVC + JDBC Template 實現資料庫查詢 (2)實現對資

SSM 筆記 -12- 開源 Spring+Spring MVC+JDBC Template 增刪改查 前期專案maven

SSM 實訓筆記 -12- 開源 Spring+Spring MVC+JDBC Template 增刪改查 前期專案(maven) 實訓前期小專案,大佬勿笑 僱員資訊管理系統 2019-01-11 專案簡介: 內容: Spring + Spring MVC

SSM 筆記 -09- 使用 Spring MVC + JDBC Template 實現登入maven

SSM 實訓筆記 -09- 使用 Spring MVC + JDBC Template 實現登入(maven) 本篇內容: (1)使用 Spring MVC 替代原來的令人腦殼兒疼的 Servlet。 (2)先看下 Spring MVC 對比 Servlet 的簡化程度:

SSM 筆記 -08- 使用 Spring JDBC Template 對資料庫查詢登入和刪除maven

SSM 實訓筆記 -08- 使用 Spring JDBC Template 對資料庫查詢(登入)和刪除(maven) Spring JDBC 提供了 Spring JDBC Template,大大簡化了對資料庫的操作,其中JdbcTemplate 是最常用的,下面介紹 本篇內容:

SSM 筆記 -10- 使用 sessionStorage 儲存資料js 圖片驗證碼登入載入動畫

SSM 實訓筆記 -10- 使用 sessionStorage 儲存資料、js 圖片驗證碼、登入載入動畫 本篇內容: (1)在登入成功時,使用 sessionStorage 儲存使用者的使用者名稱,並在登入成功後的首頁上展示使用者名稱。 (2)js 圖片驗證碼,使用 js 和

Spring MVC中@RequestParam/@RequestBody/@RequestHeader的用法收集

writer) public target load spa ive com query 我們 簡介: handler method參數綁定常用的註解,我們根據他們處理的Request的不同內容部分分為四類:(主要講解常用類型) A、處理requet uri部分(這裏指

Spring MVC通過CROS協議解決跨域問題轉載

Spring MVC通過CROS協議解決跨域問題 (轉載) 現在接手學校網路中心的一個專案,根據團隊成員的實際情況以及開發需要,老師希望做到前後端完全分離。後臺使用java提供restful API 作為核心,前臺

Spring 筆記 -06- 從 MySQL 建庫到 登入驗證資料庫資訊maven

Spring 筆記 -06- 從 MySQL 建庫到 登入驗證資料庫資訊(maven) 本篇和 Spring 沒有什麼關係,只是學習 Spring,必備一些知識,所以放在這裡了。 本篇內容: (1)MySQL 命令列,建立使用者資訊資料庫,密碼用 md5 加密 (2)jdbc 連結 MySQL 8.0

Spring 筆記 -04- 依賴注入例項 - 實現印表機功能maven

Spring 筆記 -04- 依賴注入例項 - 實現印表機功能(maven) 前面的文章是基礎,如果是剛開始學習,最好依次檢視:Spring 筆記 你可能會感覺這是把一個簡單的問題弄複雜了, 不要慌,你的錯覺沒有錯, 這裡主要是學習 Spring 的依賴注入, 很重要! 問題

Spring MVC中Session的正確用法之我見

Spring MVC是個非常優秀的框架,其優秀之處繼承自Spring本身依賴注入(Dependency Injection)的強大的模組化和可配置性,其設計處處透露著易用性、可複用性與易整合性。優良的設計模式遍及各處,使得其框架雖然學習曲線陡峭,但一旦掌握則欲罷不能。初學者並不需要過多瞭解框架的實現原理,隨

spring MVC 獲取servletContext,實現文件下載功能

項目 down min frame ping cte .get vax readfile 以下是獲取servletContext: import javax.servlet.ServletContext; import org.springframework.web.co

Spring整合Quartz框架實現定時任務跑批Maven完整版

觸發器 delay cut www 方法 lin job 定時任務 任務調度 Quartz 介紹Quartz is a full-featured, open source job scheduling service that can be integrated with

Spring Cloud 服務註冊與發現高可用Eureka

Spring Cloud 服務註冊與發現、高可用(Eureka) 一、服務發現 1.1 服務發現 在微服務架構中,服務發現元件是很

Spring MVC筆記(三) Spring MVC表單處理

名稱 command -i mat ppi post doctype form hello 創建動態WEB工程 FormHandling,並添加SpringMVC相關jar包(同Hello world示例一致),添加DispatcherServlet配置,如下: web.

筆記34 Spring MVC的高級技術——處理multipart形式的數據

sof AS con dynamic 解析器 upload pro reg 任務 一、需求介紹: Spittr應用在兩個地方需要文件上傳。當新用戶註冊應用的時候,我 們希望他們能夠上傳一張圖片,從而與他們的個人信息相關聯。當用 戶提交新的Spittle時,除了文本消息以外,

09.11:java重點內容介紹

package test; /* * * OP:面向物件的簡稱 * 類:同一特徵的屬性 * * 類的定義:具有相同特徵和行為的事物的抽象。(不具體化) * 物件(例項物件):具體真實存在的例項。 * 類是物件的例項: * * Java

筆記Spring MVC攔截入參出參實現入參解密,出參加密統一管理

需求:為提高介面的安全性,對資料傳輸加密。 前提:Controller層使用@RequestBody接收入參,@ResponseBody出參 入參解密 package com.sep6th.base.core.advice; import java.lang.re

Spring MVC學習筆記1——Spring MVC 簡介

什麼是Spring MVC Spring MVC是Spring框架的一個模組,Spring MVC和Spring無需通過中間整合層進行整合。 Spring MVC是一個基於MVC的web框架。 Spring MVC框架原理 發起請求到前端控制器(Dispat

ssm小結

   在本次實訓中,學習了SpringMVC、Mybatis及兩者的整合即ssm框架。SpringMVC的實現原理是通過Servlet攔截所有URL來達到控制的目的,也瞭解到和掌握了SpringMVC和Mybatis的使用步驟。 SpringMVC的流程 

最為一個大學生的筆記

實訓筆記:供於自己以後參考,大家也可以讀讀。 實訓第三天; 今天給我們將了敏捷開發的創始:thoughtworks thoughtworks:ThoughtWorks在90年代後期與相關專家合作首創了