CSDN話題挑戰(zhàn)賽第2期
參賽話題:學(xué)習(xí)筆記
*學(xué)習(xí)之路,長(zhǎng)路漫漫,寫(xiě)學(xué)習(xí)筆記的過(guò)程就是把知識(shí)講給自己聽(tīng)的過(guò)程。這個(gè)過(guò)程中,我們?nèi)ビ涗浰伎嫉倪^(guò)程,便于日后復(fù)習(xí),梳理自己的思路。學(xué)習(xí)之樂(lè),獨(dú)樂(lè)樂(lè),不如眾樂(lè)樂(lè),把知識(shí)講給更多的人聽(tīng),何樂(lè)而不為呢?
??博客x主頁(yè):己不由心王道長(zhǎng)??!
??文章說(shuō)明:spring??
?系列專(zhuān)欄:spring
??本篇內(nèi)容:使用spring、web監(jiān)聽(tīng)器、過(guò)濾器+servlet+druid+mybatis實(shí)現(xiàn)免登錄時(shí)長(zhǎng)兩天半(對(duì)所需知識(shí)點(diǎn)進(jìn)行選擇閱讀呀~)??
??每日一語(yǔ):人生寂寞是一種力量。人經(jīng)得起寂寞,就能獲得自由;耐不住寂寞,就會(huì)受人牽制。??
??作者詳情:作者是一名雙非大三在校生,喜歡Java,歡迎大家探討學(xué)習(xí),喜歡的話請(qǐng)給博主一個(gè)三連鼓勵(lì)。??
??前言
- 凡事自己多動(dòng)手動(dòng)腦、聽(tīng)取他人優(yōu)秀的觀點(diǎn),做一個(gè)會(huì)學(xué)習(xí)的人。
??一、創(chuàng)建項(xiàng)目框架
①在這里創(chuàng)建一個(gè)maven工程,選擇web模板,JDK選擇1.8:
創(chuàng)建maver工程以后,可能目錄結(jié)構(gòu)并不完整,接下來(lái)我們要構(gòu)建一個(gè)完整的目錄結(jié)構(gòu),只有完整的目錄結(jié)構(gòu),在后面部署在tomcat服務(wù)器上時(shí)才能正常使用。
②補(bǔ)全maven目錄結(jié)構(gòu):打開(kāi)項(xiàng)目—>找到src目錄—>選中右鍵—>選擇新建(new)—>選擇目錄—>重復(fù)四次,直到把目錄補(bǔ)全。
至此我們就完全了一個(gè)工程的創(chuàng)建。
??二、導(dǎo)入前端頁(yè)面資源
要實(shí)現(xiàn)免登錄時(shí)長(zhǎng)兩天半,我們得找到前端資源,并導(dǎo)入我們的項(xiàng)目中。
① 前端資源怎么找?我這里提供了本次項(xiàng)目的前端資源,需要的可以私信我?;蛘咧苯铀阉鱦Query之家也可以獲取相應(yīng)資源
② 導(dǎo)入到哪里? 導(dǎo)入到項(xiàng)目下的Webapp目錄下
③前端效果展示
④前端頁(yè)面分析:可以看到兩個(gè)頁(yè)面是相互跳轉(zhuǎn)的,當(dāng)在登錄頁(yè)面填好密碼和賬號(hào)以后,點(diǎn)擊登錄就會(huì)向下經(jīng)過(guò)Web層調(diào)控,然后調(diào)用Sevice。。。往下查詢數(shù)據(jù)庫(kù)并返回查詢結(jié)構(gòu),有用戶存在則跳轉(zhuǎn)到歡迎頁(yè),沒(méi)有則跳轉(zhuǎn)登錄頁(yè)。相應(yīng)的注冊(cè)成功之后也跳轉(zhuǎn)到登錄頁(yè)面。在這個(gè)過(guò)程中使用過(guò)濾器過(guò)濾,沒(méi)有登錄則不能訪問(wèn)歡迎頁(yè),訪問(wèn)則直接跳轉(zhuǎn)到登錄頁(yè)。
通過(guò)分析,前端頁(yè)面要執(zhí)行的主要操作有兩個(gè)個(gè)。一個(gè)是登錄(login)、一個(gè)是注冊(cè)(register)。
??三、pom.xml依賴坐標(biāo)導(dǎo)入
在原先免登錄時(shí)長(zhǎng)兩天半的依賴上,本次結(jié)合了spring方法,使用的是xml格式開(kāi)發(fā)項(xiàng)目。需要導(dǎo)入spring依賴、測(cè)試依賴和spring集成web依賴。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- 添加測(cè)試依賴-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 添加mybatis依賴-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- 添加servlet依賴-->
<!-- 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>
<!-- 添加MySQL依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!-- 添加jsp依賴-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!-- 添加el表達(dá)式依賴-->
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- 添加jstl標(biāo)簽庫(kù)依賴-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!--添加druid依賴-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--添加spring依賴、添加core就行了。core包含了依賴注入和控制反轉(zhuǎn)-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.22</version>
</dependency>
<!--添加spring-web集成-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.22</version>
</dependency>
</dependencies>
</project>
??四、Web層(controller)層搭建
通過(guò)上面的分析,我們先創(chuàng)建兩個(gè)servlet,即LoginServlet、RegisterServlet。實(shí)現(xiàn)對(duì)應(yīng)接口,重寫(xiě)doGet、doPost方法,并在doPost方法中調(diào)用doGet方法。
??五、Spring、Spring集成Web
要使用Spring、大致分為以下幾步:
① 導(dǎo)入Spring依賴坐標(biāo): pom.xml文件已經(jīng)導(dǎo)入完畢。
② 編寫(xiě)核心配置文件applicationContext.xml: 不一定叫這個(gè)名兒。只是約定俗成而已,但是這個(gè)見(jiàn)名知意,推薦使用
<?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 https://www.springframework.org/schema/context/spring-context.xsd">
</beans>
Spring集成Web也分為下面幾步:
① 導(dǎo)入Spring-web依賴坐標(biāo): pom.xml文件已經(jīng)導(dǎo)入完畢。
② 配置ContextLoaderListener監(jiān)聽(tīng)器
首先思考一個(gè)問(wèn)題一個(gè)問(wèn)題,我們要用到Spring,那么Spring總得創(chuàng)建吧?怎么創(chuàng)建呢?是調(diào)用一次就new一次嗎?顯然不是,那到底什么時(shí)候創(chuàng)建,放在什么地方才能保存持久?。。。?br>答:使用監(jiān)聽(tīng)器在加載項(xiàng)目的時(shí)候創(chuàng)建,存儲(chǔ)在應(yīng)用域,應(yīng)用域范圍廣
③ 在web層調(diào)用Spring
怎么獲取Spring的上下文對(duì)象,Spring-Web提供了一個(gè)工具WebApplicationContextUtils供使用者獲得應(yīng)用上下文對(duì)象,想使用,直接調(diào)用,一遍獲取前面都要加一個(gè)get,所以方法是getApplicationContext
package com.bipt.controller;
import com.bipt.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 不止于夢(mèng)想
* @date 2022/10/7 22:55
*/
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//獲取servletContext上下文對(duì)象、Spring容器創(chuàng)建保留在這里。
ServletContext servletContext = this.getServletContext();
//獲取spring容器、傳入servletContext上下文對(duì)象
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//調(diào)用容器內(nèi)的方法、獲取容器內(nèi)的對(duì)象
UserService userService = app.getBean(UserService.class);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
至此,完成了Spring和Web的集成。
??六、Spring控制反轉(zhuǎn)和依賴注入
控制反轉(zhuǎn)IOC
控制反轉(zhuǎn)是Spring的一個(gè)特性,以前的對(duì)象是我們自己創(chuàng)建的,想要的時(shí)候就new一個(gè)。現(xiàn)在用到Spring、Spring是一個(gè)大容器,我們把對(duì)象的創(chuàng)建交給Spring,當(dāng)加載Spring配置文件時(shí),就會(huì)創(chuàng)建相應(yīng)的bean對(duì)象,并且保存在Spring容器中。
值得注意的是,id在Spring當(dāng)中作為唯一標(biāo)識(shí)、用來(lái)識(shí)別不同的bean,id不能相同!!
同一個(gè)類(lèi)型可以創(chuàng)建多個(gè),但要調(diào)整scope(范圍),scope里常用的singleton(單例)prototype(多例)。創(chuàng)建多個(gè)相同類(lèi)型時(shí),scope設(shè)置為prototype。默認(rèn)情況下是singleton(單例)。
依賴注入DI
用到spring,可以對(duì)程序進(jìn)行解耦合,但不代表沒(méi)有聯(lián)系。在Service層中想要調(diào)用Mapper層的方法,在過(guò)去要在Service層中添加Mapper層對(duì)象作為屬性,這屬于代碼層面的綁定死。
上面的綁定死肯定不是我們要的,那么怎么辦?通過(guò)Spring 依賴注入(DI)把相應(yīng)的需求在配置文件中進(jìn)行設(shè)置,把耦合關(guān)系從代碼中抽離出來(lái)。在配置文件中,我們需求改變的時(shí)候可以直接修改配置文件,而不用修改源代碼,這符合軟件設(shè)計(jì)的原則
??七、Spring整合druid數(shù)據(jù)庫(kù)連接池和加載jdbcTemple
druid(德魯伊)數(shù)據(jù)庫(kù)連接池是一個(gè)優(yōu)秀的工具,在這里我們整合其作為我們的一部分。同樣的,第一部分也是導(dǎo)入相應(yīng)的位置坐標(biāo)。
然后在spring核心配置文件進(jìn)入bean導(dǎo)入,這樣就可以在啟用時(shí)創(chuàng)建在ioc容器當(dāng)中。
這里我配置了一個(gè)jdbc.properties配置文件做為存儲(chǔ)jdbc代碼的地方。當(dāng)然,這里不配置也沒(méi)事。沒(méi)有配置則直接在value處添加相應(yīng)數(shù)據(jù)。例如在name=driver的property中的value也可以為:com.mysql.jdbc.driver
如果配置了配置文件,就把它引進(jìn)來(lái)即可,然后像上面一樣依次取值。
加載jdbcTemple
同樣的,需要導(dǎo)入相應(yīng)坐標(biāo),這個(gè)是spring關(guān)于jdbc的一個(gè)模板工具。
然后配置:
值得注意的是這個(gè)bean的參數(shù)name也叫dataSource,而ref后的dataSource是上面druid配置數(shù)據(jù)源的信息。
至此,已經(jīng)把druid、jdbcTemple與spring整合好了。
??八、集成Web、druid、jdbcTemple實(shí)現(xiàn)免登錄時(shí)長(zhǎng)兩天半
至此,進(jìn)入我們最后一步,具體的實(shí)現(xiàn)各個(gè)層面的應(yīng)用。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-639657.html
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>spring-web</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- 添加測(cè)試依賴-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- 添加mybatis依賴-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- 添加servlet依賴-->
<!-- 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>
<!-- 添加MySQL依賴-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!-- 添加jsp依賴-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!-- 添加el表達(dá)式依賴-->
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<!-- 添加jstl標(biāo)簽庫(kù)依賴-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!--添加druid依賴-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--添加spring依賴、添加core就行了。core包含了依賴注入和控制反轉(zhuǎn)-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<!--添加spring-web集成-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- spring事務(wù)依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
</dependencies>
</project>
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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--控制反轉(zhuǎn)+依賴注入-->
<bean id="UserMapper" class="com.bipt.mapper.impl.implUserMapper">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="UserService" class="com.bipt.service.implService.ImplUserService">
<property name="userMapper" ref="UserMapper"></property>
</bean>
<!--加載導(dǎo)入配置文件-->
<context:property-placeholder location="classpath:/dbConfig.properties"/>
<!--配置數(shù)據(jù)源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
??總結(jié)
在這次的實(shí)踐中,發(fā)現(xiàn)了很多問(wèn)題,有的問(wèn)題還有待修復(fù),但是總體完成了。需要源原件的可以聯(lián)系博主。**文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-639657.html
到了這里,關(guān)于【spring】集成Web、druid、jdbcTemple實(shí)現(xiàn)免登錄時(shí)長(zhǎng)兩天半的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!