代码拉取完成,页面将自动刷新
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title></title>
<link href="/javanblog/2021/04/25/SpringMVC/"/>
<url>/javanblog/2021/04/25/SpringMVC/</url>
<content type="html"><![CDATA[<h1 id="Spring-MVC"><a href="#Spring-MVC" class="headerlink" title="Spring MVC"></a>Spring MVC</h1><h2 id="1-什么是Spirng-MVC"><a href="#1-什么是Spirng-MVC" class="headerlink" title="1 什么是Spirng MVC"></a>1 什么是Spirng MVC</h2><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201201212217243.png" alt="image-20201201212217243"></p><blockquote><p>概述</p></blockquote><p>Spirng MVC是Spring Framework的一部分,是基于java实现MVC的轻量级Web框架。</p><p>官方文档:<a href="https://docs.spring.io/spring-framework/docs/current/reference/html/web.html">https://docs.spring.io/spring-framework/docs/current/reference/html/web.html</a></p><p>为什么要学习Spring MVC?</p><p>Spring MVC 特点</p><ul><li>轻量级,简单易学</li><li>高效,基于请求响应的MVC框架</li><li>与 Spring 兼容性好,无缝结合</li><li>约定大于配置</li><li>功能强大,RESTful,数据验证,格式化,本地化,主题等</li><li>简洁灵活</li></ul><p>Spring MVC的web框架围绕<strong>DispatcherServlet</strong> [ 调度Servlet ] 设计。</p><p>DispatcherServlet的作用是将请求分发到不同的处理器。从Spring 2.5开始,使用Java 5或者以上版本的用户可以采用基于注解形式进行开发,十分简洁。</p><p><strong>最重要的一点还是用的人多 , 使用的公司多 。</strong></p><blockquote><p>中心控制器</p></blockquote><p>Spring MVC框架像许多其他MVC框架一样, <strong>以请求为驱动</strong> , <strong>围绕一个中心Servlet分派请求及提供其他功能</strong>,**DispatcherServlet是一个实际的Servlet (它继承自HttpServlet 基类)**。</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201201213059093.png" alt="image-20201201213059093"></p><p>SpringMVC的原理如下图所示:</p><p>当发起请求时被前置的控制器拦截到请求,根据请求参数生成代理请求,找到请求对应的实际控制器,控制器处理请求,创建数据模型,访问数据库,将模型响应给中心控制器,控制器使用模型与视图渲染视图结果,将结果返回给中心控制器,再将结果返回给请求者。</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201201213349055.png" alt="image-20201201213349055"></p><blockquote><p>Spring MVC 执行原理</p></blockquote><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201201213630465.png" alt="image-20201201213630465"></p><p>图为SpringMVC的一个较完整的流程图,实线表示SpringMVC框架提供的技术,不需要开发者实现,虚线表示需要开发者实现。</p><p><strong>执行流程:</strong></p><ol><li><p>DispatcherServlet表示前置控制器,是整个SpringMVC的控制中心。用户发出请求,DispatcherServlet接收请求并拦截请求。</p><p>我们假设请求的url为 : <a href="http://localhost:8080/SpringMVC/hello">http://localhost:8080/SpringMVC/hello</a></p><p><strong>如上url拆分成三部分:</strong></p><p><a href="http://localhost:8080服务器域名">http://localhost:8080服务器域名</a></p><p>SpringMVC部署在服务器上的web站点</p><p>hello表示控制器</p><p>通过分析,如上url表示为:请求位于服务器localhost:8080上的SpringMVC站点的hello控制器。</p></li><li><p>HandlerMapping为处理器映射。DispatcherServlet调用HandlerMapping,HandlerMapping根据请求url查找Handler。</p></li><li><p>HandlerExecution表示具体的Handler,其主要作用是根据url查找控制器,如上url被查找控制器为:hello。</p></li><li><p>HandlerExecution将解析后的信息传递给DispatcherServlet,如解析控制器映射等。返回HandlerExecutionChain对象给中央控制器,封装了HandlerIntercepter(处理器拦截器)、Handler(处理器对象)</p></li><li><p>HandlerAdapter表示处理器适配器,其按照特定的规则去执行Handler。</p></li><li><p>Handler让具体的Controller执行。</p></li><li><p>Controller将具体的执行信息返回给HandlerAdapter,如ModelAndView。</p></li><li><p>HandlerAdapter将视图逻辑名或模型传递给DispatcherServlet。</p></li><li><p>DispatcherServlet调用视图解析器(ViewResolver)来解析HandlerAdapter传递的逻辑视图名。</p></li><li><p>视图解析器将解析的逻辑视图名传给DispatcherServlet。</p></li><li><p>DispatcherServlet根据视图解析器解析的视图结果填充数据,调用具体的视图。</p></li><li><p>最终视图呈现给用户。</p></li></ol><h2 id="2-第一个MVC程序"><a href="#2-第一个MVC程序" class="headerlink" title="2 第一个MVC程序"></a>2 第一个MVC程序</h2><p>1、创建Maven父项目,然后删除src文件,在pom.xml中添加依赖。</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependencies</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>junit<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>junit<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>4.12<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.springframework<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>spring-webmvc<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>5.2.0.RELEASE<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>javax.servlet<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>servlet-api<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>2.5<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>javax.servlet.jsp<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>jsp-api<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>2.2<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="comment"><!--EL表达式标签支持--></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>javax.servlet<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>jstl<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>1.2<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependencies</span>></span></span><br></pre></td></tr></tbody></table></figure><p>2、新建一个Module,Maven工程,名为springmvc-02-hello,添加web框架支持!</p><p>3、正常来说需要配置Spring MVC 依赖,由于我们在父工程中配置了,所以此处可以省略。</p><p>4、配置web.xml文件,注册DispatcherServlet。</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">web-app</span> <span class="attr">xmlns</span>=<span class="string">"http://xmlns.jcp.org/xml/ns/javaee"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">version</span>=<span class="string">"4.0"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--1 配置DispatcherServlet--></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-name</span>></span>DispatcherServlet<span class="tag"></<span class="name">servlet-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-class</span>></span>org.springframework.web.servlet.DispatcherServlet<span class="tag"></<span class="name">servlet-class</span>></span></span><br><span class="line"> <span class="comment"><!--关联一个springmvc配置文件:【servlet-name】-servlet.xml--></span></span><br><span class="line"> <span class="tag"><<span class="name">init-param</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-name</span>></span>contextConfigLocation<span class="tag"></<span class="name">param-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-value</span>></span>classpath:springmvc-servlet.xml<span class="tag"></<span class="name">param-value</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">init-param</span>></span></span><br><span class="line"> <span class="comment"><!--设置启动级别,1表是跟随服务器启动--></span></span><br><span class="line"> <span class="tag"><<span class="name">load-on-startup</span>></span>1<span class="tag"></<span class="name">load-on-startup</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">servlet</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-mapping</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-name</span>></span>DispatcherServlet<span class="tag"></<span class="name">servlet-name</span>></span></span><br><span class="line"> <span class="comment"><!--/ 匹配所有请求,不包括.jsp--></span></span><br><span class="line"> <span class="comment"><!--/* 匹配所有请求,包括.jsp--></span></span><br><span class="line"> <span class="tag"><<span class="name">url-pattern</span>></span>/<span class="tag"></<span class="name">url-pattern</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">servlet-mapping</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">web-app</span>></span></span><br></pre></td></tr></tbody></table></figure><p>5、编写Spring MVC 的配置文件!,名称springmvc-servlet.xml:【servletname】-servlet.xml</p><p>名称要求按照官方的来。</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/beans/spring-beans.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">beans</span>></span></span><br></pre></td></tr></tbody></table></figure><p>6、添加处理器映射器</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"</span>/></span></span><br></pre></td></tr></tbody></table></figure><p>7、添加处理器适配器</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"</span>/></span></span><br></pre></td></tr></tbody></table></figure><p>8、添加视图解析器</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--视图解析器:DispatcherServlet给他的ModelAndView--></span></span><br><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span> <span class="attr">id</span>=<span class="string">"InternalResourceViewResolver"</span>></span></span><br><span class="line"> <span class="comment"><!--前缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"prefix"</span> <span class="attr">value</span>=<span class="string">"/WEB-INF/jsp/"</span>/></span></span><br><span class="line"> <span class="comment"><!--后缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"suffix"</span> <span class="attr">value</span>=<span class="string">".jsp"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">bean</span>></span></span><br></pre></td></tr></tbody></table></figure><p>9、编写操作业务的Controller,要么实现Controller接口,要么增加注解;需要返回一个ModelAndView,装数据,封视图。</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.ModelAndView;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.mvc.Controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 3 编写控制器</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestController</span> <span class="keyword">implements</span> <span class="title">Controller</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> ModelAndView <span class="title">handleRequest</span><span class="params">(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建模型视图</span></span><br><span class="line"> ModelAndView mv = <span class="keyword">new</span> ModelAndView();</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 调用业务层</span></span><br><span class="line"> String msg = <span class="string">"HelloSpringMVC"</span>;</span><br><span class="line"> <span class="comment">// 封装对象,放在ModelAndView中,model</span></span><br><span class="line"> mv.addObject(<span class="string">"msg"</span>, msg);</span><br><span class="line"> <span class="comment">// 封装要跳转的视图,放在ModelAndView中,view</span></span><br><span class="line"> mv.setViewName(<span class="string">"test"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> mv;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>10、Controller交给SpringIOC容器,即在Spring MVC 配置文件中注册bean</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--编写好控制器之后,需要在springmvc的配置文件中注册bean--></span></span><br><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"/test"</span> <span class="attr">class</span>=<span class="string">"com.zzb.controller.TestController"</span>/></span></span><br></pre></td></tr></tbody></table></figure><p>11 、写要跳转的jsp页面,显示ModelandView存放的数据,以及正常页面。</p><figure class="highlight jsp"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><%@ page contentType=<span class="string">"text/html;charset=UTF-8"</span> language=<span class="string">"java"</span> %></span><br><span class="line"><html></span><br><span class="line"><head></span><br><span class="line"> <title>Title</title></span><br><span class="line"></head></span><br><span class="line"><body></span><br><span class="line">${msg}</span><br><span class="line"></body></span><br><span class="line"></html></span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>12、配置Tomcat,启动测试!</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201201215723607.png" alt="image-20201201215723607"></p><p><strong>注意:</strong></p><p><strong>可能遇到的问题:访问出现404,排查步骤:</strong></p><ol><li>查看控制台输出,看一下是不是缺少了什么jar包。</li><li>如果jar包存在,显示无法输出,就在IDEA的项目发布中,添加lib依赖!</li><li>重启Tomcat 即可解决!</li></ol><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201201215830687.png" alt="image-20201201215830687"></p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201201215952561.png" alt="image-20201201215952561"></p><p><strong>使用注解开发</strong></p><p>1、新建一个Module,<strong>springmvc-03-hello-annotation</strong>,添加web支持,配置Tomcat,如上一步添加jar包。</p><p>2、在pom.xml文件引入相关的依赖:主要有Spring框架核心库、Spring MVC、servlet , JSTL等。我们在父依赖中已经引入了!</p><p>3、配置web.xml文件,注册DispatcherServlet</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">web-app</span> <span class="attr">xmlns</span>=<span class="string">"http://xmlns.jcp.org/xml/ns/javaee"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">version</span>=<span class="string">"4.0"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--1 配置DispatcherServlet--></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-name</span>></span>DispatcherServlet<span class="tag"></<span class="name">servlet-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-class</span>></span>org.springframework.web.servlet.DispatcherServlet<span class="tag"></<span class="name">servlet-class</span>></span></span><br><span class="line"> <span class="comment"><!--关联一个springmvc配置文件:【servlet-name】-servlet.xml--></span></span><br><span class="line"> <span class="tag"><<span class="name">init-param</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-name</span>></span>contextConfigLocation<span class="tag"></<span class="name">param-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-value</span>></span>classpath:springmvc-servlet.xml<span class="tag"></<span class="name">param-value</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">init-param</span>></span></span><br><span class="line"> <span class="comment"><!--设置启动级别,1表是跟随服务器启动--></span></span><br><span class="line"> <span class="tag"><<span class="name">load-on-startup</span>></span>1<span class="tag"></<span class="name">load-on-startup</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">servlet</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-mapping</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-name</span>></span>DispatcherServlet<span class="tag"></<span class="name">servlet-name</span>></span></span><br><span class="line"> <span class="comment"><!--/ 匹配所有请求,不包括.jsp--></span></span><br><span class="line"> <span class="comment"><!--/* 匹配所有请求,包括.jsp--></span></span><br><span class="line"> <span class="tag"><<span class="name">url-pattern</span>></span>/<span class="tag"></<span class="name">url-pattern</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">servlet-mapping</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">web-app</span>></span></span><br></pre></td></tr></tbody></table></figure><p>注意事项:</p><ul><li>注意web.xml版本问题,要最新版!</li><li>注册DispatcherServlet</li><li>关联SpringMVC的配置文件</li><li>启动级别为1</li><li>映射路径为 / 【不要用/*,会404】</li></ul><p>4、添加Spring MVC配置文件</p><p>在resource目录下添加springmvc-servlet.xml配置文件,配置的形式与Spring容器配置基本类似,为了支持基于注解的IOC,设置了自动扫描包的功能,具体配置信息如下:</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:context</span>=<span class="string">"http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:mvc</span>=<span class="string">"http://www.springframework.org/schema/mvc"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理--></span></span><br><span class="line"> <span class="tag"><<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"com.zzb.controller"</span>/></span></span><br><span class="line"> <span class="comment"><!--让Spring MVC不处理静态资源,例如.mp3 .css .jpg等不走视图解析器--></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:default-servlet-handler</span>/></span></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> 支持mvc注解驱动</span></span><br><span class="line"><span class="comment"> 在spring中一般采用@RequestMapping注解来完成映射关系</span></span><br><span class="line"><span class="comment"> 要想使@RequestMapping注解生效</span></span><br><span class="line"><span class="comment"> 必须向上下文中注册DefaultAnnotationHandlerMapping</span></span><br><span class="line"><span class="comment"> 和一个AnnotationMethodHandlerAdapter实例</span></span><br><span class="line"><span class="comment"> 这两个实例分别在类级别和方法级别处理。</span></span><br><span class="line"><span class="comment"> 而annotation-driven配置帮助我们自动完成上述两个实例的注入</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:annotation-driven</span>/></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--视图解析器--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span> <span class="attr">id</span>=<span class="string">"InternalResourceViewResolver"</span>></span></span><br><span class="line"> <span class="comment"><!--前缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"prefix"</span> <span class="attr">value</span>=<span class="string">"/WEB-INF/jsp/"</span>/></span></span><br><span class="line"> <span class="comment"><!--后缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"suffix"</span> <span class="attr">value</span>=<span class="string">".jsp"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"><span class="tag"></<span class="name">beans</span>></span></span><br></pre></td></tr></tbody></table></figure><p>在视图解析器中我们把所有的视图都存放在/WEB-INF/目录下,这样可以保证视图安全,因为这个目录下的文件,客户端不能直接访问。</p><p>上述Spring MVC的配置文件主要完成了以下工作:</p><ul><li>让IOC的注解生效</li><li>静态资源过滤 :HTML . JS . CSS . 图片 , 视频 …..</li><li>MVC的注解驱动</li><li>配置视图解析器</li></ul><p>5、创建Controller</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.ui.Model;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping("/HelloController")</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloController</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/hello")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">hello</span><span class="params">(Model model)</span></span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 封装数据</span></span><br><span class="line"> model.addAttribute(<span class="string">"msg"</span>, <span class="string">"Hello, Spring MVC Annotation!"</span>);</span><br><span class="line"> <span class="comment">// 返回的字符串会被视图解析器解析</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"hello"</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><ul><li>@Controller是为了让Spring IOC容器初始化时自动扫描到;</li><li>@RequestMapping是为了映射请求路径,这里因为类与方法上都有映射所以访问时应该是/HelloController/hello;</li><li>方法中声明Model类型的参数是为了把Action中的数据带到视图中;</li><li>方法返回的结果是视图的名称hello,加上配置文件中的前后缀变成WEB-INF/jsp/<strong>hello</strong>.jsp。</li></ul><p>6、创建视图层</p><ol><li><p>在WEB-INF/ jsp目录中创建hello.jsp , 视图可以直接取出并展示从Controller带回的信息;</p></li><li><p>可以通过EL表达式取出Model中存放的值,或者对象。</p></li><li><p>```jsp<br><%@ page contentType=”text/html;charset=UTF-8” language=”java” %></p> <title>Title</title>${msg}<figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">7、配置Tomcat,启动运行</span><br><span class="line"></span><br><span class="line">1. ![image-20201203225601938](https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201203225601938.png)</span><br><span class="line"></span><br><span class="line">**总结:**</span><br><span class="line"></span><br><span class="line">实现步骤其实非常的简单:</span><br><span class="line"></span><br><span class="line">1. 新建一个web项目</span><br><span class="line">2. 导入相关jar包</span><br><span class="line">3. 编写web.xml , 注册DispatcherServlet</span><br><span class="line">4. 编写springmvc配置文件</span><br><span class="line">5. 接下来就是去创建对应的控制类 , controller</span><br><span class="line">6. 最后完善前端视图和controller之间的对应</span><br><span class="line">7. 测试运行调试.</span><br><span class="line">8. 使用springMVC必须配置的三大件:**处理器映射器、处理器适配器、视图解析器**</span><br><span class="line">9. 通常,我们只需要**手动配置视图解析器**,而**处理器映射器**和**处理器适配器**只需要开启**注解驱动**即可,而省去了大段的xml配置</span><br><span class="line"></span><br><span class="line">## 3 Controller(控制器)和RestFul风格</span><br><span class="line"></span><br><span class="line">**控制器Controller**</span><br><span class="line"></span><br><span class="line">- 控制器负责提供访问应用程序的行为,通常通过接口定义或注解定义两种方法实现。</span><br><span class="line">- 控制器负责解析用户的请求并将其转换为一个模型。</span><br><span class="line">- 在Spring MVC中一个控制器类可以包含多个方法。</span><br><span class="line">- 在Spring MVC中,对于Controller的配置方式有很多种。</span><br><span class="line"></span><br><span class="line">module:springmvc-04-controller</span><br><span class="line"></span><br><span class="line">> 环境搭建</span><br><span class="line"></span><br><span class="line">**添加web框架支持,以及Artifacts中的lib配置!**</span><br><span class="line"></span><br><span class="line">web.xml</span><br><span class="line"></span><br><span class="line">```xml</span><br><span class="line"><?xml version="1.0" encoding="UTF-8"?></span><br><span class="line"><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"</span><br><span class="line"> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"</span><br><span class="line"> xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"</span><br><span class="line"> version="4.0"></span><br><span class="line"></span><br><span class="line"> <!--1 配置DispatcherServlet--></span><br><span class="line"> <servlet></span><br><span class="line"> <servlet-name>DispatcherServlet</servlet-name></span><br><span class="line"> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></span><br><span class="line"> <init-param></span><br><span class="line"> <param-name>contextConfigLocation</param-name></span><br><span class="line"> <param-value>classpath:springmvc-servlet.xml</param-value></span><br><span class="line"> </init-param></span><br><span class="line"> <load-on-startup>1</load-on-startup></span><br><span class="line"> </servlet></span><br><span class="line"> <servlet-mapping></span><br><span class="line"> <servlet-name>DispatcherServlet</servlet-name></span><br><span class="line"> <url-pattern>/</url-pattern></span><br><span class="line"> </servlet-mapping></span><br><span class="line"></span><br><span class="line"></web-app></span><br></pre></td></tr></tbody></table></figure></li></ol><p>springmvc-servlet.xml</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:context</span>=<span class="string">"http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:mvc</span>=<span class="string">"http://www.springframework.org/schema/mvc"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/context</span></span></span><br><span class="line"><span class="tag"><span class="string"> https://www.springframework.org/schema/context/spring-context.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/mvc</span></span></span><br><span class="line"><span class="tag"><span class="string"> https://www.springframework.org/schema/mvc/spring-mvc.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理--></span></span><br><span class="line"> <span class="tag"><<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"com.zzb.controller"</span>/></span></span><br><span class="line"> <span class="comment"><!--让Spring MVC不处理静态资源,例如.mp3 .css .jpg等不走视图解析器--></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:default-servlet-handler</span>/></span></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> 支持mvc注解驱动</span></span><br><span class="line"><span class="comment"> 在spring中一般采用@RequestMapping注解来完成映射关系</span></span><br><span class="line"><span class="comment"> 要想使@RequestMapping注解生效</span></span><br><span class="line"><span class="comment"> 必须向上下文中注册DefaultAnnotationHandlerMapping</span></span><br><span class="line"><span class="comment"> 和一个AnnotationMethodHandlerAdapter实例</span></span><br><span class="line"><span class="comment"> 这两个实例分别在类级别和方法级别处理。</span></span><br><span class="line"><span class="comment"> 而annotation-driven配置帮助我们自动完成上述两个实例的注入</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:annotation-driven</span>/></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--视图解析器--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span> <span class="attr">id</span>=<span class="string">"InternalResourceViewResolver"</span>></span></span><br><span class="line"> <span class="comment"><!--前缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"prefix"</span> <span class="attr">value</span>=<span class="string">"/WEB-INF/jsp/"</span>/></span></span><br><span class="line"> <span class="comment"><!--后缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"suffix"</span> <span class="attr">value</span>=<span class="string">".jsp"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"><span class="tag"></<span class="name">beans</span>></span></span><br></pre></td></tr></tbody></table></figure><blockquote><p>代码实现</p></blockquote><h3 id="实现Controller接口"><a href="#实现Controller接口" class="headerlink" title="实现Controller接口"></a>实现Controller接口</h3><p>Controller是一个接口,在org.springframework.web.servlet.mvc包下,接口中只有一个方法;</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FunctionalInterface</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Controller</span> </span>{</span><br><span class="line"> <span class="meta">@Nullable</span></span><br><span class="line"> <span class="function">ModelAndView <span class="title">handleRequest</span><span class="params">(HttpServletRequest var1, HttpServletResponse var2)</span> <span class="keyword">throws</span> Exception</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>1、编写一个ControllerTest1类,实现Contoller接口</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.ModelAndView;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.mvc.Controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ControllerTest1</span> <span class="keyword">implements</span> <span class="title">Controller</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> ModelAndView <span class="title">handleRequest</span><span class="params">(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建模型视图</span></span><br><span class="line"> ModelAndView modelAndView = <span class="keyword">new</span> ModelAndView();</span><br><span class="line"> <span class="comment">// 调用业务层</span></span><br><span class="line"> String msg = <span class="string">"ControllerTest1"</span>;</span><br><span class="line"> modelAndView.addObject(<span class="string">"msg"</span>, msg);</span><br><span class="line"> <span class="comment">// 设置跳转视图</span></span><br><span class="line"> modelAndView.setViewName(<span class="string">"test"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> modelAndView;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>2、去Spring配置文件中注册请求的bean,name或id对应请求路径,class对应处理请求的类</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"/t1"</span> <span class="attr">class</span>=<span class="string">"com.zzb.controller.ControllerTest1"</span>/></span></span><br></pre></td></tr></tbody></table></figure><p>3、编写前端展示页面test.jsp,注意在WEB-INF/jsp目录</p><figure class="highlight jsp"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><%@ page contentType=<span class="string">"text/html;charset=UTF-8"</span> language=<span class="string">"java"</span> %></span><br><span class="line"><html></span><br><span class="line"><head></span><br><span class="line"> <title>Title</title></span><br><span class="line"></head></span><br><span class="line"><body></span><br><span class="line">${msg}</span><br><span class="line"></body></span><br><span class="line"></html></span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>4、配置Tomcat,启动测试!</p><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201211145922123.png" alt="image-20201211145922123"></p><p><strong>说明:</strong></p><ul><li>实现Controller接口定义控制器的方法比较老了,一般不推荐使用!</li><li>缺点是一个控制器只有一个方法,如果要多个方法则需要定义多个Controller。</li></ul><h3 id="使用注解-Controller"><a href="#使用注解-Controller" class="headerlink" title="使用注解@Controller"></a>使用注解@Controller</h3><ul><li><p>@Controller注解类型用于声明Spring类的实例是一个控制器(在讲IOC时还提到了另外3个注解);</p></li><li><p>Spring可以使用扫描机制来找到应用程序中所有基于注解的控制器类,为了保证Spring能找到你的控制器,需要在配置文件中声明组件扫描。</p></li></ul><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理--></span></span><br><span class="line"><span class="tag"><<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"com.zzb.controller"</span>/></span></span><br></pre></td></tr></tbody></table></figure><ul><li>增加一个ControllerTest2类,使用注解!</li></ul><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.ui.Model;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ControllerTest2</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/t2")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">test2</span><span class="params">(Model model)</span></span>{</span><br><span class="line"></span><br><span class="line"> model.addAttribute(<span class="string">"msg"</span>, <span class="string">"ControllerTest2"</span>);</span><br><span class="line"> <span class="comment">// 返回视图位置</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><ul><li>启动Tomcat测试</li></ul><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201211151124229.png" alt="image-20201211151124229"></p><p><strong>可以发现,我们的两个请求都可以指向一个视图,但是页面结果的结果是不一样的,从这里可以看出视图是被复用的,而控制器与视图之间是弱偶合关系。</strong></p><p><strong>注解方式是平时使用的最多的方式!</strong></p><h3 id="RequestMapping"><a href="#RequestMapping" class="headerlink" title="RequestMapping"></a>RequestMapping</h3><p><strong>@RequestMapping</strong></p><p>@RequestMapping注解用于映射url到控制器类或一个特定的处理程序方法。可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.ui.Model;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping("/myweb")</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ControllerTest2</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/t2")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">test2</span><span class="params">(Model model)</span></span>{</span><br><span class="line"></span><br><span class="line"> model.addAttribute(<span class="string">"msg"</span>, <span class="string">"ControllerTest2"</span>);</span><br><span class="line"> <span class="comment">// 返回视图位置</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201211151426583.png" alt="image-20201211151426583"></p><p><strong>注意观察url</strong></p><h3 id="RestFul-风格"><a href="#RestFul-风格" class="headerlink" title="RestFul 风格"></a>RestFul 风格</h3><p><strong>概念</strong></p><p>Restful就是一个资源定位及资源操作的风格。不是标准也不是协议,只是一种风格。基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制。</p><p><strong>功能</strong></p><p>资源:互联网所有的事物都可以被抽象为资源</p><p>资源操作:使用POST、DELETE、PUT、GET,使用不同方法对资源进行操作。</p><p>分别对应 添加、 删除、修改、查询。</p><p><strong>传统方式操作资源</strong> :通过不同的参数来实现不同的效果!方法单一,post 和 get</p><p> <a href="http://127.0.0.1/item/queryItem.action?id=1">http://127.0.0.1/item/queryItem.action?id=1</a> 查询,GET</p><p> <a href="http://127.0.0.1/item/saveItem.action">http://127.0.0.1/item/saveItem.action</a> 新增,POST</p><p> <a href="http://127.0.0.1/item/updateItem.action">http://127.0.0.1/item/updateItem.action</a> 更新,POST</p><p> <a href="http://127.0.0.1/item/deleteItem.action?id=1">http://127.0.0.1/item/deleteItem.action?id=1</a> 删除,GET或POST</p><p><strong>使用RESTful操作资源</strong> :可以通过不同的请求方式来实现不同的效果!如下:请求地址一样,但是功能可以不同!</p><p> <a href="http://127.0.0.1/item/1">http://127.0.0.1/item/1</a> 查询,GET</p><p> <a href="http://127.0.0.1/item">http://127.0.0.1/item</a> 新增,POST</p><p> <a href="http://127.0.0.1/item">http://127.0.0.1/item</a> 更新,PUT</p><p> <a href="http://127.0.0.1/item/1">http://127.0.0.1/item/1</a> 删除,DELETE</p><blockquote><p>代码实现</p></blockquote><p>1、新建一个RestFulController</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RestFulController</span> </span>{</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>2、在Spring MVC中可以使用 @PathVariable 注解,让方法参数的值对应绑定到一个URI模板变量上。</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.ui.Model;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.PathVariable;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RestFulController</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/t3/{p1}/{p2}")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">test3</span><span class="params">(<span class="meta">@PathVariable</span> <span class="keyword">int</span> p1,<span class="meta">@PathVariable</span> <span class="keyword">int</span> p2, Model model)</span></span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> result = p1 + p2;</span><br><span class="line"></span><br><span class="line"> model.addAttribute(<span class="string">"msg"</span>, result);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>3、测试</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201211193901159.png" alt="image-20201211193901159"></p><p><strong>使用路径变量的好处?</strong></p><ul><li><p>使路径变得更加简洁;</p></li><li><p>获得参数更加方便,框架会自动进行类型转换;</p></li><li><p>通过路径变量的类型可以约束访问参数,如果类型不一样,则访问不到对应的请求方法,如这里访问是的路径是/t3/1/a,则路径与方法不匹配,而不会是参数转换失败。</p></li></ul><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201211194058094.png" alt="image-20201211194058094"></p><p>4、修改参数类型,再次测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.ui.Model;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.PathVariable;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RestFulController</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/t3/{p1}/{p2}")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">test3</span><span class="params">(<span class="meta">@PathVariable</span> <span class="keyword">int</span> p1,<span class="meta">@PathVariable</span> String p2, Model model)</span></span>{</span><br><span class="line"></span><br><span class="line"> String result = p1 + p2;</span><br><span class="line"></span><br><span class="line"> model.addAttribute(<span class="string">"msg"</span>, result);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201211194249908.png" alt="image-20201211194249908"></p><p><strong>使用method属性指定请求类型</strong></p><p>用于约束请求的类型,可以收窄请求范围。指定请求谓词的类型如GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE等</p><p>测试:</p><p>1、增加一个方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 映射访问路径,必须是post请求</span></span><br><span class="line"><span class="meta">@RequestMapping(value = "/t4", method = {RequestMethod.POST})</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">test4</span><span class="params">(Model model)</span></span>{</span><br><span class="line"></span><br><span class="line"> model.addAttribute(<span class="string">"msg"</span>, <span class="string">"Hello"</span>);</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>2、使用浏览器地址栏进行访问默认是Get请求,会报错405</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201211194724167.png" alt="image-20201211194724167"></p><p>3、将指定方法从POST改为GET</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(value = "/t4", method = {RequestMethod.GET})</span></span><br></pre></td></tr></tbody></table></figure><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201211195054151.png" alt="image-20201211195054151"></p><p><strong>总结:</strong></p><p>Spring MVC 的 @RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。</p><p><strong>所有的地址栏请求默认都会是 HTTP GET 类型的。</strong></p><p>方法级别的注解变体有如下几个:组合注解</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping</span></span><br><span class="line"><span class="meta">@PostMapping</span></span><br><span class="line"><span class="meta">@PutMapping</span></span><br><span class="line"><span class="meta">@DeleteMapping</span></span><br><span class="line"><span class="meta">@PatchMapping</span></span><br></pre></td></tr></tbody></table></figure><p>@GetMapping 是一个组合注解,平时使用的会比较多!</p><p>它所扮演的是 @RequestMapping(method =RequestMethod.GET) 的一个快捷方式。</p><h2 id="4-数据处理及跳转"><a href="#4-数据处理及跳转" class="headerlink" title="4 数据处理及跳转"></a>4 数据处理及跳转</h2><h3 id="结果跳转方式"><a href="#结果跳转方式" class="headerlink" title="结果跳转方式"></a>结果跳转方式</h3><p><strong>ModelAndView</strong></p><p>设置 ModelAndView 对象,根据 View 的名称和视图解析器跳转到指定的页面</p><p>页面 : {视图解析器前缀} + viewName +{视图解析器后缀}</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--视图解析器--></span></span><br><span class="line"><span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span> <span class="attr">id</span>=<span class="string">"InternalResourceViewResolver"</span>></span></span><br><span class="line"> <span class="comment"><!--前缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"prefix"</span> <span class="attr">value</span>=<span class="string">"/WEB-INF/jsp/"</span>/></span></span><br><span class="line"> <span class="comment"><!--后缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"suffix"</span> <span class="attr">value</span>=<span class="string">".jsp"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">bean</span>></span></span><br></pre></td></tr></tbody></table></figure><p>对应的Controller类</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.ModelAndView;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.mvc.Controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ControllerTest1</span> <span class="keyword">implements</span> <span class="title">Controller</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> ModelAndView <span class="title">handleRequest</span><span class="params">(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建模型视图</span></span><br><span class="line"> ModelAndView modelAndView = <span class="keyword">new</span> ModelAndView();</span><br><span class="line"> <span class="comment">// 调用业务层</span></span><br><span class="line"> String msg = <span class="string">"ControllerTest1"</span>;</span><br><span class="line"> modelAndView.addObject(<span class="string">"msg"</span>, msg);</span><br><span class="line"> <span class="comment">// 设置跳转视图</span></span><br><span class="line"> modelAndView.setViewName(<span class="string">"test"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> modelAndView;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>ServletAPI</strong></p><p>通过设置ServletAPI,不需要视图解析器</p><p>1、通过HttpServletResponse进行输出</p><p>2、通过HttpServletResponse实现重定向</p><p>3、通过HttpServletRequest实现转发</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.ServletException;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping("/t3")</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ControllerTest3</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/t1")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test1</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> IOException </span>{</span><br><span class="line"></span><br><span class="line"> resp.getWriter().println(<span class="string">"Hello,Spring BY servlet API"</span>);</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"> <span class="meta">@RequestMapping("/t2")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> IOException </span>{</span><br><span class="line"> <span class="comment">// 重定向</span></span><br><span class="line"> resp.sendRedirect(req.getContextPath() + <span class="string">"/index.jsp"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/t3")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test3</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException </span>{</span><br><span class="line"> <span class="comment">// 转发</span></span><br><span class="line"> req.setAttribute(<span class="string">"msg"</span>, <span class="string">"/t3/t3"</span>);</span><br><span class="line"> req.getRequestDispatcher(<span class="string">"/WEB-INF/jsp/test.jsp"</span>).forward(req, resp);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>Spring MVC</strong></p><p>通过 Spring MVC 来实现转发和重定向——不需要视图解析器。</p><p>注意:测试前先将视图解析器注释掉。</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping("/t4")</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ControllerTest4</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/t1")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">test1</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 转发</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"/index.jsp"</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/t2")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">test2</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 显示转发</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"forward:/index.jsp"</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/t3")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">test3</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 重定向</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"redirect:/index.jsp"</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>通过 Spring MVC 来实现转发和重定向——使用视图解析器。</p><p>注意:开启视图解析器。</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping("/t5")</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ControllerTest5</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/t1")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">test1</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 转发</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/t2")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">test2</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 重定向</span></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"redirect:/index.jsp"</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><h3 id="数据处理"><a href="#数据处理" class="headerlink" title="数据处理"></a>数据处理</h3><p><strong>处理提交数据</strong></p><p>1、提交的域名数据名称和处理方法的参数名一致</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.zzb.pojo.User;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.ui.Model;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping("/data")</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DataController</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 域名参数与控制器参数名一致的情况</span></span><br><span class="line"> <span class="meta">@RequestMapping("/t1")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">test1</span><span class="params">(String name, Model model)</span></span>{</span><br><span class="line"> <span class="comment">// 1 接收前端参数</span></span><br><span class="line"> System.out.println(name);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 2 将数据返回给前端</span></span><br><span class="line"> model.addAttribute(<span class="string">"msg"</span>, name);</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201215161547471.png" alt="image-20201215161547471"></p><p>2、提交的域名数据名称和处理方法的参数名不一致</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 域名参数与控制器参数名不一致的情况</span></span><br><span class="line"><span class="meta">@RequestMapping("/t2")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">test2</span><span class="params">(<span class="meta">@RequestParam("username")</span> String name, Model model)</span></span>{</span><br><span class="line"> <span class="comment">// 1 接受前端参数</span></span><br><span class="line"> System.out.println(name);</span><br><span class="line"> <span class="comment">// 2 将参数传递给前端展示</span></span><br><span class="line"> model.addAttribute(<span class="string">"msg"</span>, name);</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201215161656888.png" alt="image-20201215161656888"></p><p>3、参数是一个对象</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">()</span> </span>{</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(<span class="keyword">int</span> id, String name, <span class="keyword">int</span> age)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.id = id;</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> <span class="keyword">this</span>.age = age;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getId</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(<span class="keyword">int</span> id)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.id = id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> age;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.age = age;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"User{"</span> +</span><br><span class="line"> <span class="string">"id="</span> + id +</span><br><span class="line"> <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", age="</span> + age +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 前端提交的参数是一个对象</span></span><br><span class="line"><span class="meta">@RequestMapping("/t3")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">test3</span><span class="params">(User user, Model model)</span></span>{</span><br><span class="line"> <span class="comment">// 1 接收前端传递的参数</span></span><br><span class="line"> System.out.println(user.toString());</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 2 将参数传递给前端页面展示</span></span><br><span class="line"> model.addAttribute(<span class="string">"msg"</span>, user.toString());</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201215161837158.png" alt="image-20201215161837158"></p><p>注意:如果传递参数是对象的话,前端传递的参数名和对象的属性名必须一致,否则为null。</p><p><strong>前端展示数据</strong></p><p>1、ModelAndView</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.ModelAndView;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.servlet.mvc.Controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ControllerTest1</span> <span class="keyword">implements</span> <span class="title">Controller</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> ModelAndView <span class="title">handleRequest</span><span class="params">(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)</span> <span class="keyword">throws</span> Exception </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建模型视图</span></span><br><span class="line"> ModelAndView modelAndView = <span class="keyword">new</span> ModelAndView();</span><br><span class="line"> <span class="comment">// 调用业务层</span></span><br><span class="line"> String msg = <span class="string">"ControllerTest1"</span>;</span><br><span class="line"> modelAndView.addObject(<span class="string">"msg"</span>, msg);</span><br><span class="line"> <span class="comment">// 设置跳转视图</span></span><br><span class="line"> modelAndView.setViewName(<span class="string">"test"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> modelAndView;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>2、ModelMap</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping("/t1")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">test1</span><span class="params">(String name, ModelMap modelMap)</span></span>{</span><br><span class="line"> <span class="comment">// 1 接收前端参数</span></span><br><span class="line"> System.out.println(name);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 2 将数据返回给前端</span></span><br><span class="line"> modelMap.addAttribute(<span class="string">"msg"</span>, name);</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>3、Model</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping("/t1")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">test1</span><span class="params">(String name, Model model)</span></span>{</span><br><span class="line"> <span class="comment">// 1 接收前端参数</span></span><br><span class="line"> System.out.println(name);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 2 将数据返回给前端</span></span><br><span class="line"> model.addAttribute(<span class="string">"msg"</span>, name);</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>3种方法对比:</p><ul><li><p>Model 只有寥寥几个方法只适合用于储存数据,简化了新手对于Model对象的操作和理解;</p></li><li><p>ModelMap 继承了 LinkedMap ,除了实现了自身的一些方法,同样的继承 LinkedMap 的方法和特性;</p></li><li><p>ModelAndView 可以在储存数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转。</p></li></ul><p><strong>乱码问题</strong></p><p>1、在index.jsp页面写一个表单</p><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><%@ page contentType=<span class="string">"text/html;charset=UTF-8"</span> language=<span class="string">"java"</span> %></span><br><span class="line"><html></span><br><span class="line"> <head></span><br><span class="line"> <title>$Title$</title></span><br><span class="line"> </head></span><br><span class="line"> <body></span><br><span class="line"> <form action=${pageContext.request.contextPath}/data/t1 method=<span class="string">"post"</span>></span><br><span class="line"> <input type=<span class="string">"text"</span> name=<span class="string">"name"</span>></span><br><span class="line"> <input type=<span class="string">"submit"</span>></span><br><span class="line"> </form></span><br><span class="line"> </body></span><br><span class="line"></html></span><br></pre></td></tr></tbody></table></figure><p>2、后端处理程序</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping("/data")</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DataController</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 域名参数与控制器参数名一致的情况</span></span><br><span class="line"> <span class="meta">@RequestMapping("/t1")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">test1</span><span class="params">(String name, Model model)</span></span>{</span><br><span class="line"> <span class="comment">// 1 接收前端参数</span></span><br><span class="line"> System.out.println(name);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 2 将数据返回给前端</span></span><br><span class="line"> model.addAttribute(<span class="string">"msg"</span>, name);</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"test"</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>3、输入中文 哈哈哈 测试</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201215163654829.png" alt="image-20201215163654829"></p><p>乱码问题通过过滤器解决,Spring MVC 提供了一个过滤器,需要在web.xml中配置!</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">filter</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-name</span>></span>encoding<span class="tag"></<span class="name">filter-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-class</span>></span>org.springframework.web.filter.CharacterEncodingFilter<span class="tag"></<span class="name">filter-class</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">init-param</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-name</span>></span>encoding<span class="tag"></<span class="name">param-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-value</span>></span>utf-8<span class="tag"></<span class="name">param-value</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">init-param</span>></span></span><br><span class="line"><span class="tag"></<span class="name">filter</span>></span></span><br><span class="line"><span class="tag"><<span class="name">filter-mapping</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-name</span>></span>encoding<span class="tag"></<span class="name">filter-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url-pattern</span>></span>/*<span class="tag"></<span class="name">url-pattern</span>></span></span><br><span class="line"><span class="tag"></<span class="name">filter-mapping</span>></span></span><br></pre></td></tr></tbody></table></figure><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201215164057192.png" alt="image-20201215164057192"></p><p>自定义过滤器:</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.filter;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.*;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequestWrapper;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.UnsupportedEncodingException;</span><br><span class="line"><span class="keyword">import</span> java.util.Map;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* 解决get和post请求 全部乱码的过滤器</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GenericEncodingFilter</span> <span class="keyword">implements</span> <span class="title">Filter</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destroy</span><span class="params">()</span> </span>{</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doFilter</span><span class="params">(ServletRequest request, ServletResponse response, FilterChain chain)</span> <span class="keyword">throws</span> IOException, ServletException </span>{</span><br><span class="line"> <span class="comment">//处理response的字符编码</span></span><br><span class="line"> HttpServletResponse myResponse=(HttpServletResponse) response;</span><br><span class="line"> myResponse.setContentType(<span class="string">"text/html;charset=UTF-8"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 转型为与协议相关对象</span></span><br><span class="line"> HttpServletRequest httpServletRequest = (HttpServletRequest) request;</span><br><span class="line"> <span class="comment">// 对request包装增强</span></span><br><span class="line"> HttpServletRequest myrequest = <span class="keyword">new</span> MyRequest(httpServletRequest);</span><br><span class="line"> chain.doFilter(myrequest, response);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">(FilterConfig filterConfig)</span> <span class="keyword">throws</span> ServletException </span>{</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">//自定义request对象,HttpServletRequest的包装类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyRequest</span> <span class="keyword">extends</span> <span class="title">HttpServletRequestWrapper</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> HttpServletRequest request;</span><br><span class="line"> <span class="comment">//是否编码的标记</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">boolean</span> hasEncode;</span><br><span class="line"> <span class="comment">//定义一个可以传入HttpServletRequest对象的构造函数,以便对其进行装饰</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">MyRequest</span><span class="params">(HttpServletRequest request)</span> </span>{</span><br><span class="line"> <span class="keyword">super</span>(request);<span class="comment">// super必须写</span></span><br><span class="line"> <span class="keyword">this</span>.request = request;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 对需要增强方法 进行覆盖</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Map <span class="title">getParameterMap</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="comment">// 先获得请求方式</span></span><br><span class="line"> String method = request.getMethod();</span><br><span class="line"> <span class="keyword">if</span> (method.equalsIgnoreCase(<span class="string">"post"</span>)) {</span><br><span class="line"> <span class="comment">// post请求</span></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">// 处理post乱码</span></span><br><span class="line"> request.setCharacterEncoding(<span class="string">"utf-8"</span>);</span><br><span class="line"> <span class="keyword">return</span> request.getParameterMap();</span><br><span class="line"> } <span class="keyword">catch</span> (UnsupportedEncodingException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (method.equalsIgnoreCase(<span class="string">"get"</span>)) {</span><br><span class="line"> <span class="comment">// get请求</span></span><br><span class="line"> Map<String, String[]> parameterMap = request.getParameterMap();</span><br><span class="line"> <span class="keyword">if</span> (!hasEncode) { <span class="comment">// 确保get手动编码逻辑只运行一次</span></span><br><span class="line"> <span class="keyword">for</span> (String parameterName : parameterMap.keySet()) {</span><br><span class="line"> String[] values = parameterMap.get(parameterName);</span><br><span class="line"> <span class="keyword">if</span> (values != <span class="keyword">null</span>) {</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < values.length; i++) {</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> <span class="comment">// 处理get乱码</span></span><br><span class="line"> values[i] = <span class="keyword">new</span> String(values[i]</span><br><span class="line"> .getBytes(<span class="string">"ISO-8859-1"</span>), <span class="string">"utf-8"</span>);</span><br><span class="line"> } <span class="keyword">catch</span> (UnsupportedEncodingException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> hasEncode = <span class="keyword">true</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> parameterMap;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">super</span>.getParameterMap();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//取一个值</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getParameter</span><span class="params">(String name)</span> </span>{</span><br><span class="line"> Map<String, String[]> parameterMap = getParameterMap();</span><br><span class="line"> String[] values = parameterMap.get(name);</span><br><span class="line"> <span class="keyword">if</span> (values == <span class="keyword">null</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> values[<span class="number">0</span>]; <span class="comment">// 取回参数的第一个值</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">//取所有值</span></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">public</span> String[] getParameterValues(String name) {</span><br><span class="line"> Map<String, String[]> parameterMap = getParameterMap();</span><br><span class="line"> String[] values = parameterMap.get(name);</span><br><span class="line"> <span class="keyword">return</span> values;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>然后在web.xml中配置这个过滤器即可!</strong></p><h2 id="5-整合SSM框架"><a href="#5-整合SSM框架" class="headerlink" title="5 整合SSM框架"></a>5 整合SSM框架</h2><blockquote><p>环境</p></blockquote><ul><li>IDEA 2020.2</li><li>MySQL 5.7.29</li><li>Tomcat 9</li><li>Maven 3.6</li></ul><blockquote><p>数据库环境</p></blockquote><p>创建一个数据库及表格</p><figure class="highlight sql"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> DATABASE `ssmbuild`;</span><br><span class="line"></span><br><span class="line">USE `ssmbuild`;</span><br><span class="line"></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> `books`;</span><br><span class="line"></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `books` (</span><br><span class="line">`bookID` <span class="type">INT</span>(<span class="number">10</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT COMMENT <span class="string">'书id'</span>,</span><br><span class="line">`bookName` <span class="type">VARCHAR</span>(<span class="number">100</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">'书名'</span>,</span><br><span class="line">`bookCounts` <span class="type">INT</span>(<span class="number">11</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">'数量'</span>,</span><br><span class="line">`detail` <span class="type">VARCHAR</span>(<span class="number">200</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">'描述'</span>,</span><br><span class="line">KEY `bookID` (`bookID`)</span><br><span class="line">) ENGINE<span class="operator">=</span>INNODB <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `books`(`bookID`,`bookName`,`bookCounts`,`detail`)<span class="keyword">VALUES</span> </span><br><span class="line">(<span class="number">1</span>,<span class="string">'Java'</span>,<span class="number">1</span>,<span class="string">'从入门到放弃'</span>),</span><br><span class="line">(<span class="number">2</span>,<span class="string">'MySQL'</span>,<span class="number">10</span>,<span class="string">'从删库到跑路'</span>),</span><br><span class="line">(<span class="number">3</span>,<span class="string">'Linux'</span>,<span class="number">5</span>,<span class="string">'从进门到进牢'</span>);</span><br></pre></td></tr></tbody></table></figure><blockquote><p>项目环境搭建</p></blockquote><p>1、新建一个Maven项目!ssmbuild,添加web支持!</p><p>2、导入相关pom依赖以及Maven静态资源过滤问题解决!</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">"http://maven.apache.org/POM/4.0.0"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">modelVersion</span>></span>4.0.0<span class="tag"></<span class="name">modelVersion</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>com.zzb<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>ssmbuild<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>1.0-SNAPSHOT<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--导入依赖,需要Junit,数据库驱动,数据库连接池,servlet,jsp,el表达式,mybatis,mybatis-spring,spring,偷懒用的lombok--></span></span><br><span class="line"> <span class="tag"><<span class="name">dependencies</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>junit<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>junit<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>4.13<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>mysql<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>mysql-connector-java<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>5.1.48<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--数据库连接池--></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>com.mchange<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>c3p0<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>0.9.5.2<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>javax.servlet<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>servlet-api<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>2.5<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>javax.servlet.jsp<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>jsp-api<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>2.2<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>javax.servlet<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>jstl<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>1.2<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.mybatis<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>mybatis<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>3.5.2<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.mybatis<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>mybatis-spring<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>2.0.5<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.springframework<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>spring-webmvc<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>5.2.0.RELEASE<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.springframework<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>spring-jdbc<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>5.1.9.RELEASE<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependencies</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--静态资源过滤问题--></span></span><br><span class="line"> <span class="tag"><<span class="name">build</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">resources</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">resource</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">directory</span>></span>src/main/java<span class="tag"></<span class="name">directory</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">includes</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">include</span>></span>**/*.properties<span class="tag"></<span class="name">include</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">include</span>></span>**/*.xml<span class="tag"></<span class="name">include</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">includes</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filtering</span>></span>true<span class="tag"></<span class="name">filtering</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">resource</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">resource</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">directory</span>></span>src/main/resources<span class="tag"></<span class="name">directory</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">includes</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">include</span>></span>**/*.properties<span class="tag"></<span class="name">include</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">include</span>></span>**/*.xml<span class="tag"></<span class="name">include</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">includes</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filtering</span>></span>true<span class="tag"></<span class="name">filtering</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">resource</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">resources</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">build</span>></span></span><br><span class="line"><span class="tag"></<span class="name">project</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、确定项目基本框架!</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201215165138707.png" alt="image-20201215165138707"></p><p>mybatis-config.xml</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">configuration</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-config.dtd"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">configuration</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">configuration</span>></span></span><br></pre></td></tr></tbody></table></figure><p>applicationContext.xml</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/beans/spring-beans.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">beans</span>></span></span><br></pre></td></tr></tbody></table></figure><p>database.properties</p><figure class="highlight properties"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">jdbc.driver</span>=<span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="meta">jdbc.url</span>=<span class="string">jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&verifyServerCertificate=false&useUnicode=true&characterEncoding=utf8</span></span><br><span class="line"><span class="meta">jdbc.username</span>=<span class="string">root</span></span><br><span class="line"><span class="meta">jdbc.password</span>=<span class="string">1234</span></span><br></pre></td></tr></tbody></table></figure><blockquote><p>MyBatis 层编写</p></blockquote><p>1、创建实体类Book</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Book</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> bookId;</span><br><span class="line"> <span class="keyword">private</span> String bookName;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> bookCount;</span><br><span class="line"> <span class="keyword">private</span> String detail;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Book</span><span class="params">()</span> </span>{</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Book</span><span class="params">(<span class="keyword">int</span> bookId, String bookName, <span class="keyword">int</span> bookCount, String detail)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.bookId = bookId;</span><br><span class="line"> <span class="keyword">this</span>.bookName = bookName;</span><br><span class="line"> <span class="keyword">this</span>.bookCount = bookCount;</span><br><span class="line"> <span class="keyword">this</span>.detail = detail;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getBookId</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> bookId;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setBookId</span><span class="params">(<span class="keyword">int</span> bookId)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.bookId = bookId;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getBookName</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> bookName;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setBookName</span><span class="params">(String bookName)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.bookName = bookName;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getBookCount</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> bookCount;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setBookCount</span><span class="params">(<span class="keyword">int</span> bookCount)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.bookCount = bookCount;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getDetail</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> detail;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setDetail</span><span class="params">(String detail)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.detail = detail;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"Book{"</span> +</span><br><span class="line"> <span class="string">"bookId="</span> + bookId +</span><br><span class="line"> <span class="string">", bookName='"</span> + bookName + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", bookCount="</span> + bookCount +</span><br><span class="line"> <span class="string">", detail='"</span> + detail + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>2、编写Mapper层Mapper接口</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.mapper;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.zzb.pojo.Book;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.annotations.Param;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">BookMapper</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 增加一本书</span></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">addBook</span><span class="params">(Book book)</span></span>;</span><br><span class="line"> <span class="comment">// 删除一本书</span></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">deleteBookById</span><span class="params">(<span class="meta">@Param("bookId")</span> <span class="keyword">int</span> id)</span></span>;</span><br><span class="line"> <span class="comment">// 更新一本书</span></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">updateBook</span><span class="params">(Book book)</span></span>;</span><br><span class="line"> <span class="comment">// 查询一本书</span></span><br><span class="line"> <span class="function">Book <span class="title">queryBookById</span><span class="params">(<span class="meta">@Param("bookId")</span> <span class="keyword">int</span> id)</span></span>;</span><br><span class="line"> <span class="comment">// 查询所有书</span></span><br><span class="line"> <span class="function">List<Book> <span class="title">queryAllBook</span><span class="params">()</span></span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>3、编写对应的Mapper.xml配置文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.zzb.mapper.BookMapper"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"addBook"</span> <span class="attr">parameterType</span>=<span class="string">"Book"</span>></span></span><br><span class="line"> INSERT INTO books(bookName, bookCount, detail) VALUES (#{bookName}, #{bookCount}, #{detail})</span><br><span class="line"> <span class="tag"></<span class="name">insert</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">delete</span> <span class="attr">id</span>=<span class="string">"deleteBookById"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span>></span></span><br><span class="line"> DELETE FROM books WHERE bookId = #{bookId}</span><br><span class="line"> <span class="tag"></<span class="name">delete</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">update</span> <span class="attr">id</span>=<span class="string">"updateBook"</span> <span class="attr">parameterType</span>=<span class="string">"Book"</span>></span></span><br><span class="line"> UPDATE books SET bookName = #{bookName}, bookCount = #{bookCount}, detail = #{detail} WHERE bookId = #{bookId}</span><br><span class="line"> <span class="tag"></<span class="name">update</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"queryBookById"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span>></span></span><br><span class="line"> SELECT * FROM book WHERE bookId = #{bookId}</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"queryAllBook"</span> <span class="attr">resultType</span>=<span class="string">"Book"</span>></span></span><br><span class="line"> SELECT * FROM book</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></tbody></table></figure><p>4、编写Service层的接口和实现类</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.zzb.pojo.Book;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">BookService</span> </span>{</span><br><span class="line"> <span class="comment">// 增加一本书</span></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">addBook</span><span class="params">(Book book)</span></span>;</span><br><span class="line"> <span class="comment">// 删除一本书</span></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">deleteBookById</span><span class="params">(<span class="keyword">int</span> id)</span></span>;</span><br><span class="line"> <span class="comment">// 更新一本书</span></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">updateBook</span><span class="params">(Book book)</span></span>;</span><br><span class="line"> <span class="comment">// 查询一本书</span></span><br><span class="line"> <span class="function">Book <span class="title">queryBookById</span><span class="params">(<span class="keyword">int</span> id)</span></span>;</span><br><span class="line"> <span class="comment">// 查询所有书</span></span><br><span class="line"> <span class="function">List<Book> <span class="title">queryAllBook</span><span class="params">()</span></span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.service;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.zzb.mapper.BookMapper;</span><br><span class="line"><span class="keyword">import</span> com.zzb.pojo.Book;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookServiceImpl</span> <span class="keyword">implements</span> <span class="title">BookService</span></span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 组合Mapper层</span></span><br><span class="line"> <span class="keyword">private</span> BookMapper bookMapper;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setBookMapper</span><span class="params">(BookMapper bookMapper)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.bookMapper = bookMapper;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">addBook</span><span class="params">(Book book)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> bookMapper.addBook(book);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">deleteBookById</span><span class="params">(<span class="keyword">int</span> id)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> bookMapper.deleteBookById(id);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">updateBook</span><span class="params">(Book book)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> bookMapper.updateBook(book);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Book <span class="title">queryBookById</span><span class="params">(<span class="keyword">int</span> id)</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> bookMapper.queryBookById(id);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> List<Book> <span class="title">queryAllBook</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> bookMapper.queryAllBook();</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>5、配置MyBatis核心配置文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">configuration</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-config.dtd"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">configuration</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--给实体类起别名--></span></span><br><span class="line"> <span class="tag"><<span class="name">typeAliases</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">package</span> <span class="attr">name</span>=<span class="string">"com.zzb.pojo"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">typeAliases</span>></span></span><br><span class="line"> <span class="comment"><!--注册Mapper--></span></span><br><span class="line"> <span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.zzb.mapper.BookMapper"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">mappers</span>></span></span><br><span class="line"><span class="tag"></<span class="name">configuration</span>></span></span><br></pre></td></tr></tbody></table></figure><blockquote><p>Spring 层编写</p></blockquote><p>1、Spring 整合 MyBatis层,spring-dao.xml</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:context</span>=<span class="string">"http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/context</span></span></span><br><span class="line"><span class="tag"><span class="string"> https://www.springframework.org/schema/context/spring-context.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--1、关联数据库配置文件--></span></span><br><span class="line"> <span class="tag"><<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">"classpath:database.properties"</span>/></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--2、数据库连接池--></span></span><br><span class="line"> <span class="comment"><!--数据库连接池</span></span><br><span class="line"><span class="comment"> dbcp 半自动化操作 不能自动连接</span></span><br><span class="line"><span class="comment"> c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"dataSource"</span> <span class="attr">class</span>=<span class="string">"com.mchange.v2.c3p0.ComboPooledDataSource"</span>></span></span><br><span class="line"> <span class="comment"><!-- 配置连接池属性 --></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driverClass"</span> <span class="attr">value</span>=<span class="string">"${jdbc.driver}"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"jdbcUrl"</span> <span class="attr">value</span>=<span class="string">"${jdbc.url}"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"user"</span> <span class="attr">value</span>=<span class="string">"${jdbc.username}"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"${jdbc.password}"</span>/></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!-- c3p0连接池的私有属性 --></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"maxPoolSize"</span> <span class="attr">value</span>=<span class="string">"30"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"minPoolSize"</span> <span class="attr">value</span>=<span class="string">"10"</span>/></span></span><br><span class="line"> <span class="comment"><!-- 关闭连接后不自动commit --></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"autoCommitOnClose"</span> <span class="attr">value</span>=<span class="string">"false"</span>/></span></span><br><span class="line"> <span class="comment"><!-- 获取连接超时时间 --></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"checkoutTimeout"</span> <span class="attr">value</span>=<span class="string">"10000"</span>/></span></span><br><span class="line"> <span class="comment"><!-- 当获取连接失败重试次数 --></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"acquireRetryAttempts"</span> <span class="attr">value</span>=<span class="string">"2"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--3、SqlSessionFactory--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"sqlSessionFactory"</span> <span class="attr">class</span>=<span class="string">"org.mybatis.spring.SqlSessionFactoryBean"</span>></span></span><br><span class="line"> <span class="comment"><!--注入数据库连接池--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"dataSource"</span> <span class="attr">ref</span>=<span class="string">"dataSource"</span>/></span></span><br><span class="line"> <span class="comment"><!--配置MyBatis核心配置文件--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"configLocation"</span> <span class="attr">value</span>=<span class="string">"classpath:mybatis-config.xml"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--4、配置扫描Dao接口包,动态实现Dao接口注入到spring容器中--></span></span><br><span class="line"> <span class="comment"><!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.mybatis.spring.mapper.MapperScannerConfigurer"</span>></span></span><br><span class="line"> <span class="comment"><!--注入SqlSessionFactory--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"sqlSessionFactoryBeanName"</span> <span class="attr">value</span>=<span class="string">"sqlSessionFactory"</span>/></span></span><br><span class="line"> <span class="comment"><!--配置需要扫描的包--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"basePackage"</span> <span class="attr">value</span>=<span class="string">"com.zzb.mapper"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"><span class="tag"></<span class="name">beans</span>></span></span><br></pre></td></tr></tbody></table></figure><p>2、Spring 整合 Service 层,spring-service.xml</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:context</span>=<span class="string">"http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--1、扫描service层下面相关的包--></span></span><br><span class="line"> <span class="tag"><<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"com.zzb.service"</span>/></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--2、将业务层实现类注入到Spring中--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"bookServiceImpl"</span> <span class="attr">class</span>=<span class="string">"com.zzb.service.BookServiceImpl"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"bookMapper"</span> <span class="attr">ref</span>=<span class="string">"bookMapper"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--3、配置事务管理器--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"transactionManager"</span> <span class="attr">class</span>=<span class="string">"org.springframework.jdbc.datasource.DataSourceTransactionManager"</span>></span></span><br><span class="line"> <span class="comment"><!--注入数据库连接池--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"dataSource"</span> <span class="attr">ref</span>=<span class="string">"dataSource"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--4、AOP织入事务管理--></span></span><br><span class="line"><span class="tag"></<span class="name">beans</span>></span></span><br></pre></td></tr></tbody></table></figure><blockquote><p>Spring MVC 层</p></blockquote><p>1、spring-mvc.xml</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:context</span>=<span class="string">"http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:mvc</span>=<span class="string">"http://www.springframework.org/schema/mvc"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/context</span></span></span><br><span class="line"><span class="tag"><span class="string"> https://www.springframework.org/schema/context/spring-context.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/mvc</span></span></span><br><span class="line"><span class="tag"><span class="string"> https://www.springframework.org/schema/mvc/spring-mvc.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理--></span></span><br><span class="line"> <span class="tag"><<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"com.zzb.controller"</span>/></span></span><br><span class="line"> <span class="comment"><!--让Spring MVC不处理静态资源,例如.mp3 .css .jpg等不走视图解析器--></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:default-servlet-handler</span>/></span></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> 支持mvc注解驱动</span></span><br><span class="line"><span class="comment"> 在spring中一般采用@RequestMapping注解来完成映射关系</span></span><br><span class="line"><span class="comment"> 要想使@RequestMapping注解生效</span></span><br><span class="line"><span class="comment"> 必须向上下文中注册DefaultAnnotationHandlerMapping</span></span><br><span class="line"><span class="comment"> 和一个AnnotationMethodHandlerAdapter实例</span></span><br><span class="line"><span class="comment"> 这两个实例分别在类级别和方法级别处理。</span></span><br><span class="line"><span class="comment"> 而annotation-driven配置帮助我们自动完成上述两个实例的注入</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:annotation-driven</span>/></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--视图解析器--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span> <span class="attr">id</span>=<span class="string">"InternalResourceViewResolver"</span>></span></span><br><span class="line"> <span class="comment"><!--前缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"prefix"</span> <span class="attr">value</span>=<span class="string">"/WEB-INF/jsp/"</span>/></span></span><br><span class="line"> <span class="comment"><!--后缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"suffix"</span> <span class="attr">value</span>=<span class="string">".jsp"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">beans</span>></span></span><br></pre></td></tr></tbody></table></figure><p>2、Spring 配置文件整合,applicationContext.xml</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/beans/spring-beans.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">import</span> <span class="attr">resource</span>=<span class="string">"spring-dao.xml"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">import</span> <span class="attr">resource</span>=<span class="string">"spring-service.xml"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">import</span> <span class="attr">resource</span>=<span class="string">"spring-mvc.xml"</span>/></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">beans</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、web.xml</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">web-app</span> <span class="attr">xmlns</span>=<span class="string">"http://xmlns.jcp.org/xml/ns/javaee"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">version</span>=<span class="string">"4.0"</span>></span></span><br><span class="line"> </span><br><span class="line"> <span class="comment"><!--注册dispatcherServlet--></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-name</span>></span>dispatcherServlet<span class="tag"></<span class="name">servlet-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-class</span>></span>org.springframework.web.servlet.DispatcherServlet<span class="tag"></<span class="name">servlet-class</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">init-param</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-name</span>></span>contextConfigLocation<span class="tag"></<span class="name">param-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-value</span>></span>classpath:applicationContext.xml<span class="tag"></<span class="name">param-value</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">init-param</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">load-on-startup</span>></span>1<span class="tag"></<span class="name">load-on-startup</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">servlet</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-mapping</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-name</span>></span>dispatcherServlet<span class="tag"></<span class="name">servlet-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url-pattern</span>></span>/<span class="tag"></<span class="name">url-pattern</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">servlet-mapping</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--配置编码过滤器--></span></span><br><span class="line"> <span class="tag"><<span class="name">filter</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-name</span>></span>encodingFilter<span class="tag"></<span class="name">filter-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-class</span>></span>org.springframework.web.filter.CharacterEncodingFilter<span class="tag"></<span class="name">filter-class</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">init-param</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-name</span>></span>encoding<span class="tag"></<span class="name">param-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-value</span>></span>utf-8<span class="tag"></<span class="name">param-value</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">init-param</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">filter</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-mapping</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-name</span>></span>encodingFilter<span class="tag"></<span class="name">filter-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url-pattern</span>></span>/*<span class="tag"></<span class="name">url-pattern</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">filter-mapping</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--Session过期时间--></span></span><br><span class="line"> <span class="tag"><<span class="name">session-config</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">session-timeout</span>></span>15<span class="tag"></<span class="name">session-timeout</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">session-config</span>></span></span><br><span class="line"><span class="tag"></<span class="name">web-app</span>></span></span><br></pre></td></tr></tbody></table></figure><p><strong>Controller 和视图层编写</strong></p><p>1、查询全部书籍, BookController 类编写</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.zzb.pojo.Book;</span><br><span class="line"><span class="keyword">import</span> com.zzb.service.BookService;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Qualifier;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.ui.Model;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="meta">@RequestMapping("/book")</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BookController</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// controller层调用业务层</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// 自动装备指定参数</span></span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="meta">@Qualifier("bookServiceImpl")</span></span><br><span class="line"> <span class="keyword">private</span> BookService bookService;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/allBook")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">allBook</span><span class="params">(Model model)</span></span>{</span><br><span class="line"> List<Book> bookList = bookService.queryAllBook();</span><br><span class="line"></span><br><span class="line"> model.addAttribute(<span class="string">"bookList"</span>, bookList);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"allBook"</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>2、编写首页 index.jsp</p><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><%@ page contentType=<span class="string">"text/html;charset=UTF-8"</span> language=<span class="string">"java"</span> %></span><br><span class="line"><html></span><br><span class="line"> <head></span><br><span class="line"> <title>$Title$</title></span><br><span class="line"> <style type=<span class="string">"text/css"</span>></span><br><span class="line"> a {</span><br><span class="line"> text-decoration: none;</span><br><span class="line"> color: black;</span><br><span class="line"> font-size: 18px;</span><br><span class="line"> }</span><br><span class="line"> h3 {</span><br><span class="line"> width: 180px;</span><br><span class="line"> height: 38px;</span><br><span class="line"> margin: 100px auto;</span><br><span class="line"> text-align: center;</span><br><span class="line"> line-height: 38px;</span><br><span class="line"> background: deepskyblue;</span><br><span class="line"> border-radius: 4px;</span><br><span class="line"> }</span><br><span class="line"> </style></span><br><span class="line"> </head></span><br><span class="line"> <body></span><br><span class="line"> <h3></span><br><span class="line"> <a href="${pageContext.request.contextPath}/book/allBook">进入书籍展示页面</a></span><br><span class="line"> </h3></span><br><span class="line"> </body></span><br><span class="line"></html></span><br></pre></td></tr></tbody></table></figure><p>3、数据列表页面 allBook.jsp</p><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><%@ taglib prefix=<span class="string">"c"</span> uri=<span class="string">"http://java.sun.com/jsp/jstl/core"</span> %></span><br><span class="line"><%@ page contentType=<span class="string">"text/html;charset=UTF-8"</span> language=<span class="string">"java"</span> %></span><br><span class="line"><html></span><br><span class="line"><head></span><br><span class="line"> <title>书籍列表</title></span><br><span class="line"> <meta name=<span class="string">"viewport"</span> content=<span class="string">"width=device-width, initial-scale=1.0"</span>></span><br><span class="line"> <!-- 引入 Bootstrap --></span><br><span class="line"> <link href=<span class="string">"https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"</span> rel=<span class="string">"stylesheet"</span>></span><br><span class="line"></head></span><br><span class="line"><body></span><br><span class="line"></span><br><span class="line"><div <span class="class"><span class="keyword">class</span></span>=<span class="string">"container"</span>></span><br><span class="line"></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"row clearfix"</span>></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"col-md-12 column"</span>></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"page-header"</span>></span><br><span class="line"> <h1></span><br><span class="line"> <small>书籍列表 —— 显示所有书籍</small></span><br><span class="line"> </h1></span><br><span class="line"> </div></span><br><span class="line"> </div></span><br><span class="line"> </div></span><br><span class="line"></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"row"</span>></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"col-md-4 column"</span>></span><br><span class="line"> <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增</a></span><br><span class="line"> </div></span><br><span class="line"> </div></span><br><span class="line"></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"row clearfix"</span>></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"col-md-12 column"</span>></span><br><span class="line"> <table <span class="class"><span class="keyword">class</span></span>=<span class="string">"table table-hover table-striped"</span>></span><br><span class="line"> <thead></span><br><span class="line"> <tr></span><br><span class="line"> <th>书籍编号</th></span><br><span class="line"> <th>书籍名字</th></span><br><span class="line"> <th>书籍数量</th></span><br><span class="line"> <th>书籍详情</th></span><br><span class="line"> <th>操作</th></span><br><span class="line"> </tr></span><br><span class="line"> </thead></span><br><span class="line"></span><br><span class="line"> <tbody></span><br><span class="line"> <c:forEach <span class="keyword">var</span>=<span class="string">"book"</span> items=<span class="string">"${requestScope.get('bookList')}"</span>></span><br><span class="line"></span><br><span class="line"> <tr></span><br><span class="line"> <td>${book.getBookId()}</td></span><br><span class="line"> <td>${book.getBookName()}</td></span><br><span class="line"> <td>${book.getBookCount()}</td></span><br><span class="line"> <td>${book.getDetail()}</td></span><br><span class="line"> <td></span><br><span class="line"> <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.getBookId()}">更改</a></span><br><span class="line"> |</span><br><span class="line"> <a href="${pageContext.request.contextPath}/book/del/${book.getBookId()}">删除</a></span><br><span class="line"> </td></span><br><span class="line"> </tr></span><br><span class="line"> </c:forEach></span><br><span class="line"> </tbody></span><br><span class="line"> </table></span><br><span class="line"> </div></span><br><span class="line"> </div></span><br><span class="line"></div></span><br><span class="line"></body></span><br><span class="line"></html></span><br></pre></td></tr></tbody></table></figure><p>4、添加书籍,BookController类编写</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 添加书籍功能</span></span><br><span class="line"><span class="comment">// 首先跳转到添加书籍页面</span></span><br><span class="line"><span class="meta">@RequestMapping("toAddBook")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">toAddBook</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"addBook"</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 执行数据添加</span></span><br><span class="line"><span class="meta">@RequestMapping("addBook")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">addBook</span><span class="params">(Book book)</span></span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> i = bookService.addBook(book);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"书籍添加成果!"</span>);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"书籍添加失败!"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"redirect:/book/allBook"</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>5、添加书籍jsp页面</p><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><%@ taglib prefix=<span class="string">"c"</span> uri=<span class="string">"http://java.sun.com/jsp/jstl/core"</span> %></span><br><span class="line"><%@ page contentType=<span class="string">"text/html;charset=UTF-8"</span> language=<span class="string">"java"</span> %></span><br><span class="line"><html></span><br><span class="line"><head></span><br><span class="line"> <title>新增书籍</title></span><br><span class="line"> <meta name=<span class="string">"viewport"</span> content=<span class="string">"width=device-width, initial-scale=1.0"</span>></span><br><span class="line"> <!-- 引入 Bootstrap --></span><br><span class="line"> <link href=<span class="string">"https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"</span> rel=<span class="string">"stylesheet"</span>></span><br><span class="line"></head></span><br><span class="line"><body></span><br><span class="line"></span><br><span class="line"><div <span class="class"><span class="keyword">class</span></span>=<span class="string">"container"</span>></span><br><span class="line"></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"row clearfix"</span>></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"col-md-12 column"</span>></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"page-header"</span>></span><br><span class="line"> <h1></span><br><span class="line"> <small>新增书籍</small></span><br><span class="line"> </h1></span><br><span class="line"> </div></span><br><span class="line"> </div></span><br><span class="line"> </div></span><br><span class="line"> <form action=<span class="string">"${pageContext.request.contextPath}/book/addBook"</span> method=<span class="string">"post"</span>></span><br><span class="line"> 书籍名称:<input type=<span class="string">"text"</span> name=<span class="string">"bookName"</span>><br><br><br></span><br><span class="line"> 书籍数量:<input type=<span class="string">"text"</span> name=<span class="string">"bookCount"</span>><br><br><br></span><br><span class="line"> 书籍详情:<input type=<span class="string">"text"</span> name=<span class="string">"detail"</span>><br><br><br></span><br><span class="line"> <input type=<span class="string">"submit"</span> value=<span class="string">"添加"</span>></span><br><span class="line"> </form></span><br><span class="line"></span><br><span class="line"></div></span><br><span class="line"></body></span><br><span class="line"></html></span><br></pre></td></tr></tbody></table></figure><p>6、修改书籍,BookController类编写</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 修改书籍信息</span></span><br><span class="line"><span class="comment">// 跳转到书籍信息修改页面</span></span><br><span class="line"><span class="meta">@RequestMapping("toUpdateBook")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">toUpdateBook</span><span class="params">(<span class="meta">@RequestParam("id")</span> <span class="keyword">int</span> bookId, Model model)</span></span>{</span><br><span class="line"> <span class="comment">// 首先查询到书籍的原信息,在页面中进行展示</span></span><br><span class="line"> Book book = bookService.queryBookById(bookId);</span><br><span class="line"> System.out.println(book.toString());</span><br><span class="line"> model.addAttribute(<span class="string">"book"</span>, book);</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"updateBook"</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// 执行修改数据操作</span></span><br><span class="line"><span class="meta">@RequestMapping("updateBook")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">updateBook</span><span class="params">(Book book)</span></span>{</span><br><span class="line"> <span class="keyword">int</span> i = bookService.updateBook(book);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"修改书籍成功!"</span>);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"修改书籍失败!"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> <span class="string">"redirect:/book/allBook"</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>7、修改书籍jsp页面</p><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><%@ taglib prefix=<span class="string">"c"</span> uri=<span class="string">"http://java.sun.com/jsp/jstl/core"</span> %></span><br><span class="line"><%@ page contentType=<span class="string">"text/html;charset=UTF-8"</span> language=<span class="string">"java"</span> %></span><br><span class="line"><html></span><br><span class="line"><head></span><br><span class="line"> <title>新增书籍</title></span><br><span class="line"> <meta name=<span class="string">"viewport"</span> content=<span class="string">"width=device-width, initial-scale=1.0"</span>></span><br><span class="line"> <!-- 引入 Bootstrap --></span><br><span class="line"> <link href=<span class="string">"https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"</span> rel=<span class="string">"stylesheet"</span>></span><br><span class="line"></head></span><br><span class="line"><body></span><br><span class="line"></span><br><span class="line"><div <span class="class"><span class="keyword">class</span></span>=<span class="string">"container"</span>></span><br><span class="line"></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"row clearfix"</span>></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"col-md-12 column"</span>></span><br><span class="line"> <div <span class="class"><span class="keyword">class</span></span>=<span class="string">"page-header"</span>></span><br><span class="line"> <h1></span><br><span class="line"> <small>修改书籍书籍</small></span><br><span class="line"> </h1></span><br><span class="line"> </div></span><br><span class="line"> </div></span><br><span class="line"> </div></span><br><span class="line"> <form action=<span class="string">"${pageContext.request.contextPath}/book/updateBook"</span> method=<span class="string">"post"</span>></span><br><span class="line"> <%--隐藏域传递书籍id--%></span><br><span class="line"> <input type=<span class="string">"hidden"</span> name=<span class="string">"bookId"</span> value=<span class="string">"${book.getBookId()}"</span>></span><br><span class="line"> 书籍名称:<input type=<span class="string">"text"</span> name=<span class="string">"bookName"</span> value=<span class="string">"${book.getBookName()}"</span>><br><br><br></span><br><span class="line"> 书籍数量:<input type=<span class="string">"text"</span> name=<span class="string">"bookCount"</span> value=<span class="string">"${book.getBookCount()}"</span>><br><br><br></span><br><span class="line"> 书籍详情:<input type=<span class="string">"text"</span> name=<span class="string">"detail"</span> value=<span class="string">"${book.getDetail()}"</span>><br><br><br></span><br><span class="line"> <input type=<span class="string">"submit"</span> value=<span class="string">"修改"</span>></span><br><span class="line"> </form></span><br><span class="line"></span><br><span class="line"></div></span><br><span class="line"></body></span><br><span class="line"></html></span><br></pre></td></tr></tbody></table></figure><p>8、删除数据,BookController类编写</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 删除一本书籍</span></span><br><span class="line"><span class="meta">@RequestMapping("/deleteBook/{id}")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">deleteBook</span><span class="params">(<span class="meta">@PathVariable("id")</span> <span class="keyword">int</span> bookId)</span></span>{</span><br><span class="line"> <span class="keyword">int</span> i = bookService.deleteBookById(bookId);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"删除书籍成功!"</span>);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"删除书籍失败!"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"redirect:/book/allBook"</span>;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>9、配置Tomcat,运行,测试</p><p><strong>问题说明:</strong></p><p>1、手动为项目添加lib文件夹,在其中添加项目所需要的包,否则项目会报错!</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201217214032939.png" alt="image-20201217214032939"></p><p>2、由于xml文件中使用了中文注释,需要在pom配置文件中,添加如下配置,使项目整体编码为utf-8,否则报错!</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">properties</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">project.build.sourceEncoding</span>></span>UTF8<span class="tag"></<span class="name">project.build.sourceEncoding</span>></span></span><br><span class="line"><span class="tag"></<span class="name">properties</span>></span></span><br></pre></td></tr></tbody></table></figure><p><strong>项目结构图:</strong></p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201217214229768.png" alt="image-20201217214229768"></p><h2 id="6-JSON"><a href="#6-JSON" class="headerlink" title="6 JSON"></a>6 JSON</h2><blockquote><p>什么使 JSON ? </p></blockquote><ul><li><p>JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式,目前使用特别广泛;</p></li><li><p>采用完全独立于编程语言的<strong>文本格式</strong>来存储和表示数据。</p></li><li><p>简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。</p></li><li><p>易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。</p></li></ul><p>在 JavaScript 语言中,一切都是对象。因此,任何JavaScript 支持的类型都可以通过 JSON 来表示,例如字符串、数字、对象、数组等。看看他的要求和语法格式:</p><ul><li>对象表示为键值对,数据由逗号分隔</li><li>花括号保存对象</li><li>方括号保存数组</li></ul><p><strong>JSON 键值对</strong>是用来保存 JavaScript 对象的一种方式,和 JavaScript 对象的写法也大同小异,键/值对组合中的键名写在前面并用双引号 “” 包裹,使用冒号 : 分隔,然后紧接着值:</p><figure class="highlight json"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">{<span class="attr">"name"</span>: <span class="string">"Zzb"</span>}</span><br><span class="line">{<span class="attr">"age"</span>: <span class="string">"1"</span>}</span><br><span class="line">{<span class="attr">"sex"</span>: <span class="string">"男"</span>}</span><br></pre></td></tr></tbody></table></figure><p>JSON 是 JavaScript 对象的字符串表示法,它使用文本表示一个 JS 对象的信息,本质是一个字符串。</p><figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = {<span class="attr">a</span>: <span class="string">'Hello'</span>, <span class="attr">b</span>: <span class="string">'World'</span>}; <span class="comment">//这是一个对象,注意键名也是可以使用引号包裹的</span></span><br><span class="line"><span class="keyword">var</span> json = <span class="string">'{"a": "Hello", "b": "World"}'</span>; <span class="comment">//这是一个 JSON 字符串,本质是一个字符串</span></span><br></pre></td></tr></tbody></table></figure><p><strong>JSON 和 JavaScript 对象互转</strong></p><p>JSON ——> JavaScript</p><figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = <span class="built_in">JSON</span>.parse(<span class="string">'{"a": "Hello", "b": "World"}'</span>);</span><br><span class="line"><span class="comment">//结果是 {a: 'Hello', b: 'World'}</span></span><br></pre></td></tr></tbody></table></figure><p>JavaScript ——> JSON</p><figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> json = <span class="built_in">JSON</span>.stringify({<span class="attr">a</span>: <span class="string">'Hello'</span>, <span class="attr">b</span>: <span class="string">'World'</span>});</span><br><span class="line"><span class="comment">//结果是 '{"a": "Hello", "b": "World"}'</span></span><br></pre></td></tr></tbody></table></figure><p><strong>测试</strong></p><p>1、新建module:springmvc-05-json,添加web支持!</p><p>2、在web目录下,新建一个jsonTest1.html,编写测试内容:</p><figure class="highlight html"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>JsonTest<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>></span></span><br><span class="line"><span class="javascript"> <span class="comment">//编写一个js的对象</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> user = {</span></span><br><span class="line"><span class="javascript"> name:<span class="string">"Zzb"</span>,</span></span><br><span class="line"> age:1,</span><br><span class="line"><span class="javascript"> sex:<span class="string">"男"</span></span></span><br><span class="line"> };</span><br><span class="line"><span class="javascript"> <span class="comment">//将js对象转换成json字符串</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> str = <span class="built_in">JSON</span>.stringify(user);</span></span><br><span class="line"><span class="javascript"> <span class="built_in">console</span>.log(str);</span></span><br><span class="line"></span><br><span class="line"><span class="javascript"> <span class="comment">//将json字符串转换为js对象</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> user2 = <span class="built_in">JSON</span>.parse(str);</span></span><br><span class="line"><span class="javascript"> <span class="built_in">console</span>.log(user2)</span></span><br><span class="line"><span class="javascript"> <span class="built_in">console</span>.log(user2.age,user2.name,user2.sex);</span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></tbody></table></figure><p>在网页中打开,测试结果为:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201217220934212.png" alt="image-20201217220934212"></p><blockquote><p>Controller 返回 JSON 数据</p></blockquote><p>解析工具有:Jackson、fastjson </p><p><strong>Jackson</strong></p><p>1、导入相关jar包</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --></span></span><br><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>com.fasterxml.jackson.core<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>jackson-databind<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>2.12.0<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></tbody></table></figure><p>2、配置web.xml</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">web-app</span> <span class="attr">xmlns</span>=<span class="string">"http://xmlns.jcp.org/xml/ns/javaee"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">version</span>=<span class="string">"4.0"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--1 配置DispatcherServlet--></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-name</span>></span>DispatcherServlet<span class="tag"></<span class="name">servlet-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-class</span>></span>org.springframework.web.servlet.DispatcherServlet<span class="tag"></<span class="name">servlet-class</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">init-param</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-name</span>></span>contextConfigLocation<span class="tag"></<span class="name">param-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-value</span>></span>classpath:springmvc-servlet.xml<span class="tag"></<span class="name">param-value</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">init-param</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">load-on-startup</span>></span>1<span class="tag"></<span class="name">load-on-startup</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">servlet</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-mapping</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">servlet-name</span>></span>DispatcherServlet<span class="tag"></<span class="name">servlet-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url-pattern</span>></span>/<span class="tag"></<span class="name">url-pattern</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">servlet-mapping</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">filter</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-name</span>></span>encoding<span class="tag"></<span class="name">filter-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-class</span>></span>org.springframework.web.filter.CharacterEncodingFilter<span class="tag"></<span class="name">filter-class</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">init-param</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-name</span>></span>encoding<span class="tag"></<span class="name">param-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">param-value</span>></span>utf-8<span class="tag"></<span class="name">param-value</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">init-param</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">filter</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-mapping</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filter-name</span>></span>encoding<span class="tag"></<span class="name">filter-name</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">url-pattern</span>></span>/*<span class="tag"></<span class="name">url-pattern</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">filter-mapping</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">web-app</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、springmvc-servlet.xml</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">"http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:context</span>=<span class="string">"http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xmlns:mvc</span>=<span class="string">"http://www.springframework.org/schema/mvc"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:schemaLocation</span>=<span class="string">"http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/context</span></span></span><br><span class="line"><span class="tag"><span class="string"> https://www.springframework.org/schema/context/spring-context.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string"> http://www.springframework.org/schema/mvc</span></span></span><br><span class="line"><span class="tag"><span class="string"> https://www.springframework.org/schema/mvc/spring-mvc.xsd"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--自动扫描包,让指定包下的注解生效,由IOC容器统一管理--></span></span><br><span class="line"> <span class="tag"><<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"com.zzb.controller"</span>/></span></span><br><span class="line"> <span class="comment"><!--让Spring MVC不处理静态资源,例如.mp3 .css .jpg等不走视图解析器--></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:default-servlet-handler</span>/></span></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> 支持mvc注解驱动</span></span><br><span class="line"><span class="comment"> 在spring中一般采用@RequestMapping注解来完成映射关系</span></span><br><span class="line"><span class="comment"> 要想使@RequestMapping注解生效</span></span><br><span class="line"><span class="comment"> 必须向上下文中注册DefaultAnnotationHandlerMapping</span></span><br><span class="line"><span class="comment"> 和一个AnnotationMethodHandlerAdapter实例</span></span><br><span class="line"><span class="comment"> 这两个实例分别在类级别和方法级别处理。</span></span><br><span class="line"><span class="comment"> 而annotation-driven配置帮助我们自动完成上述两个实例的注入</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:annotation-driven</span>/></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--视图解析器--></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span> <span class="attr">id</span>=<span class="string">"internalResourceViewResolver"</span>></span></span><br><span class="line"> <span class="comment"><!--前缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"prefix"</span> <span class="attr">value</span>=<span class="string">"/WEB-INF/jsp/"</span>/></span></span><br><span class="line"> <span class="comment"><!--后缀--></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"suffix"</span> <span class="attr">value</span>=<span class="string">".jsp"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">beans</span>></span></span><br></pre></td></tr></tbody></table></figure><p>4、实体类</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line"> <span class="keyword">private</span> String sex;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">()</span> </span>{</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(String name, <span class="keyword">int</span> age, String sex)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> <span class="keyword">this</span>.age = age;</span><br><span class="line"> <span class="keyword">this</span>.sex = sex;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> age;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.age = age;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getSex</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> sex;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSex</span><span class="params">(String sex)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.sex = sex;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"User{"</span> +</span><br><span class="line"> <span class="string">"name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", age="</span> + age +</span><br><span class="line"> <span class="string">", sex='"</span> + sex + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>5、编写Controller</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.fasterxml.jackson.core.JsonProcessingException;</span><br><span class="line"><span class="keyword">import</span> com.fasterxml.jackson.databind.ObjectMapper;</span><br><span class="line"><span class="keyword">import</span> com.zzb.pojo.User;</span><br><span class="line"><span class="keyword">import</span> org.springframework.stereotype.Controller;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.ResponseBody;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Controller</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserController</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/json1")</span></span><br><span class="line"> <span class="meta">@ResponseBody</span> <span class="comment">// 该注解声明返回结果是响应体,不会走视图解析器</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">jsonTest1</span><span class="params">()</span> <span class="keyword">throws</span> JsonProcessingException </span>{</span><br><span class="line"> <span class="comment">// 创建一个jackson的对象映射器,用来解析数据</span></span><br><span class="line"> ObjectMapper mapper = <span class="keyword">new</span> ObjectMapper();</span><br><span class="line"> <span class="comment">// 创建一个对象</span></span><br><span class="line"> User user = <span class="keyword">new</span> User(<span class="string">"Zzb1号"</span>, <span class="number">1</span>, <span class="string">"男"</span>);</span><br><span class="line"> String str = mapper.writeValueAsString(user);</span><br><span class="line"> <span class="keyword">return</span> str;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>6、配置Tomcat,注意项目中添加lib文件,并在其中添加依赖!</p><p>测试:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201217223843923.png" alt="image-20201217223843923"></p><p>出现乱码问题!</p><p>需要设置一下编码格式(utf-8),以及返回的类型!</p><p>通过@RequestMaping的produces属性来实现,修改下代码</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//produces:指定响应体返回类型和编码</span></span><br><span class="line"><span class="meta">@RequestMapping(value = "/json1",produces = "application/json;charset=utf-8")</span></span><br></pre></td></tr></tbody></table></figure><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201217224109783.png" alt="image-20201217224109783"></p><p>乱码问题解决!</p><blockquote><p>代码优化</p></blockquote><p><strong>乱码统一解决</strong></p><p>每次使用上述方法解决乱码问题比较麻烦,可以通过Spring配置统一指定!在springmvc的核心配置文件中添加StringHttpMessageConverter转换配置!</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">mvc:annotation-driven</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:message-converters</span> <span class="attr">register-defaults</span>=<span class="string">"true"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.http.converter.StringHttpMessageConverter"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">constructor-arg</span> <span class="attr">value</span>=<span class="string">"UTF-8"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"objectMapper"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"failOnEmptyBeans"</span> <span class="attr">value</span>=<span class="string">"false"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">property</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">mvc:message-converters</span>></span></span><br><span class="line"><span class="tag"></<span class="name">mvc:annotation-driven</span>></span></span><br></pre></td></tr></tbody></table></figure><p><strong>返回响应体</strong></p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"> <span class="meta">@RequestMapping("/json1")</span></span><br><span class="line"> <span class="meta">@ResponseBody</span> <span class="comment">// 该注解声明返回结果是响应体,不会走视图解析器</span></span><br><span class="line"><span class="meta">@RestController</span> <span class="comment">// 可以在类上直接使用该注解,该类的所有方法都只会返回响应体,不需要每个方法都添加 @ResponseBody 注解</span></span><br></pre></td></tr></tbody></table></figure><p>测试:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201217225042621.png" alt="image-20201217225042621"></p><blockquote><p>集合转JSON</p></blockquote><p>添加类方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping("/json2")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">jsonTest2</span><span class="params">()</span> <span class="keyword">throws</span> JsonProcessingException </span>{</span><br><span class="line"> <span class="comment">// 创建一个jackson的对象映射器,用来解析数据</span></span><br><span class="line"> ObjectMapper mapper = <span class="keyword">new</span> ObjectMapper();</span><br><span class="line"> <span class="comment">// 创建一个对象</span></span><br><span class="line"> User user1 = <span class="keyword">new</span> User(<span class="string">"Zzb1号"</span>, <span class="number">1</span>, <span class="string">"男"</span>);</span><br><span class="line"> User user2 = <span class="keyword">new</span> User(<span class="string">"Zzb2号"</span>, <span class="number">2</span>, <span class="string">"男"</span>);</span><br><span class="line"> User user3 = <span class="keyword">new</span> User(<span class="string">"Zzb3号"</span>, <span class="number">3</span>, <span class="string">"男"</span>);</span><br><span class="line"> User user4 = <span class="keyword">new</span> User(<span class="string">"Zzb4号"</span>, <span class="number">4</span>, <span class="string">"男"</span>);</span><br><span class="line"></span><br><span class="line"> List<User> list = <span class="keyword">new</span> ArrayList<User>();</span><br><span class="line"> list.add(user1);</span><br><span class="line"> list.add(user2);</span><br><span class="line"> list.add(user3);</span><br><span class="line"> list.add(user4);</span><br><span class="line"></span><br><span class="line"> String str = mapper.writeValueAsString(list);</span><br><span class="line"> <span class="keyword">return</span> str;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201217225403269.png" alt="image-20201217225403269"></p><blockquote><p>输出时间对象</p></blockquote><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">jsonTest3</span><span class="params">()</span> <span class="keyword">throws</span> JsonProcessingException </span>{</span><br><span class="line"> <span class="comment">// 创建一个jackson的对象映射器,用来解析数据</span></span><br><span class="line"> ObjectMapper mapper = <span class="keyword">new</span> ObjectMapper();</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 创建一个时间对象</span></span><br><span class="line"> Date date = <span class="keyword">new</span> Date();</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 显示结果为时间戳</span></span><br><span class="line"> String str = mapper.writeValueAsString(date);</span><br><span class="line"> <span class="keyword">return</span> str;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201217225657005.png" alt="image-20201217225657005"></p><ul><li>默认日期格式会变成一个数字,是1970年1月1日到当前日期的毫秒数!</li><li>Jackson 默认是会把时间转成timestamps形式</li></ul><p><strong>自定义时间格式</strong></p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping("/json4")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">jsonTest4</span><span class="params">()</span> <span class="keyword">throws</span> JsonProcessingException </span>{</span><br><span class="line"> <span class="comment">// 创建一个jackson的对象映射器,用来解析数据</span></span><br><span class="line"> ObjectMapper mapper = <span class="keyword">new</span> ObjectMapper();</span><br><span class="line"> <span class="comment">// 不使用时间戳的方式</span></span><br><span class="line"> mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, <span class="keyword">false</span>);</span><br><span class="line"> <span class="comment">// 自定义时间格式</span></span><br><span class="line"> SimpleDateFormat sdf = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>);</span><br><span class="line"> <span class="comment">// 设置时间格式</span></span><br><span class="line"> mapper.setDateFormat(sdf);</span><br><span class="line"> <span class="comment">// 创建一个时间对象</span></span><br><span class="line"> Date date = <span class="keyword">new</span> Date();</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 显示结果为时间戳</span></span><br><span class="line"> String str = mapper.writeValueAsString(date);</span><br><span class="line"> <span class="keyword">return</span> str;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201217230215694.png" alt="image-20201217230215694"></p><blockquote><p>抽取为工具类</p></blockquote><p>将JSON的转换封装成工具类</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.utils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.fasterxml.jackson.core.JsonProcessingException;</span><br><span class="line"><span class="keyword">import</span> com.fasterxml.jackson.databind.ObjectMapper;</span><br><span class="line"><span class="keyword">import</span> com.fasterxml.jackson.databind.SerializationFeature;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">JsonUtils</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getJson</span><span class="params">(Object object)</span></span>{</span><br><span class="line"> <span class="keyword">return</span> getJson(object,<span class="string">"yyyy-MM-dd HH:mm:ss"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getJson</span><span class="params">(Object object, String dateFormat)</span></span>{</span><br><span class="line"> <span class="comment">// 创建一个jackson的对象映射器,用来解析数据</span></span><br><span class="line"> ObjectMapper mapper = <span class="keyword">new</span> ObjectMapper();</span><br><span class="line"> <span class="comment">// 不使用时间戳的方式</span></span><br><span class="line"> mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, <span class="keyword">false</span>);</span><br><span class="line"> <span class="comment">// 自定义时间格式</span></span><br><span class="line"> SimpleDateFormat sdf = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"dateFormat"</span>);</span><br><span class="line"> <span class="comment">// 设置时间格式</span></span><br><span class="line"> mapper.setDateFormat(sdf);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 显示结果为时间戳</span></span><br><span class="line"> String str = <span class="keyword">null</span>;</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> str = mapper.writeValueAsString(object);</span><br><span class="line"> } <span class="keyword">catch</span> (JsonProcessingException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> str;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>使用工具类编写代码</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping("/json5")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">jsonTest5</span><span class="params">()</span></span>{</span><br><span class="line"> Date date = <span class="keyword">new</span> Date();</span><br><span class="line"> String json = JsonUtils.getJson(date);</span><br><span class="line"> <span class="keyword">return</span> json;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201217232330324.png" alt="image-20201217232330324"></p><blockquote><p>FastJson</p></blockquote><p>FastJson 由阿里巴巴开发,专门用于java的包,可以方便的实现json对象与JavaBean对象的转换,实现JavaBean对象与json字符串的转换,实现json对象与json字符串的转换。实现json的转换方法很多,最后的实现结果都是一样的。</p><p>jar包</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>com.alibaba<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>fastjson<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>1.2.60<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></tbody></table></figure><p>FastJson 三个主要的类:</p><p><strong>JSONObject 代表 json 对象</strong> </p><ul><li>JSONObject实现了Map接口, JSONObject底层操作是由Map实现的。</li><li>JSONObject对应json对象,通过各种形式的get()方法可以获取json对象中的数据,也可利用诸如size(),isEmpty()等方法获取”键:值”对的个数和判断是否为空。其本质是通过实现Map接口并调用接口中的方法完成的。</li></ul><p><strong>JSONArray 代表 json 对象数组</strong></p><ul><li>内部是有List接口中的方法来完成操作的。</li></ul><p><strong>JSON代表 JSONObject和JSONArray的转化</strong></p><ul><li>仔细观察这些方法,主要是实现json对象,json对象数组,javabean对象,json字符串之间的相互转化。</li></ul><p>代码测试:</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson.JSON;</span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson.JSONObject;</span><br><span class="line"><span class="keyword">import</span> com.zzb.pojo.User;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FastJsonTest</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>{</span><br><span class="line"> <span class="comment">// 创建对象</span></span><br><span class="line"> User user1 = <span class="keyword">new</span> User(<span class="string">"Zzb1号"</span>, <span class="number">1</span>, <span class="string">"男"</span>);</span><br><span class="line"> User user2 = <span class="keyword">new</span> User(<span class="string">"Zzb2号"</span>, <span class="number">2</span>, <span class="string">"男"</span>);</span><br><span class="line"> User user3 = <span class="keyword">new</span> User(<span class="string">"Zzb3号"</span>, <span class="number">3</span>, <span class="string">"男"</span>);</span><br><span class="line"> User user4 = <span class="keyword">new</span> User(<span class="string">"Zzb4号"</span>, <span class="number">4</span>, <span class="string">"男"</span>);</span><br><span class="line"></span><br><span class="line"> List<User> list = <span class="keyword">new</span> ArrayList<User>();</span><br><span class="line"> list.add(user1);</span><br><span class="line"> list.add(user2);</span><br><span class="line"> list.add(user3);</span><br><span class="line"> list.add(user4);</span><br><span class="line"></span><br><span class="line"> System.out.println(<span class="string">"java对象转json字符串"</span>);</span><br><span class="line"> String str1 = JSON.toJSONString(list);</span><br><span class="line"> System.out.println(<span class="string">"java -> json (list)"</span> + str1);</span><br><span class="line"> String str2 = JSON.toJSONString(user1);</span><br><span class="line"> System.out.println(<span class="string">"java -> json (user1)"</span> + str1);</span><br><span class="line"></span><br><span class="line"> System.out.println(<span class="string">"json字符串转java对象"</span>);</span><br><span class="line"> User jUser1 = JSON.parseObject(str2, User.class);</span><br><span class="line"> System.out.println(<span class="string">"jUser1"</span> + jUser1.toString());</span><br><span class="line"></span><br><span class="line"> System.out.println(<span class="string">"java对象转json对象"</span>);</span><br><span class="line"> JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);</span><br><span class="line"> System.out.println(<span class="string">"JSONObject (user2)"</span> + jsonObject1.getString(<span class="string">"name"</span>));</span><br><span class="line"></span><br><span class="line"> System.out.println(<span class="string">"json对象转java对象"</span>);</span><br><span class="line"> User jUser2 = JSON.toJavaObject(jsonObject1, User.class);</span><br><span class="line"> System.out.println(<span class="string">"jUser2 :"</span> + jUser2.toString());</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201217233501499.png" alt="image-20201217233501499"></p><h2 id="7-Ajax"><a href="#7-Ajax" class="headerlink" title="7 Ajax"></a>7 Ajax</h2><blockquote><p>简介</p></blockquote><ul><li><p><strong>AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。</strong> </p></li><li><p>AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。</p></li><li><p><strong>Ajax 不是一种新的编程语言,而是一种用于创建更好更快以及交互性更强的Web应用程序的技术。</strong></p></li><li><p>在 2005 年,Google 通过其 Google Suggest 使 AJAX 变得流行起来。Google Suggest能够自动帮你完成搜索单词。</p></li><li><p>Google Suggest 使用 AJAX 创造出动态性极强的 web 界面:当您在谷歌的搜索框输入关键字时,JavaScript 会把这些字符发送到服务器,然后服务器会返回一个搜索建议的列表。</p></li><li><p>传统的网页(即不使用ajax技术的网页),想要更新内容或者提交一个表单,都需要重新加载整个网页。</p></li><li><p>使用ajax技术的网页,通过在后台服务器进行少量的数据交换,就可以实现异步局部更新。</p></li><li><p>使用Ajax,用户可以创建接近本地桌面应用的直接、高可用、更丰富、更动态的Web用户界面。</p></li></ul><blockquote><p>伪造Ajax</p></blockquote><p>1、新建module springmvc-06-ajax,导入web支持,测试项目,是否能成功运行!</p><p>2、编写一个 test.html 使用 iframe测试</p><figure class="highlight html"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>伪造AJAX<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">script</span> <span class="attr">type</span>=<span class="string">"text/javascript"</span>></span></span><br><span class="line"><span class="javascript"> <span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> myDate = <span class="keyword">new</span> <span class="built_in">Date</span>();</span></span><br><span class="line"><span class="javascript"> <span class="built_in">document</span>.getElementById(<span class="string">'currentTime'</span>).innerText = myDate.getTime();</span></span><br><span class="line"> };</span><br><span class="line"></span><br><span class="line"><span class="javascript"> <span class="function"><span class="keyword">function</span> <span class="title">LoadPage</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> targetUrl = <span class="built_in">document</span>.getElementById(<span class="string">'url'</span>).value;</span></span><br><span class="line"><span class="javascript"> <span class="built_in">console</span>.log(targetUrl);</span></span><br><span class="line"><span class="javascript"> <span class="built_in">document</span>.getElementById(<span class="string">"iframePosition"</span>).src = targetUrl;</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">script</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>请输入要加载的地址:<span class="tag"><<span class="name">span</span> <span class="attr">id</span>=<span class="string">"currentTime"</span>></span><span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">id</span>=<span class="string">"url"</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">value</span>=<span class="string">"https://www.baidu.com/"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"提交"</span> <span class="attr">onclick</span>=<span class="string">"LoadPage()"</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h3</span>></span>加载页面位置:<span class="tag"></<span class="name">h3</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">iframe</span> <span class="attr">id</span>=<span class="string">"iframePosition"</span> <span class="attr">style</span>=<span class="string">"width: 100%;height: 500px;"</span>></span><span class="tag"></<span class="name">iframe</span>></span></span><br><span class="line"><span class="tag"></<span class="name">div</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、在IDEA中打开浏览器查看效果!</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20210106135930189.png" alt="image-20210106135930189"></p><p><strong>利用AJAX可以做:</strong></p><ul><li>注册时,检测用户名是否已注册</li><li>登录时,提示用户名密码错误</li><li>等等</li></ul><blockquote><p>jQuery.ajax</p></blockquote><ul><li><p>这里使用jquery来实现ajax; </p></li><li><p>Ajax的核心是XMLHttpRequest对象(XHR)。XHR为向服务器发送请求和解析服务器响应提供了接口。能够以异步方式从服务器获取新数据;</p></li><li><p>jQuery 提供多个与 AJAX 有关的方法;</p></li><li><p>通过jQuery.AJAX方法,可以使用HTTP GET 何HTTP POST从远程服务器上请求文本、HTML、XML活JSON,同时也可以把这些外部数据直接载入网页的被选元素中;</p></li><li><p>jQuery 不是生产者,而是大自然搬运工;</p></li><li><p>jQuery Ajax本质就是 XMLHttpRequest,对他进行了封装,方便调用!</p></li></ul><figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">jQuery.ajax(...)</span><br><span class="line"> 部分参数:</span><br><span class="line"> url:请求地址</span><br><span class="line"> type:请求方式,GET、POST(<span class="number">1.9</span><span class="number">.0</span>之后用method)</span><br><span class="line"> headers:请求头</span><br><span class="line"> data:要发送的数据</span><br><span class="line"> contentType:即将发送信息至服务器的内容编码类型(默认: <span class="string">"application/x-www-form-urlencoded; charset=UTF-8"</span>)</span><br><span class="line"> <span class="keyword">async</span>:是否异步</span><br><span class="line"> timeout:设置请求超时时间(毫秒)</span><br><span class="line"> beforeSend:发送请求前执行的函数(全局)</span><br><span class="line"> complete:完成之后执行的回调函数(全局)</span><br><span class="line"> success:成功之后执行的回调函数(全局)</span><br><span class="line"> error:失败之后执行的回调函数(全局)</span><br><span class="line"> accepts:通过请求头发送给服务器,告诉服务器当前客户端可接受的数据类型</span><br><span class="line"> dataType:将服务器端返回的数据转换成指定类型</span><br><span class="line"> <span class="string">"xml"</span>: 将服务器端返回的内容转换成xml格式</span><br><span class="line"> <span class="string">"text"</span>: 将服务器端返回的内容转换成普通文本格式</span><br><span class="line"> <span class="string">"html"</span>: 将服务器端返回的内容转换成普通文本格式,在插入DOM中时,如果包含JavaScript标签,则会尝试去执行。</span><br><span class="line"> <span class="string">"script"</span>: 尝试将返回值当作JavaScript去执行,然后再将服务器端返回的内容转换成普通文本格式</span><br><span class="line"> <span class="string">"json"</span>: 将服务器端返回的内容转换成相应的JavaScript对象</span><br><span class="line"> <span class="string">"jsonp"</span>: JSONP 格式使用 JSONP 形式调用函数时,如 <span class="string">"myurl?callback=?"</span> jQuery 将自动替换 ? 为正确的函数名,以执行回调函数</span><br></pre></td></tr></tbody></table></figure><p><strong>使用 HttpServletResponse 处理,实现简单的测试!</strong></p><p>1、配置 web.xml 和 springmvc 的配置文件。</p><p>2、编写一个AjaxController</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.controller;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RequestMapping;</span><br><span class="line"><span class="keyword">import</span> org.springframework.web.bind.annotation.RestController;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">AjaxController</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="meta">@RequestMapping("/a1")</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">a1</span><span class="params">(String name, HttpServletResponse response)</span> <span class="keyword">throws</span> IOException </span>{</span><br><span class="line"> System.out.println(<span class="string">"a1:param=>"</span> + name);</span><br><span class="line"> <span class="keyword">if</span>(<span class="string">"admin"</span>.equals(name)){</span><br><span class="line"> response.getWriter().print(<span class="string">"true"</span>);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> response.getWriter().print(<span class="string">"false"</span>);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>3、编写index.jsp</p><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><%@ page contentType=<span class="string">"text/html;charset=UTF-8"</span> language=<span class="string">"java"</span> %></span><br><span class="line"><html></span><br><span class="line"> <head></span><br><span class="line"> <title>$Title$</title></span><br><span class="line"> <%--导入在线jquery--%></span><br><span class="line"> <script src="${pageContext.request.contextPath}/statics/js/jquery-3.5.1.js"></script></span><br><span class="line"></span><br><span class="line"> <script></span><br><span class="line"> <span class="function">function <span class="title">a</span><span class="params">()</span></span>{</span><br><span class="line"> $.post({</span><br><span class="line"> url:<span class="string">"${pageContext.request.contextPath}/a1"</span>,</span><br><span class="line"> data:{<span class="string">'name'</span>:$(<span class="string">"#txtName"</span>).val()},</span><br><span class="line"> success:function (data){</span><br><span class="line"> alert(data);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"> </script></span><br><span class="line"></span><br><span class="line"> </head></span><br><span class="line"> <body></span><br><span class="line"></span><br><span class="line"> <%--失去焦点触发事件--%></span><br><span class="line"> 用户名:<input type=<span class="string">"text"</span> id=<span class="string">"txtName"</span> onblur=<span class="string">"a()"</span>></span><br><span class="line"> </body></span><br><span class="line"></html></span><br></pre></td></tr></tbody></table></figure><p>4、启动tomcat测试</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20210106191523577.png" alt="image-20210106191523577"></p><p><strong>SpringMVC实现</strong></p><p>1、实体类</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line"> <span class="keyword">private</span> String sex;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">()</span> </span>{</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(String name, <span class="keyword">int</span> age, String sex)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> <span class="keyword">this</span>.age = age;</span><br><span class="line"> <span class="keyword">this</span>.sex = sex;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> age;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.age = age;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getSex</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> sex;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSex</span><span class="params">(String sex)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.sex = sex;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"User{"</span> +</span><br><span class="line"> <span class="string">"name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", age="</span> + age +</span><br><span class="line"> <span class="string">", sex='"</span> + sex + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>2、Controller类编写</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping("/a2")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> List<User> <span class="title">a2</span><span class="params">()</span></span>{</span><br><span class="line"> List<User> userList = <span class="keyword">new</span> ArrayList<User>();</span><br><span class="line"> userList.add(<span class="keyword">new</span> User(<span class="string">"Zzb1号"</span>,<span class="number">1</span>,<span class="string">"男"</span>));</span><br><span class="line"> userList.add(<span class="keyword">new</span> User(<span class="string">"Zzb2号"</span>,<span class="number">2</span>,<span class="string">"男"</span>));</span><br><span class="line"> userList.add(<span class="keyword">new</span> User(<span class="string">"Zzb3号"</span>,<span class="number">3</span>,<span class="string">"男"</span>));</span><br><span class="line"> <span class="keyword">return</span> userList; <span class="comment">// 由于@RestController注解,将list转成json格式返回</span></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>3、前端页面</p><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><%@ page contentType=<span class="string">"text/html;charset=UTF-8"</span> language=<span class="string">"java"</span> %></span><br><span class="line"><html></span><br><span class="line"><head></span><br><span class="line"> <title>Title</title></span><br><span class="line"> <script src="${pageContext.request.contextPath}/statics/js/jquery-3.5.1.js"></script></span><br><span class="line"></span><br><span class="line"></head></span><br><span class="line"><body></span><br><span class="line"></span><br><span class="line"><input type=<span class="string">"button"</span> id=<span class="string">"btn"</span> value=<span class="string">"获取数据"</span>/></span><br><span class="line"><table width=<span class="string">"80%"</span> align=<span class="string">"center"</span>></span><br><span class="line"> <tr></span><br><span class="line"> <td>姓名</td></span><br><span class="line"> <td>年龄</td></span><br><span class="line"> <td>性别</td></span><br><span class="line"> </tr></span><br><span class="line"> <tbody id=<span class="string">"content"</span>></span><br><span class="line"></span><br><span class="line"> </tbody></span><br><span class="line"></table></span><br><span class="line"></span><br><span class="line"><script></span><br><span class="line"> $(<span class="string">"#btn"</span>).click(function (){</span><br><span class="line"> $.post(<span class="string">"${pageContext.request.contextPath}/a2"</span>, function (data){</span><br><span class="line"> console.log(data)</span><br><span class="line"> <span class="keyword">var</span> html = <span class="string">""</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>; i < data.length; ++i){</span><br><span class="line"> html+=<span class="string">"<tr>"</span> + <span class="string">"<td>"</span> + data[i].name + <span class="string">"</td>"</span> + <span class="string">"<td>"</span> + data[i].age + <span class="string">"</td>"</span> + <span class="string">"<td>"</span> + data[i].sex + <span class="string">"</td>"</span> + <span class="string">"</tr>"</span></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> $(<span class="string">"#content"</span>).html(html);</span><br><span class="line"> })</span><br><span class="line"> })</span><br><span class="line"></script></span><br><span class="line"></span><br><span class="line"></body></span><br><span class="line"></html></span><br></pre></td></tr></tbody></table></figure><p>4、测试</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20210106193750816.png" alt="image-20210106193750816"></p><blockquote><p>注册提示效果</p></blockquote><p>1、编写Controller类</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping("/a3")</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> String <span class="title">a3</span><span class="params">(String name)</span></span>{</span><br><span class="line"> String msg = <span class="string">""</span>;</span><br><span class="line"> <span class="comment">// 模拟数据库中存在数据</span></span><br><span class="line"> <span class="keyword">if</span>(name != <span class="keyword">null</span>){</span><br><span class="line"> <span class="keyword">if</span>(<span class="string">"admin"</span>.equals(name)){</span><br><span class="line"> msg = <span class="string">"ok"</span>;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> msg = <span class="string">"用户名不存在!"</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> msg;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>2、前端页面</p><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><%@ page contentType=<span class="string">"text/html;charset=UTF-8"</span> language=<span class="string">"java"</span> %></span><br><span class="line"><html></span><br><span class="line"><head></span><br><span class="line"> <title>Title</title></span><br><span class="line"> <script src="${pageContext.request.contextPath}/statics/js/jquery-3.5.1.js"></script></span><br><span class="line"></head></span><br><span class="line"><body></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><p></span><br><span class="line"> 用户名:<input type=<span class="string">"text"</span> id=<span class="string">"name"</span> onblur=<span class="string">"a()"</span>></span><br><span class="line"> <span id="userInfo"></span></span><br><span class="line"></p></span><br><span class="line"><p></span><br><span class="line"> 密码:<input type=<span class="string">"password"</span> id=<span class="string">"pwd"</span> onblur=<span class="string">"a()"</span>></span><br><span class="line"></p></span><br><span class="line"></span><br><span class="line"><script></span><br><span class="line"> <span class="function">function <span class="title">a</span><span class="params">()</span></span>{</span><br><span class="line"> $.post({</span><br><span class="line"> url:<span class="string">"${pageContext.request.contextPath}/a3"</span>,</span><br><span class="line"> data:{<span class="string">'name'</span>:$(<span class="string">"#name"</span>).val()},</span><br><span class="line"> success:function (data){</span><br><span class="line"> console.log(data);</span><br><span class="line"> <span class="keyword">if</span>(data.toString() == <span class="string">"ok"</span>){</span><br><span class="line"> $(<span class="string">"#userInfo"</span>).css(<span class="string">"color"</span>, <span class="string">"green"</span>);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> $(<span class="string">"#userInfo"</span>).css(<span class="string">"color"</span>, <span class="string">"red"</span>);</span><br><span class="line"> }</span><br><span class="line"> $(<span class="string">"#userInfo"</span>).html(data);</span><br><span class="line"> }</span><br><span class="line"> });</span><br><span class="line"> }</span><br><span class="line"></script></span><br><span class="line"></span><br><span class="line"></body></span><br><span class="line"></html></span><br></pre></td></tr></tbody></table></figure><p>注意:因为传递的是json对象,所以要处理json字符串的乱码问题</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">mvc:annotation-driven</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mvc:message-converters</span> <span class="attr">register-defaults</span>=<span class="string">"true"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.http.converter.StringHttpMessageConverter"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">constructor-arg</span> <span class="attr">value</span>=<span class="string">"UTF-8"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"objectMapper"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">bean</span> <span class="attr">class</span>=<span class="string">"org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"failOnEmptyBeans"</span> <span class="attr">value</span>=<span class="string">"false"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">property</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">bean</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">mvc:message-converters</span>></span></span><br><span class="line"><span class="tag"></<span class="name">mvc:annotation-driven</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、测试</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20210106195420085.png" alt="image-20210106195420085"></p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20210106195432238.png" alt="image-20210106195432238"></p><blockquote><p>获取百度接口Demo</p></blockquote><figure class="highlight html"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">HTML</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">"Content-Type"</span> <span class="attr">content</span>=<span class="string">"text/html; charset=utf-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>JSONP百度搜索<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">style</span>></span></span><br><span class="line"> #q{</span><br><span class="line"><span class="css"> <span class="attribute">width</span>: <span class="number">500px</span>;</span></span><br><span class="line"><span class="css"> <span class="attribute">height</span>: <span class="number">30px</span>;</span></span><br><span class="line"><span class="css"> <span class="attribute">border</span>:<span class="number">1px</span> solid <span class="number">#ddd</span>;</span></span><br><span class="line"><span class="css"> <span class="attribute">line-height</span>: <span class="number">30px</span>;</span></span><br><span class="line"><span class="css"> <span class="attribute">display</span>: block;</span></span><br><span class="line"><span class="css"> <span class="attribute">margin</span>: <span class="number">0</span> auto;</span></span><br><span class="line"><span class="css"> <span class="attribute">padding</span>: <span class="number">0</span> <span class="number">10px</span>;</span></span><br><span class="line"><span class="css"> <span class="attribute">font-size</span>: <span class="number">14px</span>;</span></span><br><span class="line"> }</span><br><span class="line"> #ul{</span><br><span class="line"><span class="css"> <span class="attribute">width</span>: <span class="number">520px</span>;</span></span><br><span class="line"><span class="css"> <span class="attribute">list-style</span>: none;</span></span><br><span class="line"><span class="css"> <span class="attribute">margin</span>: <span class="number">0</span> auto;</span></span><br><span class="line"><span class="css"> <span class="attribute">padding</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="css"> <span class="attribute">border</span>:<span class="number">1px</span> solid <span class="number">#ddd</span>;</span></span><br><span class="line"><span class="css"> <span class="attribute">margin-top</span>: -<span class="number">1px</span>;</span></span><br><span class="line"><span class="css"> <span class="attribute">display</span>: none;</span></span><br><span class="line"> }</span><br><span class="line"><span class="css"> <span class="selector-id">#ul</span> <span class="selector-tag">li</span>{</span></span><br><span class="line"><span class="css"> <span class="attribute">line-height</span>: <span class="number">30px</span>;</span></span><br><span class="line"><span class="css"> <span class="attribute">padding</span>: <span class="number">0</span> <span class="number">10px</span>;</span></span><br><span class="line"> }</span><br><span class="line"><span class="css"> <span class="selector-id">#ul</span> <span class="selector-tag">li</span><span class="selector-pseudo">:hover</span>{</span></span><br><span class="line"><span class="css"> <span class="attribute">background-color</span>: <span class="number">#f60</span>;</span></span><br><span class="line"><span class="css"> <span class="attribute">color</span>: <span class="number">#fff</span>;</span></span><br><span class="line"> }</span><br><span class="line"> <span class="tag"></<span class="name">style</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">script</span>></span></span><br><span class="line"></span><br><span class="line"><span class="javascript"> <span class="comment">// 2.步骤二</span></span></span><br><span class="line"><span class="javascript"> <span class="comment">// 定义demo函数 (分析接口、数据)</span></span></span><br><span class="line"><span class="javascript"> <span class="function"><span class="keyword">function</span> <span class="title">demo</span>(<span class="params">data</span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> Ul = <span class="built_in">document</span>.getElementById(<span class="string">'ul'</span>);</span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> html = <span class="string">''</span>;</span></span><br><span class="line"><span class="javascript"> <span class="comment">// 如果搜索数据存在 把内容添加进去</span></span></span><br><span class="line"> if (data.s.length) {</span><br><span class="line"><span class="javascript"> <span class="comment">// 隐藏掉的ul显示出来</span></span></span><br><span class="line"><span class="javascript"> Ul.style.display = <span class="string">'block'</span>;</span></span><br><span class="line"><span class="javascript"> <span class="comment">// 搜索到的数据循环追加到li里</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">0</span>;i<data.s.length;i++){</span></span><br><span class="line"><span class="javascript"> html += <span class="string">'<li>'</span>+data.s[i]+<span class="string">'</li>'</span>;</span></span><br><span class="line"> }</span><br><span class="line"><span class="javascript"> <span class="comment">// 循环的li写入ul</span></span></span><br><span class="line"> Ul.innerHTML = html;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"><span class="javascript"> <span class="comment">// 1.步骤一</span></span></span><br><span class="line"><span class="javascript"> <span class="built_in">window</span>.onload = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="comment">// 获取输入框和ul</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> Q = <span class="built_in">document</span>.getElementById(<span class="string">'q'</span>);</span></span><br><span class="line"></span><br><span class="line"><span class="javascript"> <span class="comment">// 事件鼠标抬起时候</span></span></span><br><span class="line"><span class="javascript"> Q.onkeyup = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>{</span></span><br><span class="line"><span class="javascript"> <span class="comment">// 如果输入框不等于空</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">if</span> (<span class="built_in">this</span>.value != <span class="string">''</span>) {</span></span><br><span class="line"><span class="javascript"> <span class="comment">// ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆JSONPz重点☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆</span></span></span><br><span class="line"><span class="javascript"> <span class="comment">// 创建标签</span></span></span><br><span class="line"><span class="javascript"> <span class="keyword">var</span> script = <span class="built_in">document</span>.createElement(<span class="string">'script'</span>);</span></span><br><span class="line"><span class="javascript"> <span class="comment">//给定要跨域的地址 赋值给src</span></span></span><br><span class="line"><span class="javascript"> <span class="comment">//这里是要请求的跨域的地址 我写的是百度搜索的跨域地址</span></span></span><br><span class="line"><span class="javascript"> <span class="comment">//&cb=demo ---》 会返回 demo(data)</span></span></span><br><span class="line"><span class="javascript"> script.src = <span class="string">'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd='</span>+<span class="built_in">this</span>.value+<span class="string">'&cb=demo'</span>;</span></span><br><span class="line"><span class="javascript"> <span class="comment">// 将组合好的带src的script标签追加到body里</span></span></span><br><span class="line"><span class="javascript"> <span class="built_in">document</span>.body.appendChild(script);</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="tag"></<span class="name">script</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"><span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">id</span>=<span class="string">"q"</span> /></span></span><br><span class="line"><span class="tag"><<span class="name">ul</span> <span class="attr">id</span>=<span class="string">"ul"</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">ul</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></tbody></table></figure>]]></content>
</entry>
<entry>
<title></title>
<link href="/javanblog/2021/04/25/Mybatis/"/>
<url>/javanblog/2021/04/25/Mybatis/</url>
<content type="html"><![CDATA[<h1 id="MyBatis"><a href="#MyBatis" class="headerlink" title="MyBatis"></a>MyBatis</h1><h2 id="1-MyBatis简介"><a href="#1-MyBatis简介" class="headerlink" title="1 MyBatis简介"></a>1 MyBatis简介</h2><blockquote><p>环境说明</p></blockquote><ul><li>jdk 1.8</li><li>Mysql 5.7.29</li><li>maven 3.6.1</li><li>IDEA</li></ul><blockquote><p>什么是MyBatis</p></blockquote><ul><li>MyBatis是一个基于Java的<strong>持久层框架</strong></li><li>MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作</li><li>MyBatis 可以通过简单的 XML 或注解来配置,将接口和 Java 的 实体类 【Plain Old Java Objects,普通的 Java对象】映射成数据库中的记录。</li><li>MyBatis 本是apache的一个开源项目iBatis,, 2010年这个项目由apache 迁移到了google code,并且改名为MyBatis 。</li><li>2013年11月迁移到<strong>GitHub</strong> .</li><li>MyBatis官方文档 : <a href="http://www.mybatis.org/mybatis-3/zh/index.html">http://www.mybatis.org/mybatis-3/zh/index.html</a></li><li>GitHub : <a href="https://github.com/mybatis/mybatis-3">https://github.com/mybatis/mybatis-3</a></li></ul><blockquote><p>什么是持久化</p></blockquote><p><strong>持久化是将程序数据在持久状态和瞬时状态间转换的机制。</strong></p><ul><li>瞬时数据(比如内存中的数据,是不能永久保存的)持久化为持久数据(比如持久化至数据库中,能够长久保存)。</li><li>持久化的主要应用是将内存中的对象存储在数据库中,或者存储在磁盘文件中、XML数据文件中等等。</li><li>JDBC就是一种持久化机制。文件IO也是一种持久化机制。</li></ul><p><strong>为什么需要持久化</strong></p><ul><li>内存断电后数据会丢失</li><li>内存过于昂贵</li></ul><blockquote><p>持久层</p></blockquote><p><strong>什么是持久层</strong></p><ul><li>完成持久化工作的代码块——Dao层。</li><li>数据持久化通常意味着将内存中的数据保存到磁盘上固化,而持久化的实现大多通过各种关系数据库来完成。</li><li>系统架构中,应该有一个相对独立的逻辑层,专注于数据持久化逻辑的实现。</li></ul><blockquote><p>为什么需要Mybatis</p></blockquote><ul><li><p>Mybatis帮助程序员将数据存入数据库中,和从数据库中获取数据。</p></li><li><p>传统的JDBC操作,存在重复代码块,开发效率低,使用MyBatis可以<strong>提高开发效率</strong>。</p></li><li><p>MyBatis 是一个半自动化的<strong>ORM框架 (Object Relationship Mapping) ——>对象关系映射</strong></p></li><li><p>MyBatis的优点</p><ul><li>简单易学:安装及配置简单,能够通过阅读文档和源代码掌握它的设计思想及实现。</li><li>使用灵活:SQL语句写在xml配置文件中,便于统一管理和优化。</li><li>解除耦合:SQL语句与程序代码分离,提高可维护性。</li><li>支持动态编写SQL</li><li>……</li></ul></li></ul><h2 id="2-MyBatis第一个程序"><a href="#2-MyBatis第一个程序" class="headerlink" title="2 MyBatis第一个程序"></a>2 MyBatis第一个程序</h2><p><strong>思路流程:搭建环境—>导入MyBatis—>编写代码—>测试</strong></p><p><strong>该程序实现CRUD操作,项目完整结构图及常见问题在后文说明。</strong></p><blockquote><p>代码演示</p></blockquote><p>1、 搭建实验所需数据库</p><figure class="highlight sql"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> DATABASE `mybatis`;</span><br><span class="line"></span><br><span class="line">USE `mybatis`;</span><br><span class="line"># 如果表格存在,则删除</span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> `<span class="keyword">user</span>`;</span><br><span class="line"></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `<span class="keyword">user</span>` (</span><br><span class="line">`id` <span class="type">int</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">`name` <span class="type">varchar</span>(<span class="number">30</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line">`pwd` <span class="type">varchar</span>(<span class="number">30</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"><span class="keyword">PRIMARY</span> KEY (`id`)</span><br><span class="line">) ENGINE<span class="operator">=</span>InnoDB <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> `<span class="keyword">user</span>`(`id`,`name`,`pwd`) <span class="keyword">values</span> (<span class="number">1</span>,<span class="string">'BG'</span>,<span class="string">'123456'</span>),(<span class="number">2</span>,<span class="string">'张三'</span>,<span class="string">'abcdef'</span>),(<span class="number">3</span>,<span class="string">'李四'</span>,<span class="string">'987654'</span>),(<span class="number">4</span>,<span class="string">'王五'</span>,<span class="string">'123444'</span>);</span><br></pre></td></tr></tbody></table></figure><p>2、IDEA创建项目</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201126112906566.png" alt="image-20201126112906566"></p><p>选择Maven项目,选择JDK版本,然后点击Next。</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201126113256584.png" alt="image-20201126113256584"></p><p>设置项目名称,及GroupId(一般为公司域名),然后点击Finish,完成项目创建。</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201126113436936.png" alt="image-20201126113436936"></p><p>完成项目创建后的结构图,删除掉src文件夹,将该目录作为父工程,此pom.xml为父工程所有,可供所有子模块使用。</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201126114811520.png" alt="image-20201126114811520"></p><p>右键当前项目,创建新模块。</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201126114903827.png" alt="image-20201126114903827"></p><p>输入子模块名称,点击Finish,即完成模块创建。</p><p>3、导入相关jar包</p><ul><li>在父工程的pom.xml文件中导入相关jar包,在<a href="https://mvnrepository.com/">Maven</a>仓库中查找</li></ul><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">dependencies</span>></span></span><br><span class="line"> <span class="comment"><!--mysql驱动--></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>mysql<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>mysql-connector-java<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>5.1.48<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.mybatis<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>mybatis<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>3.5.2<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--用于测试--></span></span><br><span class="line"> <span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>junit<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>junit<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>4.12<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">dependencies</span>></span></span><br></pre></td></tr></tbody></table></figure><p>4、编写MyBatis核心配置文件</p><ul><li>查看帮助文档,即Mybatis官方文档</li></ul><p><strong>mybatis-config.xml</strong></p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">configuration</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-config.dtd"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">configuration</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"com.mysql.jdbc.Driver"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"jdbc:mysql://localhost:3306/mybatis?Unicode=true<span class="symbol">&amp;</span>characterEncoding=utf8"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"root"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"1234"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">dataSource</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">environment</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">environments</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--编写玩Mapper接口及对应的XML配置文件后,需要在MyBatis核心配置文件中注册,以下两种格式都可以--></span></span><br><span class="line"> <span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.bg.mapper.UserMapper"</span>/></span></span><br><span class="line"><span class="comment"><!--<mapper resource="com/bg/mapper/UserMapper.xml"/>--></span></span><br><span class="line"> <span class="tag"></<span class="name">mappers</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">configuration</span>></span></span><br></pre></td></tr></tbody></table></figure><p>5、编写MyBatis工具类</p><ul><li>查看帮助文档</li></ul><p><strong>MyBatisTool.java</strong></p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.bg.tools;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.io.Resources;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSession;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSessionFactory;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSessionFactoryBuilder;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStream;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyBatisTool</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> InputStream inputStream;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> SqlSessionFactory sqlSessionFactory;</span><br><span class="line"> <span class="keyword">static</span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> String resource = <span class="string">"mybatis-config.xml"</span>;</span><br><span class="line"> <span class="comment">// 加载MyBatis核心配置文件</span></span><br><span class="line"> inputStream = Resources.getResourceAsStream(resource);</span><br><span class="line"> <span class="comment">// 通过输入流构建SqlSessionFactory</span></span><br><span class="line"> sqlSessionFactory = <span class="keyword">new</span> SqlSessionFactoryBuilder().build(inputStream);</span><br><span class="line"> } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }<span class="keyword">finally</span> {</span><br><span class="line"> <span class="comment">// 关闭输入流</span></span><br><span class="line"> <span class="keyword">if</span>(inputStream == <span class="keyword">null</span>){</span><br><span class="line"> <span class="keyword">try</span> {</span><br><span class="line"> inputStream.close();</span><br><span class="line"> } <span class="keyword">catch</span> (IOException e) {</span><br><span class="line"> e.printStackTrace();</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 获取SqlSession实例,用来操作数据库</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> SqlSession <span class="title">getSqlSession</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> sqlSessionFactory.openSession();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>6、创建实体类</p><p><strong>User.java</strong></p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.bg.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"> <span class="keyword">private</span> String pwd;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">()</span> </span>{</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">User</span><span class="params">(<span class="keyword">int</span> id, String name, String pwd)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.id = id;</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> <span class="keyword">this</span>.pwd = pwd;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getId</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(<span class="keyword">int</span> id)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.id = id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getPwd</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> pwd;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setPwd</span><span class="params">(String pwd)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.pwd = pwd;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"User{"</span> +</span><br><span class="line"> <span class="string">"id="</span> + id +</span><br><span class="line"> <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", pwd='"</span> + pwd + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>7、编写Mapper接口类</p><p><strong>UserMapper.java</strong></p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.bg.mapper;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.bg.pojo.User;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserMapper</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 查询表格中的所有用户</span></span><br><span class="line"> <span class="function">List<User> <span class="title">getUserList</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 根据Id查询用户</span></span><br><span class="line"> <span class="function">User <span class="title">getUserById</span><span class="params">(<span class="keyword">int</span> userId)</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 增加一个用户</span></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">addUser</span><span class="params">(User user)</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 修改一个用户</span></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">updateUser</span><span class="params">(User user)</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 删除一个用户</span></span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">deleteUser</span><span class="params">(<span class="keyword">int</span> userId)</span></span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>8、编写Mapper.xml配置文件</p><p><strong>UserMapper.xml</strong></p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Mapper 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span><br><span class="line"><span class="comment"><!--绑定接口--></span></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.bg.mapper.UserMapper"</span>></span></span><br><span class="line"> <span class="comment"><!--绑定函数和返回对象类型--></span></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserList"</span> <span class="attr">resultType</span>=<span class="string">"com.bg.pojo.User"</span>></span></span><br><span class="line"> SELECT * FROM user</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserById"</span> <span class="attr">resultType</span>=<span class="string">"com.bg.pojo.User"</span>></span></span><br><span class="line"> SELECT * FROM user WHERE id = #{userId}</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--返回类型为基本类型,可以省略--></span></span><br><span class="line"> <span class="tag"><<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"addUser"</span> <span class="attr">parameterType</span>=<span class="string">"com.bg.pojo.User"</span>></span></span><br><span class="line"> INSERT INTO user(id, name, pwd) VALUES (#{id}, #{name}, #{pwd})</span><br><span class="line"> <span class="tag"></<span class="name">insert</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">update</span> <span class="attr">id</span>=<span class="string">"updateUser"</span> <span class="attr">parameterType</span>=<span class="string">"com.bg.pojo.User"</span>></span></span><br><span class="line"> UPDATE user SET name = #{name}, pwd = #{pwd} WHERE id = #{id}</span><br><span class="line"> <span class="tag"></<span class="name">update</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--输入参数为基本数据类型,也可以省略不写--></span></span><br><span class="line"> <span class="tag"><<span class="name">delete</span> <span class="attr">id</span>=<span class="string">"deleteUser"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span>></span></span><br><span class="line"> DELETE FROM user WHERE id = #{userId};</span><br><span class="line"> <span class="tag"></<span class="name">delete</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></tbody></table></figure><p>9、编写测试类</p><ul><li>Junit包测试</li></ul><p>MyTest.java</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.bg.mapper.UserMapper;</span><br><span class="line"><span class="keyword">import</span> com.bg.pojo.User;</span><br><span class="line"><span class="keyword">import</span> com.bg.tools.MyBatisTool;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSession;</span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyTest</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用junit进行测试,需要添加该注解</span></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getUserList</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 首先获得SqlSession对象</span></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="comment">// 通过SqlSession对象获取Mapper接口对象</span></span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// 调用mapper中的函数,执行sql</span></span><br><span class="line"> List<User> userList = mapper.getUserList();</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 遍历结果集,并输出</span></span><br><span class="line"> <span class="keyword">for</span> (User user : userList) {</span><br><span class="line"> System.out.println(user.toString());</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用完毕后,关闭SqlSession对象</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getUserById</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 首先获得SqlSession对象</span></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="comment">// 通过SqlSession对象获取Mapper接口对象</span></span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// 调用mapper中的函数,执行sql</span></span><br><span class="line"> User user = mapper.getUserById(<span class="number">3</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 遍历结果,并输出</span></span><br><span class="line"> System.out.println(user.toString());</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用完毕后,关闭SqlSession对象</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addUser</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 首先获得SqlSession对象</span></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="comment">// 通过SqlSession对象获取Mapper接口对象</span></span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// 调用mapper中的函数,执行sql</span></span><br><span class="line"> User user = <span class="keyword">new</span> User(<span class="number">5</span>, <span class="string">"赵六"</span>, <span class="string">"9527"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> i = mapper.addUser(user);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"插入数据成功"</span>);</span><br><span class="line"> <span class="comment">// 因为SqlSession默认关闭事务自动提交,因此插入数据成功后,我们需要手动提交事务,才能在数据库中完成插入</span></span><br><span class="line"> sqlSession.commit();</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"插入数据失败"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用完毕后,关闭SqlSession对象</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateUser</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 首先获得SqlSession对象</span></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="comment">// 通过SqlSession对象获取Mapper接口对象</span></span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// 调用mapper中的函数,执行sql</span></span><br><span class="line"> User user = <span class="keyword">new</span> User(<span class="number">3</span>, <span class="string">"张飞"</span>, <span class="string">"00222555"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> i = mapper.updateUser(user);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"更新数据成功"</span>);</span><br><span class="line"> <span class="comment">// 因为SqlSession默认关闭事务自动提交,因此更新数据成功后,我们需要手动提交事务,才能在数据库中完成插入</span></span><br><span class="line"> sqlSession.commit();</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"更新数据失败"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用完毕后,关闭SqlSession对象</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deleteUser</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 首先获得SqlSession对象</span></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="comment">// 通过SqlSession对象获取Mapper接口对象</span></span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// 调用mapper中的函数,执行sql</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> i = mapper.deleteUser(<span class="number">2</span>);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"删除数据成功"</span>);</span><br><span class="line"> <span class="comment">// 因为SqlSession默认关闭事务自动提交,因此删除数据成功后,我们需要手动提交事务,才能在数据库中完成插入</span></span><br><span class="line"> sqlSession.commit();</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"删除数据失败"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用完毕后,关闭SqlSession对象</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>10、运行测试,成功查询数据</p><blockquote><p>项目完整结构图</p></blockquote><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201126152933310.png" alt="image-20201126152933310"></p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201126152951513.png" alt="image-20201126152951513"></p><blockquote><p>问题说明</p></blockquote><p><strong>Maven静态资源过滤问题</strong></p><p>通常编译maven项目时,只能过滤resources下面的配置文件,当我们把配置文件写到java文件夹下面时,需要在pom.xml中,显示的开启资源过滤,使java文件夹下的配置文件能够被读取并编译。</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--在build中配置resource,来防止我们资源导出失败的问题,true代表允许过滤--></span></span><br><span class="line"><span class="tag"><<span class="name">build</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">resources</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">resource</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">directory</span>></span>src/main/resources<span class="tag"></<span class="name">directory</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">includes</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">include</span>></span>**/*.properties<span class="tag"></<span class="name">include</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">include</span>></span>**/*.xml<span class="tag"></<span class="name">include</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">includes</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filtering</span>></span>true<span class="tag"></<span class="name">filtering</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">resource</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">resource</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">directory</span>></span>src/main/java<span class="tag"></<span class="name">directory</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">includes</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">include</span>></span>**/*.properties<span class="tag"></<span class="name">include</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">include</span>></span>**/*.xml<span class="tag"></<span class="name">include</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">includes</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">filtering</span>></span>true<span class="tag"></<span class="name">filtering</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">resource</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">resources</span>></span></span><br><span class="line"><span class="tag"></<span class="name">build</span>></span></span><br></pre></td></tr></tbody></table></figure><p><strong>配置文件中文注释问题</strong></p><p>因为在配置文件中添加了中文注释,会导致乱码,故会报错。</p><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 1 字节的 UTF-8 序列的字节 1 无效。</span><br></pre></td></tr></tbody></table></figure><p>为了解决该问题,需要在父类的pom.xml中配置项目编码。</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">properties</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">project.build.sourceEncoding</span>></span>UTF8<span class="tag"></<span class="name">project.build.sourceEncoding</span>></span></span><br><span class="line"><span class="tag"></<span class="name">properties</span>></span></span><br></pre></td></tr></tbody></table></figure><p>在父类中pom.xml中添加的所有配置信息,在所有子模块中均生效。</p><h2 id="3-CRUD详解"><a href="#3-CRUD详解" class="headerlink" title="3 CRUD详解"></a>3 CRUD详解</h2><p><strong>namespace中的包名要和 Dao/mapper接口的包名一致!</strong></p><p><strong>UserMapper.xml</strong></p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Mapper 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.zzb.dao.UserMapper"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserList"</span> <span class="attr">resultType</span>=<span class="string">"com.zzb.pojo.User"</span>></span></span><br><span class="line"> select * from user</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserListByLike"</span> <span class="attr">parameterType</span>=<span class="string">"String"</span> <span class="attr">resultType</span>=<span class="string">"com.zzb.pojo.User"</span>></span></span><br><span class="line"> select * from user where name like #{value}</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserById"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span> <span class="attr">resultType</span>=<span class="string">"com.zzb.pojo.User"</span>></span></span><br><span class="line"> select * from user where id = #{id}</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserById2"</span> <span class="attr">parameterType</span>=<span class="string">"map"</span> <span class="attr">resultType</span>=<span class="string">"com.zzb.pojo.User"</span>></span></span><br><span class="line"> select * from user where id = #{keyId}</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--对象的属性可以直接取出来--></span></span><br><span class="line"> <span class="tag"><<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"addUser"</span> <span class="attr">parameterType</span>=<span class="string">"com.zzb.pojo.User"</span>></span></span><br><span class="line"> insert into user (id, name, pwd) values ( #{id}, #{name}, #{pwd});</span><br><span class="line"> <span class="tag"></<span class="name">insert</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">update</span> <span class="attr">id</span>=<span class="string">"updateUser"</span> <span class="attr">parameterType</span>=<span class="string">"com.zzb.pojo.User"</span>></span></span><br><span class="line"> update user set name = #{name}, pwd = #{pwd} where id = #{id};</span><br><span class="line"> <span class="tag"></<span class="name">update</span>></span></span><br></pre></td></tr></tbody></table></figure><blockquote><p>namespace</p></blockquote><p>配置文件中的namespace中的名称为对应的Mapper接口的完整包名,必须一致!</p><blockquote><p>select</p></blockquote><ul><li>select标签是mybatis中最常用的标签之一</li><li>select语句中有很多属性<ul><li>parameterType:传入SQL语句的参数类型,如果是基本类型可以省略,也可以通过Map进行参数传递</li><li>resultType:SQL语句的返回值类型,如果是基本类型可以省略,其他对象必须为完整的类名(含包名)或者别名</li><li>接口中的方法名与XML映射文件中的SQL语句ID要一一对应</li><li>resultMap:为返回结果集映射,即实体类中的属性与数据库表中的表头名称无法一一对应时,需要进行结果集的映射,才能保证成功完整的取出数据。</li></ul></li></ul><p><strong>根据id查询用户</strong></p><p>1、在UserMapper接口中添加相应的方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 根据Id查询用户</span></span><br><span class="line"><span class="function">User <span class="title">getUserById</span><span class="params">(<span class="keyword">int</span> userId)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、在UserMapper.xml中添加select语句</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserById"</span> <span class="attr">resultType</span>=<span class="string">"com.bg.pojo.User"</span>></span></span><br><span class="line"> SELECT * FROM user WHERE id = #{userId}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p><strong>参数类型传递方法</strong></p><ul><li><p>直接传递参数,可以按照上述代码传递参数,也可以在接口方法的参数前添加@Param属性,显示的指定参数名。</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.apache.ibatis.annotations.Param; </span><br><span class="line"></span><br><span class="line"><span class="comment">// 根据Id查询用户</span></span><br><span class="line"> <span class="function">User <span class="title">getUserById</span><span class="params">(<span class="meta">@Param("id")</span> <span class="keyword">int</span> userId)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>那么在XML配置文件中,就要做相应的修改(userId —> id)</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserById"</span> <span class="attr">resultType</span>=<span class="string">"com.bg.pojo.User"</span>></span></span><br><span class="line"> SELECT * FROM user WHERE id = #{id}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure></li><li><p>使用万能的Map传递参数,将需要传递的参数封装程Map,将接口修改为如下所示。</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Map; </span><br><span class="line"></span><br><span class="line"><span class="comment">// 根据Id查询用户</span></span><br><span class="line"> <span class="function">User <span class="title">getUserById</span><span class="params">(Map<String, Object> map)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>XML配置文件无需修改,也可以显示的指明传入参数类型为map</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserById"</span> <span class="attr">parameterType</span>=<span class="string">"map"</span> <span class="attr">resultType</span>=<span class="string">"com.bg.pojo.User"</span>></span></span><br><span class="line"> SELECT * FROM user WHERE id = #{id}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>测试时,需要将传入参数设置为Map类型:</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getUserById</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 首先获得SqlSession对象</span></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="comment">// 通过SqlSession对象获取Mapper接口对象</span></span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// map封装参数</span></span><br><span class="line"> Map<String, Object> map = <span class="keyword">new</span> HashMap<String, Object>();</span><br><span class="line"> map.put(<span class="string">"id"</span>, <span class="number">3</span>);</span><br><span class="line"> <span class="comment">// 调用mapper中的函数,执行sql</span></span><br><span class="line"> User user = mapper.getUserById(map);</span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 遍历结果,并输出</span></span><br><span class="line"> System.out.println(user.toString());</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 使用完毕后,关闭SqlSession对象</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure></li></ul><blockquote><p>insert</p></blockquote><p><strong>给数据库增加一个用户</strong></p><p>1、在UserMapper接口中添加对应的方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 增加一个用户</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">addUser</span><span class="params">(User user)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、在UserMapper.xml中添加insert语句</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--返回类型为基本类型,可以省略--></span></span><br><span class="line"><span class="tag"><<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"addUser"</span> <span class="attr">parameterType</span>=<span class="string">"com.bg.pojo.User"</span>></span></span><br><span class="line"> INSERT INTO user(id, name, pwd) VALUES (#{id}, #{name}, #{pwd})</span><br><span class="line"><span class="tag"></<span class="name">insert</span>></span></span><br></pre></td></tr></tbody></table></figure><p>注意:参数直接去User对象中对应的属性即可。</p><p>3、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addUser</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 首先获得SqlSession对象</span></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="comment">// 通过SqlSession对象获取Mapper接口对象</span></span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// 调用mapper中的函数,执行sql</span></span><br><span class="line"> User user = <span class="keyword">new</span> User(<span class="number">5</span>, <span class="string">"赵六"</span>, <span class="string">"9527"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> i = mapper.addUser(user);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"插入数据成功"</span>);</span><br><span class="line"> <span class="comment">// 因为SqlSession默认关闭事务自动提交,因此插入数据成功后,我们需要手动提交事务,才能在数据库中完成插入</span></span><br><span class="line"> sqlSession.commit();</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"插入数据失败"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用完毕后,关闭SqlSession对象</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>注意:增、删、改操作都需要提交事务</strong></p><blockquote><p>update</p></blockquote><p>通常使用update标签进行更新数据的操作。</p><p><strong>修改用户信息</strong></p><p>1、在UserMapper接口中添加对应的方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 修改一个用户</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">updateUser</span><span class="params">(User user)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、在UserMapper.xml中添加insert语句</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">update</span> <span class="attr">id</span>=<span class="string">"updateUser"</span> <span class="attr">parameterType</span>=<span class="string">"com.bg.pojo.User"</span>></span></span><br><span class="line"> UPDATE user SET name = #{name}, pwd = #{pwd} WHERE id = #{id}</span><br><span class="line"><span class="tag"></<span class="name">update</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateUser</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 首先获得SqlSession对象</span></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="comment">// 通过SqlSession对象获取Mapper接口对象</span></span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// 调用mapper中的函数,执行sql</span></span><br><span class="line"> User user = <span class="keyword">new</span> User(<span class="number">3</span>, <span class="string">"张飞"</span>, <span class="string">"00222555"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> i = mapper.updateUser(user);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"更新数据成功"</span>);</span><br><span class="line"> <span class="comment">// 因为SqlSession默认关闭事务自动提交,因此更新数据成功后,我们需要手动提交事务,才能在数据库中完成插入</span></span><br><span class="line"> sqlSession.commit();</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"更新数据失败"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用完毕后,关闭SqlSession对象</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><blockquote><p>delete</p></blockquote><p><strong>根据id删除一个用户</strong></p><p>1、在UserMapper接口中添加对应的方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 删除一个用户</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">deleteUser</span><span class="params">(<span class="keyword">int</span> userId)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、在UserMapper.xml中添加insert语句</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--输入参数为基本数据类型,也可以省略不写--></span></span><br><span class="line"><span class="tag"><<span class="name">delete</span> <span class="attr">id</span>=<span class="string">"deleteUser"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span>></span></span><br><span class="line"> DELETE FROM user WHERE id = #{userId};</span><br><span class="line"><span class="tag"></<span class="name">delete</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deleteUser</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 首先获得SqlSession对象</span></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="comment">// 通过SqlSession对象获取Mapper接口对象</span></span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// 调用mapper中的函数,执行sql</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span> i = mapper.deleteUser(<span class="number">2</span>);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"删除数据成功"</span>);</span><br><span class="line"> <span class="comment">// 因为SqlSession默认关闭事务自动提交,因此删除数据成功后,我们需要手动提交事务,才能在数据库中完成插入</span></span><br><span class="line"> sqlSession.commit();</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"删除数据失败"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用完毕后,关闭SqlSession对象</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>模糊查询</strong></p><p>在UserMapper接口中添加对应的方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 模糊查询</span></span><br><span class="line"><span class="function">User <span class="title">getUserByLike</span><span class="params">(String s)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>方法1:java代码中添加sql通配符</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--模糊查询--></span></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserByLike"</span> <span class="attr">resultType</span>=<span class="string">"com.bg.pojo.User"</span>></span></span><br><span class="line"> SELECT * FROM user WHERE name LIKE #{s}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getUserByLike</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 首先获得SqlSession对象</span></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="comment">// 通过SqlSession对象获取Mapper接口对象</span></span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// 调用mapper中的函数,执行sql</span></span><br><span class="line"> User user = mapper.getUserByLike(<span class="string">"%张%"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 遍历结果,并输出</span></span><br><span class="line"> System.out.println(user.toString());</span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 使用完毕后,关闭SqlSession对象</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>方法2:在sql语句中拼接通配符</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--模糊查询--></span></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserByLike"</span> <span class="attr">resultType</span>=<span class="string">"com.bg.pojo.User"</span>></span></span><br><span class="line"> SELECT * FROM user WHERE name LIKE "%"#{s}"%" //这里写死了,是为了防止sql注入</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getUserByLike</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="comment">// 首先获得SqlSession对象</span></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="comment">// 通过SqlSession对象获取Mapper接口对象</span></span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// 调用mapper中的函数,执行sql</span></span><br><span class="line"> User user = mapper.getUserByLike(<span class="string">"王"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 遍历结果,并输出</span></span><br><span class="line"> System.out.println(user.toString());</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 使用完毕后,关闭SqlSession对象</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>小结:</p><ul><li>所有的增删改操作需要显示的提交事务,或者在获取SqlSession的时候显示指定开始事务自动提交。</li><li>接口所有的普通参数,尽量写实@Param参数,显示声明参数名</li><li>可以考虑使用map传递参数,方便、简单。</li><li>为了规范操作,在SQL的配置文件中,我们尽量将Parameter参数和resultType都写上。</li></ul><h2 id="4-配置文件解析"><a href="#4-配置文件解析" class="headerlink" title="4 配置文件解析"></a>4 配置文件解析</h2><blockquote><p>核心配置文件</p></blockquote><ul><li>mybatis-config.xml 系统核心配置文件。</li><li>Mybatis 的配置文件会深刻影响MyBatis行为的设置和属性信息。</li><li>配置参数如下:</li></ul><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">configuration(配置)</span><br><span class="line">properties(属性)</span><br><span class="line">settings(设置)</span><br><span class="line">typeAliases(类型别名)</span><br><span class="line">typeHandlers(类型处理器)</span><br><span class="line">objectFactory(对象工厂)</span><br><span class="line">plugins(插件)</span><br><span class="line">environments(环境配置)</span><br><span class="line">environment(环境变量)</span><br><span class="line">transactionManager(事务管理器)</span><br><span class="line">dataSource(数据源)</span><br><span class="line">databaseIdProvider(数据库厂商标识)</span><br><span class="line">mappers(映射器)</span><br><span class="line"><span class="comment"><!-- 注意元素节点的顺序!顺序不对会报错 --></span></span><br></pre></td></tr></tbody></table></figure><blockquote><p>environments元素</p></blockquote><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"com.mysql.jdbc.Driver"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"jdbc:mysql://localhost:3306/mybatis?Unicode=true<span class="symbol">&amp;</span>characterEncoding=utf8"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"root"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"1234"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">dataSource</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">environment</span>></span></span><br><span class="line"><span class="tag"></<span class="name">environments</span>></span></span><br></pre></td></tr></tbody></table></figure><ul><li><p>MyBatis可以配置多套运行环境,将SQL映射到不同的数据库上,但只能指定一个来运行,通过default来指定</p></li><li><p>子元素节点:environment</p><ul><li><p>dataSource元素使用标准的JDBC数据源接口来配置JDBC连接对象的资源,数据源是必须配置的,type可选参数如下:</p><ul><li>POOLED:使用Mybatis自带的数据库连接池来管理数据库连接,这种数据源的实现利用“池”的概念将 JDBC 连接对象组织起来 , 这是一种使得并发 Web 应用快速响应请求的流行处理方式。</li><li>UNPOOLED:不使用任何数据库连接池来管理数据库连接,即使用时请求连接,使用完关闭连接。</li><li>JNDI:jndi形式使用数据库连接、主要用于项目正常使用的时候,这个数据源的实现是为了能在如 Spring 或应用服务器这类容器中使用,容器可以集中或在外部配置数据源,然后放置一个 JNDI 上下文的引用。</li></ul></li><li><p>transactionManager 事务管理器</p></li><li><p>property 数据库连接设置</p></li></ul></li><li><p>具体环境靠id进行区分,id保证唯一!</p></li></ul><blockquote><p>mappers元素</p></blockquote><ul><li>映射器:定义映射SQL语句文件</li><li>既然 MyBatis 的行为已经由上述元素配置完了,我们现在就要来定义 SQL 映射语句了。 但首先,我们需要告诉 MyBatis 到哪里去找到这些语句。 在自动查找资源方面,Java 并没有提供一个很好的解决方案,所以最好的办法是直接告诉 MyBatis 到哪里去找映射文件。 你可以使用相对于类路径的资源引用,或完全限定资源定位符(包括 <code>file:///</code> 形式的 URL),或类名和包名等。</li></ul><p>引入资源方式,<em><strong>推荐使用第一种</strong></em></p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!-- 使用相对于类路径的资源引用 --></span></span><br><span class="line"><span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">"org/mybatis/builder/PostMapper.xml"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">mappers</span>></span></span><br></pre></td></tr></tbody></table></figure><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">这个已经废弃了,不用了</span><br><span class="line"><span class="comment"><!-- 使用完全限定资源定位符(URL) --></span></span><br><span class="line"><span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mapper</span> <span class="attr">url</span>=<span class="string">"file:///var/mappers/AuthorMapper.xml"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">mappers</span>></span></span><br></pre></td></tr></tbody></table></figure><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--</span></span><br><span class="line"><span class="comment">使用映射器接口实现类的完全限定类名</span></span><br><span class="line"><span class="comment">需要配置文件名称和接口名称一致,并且位于同一目录下</span></span><br><span class="line"><span class="comment">--></span></span><br><span class="line"><span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"org.mybatis.builder.AuthorMapper"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">mappers</span>></span></span><br></pre></td></tr></tbody></table></figure><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--</span></span><br><span class="line"><span class="comment">将包内的映射器接口实现全部注册为映射器</span></span><br><span class="line"><span class="comment">但是需要配置文件名称和接口名称一致,并且位于同一目录下</span></span><br><span class="line"><span class="comment">--></span></span><br><span class="line"><span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">package</span> <span class="attr">name</span>=<span class="string">"org.mybatis.builder"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">mappers</span>></span></span><br></pre></td></tr></tbody></table></figure><p><strong>Mapper文件</strong></p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Mapper 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.kuang.mapper.UserMapper"</span>></span></span><br><span class="line"> </span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></tbody></table></figure><ul><li>namespace 中文意思:命名空间,作用如下:<ul><li>namespace 的值必须为某个接口(含包名),即包名+类名</li><li>接口中的方法与映射文件中的SQL语句的id一一对应</li><li>namespace 和子元素的 id 联合保证唯一,区别不同的mapper</li></ul></li></ul><blockquote><p>Properties优化</p></blockquote><p>数据库这些属性都是可外部配置且可动态替换的,既可以在典型的 Java 属性文件中配置,亦可通过 properties 元素的子元素来传递。</p><p>优化MyBatis核心配置文件</p><ul><li>在resources目录下创建db.properties文件</li></ul><figure class="highlight properties"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">driver</span>=<span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line"><span class="attr">url</span>=<span class="string">jdbc:mysql://localhost:3306/mybatis?Unicode=true&amp;characterEncoding=utf8</span></span><br><span class="line"><span class="attr">username</span>=<span class="string">root</span></span><br><span class="line"><span class="attr">password</span>=<span class="string">javan1996</span></span><br></pre></td></tr></tbody></table></figure><ul><li>在mybatis-config.xml配置文件中引入properties文件</li></ul><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">configuration</span>></span></span><br><span class="line"> <span class="comment"><!--引入外部配置文件--></span></span><br><span class="line"> <span class="tag"><<span class="name">properties</span> <span class="attr">resource</span>=<span class="string">"db.properties"</span>/></span></span><br><span class="line"> </span><br><span class="line"> <span class="tag"><<span class="name">environments</span> <span class="attr">default</span>=<span class="string">"development"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">environment</span> <span class="attr">id</span>=<span class="string">"development"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">transactionManager</span> <span class="attr">type</span>=<span class="string">"JDBC"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">dataSource</span> <span class="attr">type</span>=<span class="string">"POOLED"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driver"</span> <span class="attr">value</span>=<span class="string">"${driver}"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"${url}"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"${username}"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"${password}"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">dataSource</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">environment</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">environments</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--编写玩Mapper接口及对应的XML配置文件后,需要在MyBatis核心配置文件中注册,以下两种格式都可以--></span></span><br><span class="line"> <span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.bg.mapper.UserMapper"</span>/></span></span><br><span class="line"><span class="comment"><!-- <mapper resource="com/bg/mapper/UserMapper.xml"/>--></span></span><br><span class="line"> <span class="tag"></<span class="name">mappers</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">configuration</span>></span></span><br></pre></td></tr></tbody></table></figure><p><strong>注意</strong>:<strong>配置文件的配置参数是有优先级的,优先级顺序如下,如果不知道,可以通过IDEA报错提醒。</strong></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?</span><br></pre></td></tr></tbody></table></figure><blockquote><p>typeAliases</p></blockquote><p>类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--配置别名,注意顺序--></span></span><br><span class="line"><span class="tag"><<span class="name">typeAliases</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">typeAlias</span> <span class="attr">type</span>=<span class="string">"com.bg.pojo.User"</span> <span class="attr">alias</span>=<span class="string">"User"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">typeAliases</span>></span></span><br></pre></td></tr></tbody></table></figure><p>这样配置时,就可以使用 <strong>User</strong> 代替任何 <strong>com.bg.pojo.User</strong></p><p>也可以指定一个包名,MyBatis会在包名下面搜索需要的Java Bean,比如:</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">typeAliases</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">package</span> <span class="attr">name</span>=<span class="string">"com.bg.pojo"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">typeAliases</span>></span></span><br></pre></td></tr></tbody></table></figure><p>每一个在包 <strong>com.bg.pojo</strong> 中的Java Bean,在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名。。例如,user 作为 com.bg.pojo.User 的别名。</p><p>在实体类比较少的时候,<strong>使用第一种方式</strong>,这种可以diy别名</p><p>若实体类比较多,<strong>使用第二种方式</strong>,不可以diy,但是可以在实体类用注解名字,实现别民的diy</p><p>若有注解,则别名为其注解值,如下:<strong>注解写在实体类</strong></p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Alias("user")</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>{</span><br><span class="line"> ...</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><blockquote><p>设置(settings)</p></blockquote><p>这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。具体参数含义参考官方文档。</p><p>一个配置完整的 settings 元素的示例如下:</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">settings</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"cacheEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"lazyLoadingEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"multipleResultSetsEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"useColumnLabel"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"useGeneratedKeys"</span> <span class="attr">value</span>=<span class="string">"false"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"autoMappingBehavior"</span> <span class="attr">value</span>=<span class="string">"PARTIAL"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"autoMappingUnknownColumnBehavior"</span> <span class="attr">value</span>=<span class="string">"WARNING"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"defaultExecutorType"</span> <span class="attr">value</span>=<span class="string">"SIMPLE"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"defaultStatementTimeout"</span> <span class="attr">value</span>=<span class="string">"25"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"defaultFetchSize"</span> <span class="attr">value</span>=<span class="string">"100"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"safeRowBoundsEnabled"</span> <span class="attr">value</span>=<span class="string">"false"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"mapUnderscoreToCamelCase"</span> <span class="attr">value</span>=<span class="string">"false"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"localCacheScope"</span> <span class="attr">value</span>=<span class="string">"SESSION"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"jdbcTypeForNull"</span> <span class="attr">value</span>=<span class="string">"OTHER"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"lazyLoadTriggerMethods"</span> <span class="attr">value</span>=<span class="string">"equals,clone,hashCode,toString"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">settings</span>></span></span><br></pre></td></tr></tbody></table></figure><blockquote><p>类型处理器(typeHandlers)</p></blockquote><p>MyBatis 在设置预处理语句(PreparedStatement)中的参数或从结果集中取出一个值时, 都会用类型处理器将获取到的值以合适的方式转换成 Java 类型。</p><blockquote><p>对象工厂(objectFactory)</p></blockquote><p>每次 MyBatis 创建结果对象的新实例时,它都会使用一个对象工厂(ObjectFactory)实例来完成实例化工作。 默认的对象工厂需要做的仅仅是实例化目标类,要么通过默认无参构造方法,要么通过存在的参数映射来调用带有参数的构造方法。</p><blockquote><p>生命周期和作用域</p></blockquote><p>理解我们之前讨论过的不同作用域和生命周期类别是至关重要的,因为错误的使用会导致非常严重的并发问题。</p><p>首先通过流程图,分析下MyBatis的执行过程:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201126230734563.png" alt="image-20201126230734563"></p><p>作用域理解:</p><ul><li><p>SqlSessionFactoryBuilder 的作用在于创建 SqlSessionFactory,创建成功后,SqlSessionFactoryBuilder 就失去了作用,所以它只能存在于创建 SqlSessionFactory 的方法中,而不要让其长期存在。因此 <strong>SqlSessionFactoryBuilder 实例的最佳作用域是方法作用域</strong>(也就是局部方法变量)。</p></li><li><p>SqlSessionFactory 可以被认为是一个数据库连接池,它的作用是创建 SqlSession 接口对象。因为 MyBatis 的本质就是 Java 对数据库的操作,所以 SqlSessionFactory 的生命周期存在于整个 MyBatis 的应用之中,所以一旦创建了 SqlSessionFactory,就要长期保存它,直至不再使用 MyBatis 应用,<strong>所以可以认为 SqlSessionFactory 的生命周期就等同于 MyBatis 的应用周期。</strong></p></li><li><p>由于 SqlSessionFactory 是一个对数据库的连接池,所以它占据着数据库的连接资源。如果创建多个 SqlSessionFactory,那么就存在多个数据库连接池,这样不利于对数据库资源的控制,也会导致数据库连接资源被消耗光,出现系统宕机等情况,所以尽量避免发生这样的情况。</p></li><li><p>因此在一般的应用中我们往往希望 SqlSessionFactory 作为一个单例,让它在应用中被共享。所以说 <strong>SqlSessionFactory 的最佳作用域是应用作用域。</strong></p></li><li><p>如果说 SqlSessionFactory 相当于数据库连接池,那么 SqlSession 就相当于一个数据库连接(Connection 对象),你可以在一个事务里面执行多条 SQL,然后通过它的 commit、rollback 等方法,提交或者回滚事务。所以它应该存活在一个业务请求中,处理完整个请求后,应该关闭这条连接,让它归还给 SqlSessionFactory,否则数据库资源就很快被耗费精光,系统就会瘫痪,所以用 try…catch…finally… 语句来保证其正确关闭。<strong>所以 SqlSession 的最佳的作用域是请求或方法作用域。</strong></p></li></ul><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201126231936863.png" alt="image-20201126231936863"></p><h2 id="5-ResultMap"><a href="#5-ResultMap" class="headerlink" title="5 ResultMap"></a>5 ResultMap</h2><p><strong>要解决的问题,属性名和数据库表格字段名不一致问题</strong></p><p>在项目中,新建一个模块mybatis-02,内容和mybatis-01一样。</p><p>1、查看数据库user表的字段名</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201127192335229.png" alt="image-20201127192335229"></p><p>2、修改Java中的实体类,使其属性名与数据库的字段名不一致</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line"> <span class="keyword">private</span> String username;</span><br><span class="line"> <span class="keyword">private</span> String password;</span><br><span class="line">......</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>3、UserMapper接口</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 根据Id查询用户</span></span><br><span class="line"><span class="function">User <span class="title">getUserById</span><span class="params">(<span class="meta">@Param("id")</span> <span class="keyword">int</span> userId)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>4、mapper映射文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserById"</span> <span class="attr">resultType</span>=<span class="string">"User"</span>></span></span><br><span class="line"> SELECT * FROM user WHERE id = #{id}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>5、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getUserById</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> User user = mapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user);</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>结果:</strong></p><ul><li>User{id=1, username=’null’, password=’null’}</li><li>查询出来的结果中username和password都为空</li></ul><p><strong>分析:</strong></p><ul><li><p>SELECT * FROM user WHERE id = #{id} 可以看做</p><p>SELECT id, name, pwd FROM user WHERE id = #{id}</p></li><li><p>mybatis会根据这些查询的列名(会将列名转化为小写,数据库不区分大小写) , 去对应的实体类中查找相应列名的set方法设值 , 由于找不到setPwd() 和setname(), 所以password返回null 。</p></li></ul><blockquote><p>解决方法</p></blockquote><p>方法一:为列名指定别名,别名和java实体类的属性名一致</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserById"</span> <span class="attr">resultType</span>=<span class="string">"User"</span>></span></span><br><span class="line"> SELECT id, name AS username, pwd AS password FROM user WHERE id = #{id}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>查询结果</p><ul><li>User{id=1, username=’BG’, password=’123456’}</li></ul><p>方法二:使用结果集映射(ResultMap)【推荐】</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"UserResult"</span> <span class="attr">type</span>=<span class="string">"User"</span>></span></span><br><span class="line"> <span class="comment"><!-- column是数据库表的列名 , property是对应实体类的属性名 --></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"name"</span> <span class="attr">property</span>=<span class="string">"username"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"pwd"</span> <span class="attr">property</span>=<span class="string">"password"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">resultMap</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserById"</span> <span class="attr">resultMap</span>=<span class="string">"UserResult"</span>></span></span><br><span class="line"> SELECT * FROM user WHERE id = #{id}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p><strong>注意:select标签的返回类型为resultMap</strong></p><blockquote><p>ResultMap</p></blockquote><p><strong>自动映射</strong></p><ul><li><p><strong>resultMap</strong> 元素是 MyBatis 中最重要最强大的元素。它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC 不支持的操作。</p></li><li><p>实际上,在为一些比如连接的复杂语句编写映射代码的时候,一份 <strong>resultMap</strong> 能够代替实现同等功能的数千行代码。</p></li><li><p><strong>ResultMap</strong> 的设计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。</p></li></ul><p>简单映射语句的示例,但并没有显式指定 resultMap。比如:</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"selectUserById"</span> <span class="attr">resultType</span>=<span class="string">"map"</span>></span></span><br><span class="line">select id , name , pwd</span><br><span class="line"> from user</span><br><span class="line"> where id = #{id}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>上述语句只是简单地将所有的列映射到 HashMap 的键上,这由 resultType 属性指定。虽然在大部分情况下都够用,但是 HashMap 不是一个很好的模型。你的程序更可能会使用 JavaBean 或 POJO(Plain Old Java Objects,普通老式 Java 对象)作为模型。</p><p><strong>手动映射</strong></p><p>1、设置返回值类型为resultMap</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"selectUserById"</span> <span class="attr">resultMap</span>=<span class="string">"UserResult"</span>></span></span><br><span class="line"> select id , name , pwd from user where id = #{id}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>2、编写resultMap,实现手动映射</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"UserResult"</span> <span class="attr">type</span>=<span class="string">"User"</span>></span></span><br><span class="line"> <span class="comment"><!-- id为主键 --></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/></span></span><br><span class="line"> <span class="comment"><!-- column是数据库表的列名 , property是对应实体类的属性名 --></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"name"</span> <span class="attr">property</span>=<span class="string">"username"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"pwd"</span> <span class="attr">property</span>=<span class="string">"password"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">resultMap</span>></span></span><br></pre></td></tr></tbody></table></figure><p>如果世界总是这么简单就好了。但是肯定不是的,数据库中,存在一对多,多对一的情况,我们之后会使用到一些高级的结果集映射,association,collection这些。</p><h2 id="6-日志工厂"><a href="#6-日志工厂" class="headerlink" title="6 日志工厂"></a>6 日志工厂</h2><p>思考:我们在测试SQL的时候,要是能够在控制台输出 SQL 的话,是不是就能够有更快的排错效率?</p><p>如果一个数据库相关的操作出现了问题,我们可以根据输出的SQL语句快速排查问题。</p><p>对于以往的开发过程,我们会经常使用到debug模式来调节,跟踪我们的代码执行过程。但是现在使用Mybatis是基于接口,配置文件的源代码执行过程。因此,我们必须选择日志工具来作为我们开发,调节程序的工具。</p><p>Mybatis内置的日志工厂提供日志功能,具体的日志实现有以下几种工具:</p><ul><li>SLF4J</li><li>Apache Commons Logging</li><li>Log4j 2</li><li>Log4j</li><li>JDK logging</li></ul><p>具体选择哪个日志实现工具由MyBatis的内置日志工厂确定。它会使用最先找到的(按上文列举的顺序查找)。如果一个都未找到,日志功能就会被禁用。</p><p><strong>标准日志实现</strong></p><p>指定 MyBatis 应该使用哪个日志记录实现。在Mybatis核心配置文件中添加如下配置:</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">settings</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"logImpl"</span> <span class="attr">value</span>=<span class="string">"STDOUT_LOGGING"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">settings</span>></span></span><br></pre></td></tr></tbody></table></figure><p>然后,测试,可以看到控制台有大量日志输出!</p><blockquote><p>Log4j</p></blockquote><p><strong>简介:</strong></p><ul><li>Log4j是Apache的一个开源项目</li><li>通过使用Log4j,我们可以控制日志信息输送的目的地:控制台,文本,GUI组件….</li><li>我们也可以控制每一条日志的输出格式;</li><li>通过定义每一条日志信息的级别,我们能够更加细致地控制日志的生成过程。最令人感兴趣的就是,这些可以通过一个配置文件来灵活地进行配置,而不需要修改应用的代码。</li></ul><p><strong>使用步骤:</strong></p><p>1、导入log4j的jar包,在父工程的pom.xml或者当前子模块的pom.xml添加</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--日志--></span></span><br><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>log4j<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>log4j<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>1.2.17<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></tbody></table></figure><p>2、编写配置文件</p><p>配置文件格式基本固定,百度也有很多资料可以参考,我们仅需找到一份适合自己的保存起来。</p><p>配置文件放在resources目录下,log4j.properties</p><figure class="highlight properties"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码</span></span><br><span class="line"><span class="meta">log4j.rootLogger</span>=<span class="string">DEBUG,console,file</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#控制台输出的相关设置</span></span><br><span class="line"><span class="meta">log4j.appender.console</span> = <span class="string">org.apache.log4j.ConsoleAppender</span></span><br><span class="line"><span class="meta">log4j.appender.console.Target</span> = <span class="string">System.out</span></span><br><span class="line"><span class="meta">log4j.appender.console.Threshold</span>=<span class="string">DEBUG</span></span><br><span class="line"><span class="meta">log4j.appender.console.layout</span> = <span class="string">org.apache.log4j.PatternLayout</span></span><br><span class="line"><span class="meta">log4j.appender.console.layout.ConversionPattern</span>=<span class="string">[%c]-%m%n</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#文件输出的相关设置</span></span><br><span class="line"><span class="meta">log4j.appender.file</span> = <span class="string">org.apache.log4j.RollingFileAppender</span></span><br><span class="line"><span class="meta">log4j.appender.file.File</span>=<span class="string">./log/bg.log</span></span><br><span class="line"><span class="meta">log4j.appender.file.MaxFileSize</span>=<span class="string">10mb</span></span><br><span class="line"><span class="meta">log4j.appender.file.Threshold</span>=<span class="string">DEBUG</span></span><br><span class="line"><span class="meta">log4j.appender.file.layout</span>=<span class="string">org.apache.log4j.PatternLayout</span></span><br><span class="line"><span class="meta">log4j.appender.file.layout.ConversionPattern</span>=<span class="string">[%p][%d{yy-MM-dd}][%c]%m%n</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#日志输出级别</span></span><br><span class="line"><span class="meta">log4j.logger.org.mybatis</span>=<span class="string">DEBUG</span></span><br><span class="line"><span class="meta">log4j.logger.java.sql</span>=<span class="string">DEBUG</span></span><br><span class="line"><span class="meta">log4j.logger.java.sql.Statement</span>=<span class="string">DEBUG</span></span><br><span class="line"><span class="meta">log4j.logger.java.sql.ResultSet</span>=<span class="string">DEBUG</span></span><br><span class="line"><span class="meta">log4j.logger.java.sql.PreparedStatement</span>=<span class="string">DEBUG</span></span><br></pre></td></tr></tbody></table></figure><p>3、setting设置日志实现</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--Log4j日志--></span></span><br><span class="line"><span class="tag"><<span class="name">settings</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"logImpl"</span> <span class="attr">value</span>=<span class="string">"LOG4J"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">settings</span>></span></span><br></pre></td></tr></tbody></table></figure><p>4、在程序中使用Log4j进行日志输出!</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.bg.mapper.UserMapper;</span><br><span class="line"><span class="keyword">import</span> com.bg.pojo.User;</span><br><span class="line"><span class="keyword">import</span> com.bg.tools.MyBatisTool;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.session.SqlSession;</span><br><span class="line"><span class="keyword">import</span> org.apache.log4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyTest</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">static</span> Logger logger = Logger.getLogger(MyTest.class);</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getUserById</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> logger.info(<span class="string">"info:进入selectUser方法"</span>);</span><br><span class="line"> logger.debug(<span class="string">"debug:进入selectUser方法"</span>);</span><br><span class="line"> logger.error(<span class="string">"error: 进入selectUser方法"</span>);</span><br><span class="line"></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> User user = mapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user);</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>5、测试,看控制台输出</p><ul><li>控制台输出日志</li><li>还生成了一个日志文件夹</li></ul><h2 id="7-LIMIT分页"><a href="#7-LIMIT分页" class="headerlink" title="7 LIMIT分页"></a>7 LIMIT分页</h2><p><strong>为什么需要分页?</strong></p><p>在对数据库进行大量数据查询时,往往需要使用分页进行查询,每次处理小部分数据,这样对数据库的压力在可控范围内。</p><p>使用LIMIT分页的常用语法</p><figure class="highlight sql"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">#数据库索引是从<span class="number">0</span>开始</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> <span class="keyword">table</span> LIMIT stratIndex,pageSize</span><br><span class="line"></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> <span class="keyword">table</span> LIMIT <span class="number">5</span>,<span class="number">10</span>; <span class="operator">/</span><span class="operator">/</span> 检索记录行 <span class="number">6</span><span class="number">-15</span> </span><br><span class="line"></span><br><span class="line">#为了检索从某一个偏移量到记录集的结束所有的记录行,可以指定第二个参数为 <span class="number">-1</span>,在最小的mysql版本中,该语法可以使用</span><br><span class="line">#在新的mysql版本 <span class="number">-1</span>参数已经不允许使用了</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> <span class="keyword">table</span> LIMIT <span class="number">95</span>,<span class="number">-1</span>; <span class="operator">/</span><span class="operator">/</span> 检索记录行 <span class="number">96</span><span class="operator">-</span>last. </span><br><span class="line"></span><br><span class="line">#如果只给定一个参数,它表示返回最大的记录行数目: </span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> <span class="keyword">table</span> LIMIT <span class="number">5</span>; <span class="operator">/</span><span class="operator">/</span>检索前 <span class="number">5</span> 个记录行 </span><br><span class="line"></span><br><span class="line">#换句话说,LIMIT n 等价于 LIMIT <span class="number">0</span>,n。 </span><br></pre></td></tr></tbody></table></figure><p><strong>使用LIMIT进行分页:</strong></p><p><strong>步骤:</strong></p><p>1、在UserMapper.java接口中,编写函数</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 分页查询</span></span><br><span class="line"><span class="function">List<User> <span class="title">getUserByLimit</span><span class="params">(Map map)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、编写UserMapper.xml文件,传入参数类型为map</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"UserResult"</span> <span class="attr">type</span>=<span class="string">"User"</span>></span></span><br><span class="line"> <span class="comment"><!-- column是数据库表的列名 , property是对应实体类的属性名 --></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"name"</span> <span class="attr">property</span>=<span class="string">"username"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"pwd"</span> <span class="attr">property</span>=<span class="string">"password"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">resultMap</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserByLimit"</span> <span class="attr">parameterType</span>=<span class="string">"map"</span> <span class="attr">resultMap</span>=<span class="string">"UserResult"</span>></span></span><br><span class="line"> SELECT * FROM user LIMIT #{startIndex}, #{pageSize}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getUserByLimit</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="comment">// 分页,查询第一页,页面大小为2</span></span><br><span class="line"> <span class="keyword">int</span> pageIndex = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> pageSize = <span class="number">2</span>;</span><br><span class="line"> <span class="comment">// 用map准备参数</span></span><br><span class="line"> Map<String, Integer> map = <span class="keyword">new</span> HashMap<String, Integer>();</span><br><span class="line"> <span class="comment">//计算起始索引</span></span><br><span class="line"> <span class="keyword">int</span> startIndex = (pageIndex - <span class="number">1</span>) * pageSize;</span><br><span class="line"> map.put(<span class="string">"startIndex"</span>, startIndex);</span><br><span class="line"> map.put(<span class="string">"pageSize"</span>, pageSize);</span><br><span class="line"> <span class="comment">// 执行查询</span></span><br><span class="line"> List<User> userList = mapper.getUserByLimit(map);</span><br><span class="line"> <span class="keyword">for</span> (User user : userList) {</span><br><span class="line"> System.out.println(user);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>使用RowBounds分页:</strong>【了解】</p><p>我们除了使用Limit在SQL层面实现分页,也可以使用RowBounds在Java代码层面实现分页。</p><p>1、在UserMapper.java接口中,编写函数</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// RowBounds分页</span></span><br><span class="line"><span class="function">List<User> <span class="title">getUserListByRowBounds</span><span class="params">()</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、编写UserMapper.xml文件,传入参数类型为map</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"UserResult"</span> <span class="attr">type</span>=<span class="string">"User"</span>></span></span><br><span class="line"> <span class="comment"><!-- column是数据库表的列名 , property是对应实体类的属性名 --></span></span><br><span class="line"> <span class="tag"><<span class="name">id</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">property</span>=<span class="string">"id"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"name"</span> <span class="attr">property</span>=<span class="string">"username"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">column</span>=<span class="string">"pwd"</span> <span class="attr">property</span>=<span class="string">"password"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">resultMap</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserListByRowBounds"</span> <span class="attr">resultMap</span>=<span class="string">"UserResult"</span>></span></span><br><span class="line"> SELECT * FROM user</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getUserListByRowBounds</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> <span class="keyword">int</span> pageIndex = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> pageSize = <span class="number">2</span>;</span><br><span class="line"> RowBounds rowBounds = <span class="keyword">new</span> RowBounds((pageIndex - <span class="number">1</span>) * pageSize, pageSize);</span><br><span class="line"> List<User> userList = sqlSession.selectList(<span class="string">"com.bg.mapper.UserMapper.getUserListByRowBounds"</span>, <span class="keyword">null</span>, rowBounds);</span><br><span class="line"> <span class="keyword">for</span> (User user : userList) {</span><br><span class="line"> System.out.println(user);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>使用插件进行分页:</strong></p><p><strong>PageHelper</strong>自行了解。</p><h2 id="8-使用注解开发"><a href="#8-使用注解开发" class="headerlink" title="8 使用注解开发"></a>8 使用注解开发</h2><blockquote><p>面向接口编程</p></blockquote><ul><li>在面向对象编程中,通常我们都是面向接口编程</li><li><strong>根本原因:解耦,可扩展,提高复用,分层开发,上层写接口,不用管具体的实现,大家遵循共同的标准,使得开发变得容易,规范性更好。</strong></li><li>在一个面向对象的系统中,系统的各个功能模块是由许许多多的不同对象协作完成,在这种情况下,各个对象内部如何实现自己的,对系统设计人员来讲就不那么重要了。</li><li>而各个对象之间的协作关系则成为系统设计的关键。小到不同类之间的通信,大到各模块之间的交互,在系统设计之初都是要着重考虑的,这也是系统设计的主要工作内容。面向接口编程就是指按照这种思想来编程。</li></ul><p><strong>关于接口的理解</strong></p><ul><li><p>接口从更深层次的理解,应是定义(规范、约束)与实现的分离。</p></li><li><p>接口的本身反映了系统设计人员对系统的抽象理解。</p></li><li><p>接口应有两类:</p><ul><li>第一类是对一个个体的抽象,它可对应为一个抽象体(abstract class);</li><li>第二类是对一个个体某一方面的抽象,即形成一个抽象面(interface);</li></ul></li><li><p>一个个体可能由多个抽象面。抽象体与抽象面是有区别的。</p></li></ul><p><strong>三个面向区别</strong></p><ul><li>面向对象是指,我们考虑问题时,以对象为单位,考虑对象的属性及方法。</li><li>面向过程是指,我们考虑问题时,以一个具体的流程为单位,考虑它的实现</li><li>接口设计与非接口设计是针对复用技术而言的,与面向对象(过程)不是一个问题,更多的体现就是对系统整体的架构。</li></ul><blockquote><p>利用注解开发</p></blockquote><ul><li><p><strong>mybatis最初配置信息是基于 XML ,映射语句(SQL)也是定义在 XML 中的。而到MyBatis 3提供了新的基于注解的配置。不幸的是,Java 注解的的表达力和灵活性十分有限。最强大的 MyBatis 映射并不能用注解来构建</strong></p></li><li><p>sql 类型主要有 :</p></li><li><ul><li>@select ()</li><li>@update ()</li><li>@Insert ()</li><li>@delete ()</li></ul></li></ul><p><strong>注意:</strong>利用注解开发就不需要UserMapper.xml映射文件了 。</p><p>依然新建一个模块mybatis-03来学习这方面的知识,内容拷贝mybatis-01的即可。</p><p>1、UserMapper.java接口中添加注解</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 查询表格中的所有用户</span></span><br><span class="line"><span class="meta">@Select("SELECT * FROM user")</span></span><br><span class="line"><span class="function">List<User> <span class="title">getUserList</span><span class="params">()</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、在mybatis核心配置文件中注册mapper</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.bg.mapper.UserMapper"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">mappers</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getUserList</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> List<User> userList = mapper.getUserList();</span><br><span class="line"> <span class="keyword">for</span> (User user : userList) {</span><br><span class="line"> System.out.println(user.toString());</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>详解Mybatis执行流程:</strong></p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/mybatis.png" alt="mybatis"></p><blockquote><p>注解实现增删改查</p></blockquote><p>首先需要改造MyBatisTool工具类中的getSession()方法,开始事务自动提交。</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取SqlSession实例,用来操作数据库,默认开启事务自动提交</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> SqlSession <span class="title">getSqlSession</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> sqlSessionFactory.openSession(<span class="keyword">true</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> SqlSession <span class="title">getSqlSession</span><span class="params">(<span class="keyword">boolean</span> flag)</span></span>{</span><br><span class="line"> <span class="keyword">return</span> sqlSessionFactory.openSession(flag);</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>根据ID查询用户:</strong></p><p>1、UserMapper.java接口中添加注解</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 根据Id查询用户</span></span><br><span class="line"><span class="meta">@Select("SELECT * FROM user WHERE id = #{id}")</span></span><br><span class="line"><span class="function">User <span class="title">getUserById</span><span class="params">(<span class="meta">@Param("id")</span> <span class="keyword">int</span> userId)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getUserById</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> User user = mapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user.toString());</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>增加一个用户:</strong></p><p>1、UserMapper.java接口中添加注解</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 增加一个用户</span></span><br><span class="line"><span class="meta">@Insert("INSERT INTO user(id, name, pwd) VALUES(#{id}, #{name}, #{pwd})")</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">addUser</span><span class="params">(User user)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addUser</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> User user = <span class="keyword">new</span> User(<span class="number">2</span>, <span class="string">"马保国"</span>,<span class="string">"WuDe"</span>);</span><br><span class="line"> <span class="keyword">int</span> i = mapper.addUser(user);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"插入用户成功"</span>);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"插入用户失败"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>修改一个用户:</strong></p><p>1、UserMapper.java接口中添加注解</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 修改一个用户</span></span><br><span class="line"><span class="meta">@Update("UPDATE user SET name = #{name}, pwd = #{pwd} WHERE id = #{id}")</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">updateUser</span><span class="params">(User user)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">UpdateUser</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> User user = <span class="keyword">new</span> User(<span class="number">3</span>, <span class="string">"张三"</span>,<span class="string">"526553"</span>);</span><br><span class="line"> <span class="keyword">int</span> i = mapper.updateUser(user);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"更新用户成功"</span>);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"更新用户失败"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>删除一个用户:</strong></p><p>1、UserMapper.java接口中添加注解</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 删除一个用户</span></span><br><span class="line"><span class="meta">@Delete("DELETE FROM user WHERE id = #{id}")</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">deleteUser</span><span class="params">(<span class="meta">@Param("id")</span> <span class="keyword">int</span> userId)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">deleteUser</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> SqlSession sqlSession = MyBatisTool.getSqlSession();</span><br><span class="line"> UserMapper mapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> <span class="keyword">int</span> i = mapper.deleteUser(<span class="number">5</span>);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"删除用户成功"</span>);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"删除用户失败"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><blockquote><p>关于@Param</p></blockquote><p>@Param注解用于给方法参数起一个名字。以下是总结的使用原则:</p><ul><li>在方法只接受一个参数的情况下,可以不使用@Param。</li><li>在方法接受多个参数的情况下,建议一定要使用@Param注解给参数命名。</li><li>如果参数是 JavaBean , 则不能使用@Param。</li></ul><blockquote><p>#与$的区别</p></blockquote><ul><li><p>#{} 的作用主要是替换预编译语句(PrepareStatement)中的占位符? 【推荐使用】</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">INSERT INTO user (name) VALUES (#{name});</span><br><span class="line">INSERT INTO user (name) VALUES (?);</span><br></pre></td></tr></tbody></table></figure></li><li><p>${} 的作用是直接进行字符串替换</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">INSERT INTO user (name) VALUES ('${name}');</span><br><span class="line">INSERT INTO user (name) VALUES ('kuangshen');</span><br></pre></td></tr></tbody></table></figure></li></ul><blockquote><p>使用注解和配置文件协同开发,才是MyBatis的最佳实践!</p></blockquote><h2 id="9-一对多和多对一处理"><a href="#9-一对多和多对一处理" class="headerlink" title="9 一对多和多对一处理"></a>9 一对多和多对一处理</h2><blockquote><p>多对一处理</p></blockquote><p>多个学生对应一个老师</p><blockquote><p>数据库设计</p></blockquote><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210170844181.png" alt="image-20201210170844181"></p><figure class="highlight sql"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `teacher` (</span><br><span class="line">`id` <span class="type">INT</span>(<span class="number">10</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">`name` <span class="type">VARCHAR</span>(<span class="number">30</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"><span class="keyword">PRIMARY</span> KEY (`id`)</span><br><span class="line">) ENGINE<span class="operator">=</span>INNODB <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> teacher(`id`, `name`) <span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'曾老师'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `student` (</span><br><span class="line">`id` <span class="type">INT</span>(<span class="number">10</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">`name` <span class="type">VARCHAR</span>(<span class="number">30</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line">`tid` <span class="type">INT</span>(<span class="number">10</span>) <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"><span class="keyword">PRIMARY</span> KEY (`id`),</span><br><span class="line">KEY `fktid` (`tid`),</span><br><span class="line"><span class="keyword">CONSTRAINT</span> `fktid` <span class="keyword">FOREIGN</span> KEY (`tid`) <span class="keyword">REFERENCES</span> `teacher` (`id`)</span><br><span class="line">) ENGINE<span class="operator">=</span>INNODB <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student` (`id`, `name`, `tid`) <span class="keyword">VALUES</span> (<span class="string">'1'</span>, <span class="string">'小明'</span>, <span class="string">'1'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student` (`id`, `name`, `tid`) <span class="keyword">VALUES</span> (<span class="string">'2'</span>, <span class="string">'小红'</span>, <span class="string">'1'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student` (`id`, `name`, `tid`) <span class="keyword">VALUES</span> (<span class="string">'3'</span>, <span class="string">'小张'</span>, <span class="string">'1'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student` (`id`, `name`, `tid`) <span class="keyword">VALUES</span> (<span class="string">'4'</span>, <span class="string">'小李'</span>, <span class="string">'1'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student` (`id`, `name`, `tid`) <span class="keyword">VALUES</span> (<span class="string">'5'</span>, <span class="string">'小王'</span>, <span class="string">'1'</span>);</span><br></pre></td></tr></tbody></table></figure><blockquote><p>搭建测试环境</p></blockquote><p>1、编写实体类</p><p>Student.java</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"> <span class="keyword">private</span> Teacher teacher;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">()</span> </span>{</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">(<span class="keyword">int</span> id, String name, Teacher teacher)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.id = id;</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> <span class="keyword">this</span>.teacher = teacher;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getId</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(<span class="keyword">int</span> id)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.id = id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Teacher <span class="title">getTeacher</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> teacher;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTeacher</span><span class="params">(Teacher teacher)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.teacher = teacher;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"Student{"</span> +</span><br><span class="line"> <span class="string">"id="</span> + id +</span><br><span class="line"> <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", teacher="</span> + teacher +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>Teacher.java</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Teacher</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Teacher</span><span class="params">()</span> </span>{</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Teacher</span><span class="params">(<span class="keyword">int</span> id, String name)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.id = id;</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getId</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(<span class="keyword">int</span> id)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.id = id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.name = name;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"Teacher{"</span> +</span><br><span class="line"> <span class="string">"id="</span> + id +</span><br><span class="line"> <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>2、编写实体类对应的Mapper接口</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.dao;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">StudentMapper</span> </span>{</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.dao;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">TeacherMapper</span> </span>{</span><br><span class="line"> </span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>3、编写接口对应的mapper.xml配置文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Mapper 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.zzb.dao.StudentMapper"</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></tbody></table></figure><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Mapper 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.zzb.dao.TeacherMapper"</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></tbody></table></figure><blockquote><p>按查询嵌套处理</p></blockquote><p>1、给 StudentMapper 接口增加方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取学生列表及对应老师的信息</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> List<Student> <span class="title">getStudents</span><span class="params">()</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、编写对应的Mapper配置文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.zzb.dao.StudentMapper"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!--按查询嵌套--></span></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getStudents"</span> <span class="attr">resultMap</span>=<span class="string">"StudentTeacher"</span>></span></span><br><span class="line"> select * from student;</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"StudentTeacher"</span> <span class="attr">type</span>=<span class="string">"Student"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"id"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"name"</span> <span class="attr">column</span>=<span class="string">"name"</span>/></span></span><br><span class="line"> <span class="comment"><!--association关联属性 property属性名 javaType属性类型 column在多的一方的表中的列名--></span></span><br><span class="line"> <span class="tag"><<span class="name">association</span> <span class="attr">property</span>=<span class="string">"teacher"</span> <span class="attr">column</span>=<span class="string">"tid"</span> <span class="attr">javaType</span>=<span class="string">"Teacher"</span> <span class="attr">select</span> = <span class="string">"getTeacher"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">resultMap</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getTeacher"</span> <span class="attr">resultType</span>=<span class="string">"Teacher"</span>></span></span><br><span class="line"> select * from teacher where id = #{tid}</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、MyBatis配置文件中注册Mapper</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--绑定接口--></span></span><br><span class="line"><span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.zzb.dao.StudentMapper"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">mappers</span>></span></span><br></pre></td></tr></tbody></table></figure><p><strong>注意:</strong></p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"StudentTeacher"</span> <span class="attr">type</span>=<span class="string">"Student"</span>></span></span><br><span class="line"> <span class="comment"><!--association关联属性 property属性名 javaType属性类型 column在多的一方的表中的列名--></span></span><br><span class="line"> <span class="tag"><<span class="name">association</span> <span class="attr">property</span>=<span class="string">"teacher"</span> <span class="attr">column</span>=<span class="string">"{id = tid, name = name}"</span> <span class="attr">javaType</span>=<span class="string">"Teacher"</span> <span class="attr">select</span>=<span class="string">"getTeacher"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">resultMap</span>></span></span><br><span class="line"><span class="comment"><!--</span></span><br><span class="line"><span class="comment">这里传递过来的id,只有一个属性的时候,下面可以写任何值</span></span><br><span class="line"><span class="comment">association中column多参数配置:</span></span><br><span class="line"><span class="comment"> column="{key=value,key=value}"</span></span><br><span class="line"><span class="comment"> 其实就是键值对的形式,key是传给下个sql的取值名称,value是sql查询的字段名。</span></span><br><span class="line"><span class="comment">--></span></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getTeacher"</span> <span class="attr">resultType</span>=<span class="string">"teacher"</span>></span></span><br><span class="line"> select * from teacher where id = #{id} and name = #{name}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>4、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getStudent</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"> StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);</span><br><span class="line"> List<Student> studentList = studentMapper.getStudents();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span>(Student student : studentList){</span><br><span class="line"> System.out.println(student);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Student{id=1, name='小明', teacher=Teacher{id=1, name='曾老师'}}</span><br><span class="line">Student{id=2, name='小红', teacher=Teacher{id=1, name='曾老师'}}</span><br><span class="line">Student{id=3, name='小张', teacher=Teacher{id=1, name='曾老师'}}</span><br><span class="line">Student{id=4, name='小李', teacher=Teacher{id=1, name='曾老师'}}</span><br><span class="line">Student{id=5, name='小王', teacher=Teacher{id=1, name='曾老师'}}</span><br></pre></td></tr></tbody></table></figure><blockquote><p>按结果嵌套查询</p></blockquote><p>1、StudentMapper接口方法编写</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> List<Student> <span class="title">getStudents2</span><span class="params">()</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、编写对应的StudentMapper.xml配置文件</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><!--按结果嵌套--></span><br><span class="line"><select id="getStudents2" resultMap="StudentTeacher2"></span><br><span class="line">SELECT S.id AS sid, s.name AS sname, t.id AS tid, t.name AS tname</span><br><span class="line">FROM student AS s, teacher AS t</span><br><span class="line">WHERE s.tid = t.id;</span><br><span class="line"></select></span><br><span class="line"></span><br><span class="line"><resultMap id="StudentTeacher2" type="Student"></span><br><span class="line"> <result property="id" column="sid"/></span><br><span class="line"> <result property="name" column="sname"/></span><br><span class="line"> <association property="teacher" javaType="Teacher"></span><br><span class="line"> <result property="id" column="tid"/></span><br><span class="line"> <result property="name" column="tname"/></span><br><span class="line"> </association></span><br><span class="line"></span><br><span class="line"></resultMap></span><br></pre></td></tr></tbody></table></figure><p>3、MyBatis配置文件中注册Mapper</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.zzb.dao.StudentMapper"</span>/></span></span><br></pre></td></tr></tbody></table></figure><p>4、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getStudent2</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"> StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);</span><br><span class="line"> List<Student> studentList = studentMapper.getStudents2();</span><br><span class="line"> <span class="keyword">for</span>(Student student : studentList){</span><br><span class="line"> System.out.println(student);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Student{id=1, name='小明', teacher=Teacher{id=1, name='曾老师'}}</span><br><span class="line">Student{id=2, name='小红', teacher=Teacher{id=1, name='曾老师'}}</span><br><span class="line">Student{id=3, name='小张', teacher=Teacher{id=1, name='曾老师'}}</span><br><span class="line">Student{id=4, name='小李', teacher=Teacher{id=1, name='曾老师'}}</span><br><span class="line">Student{id=5, name='小王', teacher=Teacher{id=1, name='曾老师'}}</span><br></pre></td></tr></tbody></table></figure><p><strong>总结:</strong></p><p>按照查询进行嵌套处理就像SQL中的子查询</p><p>按照结果进行嵌套处理就像SQL中的联表查询</p><blockquote><p>一对多处理</p></blockquote><p>module:mybatis-06</p><p>一个老师拥有多个学生</p><p>1、实体类编写</p><p>Student.java</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.pojo;</span><br><span class="line"><span class="comment">// pom.xml中引入lombok包,简化实体类的编写</span></span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> tid;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>Teacher.java</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Teacher</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> List<Student> students;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>和上述多对一一样,搭建测试环境!</p><blockquote><p>按结果嵌套查询</p></blockquote><p>1、TeacherMapper接口编写方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.dao;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.zzb.pojo.Teacher;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.annotations.Param;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">TeacherMapper</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 获取指定老师下的所有学生</span></span><br><span class="line"> <span class="function">Teacher <span class="title">getTeacher2</span><span class="params">(<span class="meta">@Param("tid")</span> <span class="keyword">int</span> id)</span></span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>2、编写接口对应的Mapper配置文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span><br><span class="line"><span class="comment"><!--configuration--></span></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.zzb.dao.TeacherMapper"</span>></span></span><br><span class="line"> <span class="comment"><!--按结果嵌套--></span></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getTeacher2"</span> <span class="attr">resultMap</span>=<span class="string">"TeacherStudent2"</span>></span></span><br><span class="line"> SELECT S.id AS sid, s.name AS sname, t.id AS tid, t.name AS tname</span><br><span class="line"> FROM student AS s, teacher AS t</span><br><span class="line"> WHERE s.tid = t.id;</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"TeacherStudent2"</span> <span class="attr">type</span>=<span class="string">"Teacher"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"tid"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"name"</span> <span class="attr">column</span>=<span class="string">"tname"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"students"</span> <span class="attr">ofType</span>=<span class="string">"Student"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"sid"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"name"</span> <span class="attr">column</span>=<span class="string">"sname"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"tid"</span> <span class="attr">column</span>=<span class="string">"tid"</span>/></span></span><br><span class="line"> <span class="tag"></<span class="name">collection</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">resultMap</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、mybatis配置文件中注册mapper</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--绑定接口--></span></span><br><span class="line"><span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.zzb.dao.TeacherMapper"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">mappers</span>></span></span><br></pre></td></tr></tbody></table></figure><p>4、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getTeacher2</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"> TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);</span><br><span class="line"> Teacher teacher = mapper.getTeacher2(<span class="number">1</span>);</span><br><span class="line"> System.out.println(teacher);</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Teacher(id=1, name=曾老师, students=[Student(id=1, name=小明, tid=1), Student(id=2, name=小红, tid=1), Student(id=3, name=小张, tid=1), Student(id=4, name=小李, tid=1), Student(id=5, name=小王, tid=1)])</span><br></pre></td></tr></tbody></table></figure><blockquote><p>按查询嵌套</p></blockquote><p>1、TeacherMapper接口编写方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 获取指定老师下的所有学生</span></span><br><span class="line"><span class="function">Teacher <span class="title">getTeacher</span><span class="params">(<span class="meta">@Param("tid")</span> <span class="keyword">int</span> id)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、编写接口对应的Mapper配置文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--按查询嵌套--></span></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getTeacher"</span> <span class="attr">resultMap</span>=<span class="string">"TeacherStudent"</span>></span></span><br><span class="line"> select * from teacher</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">resultMap</span> <span class="attr">id</span>=<span class="string">"TeacherStudent"</span> <span class="attr">type</span>=<span class="string">"teacher"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"id"</span> <span class="attr">column</span>=<span class="string">"id"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">result</span> <span class="attr">property</span>=<span class="string">"name"</span> <span class="attr">column</span>=<span class="string">"name"</span>/></span></span><br><span class="line"> javatype是属性的类型 oftype是泛型的类型</span><br><span class="line"> <span class="tag"><<span class="name">collection</span> <span class="attr">property</span>=<span class="string">"students"</span> <span class="attr">column</span>=<span class="string">"id"</span> <span class="attr">javaType</span>=<span class="string">"ArrayList"</span> <span class="attr">ofType</span>=<span class="string">"Student"</span> <span class="attr">select</span>=<span class="string">"getStudent"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">resultMap</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getStudent"</span> <span class="attr">resultType</span>=<span class="string">"Student"</span>></span></span><br><span class="line"> select * from student where tid = #{id}</span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、mybatis配置文件中注册mapper</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!--绑定接口--></span></span><br><span class="line"><span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mapper</span> <span class="attr">class</span>=<span class="string">"com.zzb.dao.TeacherMapper"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">mappers</span>></span></span><br></pre></td></tr></tbody></table></figure><p>4、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getTeacher</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"></span><br><span class="line"> TeacherMapper teacherMapper = sqlSession.getMapper(TeacherMapper.class);</span><br><span class="line"> Teacher teacher = teacherMapper.getTeacher(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"> System.out.println(teacher);</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Teacher(id=1, name=曾老师, students=[Student(id=1, name=小明, tid=1), Student(id=2, name=小红, tid=1), Student(id=3, name=小张, tid=1), Student(id=4, name=小李, tid=1), Student(id=5, name=小王, tid=1)])</span><br></pre></td></tr></tbody></table></figure><p><strong>总结:</strong></p><p>1、关联-association</p><p>2、集合-collection</p><p>3、所以association是用于一对一和多对一,而collection是用于一对多的关系</p><p>4、JavaType和ofType都是用来指定对象类型的</p><ul><li>JavaType是用来指定pojo中属性的类型</li><li>ofType指定的是映射到List集合属性中pojo的类型。</li></ul><p><strong>注意说明:</strong></p><p>1、保证SQL的可读性,尽量通俗易懂</p><p>2、根据实际要求,尽量编写性能更高的SQL语句</p><p>3、注意属性名和字段不一致的问题</p><p>4、注意一对多和多对一 中:字段和属性对应的问题</p><p>5、尽量使用Log4j,通过日志来查看自己的错误</p><h2 id="10-动态SQL"><a href="#10-动态SQL" class="headerlink" title="10 动态SQL"></a>10 动态SQL</h2><blockquote><p>介绍</p></blockquote><p>==什么是动态SQL?==</p><p><strong>动态SQL指的是根据不同的查询条件 , 生成不同的Sql语句。</strong></p><blockquote><p>官网描述:<br>MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。<br>虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。<br>动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。</p><hr><ul><li>if</li><li>choose (when, otherwise)</li><li>trim (where, set)</li><li>foreach</li></ul><hr></blockquote><p>我们之前写的 SQL 语句都比较简单,如果有比较复杂的业务,我们需要写复杂的 SQL 语句,往往需要拼接,而拼接 SQL ,稍微不注意,由于引号,空格等缺失可能都会导致错误。</p><p>那么怎么去解决这个问题呢?这就要使用 mybatis 动态SQL,通过 if, choose, when, otherwise, trim, where, set, foreach等标签,可组合成非常灵活的SQL语句,从而在提高 SQL 语句的准确性的同时,也大大提高了开发人员的效率。</p><blockquote><p>搭建环境</p></blockquote><p>在mybatis数据库中新建一个表:blog</p><figure class="highlight sql"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `blog` (</span><br><span class="line">`id` <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">'博客id'</span>,</span><br><span class="line">`title` <span class="type">varchar</span>(<span class="number">100</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">'博客标题'</span>,</span><br><span class="line">`author` <span class="type">varchar</span>(<span class="number">30</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">'博客作者'</span>,</span><br><span class="line">`create_time` datetime <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">'创建时间'</span>,</span><br><span class="line">`views` <span class="type">int</span>(<span class="number">30</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">'浏览量'</span></span><br><span class="line">) ENGINE<span class="operator">=</span>InnoDB <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8</span><br></pre></td></tr></tbody></table></figure><p>1、创建Mybatis基础工程,module:mybatis-07</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210202134351.png" alt="image-20201210202134351"></p><p>2、IDUtil 工具类</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.utils;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.UUID;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">IDUtil</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> String <span class="title">getId</span><span class="params">()</span></span>{</span><br><span class="line"> <span class="keyword">return</span> UUID.randomUUID().toString().replaceAll(<span class="string">"-"</span>,<span class="string">""</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>3、编写实体类Blog</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Blog</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> String id;</span><br><span class="line"> <span class="keyword">private</span> String title;</span><br><span class="line"> <span class="keyword">private</span> String author;</span><br><span class="line"> <span class="keyword">private</span> Date createTime;</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> views;</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Blog</span><span class="params">()</span> </span>{</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="title">Blog</span><span class="params">(String id, String title, String author, Date createTime, <span class="keyword">int</span> views)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.id = id;</span><br><span class="line"> <span class="keyword">this</span>.title = title;</span><br><span class="line"> <span class="keyword">this</span>.author = author;</span><br><span class="line"> <span class="keyword">this</span>.createTime = createTime;</span><br><span class="line"> <span class="keyword">this</span>.views = views;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getId</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(String id)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.id = id;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getTitle</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> title;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setTitle</span><span class="params">(String title)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.title = title;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">getAuthor</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> author;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAuthor</span><span class="params">(String author)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.author = author;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> Date <span class="title">getCreateTime</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> createTime;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCreateTime</span><span class="params">(Date createTime)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.createTime = createTime;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getViews</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> views;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setViews</span><span class="params">(<span class="keyword">int</span> views)</span> </span>{</span><br><span class="line"> <span class="keyword">this</span>.views = views;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>{</span><br><span class="line"> <span class="keyword">return</span> <span class="string">"Blog{"</span> +</span><br><span class="line"> <span class="string">"id='"</span> + id + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", title='"</span> + title + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", author='"</span> + author + <span class="string">'\''</span> +</span><br><span class="line"> <span class="string">", createTime="</span> + createTime +</span><br><span class="line"> <span class="string">", views="</span> + views +</span><br><span class="line"> <span class="string">'}'</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>4、编写Mapper接口及xml配置文件</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">BlogMapper</span> </span>{</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Mapper 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.zzb.mapper.BlogMapper"</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></tbody></table></figure><p>5、编写mybatis核心配置文件,注册mapper、下划线驼峰自动转换以及日志输出格式</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">settings</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"mapUnderscoreToCamelCase"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br><span class="line"> <span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"logImpl"</span> <span class="attr">value</span>=<span class="string">"STDOUT_LOGGING"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">settings</span>></span></span><br><span class="line"><span class="comment"><!--注册Mapper.xml--></span></span><br><span class="line"><span class="tag"><<span class="name">mappers</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">mapper</span> <span class="attr">resource</span>=<span class="string">"mapper/BlogMapper.xml"</span>/></span></span><br><span class="line"><span class="tag"></<span class="name">mappers</span>></span></span><br></pre></td></tr></tbody></table></figure><p>6、插入初始数据</p><p>编写接口方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 新增一个博客</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">addBlog</span><span class="params">(Blog blog)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>BlogMapper.xml配置文件编写</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"addBlog"</span> <span class="attr">parameterType</span>=<span class="string">"Blog"</span>></span></span><br><span class="line"> insert into blog (id, title, author, create_time, views) values (#{id}, #{title}, #{author}, #{createTime}, #{views})</span><br><span class="line"><span class="tag"></<span class="name">insert</span>></span></span><br></pre></td></tr></tbody></table></figure><p>测试,插入博客数据:</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">addBlog</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"> BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);</span><br><span class="line"></span><br><span class="line"> Blog blog = <span class="keyword">new</span> Blog();</span><br><span class="line"> blog.setId(IDUtil.getId());</span><br><span class="line"> blog.setTitle(<span class="string">"Mybatis如此简单"</span>);</span><br><span class="line"> blog.setAuthor(<span class="string">"ZZB"</span>);</span><br><span class="line"> blog.setCreateTime(<span class="keyword">new</span> Date());</span><br><span class="line"> blog.setViews(<span class="number">9999</span>);</span><br><span class="line"></span><br><span class="line"> mapper.addBlog(blog);</span><br><span class="line"></span><br><span class="line"> blog.setId(IDUtil.getId());</span><br><span class="line"> blog.setTitle(<span class="string">"Java如此简单"</span>);</span><br><span class="line"> mapper.addBlog(blog);</span><br><span class="line"></span><br><span class="line"> blog.setId(IDUtil.getId());</span><br><span class="line"> blog.setTitle(<span class="string">"Spring如此简单"</span>);</span><br><span class="line"> mapper.addBlog(blog);</span><br><span class="line"></span><br><span class="line"> blog.setId(IDUtil.getId());</span><br><span class="line"> blog.setTitle(<span class="string">"微服务如此简单"</span>);</span><br><span class="line"> mapper.addBlog(blog);</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>初始化数据完毕!</p><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210202941177.png" alt="image-20201210202941177"></p><blockquote><p>if </p></blockquote><p><strong>需求:根据作者名字和博客名字来查询博客!如果作者名字为空,那么只根据博客名字查询,反之,则根据作者名来查询</strong></p><p>1、编写接口类</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 查询博客</span></span><br><span class="line"><span class="function">List<Blog> <span class="title">queryBlogByIf</span><span class="params">(Map map)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、编写BlogMapper.xml文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">sql</span> <span class="attr">id</span>=<span class="string">"if-title-author"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">if</span> <span class="attr">test</span>=<span class="string">"title != null"</span>></span></span><br><span class="line"> title = #{title}</span><br><span class="line"> <span class="tag"></<span class="name">if</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">if</span> <span class="attr">test</span>=<span class="string">"author != null"</span>></span></span><br><span class="line"> and author = #{author}</span><br><span class="line"> <span class="tag"></<span class="name">if</span>></span></span><br><span class="line"><span class="tag"></<span class="name">sql</span>></span></span><br><span class="line"><span class="comment"><!--需求1:</span></span><br><span class="line"><span class="comment">根据作者名字和博客名字来查询博客!</span></span><br><span class="line"><span class="comment">如果作者名字为空,那么只根据博客名字查询,反之,则根据作者名来查询</span></span><br><span class="line"><span class="comment">select * from blog where title = #{title} and author = #{author}</span></span><br><span class="line"><span class="comment">--></span></span><br><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"queryBlogByIf"</span> <span class="attr">parameterType</span>=<span class="string">"map"</span> <span class="attr">resultType</span>=<span class="string">"Blog"</span>></span></span><br><span class="line"> select * from blog</span><br><span class="line"> <span class="tag"><<span class="name">where</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"if-title-author"</span>></span><span class="tag"></<span class="name">include</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">where</span>></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">queryBlogByIf</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"></span><br><span class="line"> BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);</span><br><span class="line"> Map map = <span class="keyword">new</span> HashMap();</span><br><span class="line"> map.put(<span class="string">"title"</span>, <span class="string">"Java如此简单"</span>);</span><br><span class="line"> map.put(<span class="string">"author"</span>, <span class="string">"ZZB"</span>);</span><br><span class="line"></span><br><span class="line"> List<Blog> blogs = mapper.queryBlogByIf(map);</span><br><span class="line"> <span class="keyword">for</span> (Blog blog : blogs) {</span><br><span class="line"> System.out.println(blog);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Blog{id='01a7d83785c8483cae5d5384772f5c68', title='Java如此简单', author='ZZB', createTime=Thu Dec 10 20:29:03 CST 2020, views=9999}</span><br></pre></td></tr></tbody></table></figure><p>这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。</p><blockquote><p>Set</p></blockquote><p><strong>需求:更新一个博客</strong></p><p>1、编写接口方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 更新一个博客</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">updateBlog</span><span class="params">(Map map)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、编写BlogMapper.xml配置文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">update</span> <span class="attr">id</span>=<span class="string">"updateBlog"</span> <span class="attr">parameterType</span>=<span class="string">"map"</span>></span></span><br><span class="line"> update blog</span><br><span class="line"> <span class="tag"><<span class="name">set</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">if</span> <span class="attr">test</span>=<span class="string">"title != null"</span>></span></span><br><span class="line"> title = #{title},</span><br><span class="line"> <span class="tag"></<span class="name">if</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">if</span> <span class="attr">test</span>=<span class="string">"author != null"</span>></span></span><br><span class="line"> author = #{author}</span><br><span class="line"> <span class="tag"></<span class="name">if</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">set</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">where</span>></span></span><br><span class="line"> id = #{id}</span><br><span class="line"> <span class="tag"></<span class="name">where</span>></span></span><br><span class="line"><span class="tag"></<span class="name">update</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateBlog</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"></span><br><span class="line"> BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);</span><br><span class="line"> Map map = <span class="keyword">new</span> HashMap();</span><br><span class="line"> map.put(<span class="string">"title"</span>, <span class="string">"Java如此简单2"</span>);</span><br><span class="line"> map.put(<span class="string">"author"</span>, <span class="string">"zb"</span>);</span><br><span class="line"> map.put(<span class="string">"id"</span>, <span class="string">"01a7d83785c8483cae5d5384772f5c68"</span>);</span><br><span class="line"></span><br><span class="line"> mapper.updateBlog(map);</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210204512899.png" alt="image-20201210204512899"></p><blockquote><p>choose</p></blockquote><p>有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java 的 switch 语句</p><p>1、编写接口方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">List<Blog> <span class="title">queryBlogByChoose</span><span class="params">(Map map)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、编写BlogMapper.xml配置文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"queryBlogByChoose"</span> <span class="attr">parameterType</span>=<span class="string">"map"</span> <span class="attr">resultType</span>=<span class="string">"Blog"</span>></span></span><br><span class="line"> select * from blog</span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">where</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">choose</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">when</span> <span class="attr">test</span>=<span class="string">"title != null"</span>></span></span><br><span class="line"> title = #{title}</span><br><span class="line"> <span class="tag"></<span class="name">when</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">when</span> <span class="attr">test</span>=<span class="string">"author != null "</span>></span></span><br><span class="line"> author = #{author}</span><br><span class="line"> <span class="tag"></<span class="name">when</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">otherwise</span>></span></span><br><span class="line"> views = #{views}</span><br><span class="line"> <span class="tag"></<span class="name">otherwise</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"></<span class="name">choose</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">where</span>></span></span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">queryBlogByChoose</span><span class="params">()</span></span>{</span><br><span class="line"></span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"></span><br><span class="line"> BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);</span><br><span class="line"> Map map = <span class="keyword">new</span> HashMap();</span><br><span class="line"> map.put(<span class="string">"title"</span>, <span class="string">"Java如此简单2"</span>);</span><br><span class="line"> map.put(<span class="string">"author"</span>, <span class="string">"ZZB"</span>);</span><br><span class="line"></span><br><span class="line"> List<Blog> blogs = mapper.queryBlogByChoose(map);</span><br><span class="line"> <span class="keyword">for</span> (Blog blog : blogs) {</span><br><span class="line"> System.out.println(blog);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p>注意SQL语句</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210204946348.png" alt="image-20201210204946348"></p><blockquote><p>SQL片段</p></blockquote><p>有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。</p><p><strong>提取SQL片段:</strong></p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">sql</span> <span class="attr">id</span>=<span class="string">"if-title-author"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">if</span> <span class="attr">test</span>=<span class="string">"title != null"</span>></span></span><br><span class="line"> title = #{title}</span><br><span class="line"> <span class="tag"></<span class="name">if</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">if</span> <span class="attr">test</span>=<span class="string">"author != null"</span>></span></span><br><span class="line"> and author = #{author}</span><br><span class="line"> <span class="tag"></<span class="name">if</span>></span></span><br><span class="line"><span class="tag"></<span class="name">sql</span>></span></span><br></pre></td></tr></tbody></table></figure><p><strong>引用SQL片段:</strong></p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"queryBlogIf"</span> <span class="attr">parameterType</span>=<span class="string">"map"</span> <span class="attr">resultType</span>=<span class="string">"blog"</span>></span></span><br><span class="line"> select * from blog</span><br><span class="line"> <span class="tag"><<span class="name">where</span>></span></span><br><span class="line"> <span class="comment"><!-- 引用 sql 片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace --></span></span><br><span class="line"> <span class="tag"><<span class="name">include</span> <span class="attr">refid</span>=<span class="string">"if-title-author"</span>></span><span class="tag"></<span class="name">include</span>></span></span><br><span class="line"> <span class="comment"><!-- 在这里还可以引用其他的 sql 片段 --></span></span><br><span class="line"> <span class="tag"></<span class="name">where</span>></span></span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>注意:</p><p>1、最好基于 单表来定义 sql 片段,提高片段的可重用性;</p><p>2、在 sql 片段中不要包括 where。</p><blockquote><p>ForEach</p></blockquote><p>首先将数据库中的表格id修改为1,2,3,4</p><p><strong>需求:查询 id 分别为1,3,4 的博客信息</strong></p><p>1、编写接口方法</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ForEach</span></span><br><span class="line"><span class="function">List<Blog> <span class="title">queryBlogForeach</span><span class="params">(Map map)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>2、编写BlogMapper.xml配置文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"queryBlogForeach"</span> <span class="attr">parameterType</span>=<span class="string">"map"</span> <span class="attr">resultType</span>=<span class="string">"blog"</span>></span></span><br><span class="line"> select * from blog</span><br><span class="line"> <span class="tag"><<span class="name">where</span>></span></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> collection:指定输入对象中的集合属性</span></span><br><span class="line"><span class="comment"> item:每次遍历生成的对象</span></span><br><span class="line"><span class="comment"> open:开始遍历时的拼接字符串</span></span><br><span class="line"><span class="comment"> close:结束时拼接的字符串</span></span><br><span class="line"><span class="comment"> separator:遍历对象之间需要拼接的字符串</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">foreach</span> <span class="attr">collection</span>=<span class="string">"ids"</span> <span class="attr">item</span>=<span class="string">"id"</span> <span class="attr">open</span>=<span class="string">"and ("</span> <span class="attr">close</span>=<span class="string">")"</span> <span class="attr">separator</span>=<span class="string">"or"</span>></span></span><br><span class="line"> id = #{id}</span><br><span class="line"> <span class="tag"></<span class="name">foreach</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">where</span>></span></span><br><span class="line"><span class="tag"></<span class="name">select</span>></span></span><br></pre></td></tr></tbody></table></figure><p>3、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">queryBlogForeach</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"> BlogMapper mapper = sqlSession.getMapper(BlogMapper.class);</span><br><span class="line"> Map map = <span class="keyword">new</span> HashMap();</span><br><span class="line"> List<Integer> ids = <span class="keyword">new</span> ArrayList<Integer>();</span><br><span class="line"> ids.add(<span class="number">1</span>);</span><br><span class="line"> ids.add(<span class="number">3</span>);</span><br><span class="line"> ids.add(<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line"> map.put(<span class="string">"ids"</span>, ids);</span><br><span class="line"> List<Blog> blogList = mapper.queryBlogForeach(map);</span><br><span class="line"> <span class="keyword">for</span> (Blog blog : blogList) {</span><br><span class="line"> System.out.println(blog);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p>注意SQL语句</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210210021528.png" alt="image-20201210210021528"></p><p><strong>总结:</strong></p><p>==其实动态 sql 语句的编写往往就是一个拼接的问题,为了保证拼接准确,我们最好首先要写原生的 sql 语句出来,然后在通过 mybatis 动态sql 对照着改,防止出错。多在实践中使用才是熟练掌握它的技巧==</p><p>建议:</p><p>先在mysql写出完整的sql,再对应的修改为动态的sql</p><h2 id="11-缓存"><a href="#11-缓存" class="headerlink" title="11 缓存"></a>11 缓存</h2><blockquote><p>简介</p></blockquote><p>1、什么是缓存 [ Cache ] ?</p><ul><li>存在内存中的临时数据</li><li>将用户经常查询的数据存放在缓存中(内存),用户去查询数据就不用从磁盘上(关系型数据库文件)查询,从缓存中直接查询,从而提高查询效率,解决了高并发系统的性能问题。</li></ul><p>2、为什么使用缓存?</p><ul><li>减少和数据库的交互次数,减少系统的开销,提高系统的效率。</li></ul><p>3、什么样的数据能使用缓存?</p><ul><li>经常查询并且不经常改变的数据。</li></ul><blockquote><p>MyBatis缓存</p></blockquote><ul><li><p>MyBatis包含一个非常强大的查询缓存特性,它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率。 </p></li><li><p>MyBatis系统中默认定义了两级缓存:<strong>一级缓存</strong>和<strong>二级缓存</strong></p></li><li><ul><li>默认情况下,只有一级缓存开启。(SqlSession级别的缓存,也称为本地缓存)</li><li>二级缓存需要手动开启和配置,他是基于namespace级别的缓存。</li><li>为了提高扩展性,MyBatis定义了缓存接口Cache。我们可以通过实现Cache接口来自定义二级缓存</li></ul></li></ul><blockquote><p>一级缓存</p></blockquote><p>一级缓存也叫本地缓存:</p><ul><li>与数据库同一次会话期间查询到的数据会放在本地缓存中;</li><li>以后如果需要获取相同的数据,直接从缓存中拿,没必须再去查询数据库。</li></ul><blockquote><p>测试</p></blockquote><p>module:mybatis-08</p><p>1、在MyBatis核心配置文件中加入日志,方便测试结果的查看。</p><p>2、编写实体类User.java</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.AllArgsConstructor;</span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"><span class="keyword">import</span> lombok.NoArgsConstructor;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.Serializable;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@AllArgsConstructor</span></span><br><span class="line"><span class="meta">@NoArgsConstructor</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>{</span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">int</span> id;</span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"> <span class="keyword">private</span> String pwd;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>3、编写UserMapper接口</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.zzb.dao;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.zzb.pojo.User;</span><br><span class="line"><span class="keyword">import</span> org.apache.ibatis.annotations.Param;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserMapper</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 根据Id查询用户</span></span><br><span class="line"> <span class="function">User <span class="title">getUserById</span><span class="params">(<span class="meta">@Param("id")</span> <span class="keyword">int</span> id)</span></span>;</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>4、编写UserMapper.xml配置文件</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8" ?></span></span><br><span class="line"><span class="meta"><!DOCTYPE <span class="meta-keyword">mapper</span></span></span><br><span class="line"><span class="meta"> <span class="meta-keyword">PUBLIC</span> <span class="meta-string">"-//mybatis.org//DTD Config 3.0//EN"</span></span></span><br><span class="line"><span class="meta"> <span class="meta-string">"http://mybatis.org/dtd/mybatis-3-mapper.dtd"</span>></span></span><br><span class="line"><span class="comment"><!--configuration--></span></span><br><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.zzb.dao.UserMapper"</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">cache</span>/></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserById"</span> <span class="attr">resultType</span>=<span class="string">"User"</span>></span></span><br><span class="line"> select * from user where id = #{id}</span><br><span class="line"> <span class="tag"></<span class="name">select</span>></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></tbody></table></figure><p>5、测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"> UserMapper userMapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"></span><br><span class="line"> User user = userMapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user);</span><br><span class="line"> User user2 = userMapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user == user2);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210212931401.png" alt="image-20201210212931401"></p><p>很显然,测试仅从数据库中查询了一次,并将结果存入了缓存中。</p><blockquote><p>一级缓存失效的四种情况</p></blockquote><p>一级缓存是SqlSession级别的缓存,是一直开启的,我们关闭不了它;</p><p>一级缓存失效情况:没有使用到当前的一级缓存,那么还需要再向数据库中发起一次查询请求!</p><p>1、SqlSession不同</p><p>测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"> SqlSession sqlSession2 = MybatisUtils.getSqlSession();</span><br><span class="line"> UserMapper userMapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);</span><br><span class="line"></span><br><span class="line"> User user = userMapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user);</span><br><span class="line"></span><br><span class="line"> User user2 = userMapper2.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user2);</span><br><span class="line"></span><br><span class="line"> System.out.println(user == user2);</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line"> sqlSession2.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210213236283.png" alt="image-20201210213236283"></p><p>测试结果中执行了两次SQL语句,表明<strong>每个SqlSession中的缓存相互独立!</strong></p><p>2、SqlSession相同,查询条件不同</p><p>测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"> UserMapper userMapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"></span><br><span class="line"> User user = userMapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user);</span><br><span class="line"> User user2 = userMapper.getUserById(<span class="number">2</span>);</span><br><span class="line"> System.out.println(user == user2);</span><br><span class="line"> </span><br><span class="line"> sqlSession.close();</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210213508832.png" alt="image-20201210213508832"></p><p>两个SQL语句不同,查询结果不同,故不会从一级缓存中命中结果!</p><p>3、SqlSession相同,但两次查询之间执行了增删改操作!</p><p>UserMapper接口中增加方法:</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 修改一个用户</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">updateUser</span><span class="params">(Map map)</span></span>;</span><br></pre></td></tr></tbody></table></figure><p>UserMapper.xml编写:</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">update</span> <span class="attr">id</span>=<span class="string">"updateUser"</span> <span class="attr">parameterType</span>=<span class="string">"map"</span>></span></span><br><span class="line"> update user set name = #{name} where id = #{id}</span><br><span class="line"><span class="tag"></<span class="name">update</span>></span></span><br></pre></td></tr></tbody></table></figure><p>测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test3</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"> UserMapper userMapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"></span><br><span class="line"> User user1 = userMapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user1);</span><br><span class="line"></span><br><span class="line"> Map<Object, Object> map = <span class="keyword">new</span> HashMap<Object, Object>();</span><br><span class="line"> map.put(<span class="string">"name"</span>, <span class="string">"zb"</span>);</span><br><span class="line"> map.put(<span class="string">"id"</span>, <span class="number">4</span>);</span><br><span class="line"> <span class="keyword">int</span> i = userMapper.updateUser(map);</span><br><span class="line"> <span class="keyword">if</span>(i == <span class="number">1</span>){</span><br><span class="line"> System.out.println(<span class="string">"修改数据成功!"</span>);</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> System.out.println(<span class="string">"修改数据失败!"</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> User user2 = userMapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user2);</span><br><span class="line"></span><br><span class="line"> System.out.println(user1 == user2);</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210214315828.png" alt="image-20201210214315828"></p><p>测试结果表明,两次查询间如果执行了增删改方法,一级缓存会被清空!<strong>因为增删改操作可能会对当前数据产生影响!</strong></p><p>4、SqlSession相同,手动清除一级缓存</p><p>测试</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test2</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"> UserMapper userMapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"></span><br><span class="line"> User user = userMapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 手动清除一级缓存</span></span><br><span class="line"> sqlSession.clearCache();</span><br><span class="line"></span><br><span class="line"> User user2 = userMapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user == user2);</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210214648807.png" alt="image-20201210214648807"></p><blockquote><p>二级缓存</p></blockquote><ul><li><p>二级缓存也叫全局缓存,一级缓存作用域太低了,所以诞生了二级缓存</p></li><li><p>基于namespace级别的缓存,一个名称空间,对应一个二级缓存;</p></li><li><p>工作机制</p></li><li><ul><li>一个会话查询一条数据,这个数据就会被放在当前会话的一级缓存中;</li><li>如果当前会话关闭了,这个会话对应的一级缓存就没了;但是我们想要的是,会话关闭了,一级缓存中的数据被保存到二级缓存中;</li><li>新的会话查询信息,就可以从二级缓存中获取内容;</li><li>不同的mapper查出的数据会放在自己对应的缓存(map)中;</li></ul></li></ul><blockquote><p>使用步骤</p></blockquote><p>1、开启全局缓存</p><p>在mybatis核心配置文件中添加如下配置:</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"cacheEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/></span></span><br></pre></td></tr></tbody></table></figure><p>2、在每一个Mapper.xml配置文件中使用二级缓存</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">cache</span>/></span></span><br><span class="line"></span><br><span class="line">官方示例=====>查看官方文档</span><br><span class="line"><cache</span><br><span class="line"> eviction="FIFO"</span><br><span class="line"> flushInterval="60000"</span><br><span class="line"> size="512"</span><br><span class="line"> readOnly="true"/></span><br><span class="line">这个更高级的配置创建了一个 FIFO 缓存,每隔 60 秒刷新,最多可以存储结果对象或列表的 512 个引用,而且返回的对象被认为是只读的,因此对它们进行修改可能会在不同线程中的调用者产生冲突。</span><br></pre></td></tr></tbody></table></figure><p>3、代码测试</p><p><strong>所有实体类先实现序列化接口。</strong></p><p>测试代码:</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span></span>{</span><br><span class="line"> SqlSession sqlSession = MybatisUtils.getSqlSession();</span><br><span class="line"> SqlSession sqlSession2 = MybatisUtils.getSqlSession();</span><br><span class="line"> UserMapper userMapper = sqlSession.getMapper(UserMapper.class);</span><br><span class="line"> UserMapper userMapper2 = sqlSession2.getMapper(UserMapper.class);</span><br><span class="line"></span><br><span class="line"> User user = userMapper.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user);</span><br><span class="line"><span class="comment">// 关闭sqlSession,是该sqlSession中的一级缓存的内容存放到二级缓存中</span></span><br><span class="line"> sqlSession.close();</span><br><span class="line"></span><br><span class="line"> User user2 = userMapper2.getUserById(<span class="number">1</span>);</span><br><span class="line"> System.out.println(user2);</span><br><span class="line"></span><br><span class="line"> System.out.println(user == user2);</span><br><span class="line"></span><br><span class="line"> sqlSession.close();</span><br><span class="line"> sqlSession2.close();</span><br><span class="line"> }</span><br></pre></td></tr></tbody></table></figure><p>测试结果:</p><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210215634682.png" alt="image-20201210215634682"></p><ul><li>只要开启了二级缓存,我们在同一个Mapper中的查询,可以在二级缓存中拿到数据</li><li>查出的数据都会被默认先放在一级缓存中</li><li>只有会话提交或者关闭以后,一级缓存中的数据才会转到二级缓存中</li></ul><blockquote><p>缓存原理图</p></blockquote><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210220100854.png" alt="image-20201210220100854"></p><blockquote><p>EhCache</p></blockquote><p><img src="https://gitee.com/zzb9527/blog-image/raw/master/img/image-20201210220223892.png" alt="image-20201210220223892"></p><p>第三方缓存实现–EhCache;</p><p>Ehcache是一种广泛使用的java分布式缓存,用于通用缓存;</p><p>要在应用程序中使用Ehcache,需要引入依赖的jar包:</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><!-- https://mvnrepository.com/artifact/org.mybatis.caches/mybatis-ehcache --></span></span><br><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.mybatis.caches<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>mybatis-ehcache<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>1.1.0<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></tbody></table></figure><p>在mapper.xml中使用对应的缓存即可</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag"><<span class="name">mapper</span> <span class="attr">namespace</span> = <span class="string">“org.acme.FooMapper”</span> ></span> </span><br><span class="line"> <span class="tag"><<span class="name">cache</span> <span class="attr">type</span> = <span class="string">“org.mybatis.caches.ehcache.EhcacheCache”</span> /></span> </span><br><span class="line"><span class="tag"></<span class="name">mapper</span>></span></span><br></pre></td></tr></tbody></table></figure><p>编写ehcache.xml文件,如果在加载时未找到/ehcache.xml资源或出现问题,则将使用默认配置。</p><figure class="highlight xml"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><?xml version="1.0" encoding="UTF-8"?></span></span><br><span class="line"><span class="tag"><<span class="name">ehcache</span> <span class="attr">xmlns:xsi</span>=<span class="string">"http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">xsi:noNamespaceSchemaLocation</span>=<span class="string">"http://ehcache.org/ehcache.xsd"</span></span></span><br><span class="line"><span class="tag"> <span class="attr">updateCheck</span>=<span class="string">"false"</span>></span></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> diskStore:为缓存路径,ehcache分为内存和磁盘两级,此属性定义磁盘的缓存位置。参数解释如下:</span></span><br><span class="line"><span class="comment"> user.home – 用户主目录</span></span><br><span class="line"><span class="comment"> user.dir – 用户当前工作目录</span></span><br><span class="line"><span class="comment"> java.io.tmpdir – 默认临时文件路径</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">diskStore</span> <span class="attr">path</span>=<span class="string">"./tmpdir/Tmp_EhCache"</span>/></span></span><br><span class="line"> </span><br><span class="line"> <defaultCache</span><br><span class="line"> eternal="false"</span><br><span class="line"> maxElementsInMemory="10000"</span><br><span class="line"> overflowToDisk="false"</span><br><span class="line"> diskPersistent="false"</span><br><span class="line"> timeToIdleSeconds="1800"</span><br><span class="line"> timeToLiveSeconds="259200"</span><br><span class="line"> memoryStoreEvictionPolicy="LRU"/></span><br><span class="line"></span><br><span class="line"> <cache</span><br><span class="line"> name="cloud_user"</span><br><span class="line"> eternal="false"</span><br><span class="line"> maxElementsInMemory="5000"</span><br><span class="line"> overflowToDisk="false"</span><br><span class="line"> diskPersistent="false"</span><br><span class="line"> timeToIdleSeconds="1800"</span><br><span class="line"> timeToLiveSeconds="1800"</span><br><span class="line"> memoryStoreEvictionPolicy="LRU"/></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> defaultCache:默认缓存策略,当ehcache找不到定义的缓存时,则使用这个缓存策略。只能定义一个。</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="comment"><!--</span></span><br><span class="line"><span class="comment"> name:缓存名称。</span></span><br><span class="line"><span class="comment"> maxElementsInMemory:缓存最大数目</span></span><br><span class="line"><span class="comment"> maxElementsOnDisk:硬盘最大缓存个数。</span></span><br><span class="line"><span class="comment"> eternal:对象是否永久有效,一但设置了,timeout将不起作用。</span></span><br><span class="line"><span class="comment"> overflowToDisk:是否保存到磁盘,当缓存存储的数据达到maxInMemory限制时</span></span><br><span class="line"><span class="comment"> timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。</span></span><br><span class="line"><span class="comment"> timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。</span></span><br><span class="line"><span class="comment"> diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.</span></span><br><span class="line"><span class="comment"> diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。</span></span><br><span class="line"><span class="comment"> diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。</span></span><br><span class="line"><span class="comment"> memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。</span></span><br><span class="line"><span class="comment"> clearOnFlush:内存数量最大时是否清除。</span></span><br><span class="line"><span class="comment"> memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。</span></span><br><span class="line"><span class="comment"> FIFO,first in first out,这个是大家最熟的,先进先出。</span></span><br><span class="line"><span class="comment"> LFU, Less Frequently Used,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的。如上面所讲,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。</span></span><br><span class="line"><span class="comment"> LRU,Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"></span><br><span class="line"><span class="tag"></<span class="name">ehcache</span>></span></span><br></pre></td></tr></tbody></table></figure>]]></content>
</entry>
<entry>
<title>算法技巧</title>
<link href="/javanblog/2021/04/25/%E7%AE%97%E6%B3%95%E6%8A%80%E5%B7%A71/"/>
<url>/javanblog/2021/04/25/%E7%AE%97%E6%B3%95%E6%8A%80%E5%B7%A71/</url>
<content type="html"><![CDATA[<h1 id="双指针法"><a href="#双指针法" class="headerlink" title="双指针法"></a>双指针法</h1><blockquote><p> 这是一种使用两个指针互相配合来存储节点以便于运算的技巧</p></blockquote><p>适用于数组、链表等线性结构,常用思路有碰撞指针、滑动窗口、快慢指针。</p><h2 id="对撞指针"><a href="#对撞指针" class="headerlink" title="对撞指针"></a>对撞指针</h2><blockquote><p>leetcode第一题两数之和</p></blockquote><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">int</span>[] twoSum(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target) {</span><br><span class="line"> <span class="comment">//双指针法</span></span><br><span class="line"> <span class="comment">//采用对撞指针法,时间复杂度降低到 o(n)</span></span><br><span class="line"> <span class="keyword">int</span> len = nums.length;</span><br><span class="line"> <span class="keyword">int</span> left=<span class="number">0</span>,right=len-<span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">int</span>[] res=<span class="keyword">new</span> <span class="keyword">int</span>[<span class="number">2</span>];</span><br><span class="line"> <span class="keyword">int</span>[] tmp1=<span class="keyword">new</span> <span class="keyword">int</span>[nums.length];</span><br><span class="line"> System.arraycopy(nums,<span class="number">0</span>,tmp1,<span class="number">0</span>,nums.length);</span><br><span class="line"></span><br><span class="line"> <span class="comment">//将数组排序</span></span><br><span class="line"> Arrays.sort(nums);</span><br><span class="line"> <span class="comment">//如何将排序后的数组下标和原有数组下标对应起来????</span></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">int</span> m=<span class="number">0</span>,n=<span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = left,j=right;i< j;){</span><br><span class="line"> <span class="keyword">if</span>(nums[i]+nums[j]==target){</span><br><span class="line"> m = i;</span><br><span class="line"> n = j;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(nums[i]+nums[j]>target){</span><br><span class="line"> <span class="comment">//右指针左移动</span></span><br><span class="line"> j--;</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(nums[i]+nums[j]<target){</span><br><span class="line"> <span class="comment">//左指针右移动</span></span><br><span class="line"> i++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//两个数不能一样,只能用两个变量了</span></span><br><span class="line"> <span class="keyword">int</span> i,j;</span><br><span class="line"> <span class="keyword">for</span> ( i=<span class="number">0</span>;i<nums.length;i++) {</span><br><span class="line"> <span class="keyword">if</span>(tmp1[i]==nums[m]){</span><br><span class="line"> res[<span class="number">0</span>] = i;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> ( j=<span class="number">0</span>;i<nums.length;j++) {</span><br><span class="line"> <span class="comment">// i!=j</span></span><br><span class="line"> <span class="keyword">if</span>(tmp1[j]==nums[n]&&i!=j){</span><br><span class="line"> res[<span class="number">1</span>] = j;</span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br><span class="line">**这道题时间复杂度巨高!!!为什么呢???用到了系统的排序算法,这也暗示我们在使用双指针法的时候,数组一般要有序!**</span><br><span class="line"></span><br><span class="line">主要学会这个思想!!!</span><br></pre></td></tr></tbody></table></figure><blockquote><p>leetcode881题救生艇</p></blockquote><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">numRescueBoats</span><span class="params">(<span class="keyword">int</span>[] people, <span class="keyword">int</span> limit)</span> </span>{</span><br><span class="line"> <span class="comment">//采用双指针法 对撞指针法</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment">//对数组进行排序</span></span><br><span class="line"> Arrays.sort(people);</span><br><span class="line"> <span class="comment">//数组长度</span></span><br><span class="line"> <span class="keyword">int</span> len = people.length;</span><br><span class="line"> <span class="comment">//计数器及总和</span></span><br><span class="line"> <span class="keyword">int</span> count=<span class="number">0</span>,sum=<span class="number">0</span>;</span><br><span class="line"> <span class="comment">//长度为1时</span></span><br><span class="line"> <span class="keyword">if</span>(len==<span class="number">1</span>){</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//定义指针</span></span><br><span class="line"> <span class="keyword">int</span> i=<span class="number">0</span>,j=len-<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(i<=j){</span><br><span class="line"> <span class="comment">//题目已经说明了一首船最多在两个人!!!</span></span><br><span class="line"> <span class="keyword">if</span>(people[i]+people[j]<=limit){</span><br><span class="line"> i++;</span><br><span class="line"> }</span><br><span class="line"> j--;</span><br><span class="line"> count++;</span><br><span class="line"> } </span><br><span class="line"> <span class="keyword">return</span> count;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><h2 id="快慢指针"><a href="#快慢指针" class="headerlink" title="快慢指针"></a>快慢指针</h2><blockquote><p>leetcode第141题 环形链表</p></blockquote><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for singly-linked list.</span></span><br><span class="line"><span class="comment"> * class ListNode {</span></span><br><span class="line"><span class="comment"> * int val;</span></span><br><span class="line"><span class="comment"> * ListNode next;</span></span><br><span class="line"><span class="comment"> * ListNode(int x) {</span></span><br><span class="line"><span class="comment"> * val = x;</span></span><br><span class="line"><span class="comment"> * next = null;</span></span><br><span class="line"><span class="comment"> * }</span></span><br><span class="line"><span class="comment"> * }</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">hasCycle</span><span class="params">(ListNode head)</span> </span>{</span><br><span class="line"> ListNode fast ;</span><br><span class="line"> ListNode slow = fast = head;</span><br><span class="line"> <span class="keyword">if</span>(head==<span class="keyword">null</span>){</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//while循环里面,快指针如果没有循环,那么链表就终止了</span></span><br><span class="line"> <span class="keyword">while</span>(fast!=<span class="keyword">null</span>&&fast.next!=<span class="keyword">null</span>){</span><br><span class="line"> slow = slow.next;</span><br><span class="line"> fast = fast.next.next;</span><br><span class="line"> <span class="keyword">if</span>(slow==fast){</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><h1 id="二分查找法"><a href="#二分查找法" class="headerlink" title="二分查找法"></a>二分查找法</h1><p>此算法要求,数据结构是顺序存储且关键字有序</p><blockquote><p>leetcode704题二分查找</p></blockquote><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">search</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target)</span> </span>{</span><br><span class="line"> <span class="comment">//暴力解</span></span><br><span class="line"> <span class="comment">// for(int i =0; i< nums.length; i++) {</span></span><br><span class="line"> <span class="comment">// if(nums[i]==target){</span></span><br><span class="line"> <span class="comment">// return i;</span></span><br><span class="line"> <span class="comment">// }</span></span><br><span class="line"> </span><br><span class="line"></span><br><span class="line"> <span class="comment">// }</span></span><br><span class="line"> <span class="comment">// return -1;</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">// 二分查找法</span></span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>, right = nums.length - <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> mid = (left + right) / <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">while</span>(left<=right){</span><br><span class="line"> <span class="keyword">if</span>(nums[mid]==target){</span><br><span class="line"> <span class="keyword">return</span> mid;</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(nums[mid]>target){</span><br><span class="line"> right = mid - <span class="number">1</span>;</span><br><span class="line"> }<span class="keyword">else</span>{</span><br><span class="line"> left = mid + <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> mid = (left + right) / <span class="number">2</span>;</span><br><span class="line"> } </span><br><span class="line"> <span class="keyword">return</span> -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><blockquote><p>leetcode 35 搜索插入位置</p></blockquote><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">searchInsert</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>, right = nums.length - <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">while</span>(left <= right) {</span><br><span class="line"> <span class="keyword">int</span> mid = (left + right) / <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(nums[mid] == target) {</span><br><span class="line"> <span class="keyword">return</span> mid;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span>(nums[mid] < target) {</span><br><span class="line"> left = mid + <span class="number">1</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> right = mid - <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> right+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><blockquote><p>leetcode162寻找峰值</p></blockquote><p>这一题,我首先想到的是暴力解,讨论各种情况,成功提交!</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">findPeakElement</span><span class="params">(<span class="keyword">int</span>[] nums)</span> </span>{</span><br><span class="line"> <span class="comment">// 暴力解法</span></span><br><span class="line"> <span class="comment">// 先讨论数组长度《3的情况</span></span><br><span class="line"> <span class="keyword">int</span> len = nums.length;</span><br><span class="line"> <span class="keyword">if</span>(len == <span class="number">1</span>){</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(len == <span class="number">2</span>){</span><br><span class="line"> <span class="keyword">if</span>(nums[<span class="number">0</span>]>nums[<span class="number">1</span>]){</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(nums[<span class="number">0</span>]<nums[<span class="number">1</span>]){</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//讨论数组长度》=3</span></span><br><span class="line"> <span class="comment">//定义三个指针</span></span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> mid = <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> right = <span class="number">2</span>;</span><br><span class="line"> <span class="comment">// 讨论峰值在边界情况</span></span><br><span class="line"> <span class="keyword">if</span>(nums[<span class="number">0</span>]>nums[<span class="number">1</span>]){</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(nums[len-<span class="number">1</span>]>nums[len-<span class="number">2</span>]){</span><br><span class="line"> <span class="keyword">return</span> len -<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 讨论峰值在区间内</span></span><br><span class="line"> <span class="keyword">while</span>(right < len){</span><br><span class="line"> <span class="keyword">if</span>(nums[mid]>nums[left]&&nums[mid]>nums[right]){</span><br><span class="line"> <span class="keyword">return</span> mid;</span><br><span class="line"> }</span><br><span class="line"> left++;</span><br><span class="line"> mid++;</span><br><span class="line"> right++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">findPeakElement</span><span class="params">(<span class="keyword">int</span>[] nums)</span> </span>{</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 二分法</span></span><br><span class="line"> <span class="comment">// 使用二分法关键是理解好核心逻辑,一般二分法要排序,但是这样排序后,索引就乱了</span></span><br><span class="line"> <span class="comment">// 重新审查题意,发现, nums[-1] = nums[n] = -∞</span></span><br><span class="line"> <span class="keyword">int</span> len = nums.length;</span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>,right = len -<span class="number">1</span>;</span><br><span class="line"> <span class="keyword">int</span> mid = (left + right) / <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">while</span>(left<right){</span><br><span class="line"> <span class="comment">// 只要找到一个峰值就可以了!!!</span></span><br><span class="line"> <span class="comment">// 说明峰值在右侧</span></span><br><span class="line"> <span class="keyword">if</span>(nums[mid+<span class="number">1</span>]>nums[mid]){</span><br><span class="line"> <span class="comment">//右边高,说明在mid右边有峰值,所以mid一定不是</span></span><br><span class="line"> <span class="comment">//mid已经不是了,排除掉</span></span><br><span class="line"> left = mid + <span class="number">1</span> ;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 说明峰值在左侧</span></span><br><span class="line"> <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">// 左边高,说明左边有峰值,可能mid就是</span></span><br><span class="line"> right = mid ;<span class="comment">// mid在下一次查找中还要考虑在内</span></span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 这里的代码很贼!,在找峰值的时候,右侧是left=mid+1,左侧是right=mid</span></span><br><span class="line"> <span class="comment">// 其实核心就是往右侧靠,因为最左侧,循环到底,相对于num[-1]也是右侧!!!</span></span><br><span class="line"> mid = (left + right) / <span class="number">2</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> left; </span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>]]></content>
</entry>
<entry>
<title>Leetcode刷题笔记</title>
<link href="/javanblog/2021/04/24/Leetcode%20%E5%88%B7%E9%A2%98%E8%AE%B0%E5%BD%95/"/>
<url>/javanblog/2021/04/24/Leetcode%20%E5%88%B7%E9%A2%98%E8%AE%B0%E5%BD%95/</url>
<content type="html"><![CDATA[<h1 id="Leetcode-刷题记录"><a href="#Leetcode-刷题记录" class="headerlink" title="Leetcode 刷题记录"></a>Leetcode 刷题记录</h1><h2 id="1-两数之和(2021-4-3)"><a href="#1-两数之和(2021-4-3)" class="headerlink" title="1.两数之和(2021.4.3)"></a>1.两数之和(2021.4.3)</h2><p>给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。</p><p>你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。</p><p>你可以按任意顺序返回答案。</p><p>示例 1:</p><p>输入:nums = [2,7,11,15], target = 9<br>输出:[0,1]<br>解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。<br>示例 2:</p><p>输入:nums = [3,2,4], target = 6<br>输出:[1,2]<br>示例 3:</p><p>输入:nums = [3,3], target = 6<br>输出:[0,1]</p><p><strong>分析:</strong></p><p>该题只有一个答案,并且同一个元素不能重复出现。很容易想到用暴力解法,遍历整个数组,返回结果。</p><p><strong>暴力解法:</strong></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">class Solution {</span><br><span class="line"> public int[] twoSum(int[] nums, int target) {</span><br><span class="line"> </span><br><span class="line"> //遍历数组</span><br><span class="line"> for(int i = 0; i < nums.length-1; i++){</span><br><span class="line"> for(int j = i + 1; j < nums.length; j++){</span><br><span class="line"> if(nums[j]+nums[i]==target){</span><br><span class="line"> return new int[]{i,j};</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> return new int[0];</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line">//执行用时: 0 ms</span><br><span class="line">//内存消耗: 38.3 MB</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p><strong>哈希表法:</strong>在做第二次的时候,hashmap的containsKey()方法不知道如何使用!!!!</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">class Solution {</span><br><span class="line"> public int[] twoSum(int[] nums, int target) {</span><br><span class="line"> </span><br><span class="line"> int length = nums.length; </span><br><span class="line"> //创建哈希表,哈希表下标为0开始</span><br><span class="line"> HashMap<Integer,Integer> hashmap = new HashMap<>(length-1);</span><br><span class="line"> //遍历,寻找有无target-x值</span><br><span class="line"> for(int i = 0; i < length;i++){</span><br><span class="line"> if(hashmap.containsKey(target-nums[i])){</span><br><span class="line">//在哈希表中找到了target-x的值</span><br><span class="line">return new int[]{hashmap.get(target-nums[i]),i};</span><br><span class="line">}</span><br><span class="line">//否则将键值队存入hanshmap中</span><br><span class="line">hashmap.put(nums[i],i);</span><br><span class="line"> }</span><br><span class="line"> return new int[0];</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><h2 id="2-两数相加(2021-4-5)"><a href="#2-两数相加(2021-4-5)" class="headerlink" title="2.两数相加(2021.4.5)"></a>2.两数相加(2021.4.5)</h2><p>给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。</p><p>请你将两个数相加,并以相同形式返回一个表示和的链表。</p><p>你可以假设除了数字 0 之外,这两个数都不会以 0 开头。</p><p>示例 1:</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210405094908270.png" alt="image-20210405094908270"></p><p>输入:l1 = [2,4,3], l2 = [5,6,4]<br>输出:[7,0,8]<br>解释:342 + 465 = 807.<br>示例 2:</p><p>输入:l1 = [0], l2 = [0]<br>输出:[0]<br>示例 3:</p><p>输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]<br>输出:[8,9,9,9,0,0,0,1]</p><p><strong>分析:</strong></p><p>该题考察的是链表的操作,预先指针,执行指针以及一些数学逻辑(进位数通过加法补位)。</p><p>将两个链表看成是相同长度的进行遍历,如果一个链表较短则在前面补 00,比如 987 + 23 = 987 + 023 = 1010<br>每一位计算的同时需要考虑上一位的进位问题,而当前位计算结束后同样需要更新进位值<br>如果两个链表全部遍历完毕后,进位值为 11,则在新链表最前方添加节点 11<br><strong>小技巧:对于链表问题,返回结果为头结点时,通常需要先初始化一个预先指针 pre,该指针的下一个节点指向真正的头结点head。使用预先指针的目的在于链表初始化时无可用节点值,而且链表构造过程需要指针移动,进而会导致头指针丢失,无法返回结果。</strong></p><p><strong>心得:</strong>(这是题解)</p><p>做题的时候,可以用草稿纸进行流程图演算,不会画图的程序员不是好程序猿!!!</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">/**</span><br><span class="line"> * Definition for singly-linked list.</span><br><span class="line"> * public class ListNode {</span><br><span class="line"> * int val;</span><br><span class="line"> * ListNode next;</span><br><span class="line"> * ListNode() {}</span><br><span class="line"> * ListNode(int val) { this.val = val; }</span><br><span class="line"> * ListNode(int val, ListNode next) { this.val = val; this.next = next; }</span><br><span class="line"> * }</span><br><span class="line"> */</span><br><span class="line">class Solution {</span><br><span class="line"> public ListNode addTwoNumbers(ListNode l1, ListNode l2) {</span><br><span class="line"> //定义预先指针和执行指针</span><br><span class="line"> ListNode pre = new ListNode(0);</span><br><span class="line"> ListNode cur = pre;</span><br><span class="line"> //定义进位数</span><br><span class="line"> int carry = 0;</span><br><span class="line"> //进行遍历</span><br><span class="line"> while(l1!=null||l2!=null){</span><br><span class="line"> int n1 = l1==null ? 0:l1.val;</span><br><span class="line"> int n2 = l2==null ? 0:l2.val;</span><br><span class="line"> //定义链表每个位置的值</span><br><span class="line"> int sum = n1+n2+carry;//这个进位数carry一定不能忘记!!!</span><br><span class="line"> //判断是否有进位</span><br><span class="line"> carry = sum / 10;</span><br><span class="line"> //将值存进链表中</span><br><span class="line"> sum = sum % 10;</span><br><span class="line"> cur.next = new ListNode(sum);</span><br><span class="line"> //执行指针 移动一格</span><br><span class="line"> cur = cur.next;</span><br><span class="line"> //l1 l2 链表移动</span><br><span class="line"> if(l1!=null){</span><br><span class="line"> l1 = l1.next;</span><br><span class="line"> }</span><br><span class="line"> if(l2!=null){</span><br><span class="line"> l2 = l2.next;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> //最后进位是一的话,就要补位</span><br><span class="line"> if(carry==1){</span><br><span class="line"> cur.next = new ListNode(1);</span><br><span class="line"> }</span><br><span class="line"> //为什么是返回 pre.next呢??? </span><br><span class="line"> //因为pre所指向的数据是无效的数据0,只是用来标记的,该指针的下一个节点指向真正的头结点head</span><br><span class="line"> return pre.next;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>这是第二次做题发现的问题。第一,链表节点要先建立起来,才能一路next。第二,考虑链表的空指针问题。第三,考虑进位加法的次序问题。第四,考虑结尾节点有无进位,是否需要创建链表。</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for singly-linked list.</span></span><br><span class="line"><span class="comment"> * public class ListNode {</span></span><br><span class="line"><span class="comment"> * int val;</span></span><br><span class="line"><span class="comment"> * ListNode next;</span></span><br><span class="line"><span class="comment"> * ListNode() {}</span></span><br><span class="line"><span class="comment"> * ListNode(int val) { this.val = val; }</span></span><br><span class="line"><span class="comment"> * ListNode(int val, ListNode next) { this.val = val; this.next = next; }</span></span><br><span class="line"><span class="comment"> * }</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> ListNode <span class="title">addTwoNumbers</span><span class="params">(ListNode l1, ListNode l2)</span> </span>{</span><br><span class="line"> <span class="comment">/*</span></span><br><span class="line"><span class="comment"> 分析:</span></span><br><span class="line"><span class="comment"> 若两数同位,各个数字相加,并加上进位,其中进位为 0 或者 1。</span></span><br><span class="line"><span class="comment"> 若两数不同位,较短位补零,再加上进位。</span></span><br><span class="line"><span class="comment"> 这是一个链表结构,对链表进行的操作,注意预先指针和执行指针的用法</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"> <span class="comment">//定义预先指针和执行指针</span></span><br><span class="line"> ListNode pre = <span class="keyword">new</span> ListNode(<span class="number">0</span>);</span><br><span class="line"> ListNode cur = pre;</span><br><span class="line"></span><br><span class="line"> <span class="comment">//默认进位是0</span></span><br><span class="line"> <span class="keyword">int</span> pos = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">//进行遍历</span></span><br><span class="line"> <span class="keyword">while</span>(l1!=<span class="keyword">null</span> || l2!=<span class="keyword">null</span>){</span><br><span class="line"> <span class="comment">//数字相加,如果这样子的话,会遇到数字+空指针的情况,报出空指针异常。</span></span><br><span class="line"> <span class="comment">// int sum = l1.val+l2.val;</span></span><br><span class="line"> <span class="keyword">int</span> x = l1==<span class="keyword">null</span> ? <span class="number">0</span> : l1.val;</span><br><span class="line"> <span class="keyword">int</span> y = l2==<span class="keyword">null</span> ? <span class="number">0</span> : l2.val;</span><br><span class="line"> <span class="keyword">int</span> sum = x+y + pos; </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="comment">//确定进位</span></span><br><span class="line"> <span class="comment">// int pos = sum / 10;</span></span><br><span class="line"> <span class="comment">//确定结果数字!!! 记住这里要进位啊</span></span><br><span class="line"> <span class="keyword">int</span> result = sum % <span class="number">10</span> ;</span><br><span class="line"></span><br><span class="line"> <span class="comment">//这里再次确认进位,为下一个服务</span></span><br><span class="line"> pos = sum / <span class="number">10</span>;</span><br><span class="line"> <span class="comment">// cur.val = result;这里会报空指针异常!!!原因:你都没有定义节点啊</span></span><br><span class="line"> cur.next = <span class="keyword">new</span> ListNode(result);</span><br><span class="line"> <span class="comment">//指针下移</span></span><br><span class="line"> cur = cur.next;</span><br><span class="line"></span><br><span class="line"> <span class="comment">//判断l1 和 l2 指针情况</span></span><br><span class="line"> <span class="keyword">if</span>(l1!=<span class="keyword">null</span>){</span><br><span class="line"> l1 = l1.next;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">if</span>(l2!=<span class="keyword">null</span>){</span><br><span class="line"> l2 = l2.next;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//如果最后一个进位为 那么还要再创建一个结尾节点</span></span><br><span class="line"> <span class="keyword">if</span>(pos ==<span class="number">1</span>){</span><br><span class="line"> cur.next = <span class="keyword">new</span> ListNode(<span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="comment">//返回链表节点</span></span><br><span class="line"> <span class="keyword">return</span> pre.next;</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// return null;</span></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>这是别人的优化代码:</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">class Solution {</span><br><span class="line"> public ListNode addTwoNumbers(ListNode l1, ListNode l2) {</span><br><span class="line"> ListNode head = new ListNode(l1.val + l2.val);</span><br><span class="line"> ListNode cur = head;</span><br><span class="line"> while(l1.next != null || l2.next != null){</span><br><span class="line"> l1 = l1.next != null ? l1.next : new ListNode();</span><br><span class="line"> l2 = l2.next != null ? l2.next : new ListNode();</span><br><span class="line"> cur.next = new ListNode(l1.val + l2.val + cur.val / 10);</span><br><span class="line"> cur.val %= 10;</span><br><span class="line"> cur = cur.next;</span><br><span class="line"> }</span><br><span class="line"> if(cur.val >= 10){</span><br><span class="line"> cur.next = new ListNode(cur.val / 10);</span><br><span class="line"> cur.val %= 10;</span><br><span class="line"> }</span><br><span class="line"> return head;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><h2 id="3-无重复字符的最长字串(2021-4-10)"><a href="#3-无重复字符的最长字串(2021-4-10)" class="headerlink" title="3.无重复字符的最长字串(2021.4.10)"></a>3.无重复字符的最长字串(2021.4.10)</h2><p>给定一个字符串,请你找出其中不含有重复字符的 <strong>最长子串</strong> 的长度。</p><p>示例 1:</p><p>输入: s = “abcabcbb”<br>输出: 3<br>解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。<br>示例 2:</p><p>输入: s = “bbbbb”<br>输出: 1<br>解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。<br>示例 3:</p><p>输入: s = “pwwkew”<br>输出: 3<br>解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。<br> 请注意,你的答案必须是 子串 的长度,”pwke” 是一个子序列,不是子串。<br>示例 4:</p><p>输入: s = “”<br>输出: 0</p><p>提示:</p><p>0 <= s.length <= 5 * 104<br>s 由英文字母、数字、符号和空格组成<br>通过次数931,090提交次数2,525,077</p><p><strong>分析:本题考察的是滑动窗口。再进行最大子串的寻找过程中,通过滑动左指针,并且不断比较maxlen。</strong></p><p><strong>错误解法:</strong></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">class Solution {</span><br><span class="line"> public int lengthOfLongestSubstring(String s) {</span><br><span class="line"> </span><br><span class="line"> // count[0] = 0;</span><br><span class="line"> int len = s.length();</span><br><span class="line"> //定义计数数组</span><br><span class="line"> int[] count = new int[len];</span><br><span class="line"> if(s == ""){</span><br><span class="line"> return count[0];</span><br><span class="line"> }else{</span><br><span class="line"> int temp = 0;</span><br><span class="line"> for(int j = 0; j < 10;j++){</span><br><span class="line"> HashMap<Character ,Integer> hashmap = new HashMap<>();</span><br><span class="line"> for(int i = temp ; i < len; i++){</span><br><span class="line"> if(hashmap.containsKey(s.charAt(i))){</span><br><span class="line"> break;</span><br><span class="line"> }else{</span><br><span class="line"> hashmap.put(s.charAt(i),i);</span><br><span class="line"> count[j]++;</span><br><span class="line"> temp++;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> int max = 0;</span><br><span class="line"> for(int x =0; x<len;x++){ </span><br><span class="line"> if(max<count[x]){</span><br><span class="line"> max = count[x];</span><br><span class="line"> } </span><br><span class="line"> }</span><br><span class="line"> return max;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>若测试案例是 dvdf,则输出结果为2,实际结果是3。因为上面的解法中,没有进行窗口的滑动(将左指针滑到已经出现字符的下一个上),而是进行指针的跳跃,将指针跳跃到重复字符的位置,这种思路是不对的,不满足题目的要求。</strong></p><h4 id="解法2:-滑动窗口法"><a href="#解法2:-滑动窗口法" class="headerlink" title="解法2: 滑动窗口法"></a>解法2: 滑动窗口法</h4><p><strong>正确思路:</strong></p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">lengthOfLongestSubstring</span><span class="params">(String s)</span> </span>{</span><br><span class="line"> <span class="comment">//定义最大的字串长度</span></span><br><span class="line"> <span class="keyword">int</span> maxlen = <span class="number">0</span>;</span><br><span class="line"> <span class="comment">//初始的左值针为 0</span></span><br><span class="line"> <span class="keyword">int</span> left= <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">int</span> len = s.length();</span><br><span class="line"> HashMap<Character, Integer> hashMap = <span class="keyword">new</span> HashMap<>();</span><br><span class="line"> <span class="comment">//进行遍历</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> right = <span class="number">0</span>; right <len ; right++) {</span><br><span class="line"> <span class="keyword">if</span>(hashMap.containsKey(s.charAt(right))){</span><br><span class="line"> <span class="comment">//如果哈希表中包含该字符,更新左值针</span></span><br><span class="line"> <span class="comment">//为何要使用Math.max函数,取left和hashMap.get(s.charAt(right))+1,其中</span></span><br><span class="line"><span class="comment">// hashMap.get(s.charAt(right))+1代表在之前出现的字符向后滑动一格。</span></span><br><span class="line"> <span class="comment">//情况一:在用例为abba中,right=2时,left更新为2,当right=3(为字符a时),left更新为1,明显错误</span></span><br><span class="line"> left = Math.max(left,hashMap.get(s.charAt(right))+<span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//否则存入哈希表中</span></span><br><span class="line"> hashMap.put(s.charAt(right),right);</span><br><span class="line"> <span class="comment">//更新最大字串长度</span></span><br><span class="line"> maxlen = Math.max(maxlen,right-left+<span class="number">1</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> maxlen;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>注意:这才是滑动窗口!!!</p><h2 id="数组模块"><a href="#数组模块" class="headerlink" title="数组模块"></a>数组模块</h2><h3 id="题号35:-搜索插入位置2021-4-24"><a href="#题号35:-搜索插入位置2021-4-24" class="headerlink" title="题号35: 搜索插入位置2021/4/24"></a>题号35: 搜索插入位置2021/4/24</h3><p>给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。</p><p>你可以假设数组中无重复元素。</p><p>示例 1:</p><p>输入: [1,3,5,6], 5<br>输出: 2<br>示例 2:</p><p>输入: [1,3,5,6], 2<br>输出: 1<br>示例 3:</p><p>输入: [1,3,5,6], 7<br>输出: 4<br>示例 4:</p><p>输入: [1,3,5,6], 0<br>输出: 0</p><p>解法1:暴力解法(时间复杂度 o(n))</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">searchInsert</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target)</span> </span>{</span><br><span class="line"> <span class="comment">//获取数组长度</span></span><br><span class="line"> <span class="keyword">int</span> len = nums.length;</span><br><span class="line"> <span class="comment">//遍历,判断是否有元素存在</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>;i<len;i++){</span><br><span class="line"> <span class="keyword">if</span>(target==nums[i]){</span><br><span class="line"> <span class="comment">//若有直接返回下标</span></span><br><span class="line"> <span class="keyword">return</span> i;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//若元素不存在,判断首位位置</span></span><br><span class="line"> <span class="keyword">if</span>(target<nums[<span class="number">0</span>]){</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }<span class="keyword">else</span> <span class="keyword">if</span>(target>nums[len-<span class="number">1</span>]){</span><br><span class="line"> <span class="keyword">return</span> len;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span>{</span><br><span class="line"> <span class="comment">//接着判断中间位置</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j<len-<span class="number">1</span>;j++){</span><br><span class="line"> <span class="keyword">if</span>(target>nums[j]&&target<nums[j+<span class="number">1</span>]){</span><br><span class="line"> <span class="keyword">return</span> j+<span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><h4 id="解法2:二分查找"><a href="#解法2:二分查找" class="headerlink" title="解法2:二分查找"></a><strong>解法2:二分查找</strong></h4><p><strong>(时间复杂度 o(logn))</strong></p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">searchInsert</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> target)</span> </span>{</span><br><span class="line"> <span class="comment">//左指针和右指针</span></span><br><span class="line"> <span class="keyword">int</span> left = <span class="number">0</span>, right = nums.length - <span class="number">1</span>;</span><br><span class="line"> <span class="comment">//边界条件是 左《= 右</span></span><br><span class="line"> <span class="keyword">while</span>(left <= right) {</span><br><span class="line"> <span class="comment">//二分值</span></span><br><span class="line"> <span class="keyword">int</span> mid = (left + right) / <span class="number">2</span>;</span><br><span class="line"> <span class="keyword">if</span>(nums[mid] == target) {</span><br><span class="line"> <span class="comment">//元素相同,返回下标</span></span><br><span class="line"> <span class="keyword">return</span> mid;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span>(nums[mid] < target) {</span><br><span class="line"> <span class="comment">//说明target在右侧</span></span><br><span class="line"> left = mid + <span class="number">1</span>;</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="comment">//说明target在左侧</span></span><br><span class="line"> right = mid - <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//这里选用left是因为 left破坏了循坏条件</span></span><br><span class="line"> <span class="comment">//也可以return right + 1;(right+1==left)</span></span><br><span class="line"> <span class="keyword">return</span> left;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><em><u><strong>思考:什么时候用二分法?二分法有其他变种嘛?</strong></u></em></p><p>ans:</p><p>二分法的使用必须在数组有序的情况下使用,只有数组有序,那么target值才知道是在左边还是在右边,实现的业务逻辑就是依靠left和right指针。时间复杂度:O(logn),空间复杂度:O(1)</p><p>二分法有三个变种,主要是看target值得区间范围。</p><p><strong>变种一:target在【left,right】</strong></p><p><strong>「思考为什么要写while(left <= right), 为什么要写right = middle - 1」</strong>。</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> n = nums.size();</span><br><span class="line"><span class="keyword">int</span> left = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> right = n - <span class="number">1</span>; <span class="comment">// 定义target在左闭右闭的区间里,[left, right] </span></span><br><span class="line"><span class="keyword">while</span> (left <= right) { <span class="comment">// 当left==right,区间[left, right]依然有效</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">int</span> middle = left + ((right - left) / <span class="number">2</span>);<span class="comment">// 防止溢出 等同于(left + right)/2</span></span><br><span class="line"> <span class="keyword">if</span> (nums[middle] > target) {</span><br><span class="line"> right = middle - <span class="number">1</span>; <span class="comment">// target 在左区间,所以[left, middle - 1]</span></span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (nums[middle] < target) {</span><br><span class="line"> left = middle + <span class="number">1</span>; <span class="comment">// target 在右区间,所以[middle + 1, right]</span></span><br><span class="line"> } <span class="keyword">else</span> { <span class="comment">// nums[middle] == target</span></span><br><span class="line"> <span class="keyword">return</span> middle;</span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></tbody></table></figure><p><strong>变种二:target在【left,right)</strong></p><p><strong>「思考为什么要写while (left < right), 为什么要写right = middle」</strong>。</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> n = nums.size();</span><br><span class="line"><span class="keyword">int</span> left = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> right = n; <span class="comment">// 定义target在左闭右开的区间里,[left, right) target</span></span><br><span class="line"><span class="keyword">while</span> (left < right) { <span class="comment">// 因为left == right的时候,在[left, right)是无效的空间</span></span><br><span class="line"> <span class="keyword">int</span> middle = left + ((right - left) >> <span class="number">1</span>);</span><br><span class="line"> <span class="keyword">if</span> (nums[middle] > target) {</span><br><span class="line"> right = middle; <span class="comment">// target 在左区间,在[left, middle)中</span></span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (nums[middle] < target) {</span><br><span class="line"> left = middle + <span class="number">1</span>; <span class="comment">// target 在右区间,在 [middle+1, right)中</span></span><br><span class="line"> } <span class="keyword">else</span> { <span class="comment">// nums[middle] == target</span></span><br><span class="line"> <span class="keyword">return</span> middle; <span class="comment">// 数组中找到目标值的情况,直接返回下标</span></span><br><span class="line"> }</span><br><span class="line"> }</span><br></pre></td></tr></tbody></table></figure><p><a href="https://leetcode-cn.com/problems/binary-search/solution/er-fen-cha-zhao-de-xun-huan-bu-bian-liang-zhi-yao-/">参考解答</a></p><h3 id="题号27:-移除元素2021-4-24"><a href="#题号27:-移除元素2021-4-24" class="headerlink" title="题号27: 移除元素2021/4/24"></a>题号27: 移除元素2021/4/24</h3><p>给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。</p><p>不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。</p><p>元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。</p><p>说明:</p><p>为什么返回数值是整数,但输出的答案是数组呢?</p><p>请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。</p><p>你可以想象内部操作如下:</p><p>// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝<br>int len = removeElement(nums, val);</p><p>// 在函数里修改输入数组对于调用者是可见的。<br>// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。<br>for (int i = 0; i < len; i++) {<br> print(nums[i]);<br>}</p><p>示例 1:</p><p>输入:nums = [3,2,2,3], val = 3<br>输出:2, nums = [2,2]<br>解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。<br>示例 2:</p><p>输入:nums = [0,1,2,2,3,0,4,2], val = 2<br>输出:5, nums = [0,1,4,0,3]<br>解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。</p><p><strong>解法1:暴力解法</strong></p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">removeElement</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> val)</span> </span>{</span><br><span class="line"> <span class="comment">//定义长度</span></span><br><span class="line"> <span class="keyword">int</span> len = nums.length;</span><br><span class="line"> <span class="comment">//计数器:定义删除得个数</span></span><br><span class="line"> <span class="keyword">int</span> count = <span class="number">0</span>;</span><br><span class="line"> <span class="comment">//遍历</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i<len;i++){</span><br><span class="line"> <span class="comment">//如果等于val,那么进行数组移除操作</span></span><br><span class="line"> <span class="keyword">if</span>(nums[i]==val){</span><br><span class="line"> <span class="comment">//移除操作</span></span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = i ; j<len-<span class="number">1</span>;j++){</span><br><span class="line"> nums[j]=nums[j+<span class="number">1</span>];</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//后面补位-1,因为测试val都是正数</span></span><br><span class="line"> nums[len-<span class="number">1</span>]=-<span class="number">1</span>;</span><br><span class="line"> <span class="comment">//计数器加一</span></span><br><span class="line"> count++;</span><br><span class="line"> <span class="comment">//找到一个后,那么i要减少一,因为后面不上来啦!</span></span><br><span class="line"> i--;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//最终数组得长度</span></span><br><span class="line"> <span class="keyword">return</span> len - count;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>代码优化:</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">先设定变量 idx,指向待插入位置。idx 初始值为 <span class="number">0</span></span><br><span class="line">然后从题目的「要求/保留逻辑」出发,来决定当遍历到任意元素 x 时,应该做何种决策:</span><br><span class="line">如果当前元素 x 与移除元素 val 相同,那么跳过该元素。</span><br><span class="line">如果当前元素 x 与移除元素 val 不同,那么我们将其放到下标 idx 的位置,并让 idx 自增右移。</span><br><span class="line">最终得到的 idx 即是答案</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">removeElement</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> val)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> idx = <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> x : nums) {</span><br><span class="line"> <span class="keyword">if</span> (x != val) </span><br><span class="line"> <span class="comment">//这一步相当于重置nums数组了</span></span><br><span class="line"> nums[idx++] = x;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> idx;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><h4 id="解法2:-双指针法"><a href="#解法2:-双指针法" class="headerlink" title="解法2: 双指针法"></a>解法2: 双指针法</h4><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">removeElement</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> val)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> j = nums.length - <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i <= j; i++) {</span><br><span class="line"> <span class="keyword">if</span> (nums[i] == val) {</span><br><span class="line"> <span class="comment">//遇到相同元素进行交换</span></span><br><span class="line"> swap(nums, i--, j--);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> j + <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span>[] nums, <span class="keyword">int</span> i, <span class="keyword">int</span> j)</span> </span>{</span><br><span class="line"> <span class="keyword">int</span> tmp = nums[i];</span><br><span class="line"> nums[i] = nums[j];</span><br><span class="line"> nums[j] = tmp;</span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><h3 id="题号209:-长度最小的子数组2021-4-24"><a href="#题号209:-长度最小的子数组2021-4-24" class="headerlink" title="题号209: 长度最小的子数组2021/4/24"></a>题号209: 长度最小的子数组2021/4/24</h3><p>给定一个含有 n 个正整数的数组和一个正整数 target 。</p><p>找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。</p><p>示例 1:</p><p>输入:target = 7, nums = [2,3,1,2,4,3]<br>输出:2<br>解释:子数组 [4,3] 是该条件下的长度最小的子数组。<br>示例 2:</p><p>输入:target = 4, nums = [1,4,4]<br>输出:1<br>示例 3:</p><p>输入:target = 11, nums = [1,1,1,1,1,1,1,1]<br>输出:0</p><p><strong>解法1 :暴力解法</strong></p><p>首先最容易想到的是暴力求解,使用两个 for 循环,一个 for 循环固定一个数字比如 m,另一个 for 循环从 m 的下一个元素开始累加,当和大于等于 s 的时候终止内层循环,顺便记录下最小长度</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">minSubArrayLen</span><span class="params">(<span class="keyword">int</span> s, <span class="keyword">int</span>[] nums)</span> </span>{</span><br><span class="line"> <span class="comment">//先定义min值是一个无穷大</span></span><br><span class="line"> <span class="keyword">int</span> min = Integer.MAX_VALUE;</span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i < nums.length; i++) {</span><br><span class="line"> <span class="comment">//第一层循环,每次循环都重复定义sum</span></span><br><span class="line"> <span class="keyword">int</span> sum = nums[i];</span><br><span class="line"> <span class="comment">//发现 单次值>s,直接return 1;</span></span><br><span class="line"> <span class="keyword">if</span> (sum >= s)</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> <span class="comment">//进入二层循环</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> j = i + <span class="number">1</span>; j < nums.length; j++) {</span><br><span class="line"> <span class="comment">//sum进行累加</span></span><br><span class="line"> sum += nums[j];</span><br><span class="line"> <span class="comment">//发现sum值》s,记录最小得累加个数,注意,这里累加个数为 j-i+1</span></span><br><span class="line"> <span class="comment">//如何每次都记录号最小得值???别在用数组进行储存和判断了!!!</span></span><br><span class="line"> <span class="comment">//直接使用Math.min(a,b)或者Math.max(a,b)</span></span><br><span class="line"> <span class="keyword">if</span> (sum >= s) {</span><br><span class="line"> min = Math.min(min, j - i + <span class="number">1</span>);</span><br><span class="line"> <span class="comment">//找到min值,就可以跳出二层循环了</span></span><br><span class="line"> <span class="keyword">break</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//判断min值是不是原始得,若是,说明总的sum值一直都没有超过s,自然个数就是return 0;</span></span><br><span class="line"> <span class="keyword">return</span> min == Integer.MAX_VALUE ? <span class="number">0</span> : min;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>心得:这个暴力解法,挺看考察逻辑的!!!有两个技巧要学习下</p><p>一、每次循环重复定义</p><p>二、寻找最小值,用Math函数得min和max方法</p><h4 id="解法2:滑动窗口法"><a href="#解法2:滑动窗口法" class="headerlink" title="解法2:滑动窗口法"></a>解法2:滑动窗口法</h4><p>我们把数组中的元素不停的入队,直到总和大于等于 s 为止,接着记录下队列中元素的个数,然后再不停的出队,直到队列中元素的和小于 s 为止(如果不小于 s,也要记录下队列中元素的个数,这个个数其实就是不小于 s 的连续子数组长度,我们要记录最小的即可)。接着再把数组中的元素添加到队列中……重复上面的操作,直到数组中的元素全部使用完为止。<br>这里以 [2,3,1,2,4,3] 举例画个图来看下</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/10ca012c2f0170afcac5e5996add20c32c36a82f4bb4a6187897bb948ace5fe2-image.png" alt="image.png"></p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/2da8cf86a2a3df3c95ed7d95add574dca2d8bae8420addd0fa6b8c55fa3db081-image.png" alt="image.png"></p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/ca74b1a2ad0eb5a4ebf8647a332161b140c8ebdd71cd5d19bef16e9bf0a43c95-image.png" alt="image.png"></p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/72f39b5cd7eb5f866e24d0a31eb3eac7d57cf3ad202ad40d30f103833c1f5a69-image.png" alt="image.png"></p><p>上面画的是使用队列,但在代码中我们不直接使用队列,我们使用两个指针,一个指向队头一个指向队尾,我们来看下代码</p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">minSubArrayLen</span><span class="params">(<span class="keyword">int</span> s, <span class="keyword">int</span>[] nums)</span> </span>{</span><br><span class="line"> <span class="comment">//lo为队列头 li为队列尾</span></span><br><span class="line"> <span class="keyword">int</span> lo = <span class="number">0</span>, hi = <span class="number">0</span>, sum = <span class="number">0</span>, min = Integer.MAX_VALUE;</span><br><span class="line"> <span class="keyword">while</span> (hi < nums.length) {</span><br><span class="line"> sum += nums[hi++];</span><br><span class="line"> <span class="keyword">while</span> (sum >= s) {</span><br><span class="line"> min = Math.min(min, hi - lo);</span><br><span class="line"> sum -= nums[lo++];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> min == Integer.MAX_VALUE ? <span class="number">0</span> : min;</span><br><span class="line"> }</span><br><span class="line"><span class="comment">//用双指针来实现滑动窗口和队列进出得数据结构</span></span><br><span class="line"></span><br></pre></td></tr></tbody></table></figure><p>心得:我觉得这道题核心在于,理解滑动窗口得意义,一个窗口(连续得数组,不停得滑动),滑动过程中如何记录我们想要得数据?(用math的min和max函数!!!)</p><h3 id="题号59:螺旋矩阵-II"><a href="#题号59:螺旋矩阵-II" class="headerlink" title="题号59:螺旋矩阵 II"></a>题号59:螺旋矩阵 II</h3><h4 id="解法1-模拟法"><a href="#解法1-模拟法" class="headerlink" title="解法1 模拟法"></a>解法1 模拟法</h4><p>思路:<br>生成一个 n×n 空矩阵 mat,随后模拟整个向内环绕的填入过程:<br>定义当前左右上下边界 l,r,t,b,初始值 num = 1,迭代终止值 tar = n * n;<br>当 num <= tar 时,始终按照 从左到右 从上到下 从右到左 从下到上 填入顺序循环,每次填入后:<br>执行 num += 1:得到下一个需要填入的数字;<br>更新边界:例如从左到右填完后,上边界 t += 1,相当于上边界向内缩 1。<br>使用num <= tar而不是l < r || t < b作为迭代条件,是为了解决当n为奇数时,矩阵中心数字无法在迭代过程中被填充的问题。<br>最终返回 mat 即可。</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/ccff416fa39887c938d36fec8e490e1861813d3bba7836eda941426f13420759-Picture1.png" alt="Picture1.png"></p><figure class="highlight java"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>{</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">int</span>[][] generateMatrix(<span class="keyword">int</span> n) {</span><br><span class="line"> <span class="comment">//模拟法的应用:如何解决边界条件</span></span><br><span class="line"> <span class="comment">//四个边界</span></span><br><span class="line"> <span class="keyword">int</span> l=<span class="number">0</span>,r=n-<span class="number">1</span>,t=<span class="number">0</span>,b=n-<span class="number">1</span>;</span><br><span class="line"> <span class="comment">//count存放数值</span></span><br><span class="line"> <span class="keyword">int</span> count = <span class="number">1</span>,val = n*n;</span><br><span class="line"> <span class="comment">//这是结果数组</span></span><br><span class="line"> <span class="keyword">int</span> [][] res = <span class="keyword">new</span> <span class="keyword">int</span> [n][n];</span><br><span class="line"> <span class="keyword">while</span>(count<=val){</span><br><span class="line"> <span class="comment">//从左往右</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = l; i <= r ; i++) {</span><br><span class="line"> res[t][i] = count++;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//top边界要下移</span></span><br><span class="line"> t++;</span><br><span class="line"></span><br><span class="line"> <span class="comment">//从上往下</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = t; i <= b ; i++) {</span><br><span class="line"> res[i][r] = count++;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//right边界要左移</span></span><br><span class="line"> r--;</span><br><span class="line"></span><br><span class="line"> <span class="comment">//从右往左</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = r; i >= l ; i--) {</span><br><span class="line"> res[b][i] = count++;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//bottom边界要上移</span></span><br><span class="line"> b--;</span><br><span class="line"> <span class="comment">//从下往上</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> i = b; i >= t ; i--) {</span><br><span class="line"> res[i][l] = count++;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//left边界要右移</span></span><br><span class="line"> l++;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><h1 id="注意前方高能!!!接下来的算法学习思路为:"><a href="#注意前方高能!!!接下来的算法学习思路为:" class="headerlink" title="注意前方高能!!!接下来的算法学习思路为:"></a>注意前方高能!!!接下来的算法学习思路为:</h1><p>1.b栈小姐姐手把手教你刷leetcode视频(主要是学会常用算法套路)</p><p>2.leetcode按照模块tag刷题,同时配合代码随想录中的由易到难的路线</p><p>3.图解算法看一下,了解算法有哪些,重点怎么学习!!!</p>]]></content>
</entry>
<entry>
<title>Git初级学习笔记</title>
<link href="/javanblog/2021/04/23/Git%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"/>
<url>/javanblog/2021/04/23/Git%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/</url>
<content type="html"><![CDATA[<h1 id="基础知识"><a href="#基础知识" class="headerlink" title="基础知识"></a>基础知识</h1><h2 id="什么是版本控制"><a href="#什么是版本控制" class="headerlink" title="什么是版本控制"></a>什么是版本控制</h2><p>版本控制(Revision control)是一种在开发的过程中用于管理我们对文件、目录或工程等内容的修改历史,方便查看更改历史记录,备份以便恢复以前的版本的软件工程技术。</p><ul><li>实现跨区域多人协同开发</li><li>追踪和记载一个或者多个文件的历史记录</li><li>组织和保护你的源代码和文档</li><li>统计工作量</li><li>并行开发、提高开发效率</li><li>跟踪记录整个软件的开发过程</li><li>减轻开发人员的负担,节省时间,同时降低人为错误</li></ul><p><strong>简单说就是用于管理多人协同开发项目的技术</strong>。</p><p>没有进行版本控制或者版本控制本身缺乏正确的流程管理,在软件开发过程中将会引入很多问题,如软件代码的一致性、软件内容的冗余、软件过程的事物性、软件开发过程中的并发性、软件源代码的安全性,以及软件的整合等问题。</p><p>无论是工作还是学习,或者是自己做笔记,都经历过这样一个阶段!我们就迫切需要一个版本控制工具!</p><p><strong>多人开发就必须要使用版本控制</strong>!</p><h2 id="常见的版本控制工具"><a href="#常见的版本控制工具" class="headerlink" title="常见的版本控制工具"></a>常见的版本控制工具</h2><p>我们学习的东西,一定是当下最流行的!</p><p>主流的版本控制器有如下这些:</p><ul><li><strong>Git</strong></li><li><strong>SVN</strong>(Subversion)</li><li><strong>CVS</strong>(Concurrent Versions System)</li><li><strong>VSS</strong>(Micorosoft Visual SourceSafe)</li><li><strong>TFS</strong>(Team Foundation Server)</li><li>Visual Studio Online</li></ul><p>版本控制产品非常的多(Perforce、Rational ClearCase、RCS(GNU Revision Control System)、Serena Dimention、SVK、BitKeeper、Monotone、Bazaar、Mercurial、SourceGear Vault),现在影响力最大且使用最广泛的是Git与SVN</p><h2 id="版本控制分类"><a href="#版本控制分类" class="headerlink" title="版本控制分类"></a>版本控制分类</h2><p><strong>1、本地版本控制</strong></p><p>记录文件每次的更新,可以对每个版本做一个快照,或是记录补丁文件,适合个人用,如RCS。</p><p><strong>2、集中版本控制 SVN</strong></p><p>所有的版本数据都保存在服务器上,协同开发者从服务器上同步更新或上传自己的修改</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423185757483.png" alt="image-20210423185757483"></p><p>所有的版本数据都存在服务器上,用户的本地只有自己以前所同步的版本,如果不连网的话,用户就看不到历史版本,也无法切换版本验证问题,或在不同分支工作。而且,所有数据都保存在单一的服务器上,有很大的风险这个服务器会损坏,这样就会丢失所有的数据,当然可以定期备份。代表产品:SVN、CVS、VSS</p><p><strong>3、分布式版本控制 Git</strong></p><p><strong>每个人都拥有全部的代码!安全隐患!</strong></p><p>所有版本信息仓库全部同步到本地的每个用户,这样就可以在本地查看所有版本历史,可以离线在本地提交,只需在连网时push到相应的服务器或其他用户那里。由于每个用户那里保存的都是所有的版本数据,只要有一个用户的设备没有问题就可以恢复所有的数据,但这增加了本地存储空间的占用。</p><p>不会因为服务器损坏或者网络问题,造成不能工作的情况!</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423185932075.png" alt="image-20210423185932075"></p><h2 id="Git与SVN的主要区别"><a href="#Git与SVN的主要区别" class="headerlink" title="Git与SVN的主要区别"></a>Git与SVN的主要区别</h2><p>SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而工作的时候,用的都是自己的电脑,所以首先要从中央服务器得到最新的版本,然后工作,完成工作后,需要把自己做完的活推送到中央服务器。集中式版本控制系统是必须联网才能工作,对网络带宽要求较高。</p><p><strong><u>Git是分布式版本控制系统,没有中央服务器,每个人的电脑就是一个完整的版本库,工作的时候不需要联网了,因为版本都在自己电脑上。</u></strong></p><p>协同的方法是这样的:比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。</p><p><u><strong>Git可以直接看到更新了哪些代码和文件!</strong></u></p><p><strong>Git是目前世界上最先进的分布式版本控制系统。</strong></p><h2 id="Git环境配置"><a href="#Git环境配置" class="headerlink" title="Git环境配置"></a>Git环境配置</h2><p><strong>软件下载</strong></p><p>打开 [git官网] <a href="https://git-scm.com/%EF%BC%8C%E4%B8%8B%E8%BD%BDgit%E5%AF%B9%E5%BA%94%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F%E7%9A%84%E7%89%88%E6%9C%AC%E3%80%82">https://git-scm.com/,下载git对应操作系统的版本。</a></p><p>所有东西下载慢的话就可以去找镜像!</p><p>官网下载太慢,我们可以使用淘宝镜像下载:<a href="http://npm.taobao.org/mirrors/git-for-windows/">http://npm.taobao.org/mirrors/git-for-windows/</a></p><p>下载对应的版本即可安装!</p><p>安装:无脑下一步即可!安装完毕就可以使用了!</p><p><strong>启动Git</strong></p><p>安装成功后在开始菜单中会有Git项,菜单下有3个程序:任意文件夹下右键也可以看到对应的程序!</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190002507.png" alt="image-20210423190002507"></p><p><strong>Git Bash:</strong>Unix与Linux风格的命令行,使用最多,推荐最多</p><p><strong>Git CMD:</strong>Windows风格的命令行</p><p><strong>Git GUI</strong>:图形界面的Git,不建议初学者使用,尽量先熟悉常用命令</p><p> <strong>常用的Linux命令</strong></p><p>平时一定要多使用这些基础的命令!</p><p>1)、cd : 改变目录。</p><p>2)、cd . . 回退到上一个目录,直接cd进入默认目录</p><p>3)、pwd : 显示当前所在的目录路径。</p><p>4)、ls(ll): 都是列出当前目录中的所有文件,只不过ll(两个ll)列出的内容更为详细。</p><p>5)、touch : 新建一个文件 如 touch index.js 就会在当前目录下新建一个index.js文件。</p><p>6)、rm: 删除一个文件, rm index.js 就会把index.js文件删除。</p><p>7)、mkdir: 新建一个目录,就是新建一个文件夹。</p><p>8)、rm -r : 删除一个文件夹, rm -r src 删除src目录</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rm -rf / 切勿在Linux中尝试!删除电脑中全部文件!</span><br></pre></td></tr></tbody></table></figure><p>9)、mv 移动文件, mv index.html src index.html 是我们要移动的文件, src 是目标文件夹,当然, 这样写,必须保证文件和目标文件夹在同一目录下。</p><p>10)、reset 重新初始化终端/清屏。</p><p>11)、clear 清屏。</p><p>12)、history 查看命令历史。</p><p>13)、help 帮助。</p><p>14)、exit 退出。</p><p>15)、#表示注释</p><h2 id="Git配置"><a href="#Git配置" class="headerlink" title="Git配置"></a>Git配置</h2><p>所有的配置文件,其实都保存在本地!</p><p>查看配置 git config -l</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190020971.png" alt="image-20210423190020971"></p><p>查看不同级别的配置文件:</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">#查看系统</span><br><span class="line">configgit config --system --list </span><br><span class="line">#查看当前用户(global)配置</span><br><span class="line">git config --global --list</span><br></pre></td></tr></tbody></table></figure><p><strong>Git相关的配置文件:</strong></p><p>1)、Git\etc\gitconfig :Git 安装目录下的 gitconfig –system 系统级</p><p>2)、C:\Users\Administrator\ .gitconfig 只适用于当前登录用户的配置 –global 全局</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190037846.png" alt="image-20210423190037846"></p><p>这里可以直接编辑配置文件,通过命令设置后会响应到这里。</p><p> <strong>设置用户名与邮箱(用户标识,必要)</strong></p><p>当你安装Git后首先要做的事情是设置你的用户名称和e-mail地址。这是非常重要的,因为每次Git提交都会使用该信息。它被永远的嵌入到了你的提交中:</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">git config --global user.name "kuangshen" </span><br><span class="line">#名称</span><br><span class="line">git config --global user.email 24736743@qq.com </span><br><span class="line">#邮箱</span><br></pre></td></tr></tbody></table></figure><p>只需要做一次这个设置,如果你传递了–global 选项,因为Git将总是会使用该信息来处理你在系统中所做的一切操作。如果你希望在一个特定的项目中使用不同的名称或e-mail地址,你可以在该项目中运行该命令而不要–global选项。总之–global为全局配置,不加为某个项目的特定配置。</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190051360.png" alt="image-20210423190051360"></p><h2 id="Git基本理论(重要)"><a href="#Git基本理论(重要)" class="headerlink" title="Git基本理论(重要)"></a><strong>Git基本理论(重要)</strong></h2><h2 id="三个区域"><a href="#三个区域" class="headerlink" title="三个区域"></a>三个区域</h2><p>Git本地有三个工作区域:工作目录(Working Directory)、暂存区(Stage/Index)、资源库(Repository或Git Directory)。如果在加上远程的git仓库(Remote Directory)就可以分为四个工作区域。文件在这四个区域之间的转换关系如下:</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190108878.png" alt="image-20210423190108878"></p><ul><li>Workspace:工作区,就是你平时存放项目代码的地方</li><li>Index / Stage:暂存区,用于临时存放你的改动,事实上它只是一个文件,保存即将提交到文件列表信息</li><li>Repository:仓库区(或本地仓库),就是安全存放数据的位置,这里面有你提交到所有版本的数据。其中HEAD指向最新放入仓库的版本</li><li>Remote:远程仓库,托管代码的服务器,可以简单的认为是你项目组中的一台电脑用于远程数据交换</li></ul><p><strong>本地的三个区域确切的说应该是git仓库中HEAD指向的版本:</strong></p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190126730.png" alt="image-20210423190126730"></p><ul><li>Directory:使用Git管理的一个目录,也就是一个仓库,包含我们的工作空间和Git的管理空间。</li><li>WorkSpace:需要通过Git进行版本控制的目录和文件,这些目录和文件组成了工作空间。</li><li>.git:存放Git管理信息的目录,初始化仓库的时候自动创建。</li><li>Index/Stage:暂存区,或者叫待提交更新区,在提交进入repo之前,我们可以把所有的更新放在暂存区。</li><li>Local Repo:本地仓库,一个存放在本地的版本库;HEAD会只是当前的开发分支(branch)。</li><li>Stash:隐藏,是一个工作状态保存栈,用于保存/恢复WorkSpace中的临时状态。</li></ul><h2 id="工作流程"><a href="#工作流程" class="headerlink" title="工作流程"></a>工作流程</h2><p>git的工作流程一般是这样的:</p><p>1、在工作目录中添加、修改文件;</p><p>2、将需要进行版本管理的文件放入暂存区域;</p><p>3、将暂存区域的文件提交到git仓库。</p><p>因此,git管理的文件有三种状态:已修改(modified),已暂存(staged),已提交(committed)</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190149356.png" alt="image-20210423190149356"></p><h2 id="Git项目搭建(重要)"><a href="#Git项目搭建(重要)" class="headerlink" title="Git项目搭建(重要)"></a>Git项目搭建(重要)</h2><p><strong>创建工作目录与常用指令</strong></p><p>工作目录(WorkSpace)一般就是你希望Git帮助你管理的文件夹,可以是你项目的目录,也可以是一个空目录,建议不要有中文。</p><p>日常使用只要记住下图6个命令:<img src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="图片"></p><p><strong>本地仓库搭建</strong></p><p>创建本地仓库的方法有两种:一种是创建全新的仓库,另一种是克隆远程仓库。</p><p>1、创建全新的仓库,需要用GIT管理的项目的根目录执行:</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 在当前目录新建一个Git代码库</span><br><span class="line">$ git init</span><br></pre></td></tr></tbody></table></figure><p>2、执行后可以看到,仅仅在项目目录多出了一个.git目录,关于版本等的所有信息都在这个目录里面。</p><p><strong>克隆远程仓库</strong></p><p>1、另一种方式是克隆远程目录,由于是将远程服务器上的仓库完全镜像一份至本地!</p><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 克隆一个项目和它的整个代码历史(版本信息)</span><br><span class="line">$ git clone [url] # https://gitee.com/kuangstudy/openclass.git</span><br></pre></td></tr></tbody></table></figure><p>2、去 gitee 或者 github 上克隆一个测试!</p><h2 id="Git文件操作"><a href="#Git文件操作" class="headerlink" title="Git文件操作"></a>Git文件操作</h2><p><strong>文件的四种状态</strong></p><p>版本控制就是对文件的版本控制,要对文件进行修改、提交等操作,首先要知道文件当前在什么状态,不然可能会提交了现在还不想提交的文件,或者要提交的文件没提交上。</p><ul><li>Untracked: 未跟踪, 此文件在文件夹中, 但并没有加入到git库, 不参与版本控制. 通过git add 状态变为Staged.</li><li>Unmodify: 文件已经入库, 未修改, 即版本库中的文件快照内容与文件夹中完全一致. 这种类型的文件有两种去处, 如果它被修改, 而变为Modified. 如果使用git rm移出版本库, 则成为Untracked文件</li><li>Modified: 文件已修改, 仅仅是修改, 并没有进行其他的操作. 这个文件也有两个去处, 通过git add可进入暂存staged状态, 使用git checkout 则丢弃修改过, 返回到unmodify状态, 这个git checkout即从库中取出文件, 覆盖当前修改 !</li><li>Staged: 暂存状态. 执行git commit则将修改同步到库中, 这时库中的文件和本地文件又变为一致, 文件为Unmodify状态. 执行git reset HEAD filename取消暂存, 文件状态为Modified</li></ul><p> <strong>查看文件状态</strong></p><p>上面说文件有4种状态,通过如下命令可以查看到文件的状态:</p><figure class="highlight"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">#查看指定文件状态git status [filename]</span><br><span class="line">#查看所有文件状态git status</span><br><span class="line"># git add . 添加所有文件到暂存区</span><br><span class="line"># git commit -m "消息内容" 提交暂存区中的内容到本地仓库 -m 提交信息</span><br></pre></td></tr></tbody></table></figure><p> <strong>忽略文件</strong></p><p>有些时候我们不想把某些文件纳入版本控制中,比如数据库文件,临时文件,设计文件等</p><p>在主目录下建立”.gitignore”文件,此文件有如下规则:</p><ol><li>忽略文件中的空行或以井号(#)开始的行将会被忽略。</li><li>可以使用Linux通配符。例如:星号(*)代表任意多个字符,问号(?)代表一个字符,方括号([abc])代表可选字符范围,大括号({string1,string2,…})代表可选的字符串等。</li><li>如果名称的最前面有一个感叹号(!),表示例外规则,将不被忽略。</li><li>如果名称的最前面是一个路径分隔符(/),表示要忽略的文件在此目录下,而子目录中的文件不忽略。</li><li>如果名称的最后面是一个路径分隔符(/),表示要忽略的是此目录下该名称的子目录,而非文件(默认文件或目录都忽略)。</li></ol><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">#为注释</span><br><span class="line">*.txt #忽略所有 .txt结尾的文件,这样的话上传就不会被选中!</span><br><span class="line">!lib.txt #但lib.txt除外</span><br><span class="line">/temp #仅忽略项目根目录下的TODO文件,不包括其它目录</span><br><span class="line">tempbuild/ #忽略build/目录下的所有文件</span><br><span class="line">doc/*.txt #会忽略 doc/notes.txt 但不包括 doc/server/arch.txt</span><br></pre></td></tr></tbody></table></figure><h2 id="使用码云"><a href="#使用码云" class="headerlink" title="使用码云"></a>使用码云</h2><p> <strong>github 是有墙的,比较慢,在国内的话,我们一般使用 gitee ,公司中有时候会搭建自己的gitlab服务器</strong></p><p>这个其实可以作为大家未来找工作的一个重要信息!</p><p>1、注册登录码云,完善个人信息</p><p>2、设置本机绑定SSH公钥,实现免密码登录!(免密码登录,这一步挺重要的,码云是远程仓库,我们是平时工作在本地仓库!)</p><h3 id="何谓公钥"><a href="#何谓公钥" class="headerlink" title="何谓公钥"></a>何谓公钥</h3><ul><li>1.很多服务器都是需要认证的,ssh认证是其中的一种。在客户端生成公钥,把生成的公钥添加到服务器,你以后连接服务器就不用每次都输入用户名和密码了。</li><li>2.很多git服务器都是用ssh认证方式,你需要把你生成的公钥发送给代码仓库管理员,让他给你添加到服务器上,你就可以通过ssh自由地拉取和提交代码了。</li></ul><h3 id="生成公钥"><a href="#生成公钥" class="headerlink" title="生成公钥"></a>生成公钥</h3><ul><li>1.如果通过上面的方式找不到公钥,你就需要先生成公钥了:ssh-keygen</li><li>2.接着会确认存放公钥的地址,默认就是上面说的路径,直接enter键确认</li><li>3.接着会要求输入密码和确认密码,如果不想设置密码直接不输入内容 按enter键</li></ul><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/20170802000320339.png" alt="图片"></p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190218610.png" alt="image-20210423190218610"></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># 进入 C:\Users\Administrator\.ssh 目录</span><br><span class="line"># 生成公钥</span><br><span class="line">ssh-keygen</span><br></pre></td></tr></tbody></table></figure><p>3、将公钥信息public key 添加到码云账户中即可!</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190231684.png" alt="image-20210423190231684"></p><p>4、使用码云创建一个自己的仓库!</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190250535.png" alt="image-20210423190250535"></p><p>许可证:开源是否可以随意转载,开源但是不能商业使用,不能转载,… 限制!</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190317420.png" alt="image-20210423190317420"></p><p>克隆到本地!</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190341956.png" alt="image-20210423190341956"></p><h2 id="IDEA中集成Git"><a href="#IDEA中集成Git" class="headerlink" title="IDEA中集成Git"></a>IDEA中集成Git</h2><p>1、新建项目,绑定git。</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190359280.png" alt="image-20210423190359280"></p><p>注意观察idea中的变化</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190417911.png" alt="image-20210423190417911"></p><p>2、修改文件,使用IDEA操作git。</p><ul><li>添加到暂存区</li><li>commit 提交</li><li>push到远程仓库</li></ul><p>3、提交测试</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190430177.png" alt="image-20210423190430177"></p><h2 id="GIT分支"><a href="#GIT分支" class="headerlink" title="GIT分支"></a>GIT分支</h2><p>分支在GIT中相对较难,分支就是科幻电影里面的平行宇宙,如果两个平行宇宙互不干扰,那对现在的你也没啥影响。不过,在某个时间点,两个平行宇宙合并了,我们就需要处理一些问题了!<img src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==" alt="图片"></p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190447935.png" alt="image-20210423190447935"></p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190620188.png" alt="image-20210423190620188"></p><p>git分支中常用指令:</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"># 列出所有本地分支</span><br><span class="line">git branch</span><br><span class="line"># 列出所有远程分支</span><br><span class="line">git branch -r</span><br><span class="line"># 新建一个分支,但依然停留在当前分支</span><br><span class="line">git branch [branch-name]</span><br><span class="line"># 新建一个分支,并切换到该分支</span><br><span class="line">git checkout -b [branch]</span><br><span class="line"># 合并指定分支到当前分支</span><br><span class="line">$ git merge [branch]</span><br><span class="line"># 删除分支</span><br><span class="line">$ git branch -d [branch-name]</span><br><span class="line"># 删除远程分支</span><br><span class="line">$ git push origin --delete [branch-name]$ git branch -dr [remote/branch]</span><br></pre></td></tr></tbody></table></figure><p>IDEA中操作</p><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/image-20210423190640173.png" alt="image-20210423190640173"></p><p>如果同一个文件在合并分支时都被修改了则会引起冲突:</p><p><u><strong>解决的办法是我们可以修改冲突文件后重新提交!选择要保留他的代码还是你的代码!</strong></u></p><p><em><strong>master主分支应该非常稳定,用来发布新版本,一般情况下不允许在上面工作,工作一般情况下在新建的dev分支上工作,工作完后,比如上要发布,或者说dev分支代码稳定后可以合并到主分支master上来。</strong></em></p><h1 id="进阶知识"><a href="#进阶知识" class="headerlink" title="进阶知识"></a>进阶知识</h1><h2 id="指令的总结"><a href="#指令的总结" class="headerlink" title="指令的总结"></a>指令的总结</h2><h3 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h3><pre><code>git --version</code></pre><h3 id="初始化配置"><a href="#初始化配置" class="headerlink" title="初始化配置"></a>初始化配置</h3><pre><code>git config --global user.name "damu"git config --global user.email damu@example.com git config --list</code></pre><h3 id="初始化仓库"><a href="#初始化仓库" class="headerlink" title="初始化仓库"></a>初始化仓库</h3><pre><code>git init</code></pre><h3 id="C-新增"><a href="#C-新增" class="headerlink" title="C(新增)"></a>C(新增)</h3><pre><code>在工作目录中新增文件git statusgit add ./git commit -m "msg" </code></pre><h3 id="U-修改"><a href="#U-修改" class="headerlink" title="U(修改)"></a>U(修改)</h3><pre><code>在工作目录中修改文件git statusgit add ./git commit -m "msg" </code></pre><h3 id="D-删除-amp-重命名"><a href="#D-删除-amp-重命名" class="headerlink" title="D(删除 & 重命名)"></a>D(删除 & 重命名)</h3><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git rm 要删除的文件 git mv 老文件 新文件</span><br><span class="line">git status git status</span><br><span class="line">git commit -m "msg" git commit -m "msg"</span><br></pre></td></tr></tbody></table></figure><h3 id="R-查询"><a href="#R-查询" class="headerlink" title="R(查询)"></a>R(查询)</h3> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">git status : 查看工作目录中文件的状态(已跟踪(已提交 已暂存 已修改) 未跟踪)</span><br><span class="line"> git diff : 查看未暂存的修改</span><br><span class="line"> git diff --cache : 查看未提交的暂存</span><br><span class="line"> git log --oneline : 查看提交记录</span><br></pre></td></tr></tbody></table></figure><h3 id="分支"><a href="#分支" class="headerlink" title="分支"></a>分支</h3><pre><code>分支的本质其实就是一个提交对象!!!HEAD: 是一个指针 它默认指向master分支 切换分支时其实就是让HEAD指向不同的分支 每次有新的提交时 HEAD都会带着当前指向的分支 一起往前移动git log --oneline --decorate --graph --all : 查看整个项目的分支图 git branch : 查看分支列表git branch -v: 查看分支指向的最新的提交git branch name : 在当前提交对象上创建新的分支git branch name commithash: 在指定的提交对象上创建新的分支git checkout name : 切换分支git branch -d name : 删除空的分支 删除已经被合并的分支git branch -D name : 强制删除分支 </code></pre><p> git分支本质</p><pre><code>分支本质是一个提交对象,所有的分支都会有机会被HEAD所引用(HEAD一个时刻只会指向一个分支)当我们有新的提交的时候 HEAD会携带当前持有的分支往前移动</code></pre><h3 id="git分支命令"><a href="#git分支命令" class="headerlink" title="git分支命令"></a>git分支命令</h3><pre><code>创建分支 : git branch branchname切换分支 : git checkout branchname创建&切换分支 : git checkout -b branchname版本穿梭(时光机) : git branch branchname commitHash 普通删除分支 : git branch -d branchname强制删除分支 : git branch -D branchname合并分支 : git merge branchname 快进合并 --> 不会产生冲突 典型合并 --> 有机会产生冲突 解决冲突 --> 打开冲突的文件 进行修改 add commit 查看分支列表 : git branch查看合并到当前分支的分支列表: git branch --merged 一旦出现在这个列表中 就应该删除查看没有合并到当前分支的分支列表: git branch --no-merged 一旦出现在这个列表中 就应该观察一下是否需要合并</code></pre><h3 id="git分支的注意点"><a href="#git分支的注意点" class="headerlink" title="git分支的注意点"></a>git分支的注意点</h3><pre><code>在切换的时候 一定要保证当前分支是干净的!!! 允许切换分支: 分支上所有的内容处于 已提交状态 (避免)分支上的内容是初始化创建 处于未跟踪状态 (避免)分支上的内容是初始化创建 第一次处于已暂存状态 不允许切分支: 分支上所有的内容处于 已修改状态 或 第二次以后的已暂存状态 在分支上的工作做到一半时 如果有切换分支的需求, 我们应该将现有的工作存储起来 git stash : 会将当前分支上的工作推到一个栈中 分支切换 进行其他工作 完成其他工作后 切回原分支 git stash apply : 将栈顶的工作内容还原 但不让任何内容出栈 git stash drop : 取出栈顶的工作内容后 就应该将其删除(出栈) git stash pop : git stash apply + git stash drop git stash list : 查看存储</code></pre><h3 id="后悔药"><a href="#后悔药" class="headerlink" title="后悔药"></a>后悔药</h3><pre><code>撤销工作目录的修改 : git checkout -- filename撤销暂存区的修改 : git reset HEAD filename撤销提交 : git commit --amend</code></pre><h3 id="reset三部曲"><a href="#reset三部曲" class="headerlink" title="reset三部曲"></a>reset三部曲</h3><pre><code>git reset --soft commithash ---> 用commithash的内容重置HEAD内容git reset [--mixed] commithash ---> 用commithash的内容重置HEAD内容 重置暂存区git reset --hard commithash ---> 用commithash的内容重置HEAD内容 重置暂存区 重置工作目录</code></pre><h3 id="路径reset"><a href="#路径reset" class="headerlink" title="路径reset"></a>路径reset</h3><pre><code>所有的路径reset都要省略第一步!!! 第一步是重置HEAD内容 我们知道HEAD本质指向一个分支 分支的本质是一个提交对象 提交对象 指向一个树对象 树对象又很有可能指向多个git对象 一个git对象代表一个文件!!! HEAD可以代表一系列文件的状态!!!!git reset [--mixed] commithash filename 用commithash中filename的内容重置暂存区</code></pre><h3 id="checkout深入理解"><a href="#checkout深入理解" class="headerlink" title="checkout深入理解"></a>checkout深入理解</h3><pre><code>git checkout brancname 跟 git reset --hard commithash特别像 共同点 都需要重置 HEAD 暂存区 工作目录 区别 checkout对工作目录是安全的 reset --hard是强制覆盖 checkout动HEAD时不会带着分支走而是切换分支 reset --hard时是带着分支走 checkout + 路径 git checkout commithash filename 重置暂存区 重置工作目录 git checkout -- filename 重置工作目录 </code></pre><p> 三个必须懂得概念</p><pre><code>本地分支远程跟踪分支(remote/分支名)远程分支</code></pre><h3 id="远程协作的基本流程"><a href="#远程协作的基本流程" class="headerlink" title="远程协作的基本流程"></a>远程协作的基本流程</h3><pre><code>第一步: 项目经理创建一个空的远程仓库第二步: 项目经理创建一个待推送的本地仓库第三步: 为远程仓库配别名 配完用户名 邮箱第四步: 在本地仓库中初始化代码 提交代码第五步: 推送第六步: 邀请成员第七步: 成员克隆远程仓库第八步: 成员做出修改第九步: 成员推送自己的修改第十步: 项目经理拉取成员的修改</code></pre><h3 id="做跟踪"><a href="#做跟踪" class="headerlink" title="做跟踪"></a>做跟踪</h3><pre><code>克隆才仓库时 会自动为master做跟踪本地没有分支 git checkout --track 远程跟踪分支(remote/分支名)本地已经创建了分支 git branch -u 远程跟踪分支(remote/分支名)</code></pre><h3 id="推送"><a href="#推送" class="headerlink" title="推送"></a>推送</h3><pre><code>git push</code></pre><h3 id="拉取"><a href="#拉取" class="headerlink" title="拉取"></a>拉取</h3><pre><code>git pull</code></pre><h3 id="pull-request"><a href="#pull-request" class="headerlink" title="pull request"></a>pull request</h3><pre><code>让第三方人员参与到项目中 fork</code></pre><h3 id="使用频率最高的五个命令"><a href="#使用频率最高的五个命令" class="headerlink" title="使用频率最高的五个命令"></a>使用频率最高的五个命令</h3><pre><code>git statusgit addgit commitgit pushgit pull </code></pre><p><img src="https://gitee.com/javan-lu/figure-bed/raw/master/img/11.jpg" alt="last"></p>]]></content>
</entry>
<entry>
<title>JavaWeb笔记</title>
<link href="/javanblog/2021/04/23/JavaWeb%E7%9F%A5%E8%AF%86%E7%82%B9%E6%80%BB%E7%BB%93/"/>
<url>/javanblog/2021/04/23/JavaWeb%E7%9F%A5%E8%AF%86%E7%82%B9%E6%80%BB%E7%BB%93/</url>
<content type="html"><![CDATA[<h1 id="JavaWeb知识点总结"><a href="#JavaWeb知识点总结" class="headerlink" title="JavaWeb知识点总结"></a>JavaWeb知识点总结</h1><p><strong>HTML、CSS、JS、JQuery、XML、Tomcat、Servlet程序、Filter过滤器、Listener监听器、jsp页面、EL表达式、JSTL标签库、Cookie技术、Session会话、JSON使用、Ajax请求</strong></p><h2 id="1、Html、Css、JS、JQuery"><a href="#1、Html、Css、JS、JQuery" class="headerlink" title="1、Html、Css、JS、JQuery"></a>1、Html、Css、JS、JQuery</h2><p><strong>网页的组成:内容(Html)、表现(Css)、行为(JS)。</strong></p><h3 id="1-1-HTML-Hyper-Text-Markup-Language-(超文本标记语言)"><a href="#1-1-HTML-Hyper-Text-Markup-Language-(超文本标记语言)" class="headerlink" title="1.1 HTML:Hyper Text Markup Language (超文本标记语言)"></a>1.1 <strong>HTML:Hyper Text Markup Language (超文本标记语言)</strong></h3><p>重点标签;</p><p><strong><!--form 标签就是表单input type=text 是文件输入框 value 设置默认显示内容input type=password 是密码输入框 value 设置默认显示内容input type=radio 是单选框 name 属性可以对其进行分组 checked="checked"表示默认选中input type=checkbox 是复选框 checked="checked"表示默认选中input type=reset 是重置按钮 value 属性修改按钮上的文本input type=submit 是提交按钮 value 属性修改按钮上的文本input type=button 是按钮 value 属性修改按钮上的文本input type=file 是文件上传域input type=hidden 是隐藏域 当我们要发送某些信息,而这些信息,不需要用户参与,就可以使用隐藏域(提交的时候同时发送给服务器)select 标签是下拉列表框option 标签是下拉列表框中的选项 selected="selected"设置默认选中textarea 表示多行文本输入框 (起始标签和结束标签中的内容是默认值)rows 属性设置可以显示几行的高度cols 属性设置每行可以显示几个字符宽度--></strong></p><form action="http://localhost:8080" method="post"><input type="hidden" name="action" value="login"><h1 align="center">用户注册</h1><table align="center"><tbody><tr><td> 用户名称:</td><td><input type="text" name="username" value="默认值"></td></tr><tr><td> 用户密码:</td><td><input type="password" name="password" value="abc"></td></tr><tr><td>性别:</td><td><input type="radio" name="sex" value="boy">男<input type="radio" name="sex" checked="checked" value="girl">女</td></tr><tr><td> 兴趣爱好:</td><td><input name="hobby" type="checkbox" checked="checked" value="java">Java<input name="hobby" type="checkbox" value="js">JavaScript<input name="hobby" type="checkbox" value="cpp">C++</td></tr><tr><td>国籍:</td><td><select name="country"><option value="none">--请选择国籍--</option><option value="cn" selected="selected">中国</option><option value="usa">美国</option><option value="jp">小日本</option></select></td></tr><tr><td>自我评价:</td><td><textarea name="desc" rows="10" cols="20">我才是默认值</textarea></td></tr><tr><td><input type="reset"></td><td align="center"><input type="submit"></td></tr></tbody></table></form><h3 id="1-2-Css-「层叠样式表单」"><a href="#1-2-Css-「层叠样式表单」" class="headerlink" title="1.2 Css:「层叠样式表单」"></a>1.2 Css:「层叠样式表单」</h3><p>把 css 样式写成一个单独的 css 文件,再通过 link 标签引入即可复用。</p><p>html 的 <link rel="stylesheet" type="text/css" href="./styles.css"> 标签 导入 css 样式文件。</p><p><strong>CSS三种选择器:</strong></p><p><strong>标签名选择器:</strong></p><p>div{<br>border: 1px solid yellow;<br>color: blue;<br>font-size: 30px;<br>}</p><p><strong>id 选择器:</strong></p><p>#id002{<br>color: red;<br>font-size: 20px;<br>border: 5px blue dotted ;<br>}</p><p><strong>class 选择器(类选择器):</strong></p><p>.class01{<br>color: blue;<br>font-size: 30px;<br>border: 1px solid yellow;<br>}</p><h3 id="1-3-JavaScript语言"><a href="#1-3-JavaScript语言" class="headerlink" title="1.3 JavaScript语言"></a>1.3 JavaScript语言</h3><p><strong>JS 是弱类型,Java 是强类型</strong></p><ol><li>交互性(它可以做的就是信息的动态交互) </li><li> 安全性(不允许直接访问本地硬盘)</li><li> 跨平台性(只要是可以解释 JS 的浏览器都可以执行,和平台无关)</li></ol><p><strong>Js和Html代码的结合:或者标签中使用<script type="text/javascript"> </script>或者 使用 script 标签引入 单独的 JavaScript 代码文件。</strong></p><p><strong>JS 中的定义变量格式: var 变量名; var 变量名 = 值;</strong></p><p><strong>JavaScript 里特殊的值:</strong></p><p> undefined 未定义,所有 js 变量未赋于初始值的时候,默认值都是 undefined. </p><p> null 空值 </p><p> NaN 全称是:Not a Number。非数字。非数值。</p><p><strong>JavaScript函数:</strong></p><p>function 函数名(形参列表){ 函数体 }</p><p> 在 JavaScript 语言中,如何定义带有返回值的函数? 只需要在函数体内直接使用 return 语句返回值即可!</p><p>或者</p><p>var 函数名 = function(形参列表) { 函数体 }</p><p><strong>js 中的事件:</strong>电脑输入设备与页面进行交互的响应。我们称之为事件</p><p><strong>常用的事件:</strong></p><p>onload 加载完成事件: 页面加载完成之后,常用于做页面 js 代码初始化操作 </p><p>onclick 单击事件: 常用于按钮的点击响应操作。 </p><p>onblur 失去焦点事件: 常用用于输入框失去焦点后验证其输入内容是否合法。 </p><p>onchange 内容发生改变事件: 常用于下拉列表和输入框内容发生改变后操作</p><p> onsubmit 表单提交事件:常用于表单提交前,验证所有表单项是否合法。</p><p><strong>事件的注册又分为静态注册和动态注册两种:</strong> </p><p>什么是事件的注册(绑定)? 其实就是告诉浏览器,当事件响应后要执行哪些操作代码,叫事件注册或事件绑定。</p><p> <strong>静态注册事件:</strong></p><p>通过 html 标签的事件属性直接赋于事件响应后的代码,这种方式我们叫静态注册。 </p><p><strong>动态注册事件:</strong></p><p>是指先通过 js 代码得到标签的 dom 对象,然后再通过 dom 对象.事件名 = function(){} 这种形式赋于事件 响应后的代码,叫动态注册。 </p><p>DOM 全称是 Document Object Model 文档对象模型。</p><p><strong>DOM对象常用方法;</strong></p><p>document 对象的三个查询方法,如果有 id 属性,优先使用 getElementById 方法来进行查询<br>如果没有 id 属性,则优先使用 getElementsByName 方法来进行查询<br>如果 id 属性和 name 属性都没有最后再按标签名查 getElementsByTagName<br>以上三个方法,一定要在页面加载完成之后执行,才能查询到标签对象。</p><p><strong>动态注册基本步骤</strong>: 1、获取标签对象 2、标签对象.事件名 = fucntion(){}</p><p>window.onload = function () {<br>//1 获取标签对象<br>var passwordObj = document.getElementById(“password”);<br>// alert(passwordObj);<br>//2 通过标签对象.事件名 = function(){};<br>passwordObj.onblur = function () {<br>console.log(“动态注册失去焦点事件”);<br>}<br>}</p><h3 id="1-4-JQuery"><a href="#1-4-JQuery" class="headerlink" title="1.4 JQuery"></a>1.4 JQuery</h3><p><strong>也就是 JavaScript 和查询(Query),它就是辅助 JavaScript 开发的 js 类库。</strong></p><p>使用前要导入jquery类库</p><script type="text/javascript" src="../script/jquery-1.7.2.js"></script><p>$(function () { // 表示页面加载完成 之后,相当 window.onload = function () {}<br>var $btnObj = $(“#btnId”); // 表示按 id 查询标签对象<br>$btnObj.click(function () { // 绑定单击事件<br>alert(“jQuery 的单击事件”);<br>});<br>});</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">$ 是 jQuery 的核心函数,能完成 jQuery 的很多功能。$()就是调用$这个函数1、传入参数为 [ 函数 ] 时:</span><br><span class="line">表示页面加载完成之后。相当于 window.onload = function(){}</span><br><span class="line">2、传入参数为 [ HTML 字符串 ] 时:</span><br><span class="line">会对我们创建这个 html 标签对象</span><br><span class="line">3、传入参数为 [ 选择器字符串 ] 时:</span><br><span class="line">$(“#id 属性值”); id 选择器,根据 id 查询标签对象</span><br><span class="line">$(“标签名”); 标签名选择器,根据指定的标签名查询标签对象</span><br><span class="line">$(“.class 属性值”); 类型选择器,可以根据 class 属性查询标签对象</span><br><span class="line">4、传入参数为 [ DOM 对象 ] 时:</span><br><span class="line">会把这个 dom 对象转换为 jQuery 对象</span><br><span class="line">jQuery 对象的本质是什么?</span><br><span class="line">jQuery 对象是 dom 对象的数组 + jQuery 提供的一系列功能函数。</span><br><span class="line"></span><br><span class="line">1、dom 对象转化为 jQuery 对象(*重点)</span><br><span class="line">1、先有 DOM 对象</span><br><span class="line">2、$( DOM 对象 ) 就可以转换成为 jQuery 对象</span><br><span class="line">2、jQuery 对象转为 dom 对象(*重点)</span><br><span class="line">1、先有 jQuery 对象</span><br><span class="line">2、jQuery 对象[下标]取出相应的 DOM 对象</span><br></pre></td></tr></tbody></table></figure><p><strong>Jquery选择器:</strong></p><p>基本选择器、层级选择器、过滤选择器(基本过滤器、内容过滤器、属性过滤器、表单过滤器、表单对象属性过滤器)</p><p><strong>Jquery的属性操作:</strong></p><p>html() 它可以设置和获取起始标签和结束标签中的内容。 跟 dom 属性 innerHTML 一样。</p><p> text() 它可以设置和获取起始标签和结束标签中的文本。 跟 dom 属性 innerText 一样。</p><p> val() 它可以设置和获取表单项的 value 属性值。 跟 dom 属性 value 一样</p><p>attr() 可以设置和获取属性的值,不推荐操作 checked、readOnly、selected、disabled 等等 attr 方法还可以操作非标准的属性。比如自定义属性:abc,bbj </p><p>prop() 可以设置和获取属性的值,只推荐操作 checked、readOnly、selected、disabled 等等</p><p><em><strong>jQuery 事件操作:</strong></em>**</p><p>$( function(){} ); 和 window.onload = function(){} 的区别?</p><p><strong>他们分别是在什么时候触发?</strong> 1、jQuery 的页面加载完成之后是浏览器的内核解析完页面的标签创建好 DOM 对象之后就会马上执行。 2、原生 js 的页面加载完成之后,除了要等浏览器内核解析完标签创建好 DOM 对象,还要等标签显示时需要的内容加载 完成。</p><p><strong>他们触发的顺序?</strong> 1、jQuery 页面加载完成之后先执行 2、原生 js 的页面加载完成之后</p><p><strong>他们执行的次数?</strong> 1、原生 js 的页面加载完成之后,只会执行最后一次的赋值函数。 2、jQuery 的页面加载完成之后是全部把注册的 function 函数,依次顺序全部执行。</p><h2 id="2-XML、Tomcat"><a href="#2-XML、Tomcat" class="headerlink" title="2.XML、Tomcat"></a>2.XML、Tomcat</h2><p><strong>什么是 xml?</strong></p><p> <strong>xml 是可扩展的标记性语言。</strong></p><p><strong>xml 的主要作用有:</strong></p><p> 1、用来保存数据,而且这些数据具有自我描述性</p><p> 2、它还可以做为项目或者模块的配置文件 </p><p>3、还可以做为网络传输数据的格式(现在 JSON 为主)。</p><p><strong>xml 语法?</strong></p><ol><li>文档声明 <!--?xml version="1.0" encoding="UTF-8?--></li><li>元素(标签) </li><li>xml 属性 </li><li>xml 注释 </li><li>文本区域(CDATA 区)</li></ol><p><strong>xml 解析技术:</strong>DOM 和 Sax </p><p><strong>dom4j 解析技术:</strong></p><p>第一步: 先加载 xml 文件创建 Document 对象 </p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">SAXReader reader = new SAXReader(); </span><br><span class="line">Document document = reader.read("src/books.xml");</span><br></pre></td></tr></tbody></table></figure><p>第二步:通过 Document 对象拿到根元素对象 </p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Element root = document.getRootElement();</span><br></pre></td></tr></tbody></table></figure><p>第三步:通过根元素.elelemts(标签名); 可以返回一个集合,这个集合里放着。所有你指定的标签名的元素对象 </p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">List<Element> books = root.elements("book");</span><br></pre></td></tr></tbody></table></figure><p>第四步:找到你想要修改、删除的子元素,进行相应在的操作 </p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">for (Element book : books) {</span><br><span class="line">// 测试</span><br><span class="line">// System.out.println(book.asXML());</span><br><span class="line">// 拿到 book 下面的 name 元素对象</span><br><span class="line">Element nameElement = book.element("name");</span><br><span class="line">// 拿到 book 下面的 price 元素对象</span><br><span class="line">Element priceElement = book.element("price");</span><br><span class="line">// 拿到 book 下面的 author 元素对象</span><br><span class="line">Element authorElement = book.element("author");</span><br><span class="line">// 再通过 getText() 方法拿到起始标签和结束标签之间的文本内容</span><br><span class="line">System.out.println("书名" + nameElement.getText() + " , 价格:"</span><br><span class="line">+ priceElement.getText() + ", 作者:" + authorElement.getText());</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>第五步,保存到硬盘上</p><p><strong>什么是 JavaWeb</strong> ?</p><p>JavaWeb 是指,所有通过 Java 语言编写可以通过浏览器访问的程序的总称,叫 JavaWeb。 JavaWeb 是基于请求和响应来开发的</p><p><strong>web 资源按实现的技术和呈现的效果的不同,又分为静态资源和动态资源两种。</strong></p><p> 静态资源: html、css、js、txt、mp4 视频 , jpg 图片 </p><p>动态资源: jsp 页面、Servlet 程序</p><h2 id="3-Servlet"><a href="#3-Servlet" class="headerlink" title="3.Servlet"></a><strong>3.Servlet</strong></h2><p><strong>什么是 Servlet?</strong></p><p>1、Servlet 是 JavaEE 规范之一。规范就是<strong>接口</strong> </p><p>2、Servlet 就 JavaWeb 三大组件之一。三大组件分别是:Servlet 程序、Filter 过滤器、Listener 监听器。 </p><p>3、Servlet 是运行在服务器上的一个 java 小程序(server applet),它可以接收客户端发送过来的请求,并响应数据给客户端。</p><p><strong>手动实现 Servlet 程序</strong> </p><p>1、编写一个类去实现 Servlet 接口</p><p> 2、实现 service 方法,处理请求,并响应数据 </p><p>3、到 web.xml 中去配置 servlet 程序的访问地址</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><!-- servlet 标签给 Tomcat 配置 Servlet 程序 --></span><br><span class="line"><servlet></span><br><span class="line"><!--servlet-name 标签 Servlet 程序起一个别名(一般是类名) --></span><br><span class="line"><servlet-name>HelloServlet</servlet-name></span><br><span class="line"><!--servlet-class 是 Servlet 程序的全类名--></span><br><span class="line"><servlet-class>com.atguigu.servlet.HelloServlet</servlet-class></span><br><span class="line"></servlet></span><br><span class="line"><!--servlet-mapping 标签给 servlet 程序配置访问地址--></span><br><span class="line"><servlet-mapping></span><br><span class="line"><!--servlet-name 标签的作用是告诉服务器,我当前配置的地址给哪个 Servlet 程序使用--></span><br><span class="line"><servlet-name>HelloServlet</servlet-name></span><br><span class="line"><!--url-pattern 标签配置访问地址 <br/></span><br><span class="line">/ 斜杠在服务器解析的时候,表示地址为:http://ip:port/工程路径 <br/></span><br><span class="line">/hello 表示地址为:http://ip:port/工程路径/hello <br/></span><br><span class="line">--></span><br><span class="line"><url-pattern>/hello</url-pattern></span><br><span class="line"></servlet-mapping></span><br></pre></td></tr></tbody></table></figure><p><strong>Servlet 的生命周期?</strong></p><p>1、执行 Servlet 构造器方法 </p><p>2、执行 init 初始化方法 </p><p>第一、二步,是在第一次访问,的时候创建 Servlet 程序会调用。 </p><p>3、执行 service 方法 </p><p>第三步,每次访问都会调用。 </p><p>4、执行 destroy 销毁方法 </p><p>第四步,在 web 工程停止的时候调用</p><p><strong>通过继承 HttpServlet 实现 Servlet程序?</strong></p><p>1、编写一个类去继承 HttpServlet 类<br>2、根据业务需要重写 doGet 或 doPost 方法<br>3、到 web.xml 中的配置 Servlet 程序的访问地址</p><p><strong>ServletConfig 类的三大作用?</strong></p><p>1、可以获取 Servlet 程序的别名 servlet-name 的值 servletConfig.getServletName()<br>2、获取初始化参数 init-param servletConfig.getInitParameter(“username”)<br>3、获取 ServletContext 对象 servletConfig.getServletContext()</p><p><strong>什么是 ServletContex?</strong></p><p>1、ServletContext 是一个接口,它表示 Servlet 上下文对象<br>2、一个 web 工程,只有一个 ServletContext 对象实例。<br>3、ServletContext 对象是一个域对象。<br>4、ServletContext 是在 web 工程部署启动的时候创建。在 web 工程停止的时候</p><p><strong>ServletContext 类的四个作用?</strong></p><p>1、获取 web.xml 中配置的上下文参数 context-param context.getInitParameter(“username”)</p><!--context-param 是上下文参数(它属于整个 web 工程)--><p>2、获取当前的工程路径,格式: /工程路径 context.getContextPath()<br>3、获取工程部署后在服务器硬盘上的绝对路径 context.getRealPath(“/“)<br>4、像 Map 一样存取数据 context.getAttribute(“key1”)</p><p><strong>HttpServletRequest 类有什么作用?</strong></p><p>每次只要有请求进入 Tomcat 服务器,Tomcat 服务器就会把请求过来的 HTTP 协议信息解析好封装到 Request 对象中。<br>然后传递到 service 方法(doGet 和 doPost)中给我们使用。我们可以通过 HttpServletRequest 对象,获取到所有请求的<br>信息。</p><p><strong>HttpServletRequest 类的常用方法?</strong><br>i. getRequestURI() 获取请求的资源路径<br>ii. getRequestURL() 获取请求的统一资源定位符(绝对路径)<br>iii. getRemoteHost() 获取客户端的 ip 地址<br>iv. getHeader() 获取请求头<br>v. getParameter() 获取请求的参数<br>vi. getParameterValues() 获取请求的参数(多个值的时候使用)<br>vii. getMethod() 获取请求的方式 GET 或 POST<br>viii. setAttribute(key, value); 设置域数据<br>ix. getAttribute(key); 获取域数据<br>x. getRequestDispatcher() 获取请求转发对象</p><p><strong>请求转发是指,服务器收到请求后,从一次资源跳转到另一个资源的操作叫请求转发。</strong></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");</span><br><span class="line">// RequestDispatcher requestDispatcher = req.getRequestDispatcher("http://www.baidu.com");</span><br><span class="line">// 走向 Sevlet2(柜台 2)</span><br><span class="line">requestDispatcher.forward(req,resp);</span><br></pre></td></tr></tbody></table></figure><p><img src="C:\Users\Javan\AppData\Roaming\Typora\typora-user-images\image-20210327111833808.png" alt="image-20210327111833808"></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><!DOCTYPE html></span><br><span class="line"><html lang="zh_CN"></span><br><span class="line"><head></span><br><span class="line"><meta charset="UTF-8"></span><br><span class="line"><title>Title</title></span><br><span class="line"><!--base 标签设置页面相对路径工作时参照的地址</span><br><span class="line">href 属性就是参数的地址值 后缀一定要以 / 结尾</span><br><span class="line">--></span><br><span class="line"><base href="http://localhost:8080/07_servlet/a/b/"></span><br><span class="line"></head></span><br><span class="line"><body></span><br><span class="line">这是 a 下的 b 下的 c.html 页面<br/></span><br><span class="line"><a href="../../index.html">跳回首页</a><br/></span><br><span class="line"></body></span><br><span class="line"></html></span><br></pre></td></tr></tbody></table></figure><p><strong>web 中 / 斜杠的不同意义?</strong></p><p>在 web 中 / 斜杠 是一种绝对路径。<br>/ 斜杠 如果被<strong>浏览器</strong>解析,得到的地址是:<a href="http://ip:port/">http://ip:port/</a><br><a href="/">斜杠</a><br>/ 斜杠 如果被<strong>服务器</strong>解析,得到的地址是:<a href="http://ip:port/工程路径">http://ip:port/工程路径</a><br>1、<url-pattern>/servlet1</url-pattern><br>2、servletContext.getRealPath(“/”);<br>3、request.getRequestDispatcher(“/”);<br>特殊情况: response.sendRediect(“/”); 把斜杠发送给浏览器解析。得到 <a href="http://ip:port/">http://ip:port/</a></p><p><strong>HttpServletResponse 类的作用</strong>?</p><p>HttpServletResponse 类和 HttpServletRequest 类一样。每次请求进来,Tomcat 服务器都会创建一个 Response 对象传 递给 Servlet 程序去使用。HttpServletRequest 表示请求过来的信息,HttpServletResponse 表示所有响应的信息, 我们如果需要设置返回给客户端的信息,都可以通过 HttpServletResponse 对象来进行</p><p><strong>两个输出流的说明。</strong></p><p> 字节流 getOutputStream(); 常用于下载(传递二进制数据) </p><p>字符流 getWriter(); 常用于回传字符串(常用) </p><p>两个流同时只能使用一个。 使用了字节流,就不能再使用字符流,反之亦然,否则就会报错。</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">public class ResponseIOServlet extends HttpServlet {</span><br><span class="line">@Override</span><br><span class="line">protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,</span><br><span class="line">IOException {</span><br><span class="line">// 要求 : 往客户端回传 字符串 数据。</span><br><span class="line">PrintWriter writer = resp.getWriter();</span><br><span class="line">writer.write("response's content!!!");</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>请求重定向,是指客户端给服务器发请求,然后服务器告诉客户端说。我给你一些地址。你去新地址访问。叫请求</strong><br><strong>重定向(因为之前的地址可能已经被废弃)。</strong></p><p><img src="C:\Users\Javan\AppData\Roaming\Typora\typora-user-images\image-20210327113158366.png" alt="image-20210327113158366"></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">resp.sendRedirect("http://localhost:8080");</span><br></pre></td></tr></tbody></table></figure><h2 id="4-JSP"><a href="#4-JSP" class="headerlink" title="4.JSP"></a>4.JSP</h2><p><strong>jsp 的全换是 java server pages。Java 的服务器页面。</strong></p><p> <strong>jsp 的主要作用是代替 Servlet 程序回传 html 页面的数据。</strong></p><p><strong>因为 Servlet 程序回传 html 页面数据是一件非常繁锁的事情。开发成本和维护成本都极高。</strong></p><p><strong>jsp 的本质是什么?</strong></p><p>jsp 页面本质上是一个 Servlet 程序。</p><p><strong>jsp 的三种语法:</strong></p><p><strong>a)jsp 头部的 page 指令</strong></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><%@ page contentType="text/html;charset=UTF-8" language="java" %></span><br></pre></td></tr></tbody></table></figure><p><strong>b)jsp 中的常用脚本</strong></p><p>表达式脚本(常用) 表达式脚本的格式是:<%=表达式%> 表达式脚本的作用是:在 jsp 页面上输出数据</p><p>代码脚本的格式是: <% java 语句 %> 代码脚本的作用是:可以在 jsp 页面中,编写我们自己需要的功能(写的是 java 语句)。</p><p><strong>c)jsp 中的三种注释</strong></p><p>i. html 注释 、ii. java 注释 、iii. jsp 注释</p><p><strong>jsp 四大域对象</strong></p><p>四个域对象分别是:<br>pageContext (PageContextImpl 类) 当前 jsp 页面范围内有效<br>request (HttpServletRequest 类)、 一次请求内有效<br>session (HttpSession 类)、 一个会话范围内有效(打开浏览器访问服务器,直到关闭浏览器)<br>application (ServletContext 类) 整个 web 工程范围内都有效(只要 web 工程不停止,数据都在)<br>域对象是可以像 Map 一样存取数据的对象。四个域对象功能一样。不同的是它们对数据的存取范围。<br>虽然四个域对象都可以存取数据。在使用上它们是有优先顺序的。<br>四个域在使用的时候,优先顺序分别是,他们从小到大的范围的顺序。<br>pageContext ====>>> request ====>>> session ====>>> application</p><h2 id="5-Listener-监听器"><a href="#5-Listener-监听器" class="headerlink" title="5.Listener 监听器"></a>5.Listener 监听器</h2><p>1、Listener 监听器它是 JavaWeb 的三大组件之一。JavaWeb 的三大组件分别是:Servlet 程序、Filter 过滤器、Listener 监<br>听器。<br>2、Listener 它是 JavaEE 的规范,就是接口<br>3、监听器的作用是,监听某种事物的变化。然后通过回调函数,反馈给客户(程序)去做一些相应的处理。</p><p><strong>ServletContextListener 监听器</strong></p><p>ServletContextListener 它可以监听 ServletContext 对象的创建和销毁。 </p><p>ServletContext 对象在 web 工程启动的时候创建,在 web 工程停止的时候销毁。 监听到创建和销毁之后都会分别调用 ServletContextListener 监听器的方法反馈。</p><p><strong>如何使用 ServletContextListener 监听器监听 ServletContext 对象。</strong><br>使用步骤如下:<br>1、编写一个类去实现 ServletContextListener<br>2、实现其两个回调方法 contextInitialized() contextDestroyed()<br>3、到 web.xml 中去配置监听器</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">public class MyServletContextListenerImpl implements ServletContextListener {</span><br><span class="line">@Override</span><br><span class="line">public void contextInitialized(ServletContextEvent sce) {</span><br><span class="line">System.out.println("ServletContext 对象被创建了");</span><br><span class="line">}</span><br><span class="line">@Override</span><br><span class="line">public void contextDestroyed(ServletContextEvent sce) {</span><br><span class="line">System.out.println("ServletContext 对象被销毁了");</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>web.xml 中的配置</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><!--配置监听器--></span><br><span class="line"><listener></span><br><span class="line"><listener-class>com.atguigu.listener.MyServletContextListenerImpl</listener-class></span><br><span class="line"></listener></span><br></pre></td></tr></tbody></table></figure><h2 id="6-EL表达式、JSTL标签库"><a href="#6-EL表达式、JSTL标签库" class="headerlink" title="6.EL表达式、JSTL标签库"></a><strong>6.EL表达式、JSTL标签库</strong></h2><p>EL 表达式的全称是:Expression Language。是表达式语言。<br>EL 表达式的什么作用:EL 表达式主要是代替 jsp 页面中的表达式脚本在 jsp 页面中进行数据的输出。<br>因为 EL 表达式在输出数据的时候,要比 jsp 的表达式脚本要简洁很多</p><p><strong>EL 表达式的格式是:${表达式}</strong><br>EL 表达式在输出 null 值的时候,输出的是空串。jsp 表达式脚本输出 null 值的时候,输出的是 null</p><p><img src="C:\Users\Javan\AppData\Roaming\Typora\typora-user-images\image-20210327194844288.png" alt="image-20210327194844288"></p><p><strong>JSTL 标签库:</strong></p><p>JSTL 标签库 全称是指 JSP Standard Tag Library JSP 标准标签库。是一个不断完善的开放源代码的 JSP 标 签库。</p><p> EL 表达式主要是为了替换 jsp 中的表达式脚本,而JSTL标签库则是为了替换代码脚本。这样使得整个 jsp 页面 变得更佳简洁。</p><p><strong>JSTL 标签库的使用步骤</strong>:<br>1、先导入 jstl 标签库的 jar 包。<br>taglibs-standard-impl-1.2.1.jar<br>taglibs-standard-spec-1.2.1.jar<br>2、第二步,使用 taglib 指令引入标签库。<br><%@ taglib prefix=”c” uri=”<a href="http://java.sun.com/jsp/jstl/core"">http://java.sun.com/jsp/jstl/core"</a> %></p><p>core 核心库使用:用的时候再百度</p><h2 id="7-Cookie和Session"><a href="#7-Cookie和Session" class="headerlink" title="7.Cookie和Session"></a>7.Cookie和Session</h2><p><strong>什么是 Cookie?</strong><br>1、Cookie 翻译过来是饼干的意思。<br>2、Cookie 是服务器通知客户端保存键值对的一种技术。<br>3、客户端有了 Cookie 后,每次请求都发送给服务器。<br>4、每个 Cookie 的大小不能超过 4kb</p><p><strong>如何创建 Cookie?</strong></p><p><img src="C:\Users\Javan\AppData\Roaming\Typora\typora-user-images\image-20210327195502261.png" alt="image-20210327195502261"></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException,</span><br><span class="line">IOException {</span><br><span class="line">//1 创建 Cookie 对象Cookie cookie = new Cookie("key4", "value4");</span><br><span class="line">//2 通知客户端保存 Cookie</span><br><span class="line">resp.addCookie(cookie);</span><br><span class="line">//1 创建 Cookie 对象</span><br><span class="line">Cookie cookie1 = new Cookie("key5", "value5");</span><br><span class="line">//2 通知客户端保存 Cookie</span><br><span class="line">resp.addCookie(cookie1);</span><br><span class="line">resp.getWriter().write("Cookie 创建成功");</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>服务器如何获取 Cookie?</strong></p><p>服务器获取客户端的 Cookie 只需要一行代码:req.getCookies():Cookie[]</p><p><img src="C:\Users\Javan\AppData\Roaming\Typora\typora-user-images\image-20210327195613656.png" alt="image-20210327195613656"></p><p><strong>Cookie 值的修改?</strong></p><p>方案一:<br>1、先创建一个要修改的同名(指的就是 key)的 Cookie 对象<br>2、在构造器,同时赋于新的 Cookie 值。<br>3、调用 response.addCookie( Cookie)</p><p>方案二:<br>1、先查找到需要修改的 Cookie 对象<br>2、调用 setValue()方法赋于新的 Cookie 值。<br>3、调用 response.addCookie()通知客户端保存修改</p><p><strong>Cookie 生命控制?</strong></p><p>Cookie 的生命控制指的是如何管理 Cookie 什么时候被销毁(删除)<br>setMaxAge()<br>正数,表示在指定的秒数后过期<br>负数,表示浏览器一关,Cookie 就会被删除(默认值是-1)<br>零,表示马上删除 Cookie</p><p><strong>什么是 Session 会话?</strong></p><p>1、Session 就一个接口(HttpSession)。<br>2、Session 就是会话。它是用来维护一个客户端和服务器之间关联的一种技术。<br>3、每个客户端都有自己的一个 Session 会话。<br>4、Session 会话中,我们经常用来保存用户登录之后的信息。</p><p>如何创建和获取 Session。它们的 API 是一样的。<br>request.getSession()<br>第一次调用是:创建 Session 会话<br>之后调用都是:获取前面创建好的 Session 会话对象</p><p><strong>Session 生命周期控制?</strong></p><p>public void setMaxInactiveInterval(int interval) 设置 Session 的超时时间(以秒为单位),超过指定的时长,Session<br>就会被销毁。<br>值为正数的时候,设定 Session 的超时时长。<br>负数表示永不超时(极少使用)<br>public int getMaxInactiveInterval()获取 Session 的超时时间<br>public void invalidate() 让当前 Session 会话马上超时无效</p><p><img src="C:\Users\Javan\AppData\Roaming\Typora\typora-user-images\image-20210327200424416.png" alt="image-20210327200424416"></p><p><strong>总结:</strong></p><p>1,session 在服务器端,cookie 在客户端(浏览器)<br> 2,session 默认被存在在服务器的一个文件里(不是内存)<br> 3,session 的运行依赖 session id,而 session id 是存在 cookie 中的,也就是说,如果浏览器禁用了 cookie ,同时 session 也会失效(但是可以通过其它方式实现,比如在 url 中传递 session_id)<br> 4,session 可以放在 文件、数据库、或内存中都可以。<br> 5,用户验证这种场合一般会用 session </p><p>因此,维持一个会话的核心就是客户端的唯一标识,即 session id。</p><p>Session是在服务端保存的一个数据结构,用来跟踪用户的状态,这个数据可以保存在集群、数据库、文件中;<br>Cookie是客户端保存用户信息的一种机制,用来记录用户的一些信息,也是实现Session的一种方式。</p><h2 id="8-Filter过滤器"><a href="#8-Filter过滤器" class="headerlink" title="8.Filter过滤器"></a><strong>8.Filter过滤器</strong></h2><p>1、Filter 过滤器它是 JavaWeb 的三大组件之一。三大组件分别是:Servlet 程序、Listener 监听器、Filter 过滤器<br>2、Filter 过滤器它是 JavaEE 的规范。也就是接口<br>3、Filter 过滤器它的作用是:拦截请求,过滤响应。</p><p>拦截请求常见的应用场景有:<br>1、权限检查<br>2、日记操作<br>3、事务管理<br>……等等</p><p><strong>Filter 过滤器的使用步骤:</strong><br>1、编写一个类去实现 Filter 接口<br>2、实现过滤方法 doFilter()<br>3、到 web.xml 中去配置 Filter 的拦截路径</p><p><img src="C:\Users\Javan\AppData\Roaming\Typora\typora-user-images\image-20210327201333684.png" alt="image-20210327201333684"></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">public class AdminFilter implements Filter {</span><br><span class="line">/**</span><br><span class="line">* doFilter 方法,专门用于拦截请求。可以做权限检查</span><br><span class="line">*/</span><br><span class="line">@Override</span><br><span class="line">public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain</span><br><span class="line">filterChain) throws IOException, ServletException {</span><br><span class="line">HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;</span><br><span class="line">HttpSession session = httpServletRequest.getSession();</span><br><span class="line">Object user = session.getAttribute("user");</span><br><span class="line">// 如果等于 null,说明还没有登录</span><br><span class="line">if (user == null) {</span><br><span class="line">servletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest,servletResponse);</span><br><span class="line">return;</span><br><span class="line">} else {</span><br><span class="line">// 让程序继续往下访问用户的目标资源</span><br><span class="line">filterChain.doFilter(servletRequest,servletResponse);</span><br><span class="line">}</span><br><span class="line">}</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>web.xml 中的配置:</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><!--filter 标签用于配置一个 Filter 过滤器--></span><br><span class="line"><filter></span><br><span class="line"><!--给 filter 起一个别名--></span><br><span class="line"><filter-name>AdminFilter</filter-name></span><br><span class="line"><!--配置 filter 的全类名--></span><br><span class="line"><filter-class>com.atguigu.filter.AdminFilter</filter-class></span><br><span class="line"></filter><!--filter-mapping 配置 Filter 过滤器的拦截路径--></span><br><span class="line"><filter-mapping></span><br><span class="line"><!--filter-name 表示当前的拦截路径给哪个 filter 使用--></span><br><span class="line"><filter-name>AdminFilter</filter-name></span><br><span class="line"><!--url-pattern 配置拦截路径</span><br><span class="line">/ 表示请求地址为:http://ip:port/工程路径/ 映射到 IDEA 的 web 目录</span><br><span class="line">/admin/* 表示请求地址为:http://ip:port/工程路径/admin/*</span><br><span class="line">--></span><br><span class="line"><url-pattern>/admin/*</url-pattern></span><br><span class="line"></filter-mapping></span><br></pre></td></tr></tbody></table></figure><p><strong>Filter 的生命周期:</strong></p><p>Filter 的生命周期包含几个方法<br>1、构造器方法<br>2、init 初始化方法<br>第 1,2 步,在 web 工程启动的时候执行(Filter 已经创建)<br>3、doFilter 过滤方法<br>第 3 步,每次拦截到请求,就会执行<br>4、destroy 销毁<br>第 4 步,停止 web 工程的时候,就会执行(停止 web 工程,也会销毁 Filter 过滤器)</p><p><strong>FilterConfig 类</strong></p><p>FilterConfig 类见名知义,它是 Filter 过滤器的配置文件类。<br>Tomcat 每次创建 Filter 的时候,也会同时创建一个 FilterConfig 类,这里包含了 Filter 配置文件的配置信息。<br>FilterConfig 类的作用是获取 filter 过滤器的配置内容<br>1、获取 Filter 的名称 filter-name 的内容<br>2、获取在 Filter 中配置的 init-param 初始化参数<br>3、获取 ServletContext 对象</p><h2 id="9-JSON、AJAX"><a href="#9-JSON、AJAX" class="headerlink" title="9.JSON、AJAX"></a>9.JSON、AJAX</h2><p>JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式。轻量级指的是跟 xml 做比较。 数据交换指的是客户端和服务器之间业务数据的传递格式。</p><p>json 是由键值对组成,并且由花括号(大括号)包围。每个键由引号引起来,键和值之间使用冒号进行分隔, 多组键值对之间进行逗号进行分隔。</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">var jsonObj = {</span><br><span class="line">"key1":12,</span><br><span class="line">"key2":"abc",</span><br><span class="line">"key3":true,</span><br><span class="line">"key4":[11,"arr",false],</span><br><span class="line">"key5":{</span><br><span class="line">"key5_1" : 551,</span><br><span class="line">"key5_2" : "key5_2_value"</span><br><span class="line">},</span><br><span class="line">"key6":[{</span><br><span class="line">"key6_1_1":6611,</span><br><span class="line">"key6_1_2":"key6_1_2_value"},{</span><br><span class="line">"key6_2_1":6621,</span><br><span class="line">"key6_2_2":"key6_2_2_value"</span><br><span class="line">}]</span><br><span class="line">};</span><br></pre></td></tr></tbody></table></figure><p>json 本身是一个对象。 json 中的 key 我们可以理解为是对象中的一个属性。 json 中的 key 访问就跟访问对象的属性一样: json 对象.key</p><p>json 的存在有两种形式。<br>一种是:对象的形式存在,我们叫它 json 对象。<br>一种是:字符串的形式存在,我们叫它 json 字符串。<br><strong>一般我们要操作 json 中的数据的时候,需要 json 对象的格式。</strong><br><strong>一般我们要在客户端和服务器之间进行数据交换的时候,使用 json 字符串。</strong><br>JSON.stringify() 把 json 对象转换成为 json 字符串<br>JSON.parse() 把 json 字符串转换成为 json 对象</p><p><strong>JSON 在 java 中的使用:</strong></p><p>javaBean 和 json 的互转:<strong>tojson() 和 fromjson()的使用</strong></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">public void test1(){</span><br><span class="line">Person person = new Person(1,"国哥好帅!");</span><br><span class="line">// 创建 Gson 对象实例</span><br><span class="line">Gson gson = new Gson();</span><br><span class="line">// toJson 方法可以把 java 对象转换成为 json 字符串</span><br><span class="line">String personJsonString = gson.toJson(person);</span><br><span class="line">System.out.println(personJsonString);</span><br><span class="line">// fromJson 把 json 字符串转换回 Java 对象</span><br><span class="line">// 第一个参数是 json 字符串</span><br><span class="line">// 第二个参数是转换回去的 Java 对象类型</span><br><span class="line">Person person1 = gson.fromJson(personJsonString, Person.class);</span><br><span class="line">System.out.println(person1);</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p>List 和 json 的互转:<strong>tojson() 和 fromjson()的使用</strong></p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">@Test</span><br><span class="line">public void test2() {</span><br><span class="line">List<Person> personList = new ArrayList<>();</span><br><span class="line">personList.add(new Person(1, "国哥"));</span><br><span class="line">personList.add(new Person(2, "康师傅"));</span><br><span class="line">Gson gson = new Gson();</span><br><span class="line">// 把 List 转换为 json 字符串</span><br><span class="line">String personListJsonString = gson.toJson(personList);</span><br><span class="line">System.out.println(personListJsonString);</span><br><span class="line">//PersonListType类继承了TypeToken<ArrayList<Person>></span><br><span class="line">List<Person> list = gson.fromJson(personListJsonString, new PersonListType().getType());</span><br><span class="line">System.out.println(list);</span><br><span class="line">Person person = list.get(0);</span><br><span class="line">System.out.println(person);</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure><p><strong>AJAX请求:</strong></p><p>AJAX 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML),是指一种创建交互式网页应用的网页开发<br>技术。<br>ajax 是一种浏览器通过 js 异步发起请求,局部更新页面的技术。<br>Ajax 请求的局部更新,浏览器地址栏不会发生变化<br>局部更新不会舍弃原来页面的内容</p><p><strong>jQuery 中的 AJAX 请求</strong></p><p>$.ajax 方法 </p><p>url 表示请求的地址</p><p>type 表示请求的类型 </p><p> GET 或 POST 请求 </p><p>data 表示发送给服务器的数据 </p><p> 格式有两种: 一:name=value&name=value 二:{key:value}</p><p> success 请求成功,响应的回调函数 </p><p>dataType 响应的数据类型 常用的数据类型有: </p><p> text 表示纯文本 xml 表示 xml 数据 json 表示 json 对象</p><figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">$("#ajaxBtn").click(function(){</span><br><span class="line">$.ajax({</span><br><span class="line">url:"http://localhost:8080/16_json_ajax_i18n/ajaxServlet",</span><br><span class="line">// data:"action=jQueryAjax",</span><br><span class="line">data:{action:"jQueryAjax"},</span><br><span class="line">type:"GET",</span><br><span class="line">success:function (data) {</span><br><span class="line">// alert("服务器返回的数据是:" + data);</span><br><span class="line">// var jsonObj = JSON.parse(data);</span><br><span class="line">$("#msg").html("编号:" + data.id + " , 姓名:" + data.name);</span><br><span class="line">},</span><br><span class="line">dataType : "json"</span><br><span class="line">});</span><br><span class="line">});</span><br></pre></td></tr></tbody></table></figure>]]></content>
</entry>
<entry>
<title>Hello World</title>
<link href="/javanblog/2021/04/23/hello-world/"/>
<url>/javanblog/2021/04/23/hello-world/</url>
<content type="html"><![CDATA[<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p><h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><figure class="highlight bash"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo new <span class="string">"My New Post"</span></span><br></pre></td></tr></tbody></table></figure><p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p><h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><figure class="highlight bash"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo server</span><br></pre></td></tr></tbody></table></figure><p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p><h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><figure class="highlight bash"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo generate</span><br></pre></td></tr></tbody></table></figure><p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p><h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><figure class="highlight bash"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ hexo deploy</span><br></pre></td></tr></tbody></table></figure><p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>]]></content>
</entry>
</search>
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。