专栏首页用户6093955的专栏SpringMVC【入门】篇(转载自https://blog.csdn.net/qq_40181435/article/details/105599178)

SpringMVC【入门】篇(转载自https://blog.csdn.net/qq_40181435/article/details/105599178)

对应视频教程:https://www.bilibili.com/video/BV1Sb411s7qa?p=1

SpringMVC第一篇

  • 什么是SpringMVC?
    • 概念 Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的Spring MVC框架或集成其他MVC开发框架,如Struts1(现在一般不用),Struts 2(一般老项目使用)等。
    • 三层框架
      • Model(模型):数据模型,提供要展示的数据,因此包含数据和行为,可以认为是领域模型或 JavaBean 组件(包含数据和行为),不过现在一般都分离开来:Value Object(数据) 和 服务层(行为)。也就是模型提供了模型数据查询和模型数据的状态更新等功能,包括数据和业务。
      • View(视图):负责进行模型的展示,一般就是我们见到的用户界面,客户想看到的东西。
      • Controller(控制器):接收用户请求,委托给模型进行处理(状态改变),处理完毕后把返回的模型数据返回给视图,由视图负责展示。
    • SpringMVC运行原理
  • SpringMVC入门
    • 搭建开发环境
      • 第二步:创建之后再main下创建javaresources两个文件夹(我这里是直接创建文件夹类型就给定义好了,如果创建出来是普通的文件夹,可以右键文件夹选择Mark Directory as然后选择相应类型:java选择Sources Rootresources选择Resources Root
      • 第三步:在pom.xml中添加依赖
      • 第四步:配置web.xml文件(前端配置器)
      • 第五步:在resources文件夹下创建配置文件
    • 编写入门程序
      • 首先在index.jsp文件中添加一个超链接标签用于跳转
      • 然后新建一个controller类:方法返回值可做跳转页面的文件名
      • springmvc.xml主配置文件中进行配置

      WEB-INF下创建文件夹pages,并在pages中创建跳转页面success.jsp

      • 配置web.xml
      • 开启tomcat运行测试,运行结果

      运行原理解析

    补充:

 @RequestMapping
    /**
     * 作用:用于建立请求URL和处理请求方法之间的对应关系(及时能够响应index.jsp的超链接请求)
     * 作用范围:可以作用在方法上,也可以作用在类上   如:user/hello
     *
     * 属性:
     *      *name:用处不大*
     *      value:指定映射路径,作用与path相同
     *      path:指定映射路径,作用与value相同
     *      method:指定当前方法/类可以接收什么样的请求方式
     *          method = {RequestMethod.POST,RequestMethod.GET}
     *      params:指定限制请求参数的条件。它支持简单的表达式。要求请求的参数的key和value必须配置的一模一样
     *          如:params = {"accountName"},则请求参数必须有accountName:如:<a href="user/hello?accountName=hehe">点击链接跳转</a>
     *             params = {"accountName=heihei"},则请求参数必须是accountName=heihei:如:<a href="user/hello?accountName=heihei">点击链接跳转</a>
     *             params = {"money!100"},表示请求参数中的money不能是100
     *      headers:用于指定限制请求消息头的条件
     *
     *      注意:
     *          以上属性只要出现2个或以上时,他们的关系是与关系
     */
  • 请求参数的绑定(非常重要)
1. 请求参数邦定入门
/*param.jsp*/
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

<title>Title</title>

</head>

<body>

<a href="param/testParam?username=user&password=123456">请求参数绑定</a>

</body>

</html>
package it.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @Author: 东方老赢
 * @Date: 2020/4/19 14:18
 */
@Controller
@RequestMapping("param")
public class ParamController {

    @RequestMapping("testParam")
    public String testParam(String username,String password){
        System.out.println("执行了");
        System.out.println(username);
        System.out.println(password);
        return "success";
    }
}

运行结果

2. 请求参数绑定实体类型
//1.创建user实体类
package it.mvc.domain;
import java.io.Serializable;
/**


@Author: 东方老赢


@Date: 2020/4/19 14:42

*/

public class User implements Serializable {

private String uname;

private Integer age;
public String getUname() {

return uname;

}
public void setUname(String uname) {

this.uname = uname;

}
public Integer getAge() {

return age;

}
public void setAge(Integer age) {

this.age = age;

}
@Override

public String toString() {

return "User{" +

"uname='" + uname + ''' +

", age=" + age +

'}';

}

}
//2.创建Account实体类,并引用user实体类

package it.mvc.domain;

import java.io.Serializable;

/**
 * @Author: 东方老赢
 * @Date: 2020/4/19 14:32
 */
public class Account implements Serializable {
    private String username;
    private String password;

//    实体类中有引用实体类
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Account{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", user=" + user +
                '}';
    }
}
//在ParamController中创建方法,接受参数为实体类

 @RequestMapping("saveParam")
    public String saveParam(Account account){
        System.out.println("执行了");
        System.out.println(account);
        return "success";
    }
<%-- 使用表单提交的方式提交信息 --%>

<body>
<form action="param/saveParam" method="post">
    账户<input type="text" name="username"/><br/>
    密码<input type="text" name="password"/><br/>
<%--    实体类中有引用实体类的传参--%>
    姓名<input type="text" name="user.uname"/></br>
    年龄<input type="text" name="user.age"/></br>
    <input type="submit" value="提交">
</form>
</body>

测试结果:

在这里如果你输入中文就会发现控制台输出乱码,这时我们就要配置解决中文乱码的过滤器 在主配置文件web.xml中加入下列代码(==注意!!==要写在前端控制器之前,否则排列顺序不对,web-app标签会标红)

<!--配置解决中文乱码的过滤器-->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

乱码问题就解决啦

3. 请求参数绑定集合类型

与上述步骤基本一致,这里只展示不同部分

//实体类中引用集合,添加get、set和toString方法
private List<User> list;

private Map<String,User> map;
<span class="token keyword">public</span> List<span class="token generics function"><span class="token punctuation">&lt;</span>User<span class="token punctuation">&gt;</span></span> <span class="token function">getList</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> list<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setList</span><span class="token punctuation">(</span>List<span class="token generics function"><span class="token punctuation">&lt;</span>User<span class="token punctuation">&gt;</span></span> list<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>list <span class="token operator">=</span> list<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> Map<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span> User<span class="token punctuation">&gt;</span></span> <span class="token function">getMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> map<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setMap</span><span class="token punctuation">(</span>Map<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span> User<span class="token punctuation">&gt;</span></span> map<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">this</span><span class="token punctuation">.</span>map <span class="token operator">=</span> map<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<form action="param/saveParam" method="post">
    账户<input type="text" name="username"/><br/>
    密码<input type="text" name="password"/><br/>
<%--    实体类中有集合传参--%>
    姓名<input type="text" name="list[0].uname"/></br>
    年龄<input type="text" name="list[0].age"/></br>
    姓名<input type="text" name="map['one'].uname"/></br>
    年龄<input type="text" name="map['one'].age"/></br>
    <input type="submit" value="提交">
</form>

运行结果

4.自定义类型转换器代码编写
问题:网页传参都是String格式,而SpringMVC在传参的过程中自动进行了类型转换,但是也有个别案例。
	如:默认可转换的日期格式为 yyyy/MM/dd,如果输入日期格式为 yyyy-MM-dd,则会报错

代码编写:(SpringMVC为我们提供了一个接口,用于我们自己编写转换代码)

1.在User实体类中加入Date属性,并添加get、set方法(此处就不展示了)

//2.创建字符串转化类
package it.mvc.utiles;
import org.springframework.core.convert.converter.Converter;
import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;
/**

@Author: 东方老赢
@Date: 2020/4/19 17:19

将字符串转换为指定格式日期

*/

public class StringToDateConverter implements Converter<String, Date> {

@Override

public Date convert(String s) {

if (s == null){

throw new RuntimeException("没有数据!!!");

}

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

try {

return df.parse(s);

} catch (ParseException e) {

throw new RuntimeException("数据类型转换出现错误!!!");

}

}

}
<!--3.在主配置文件springmvc.xml文件中配置-->

<!--    配置自定义类型转换器-->
    <bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
<!--                配置路径-->
                <bean class="it.mvc.utiles.StringToDateConverter"></bean>
            </set>
        </property>
    </bean>

<!--    开启springmvc框架注解支持  手动开启类型转换-->
    <mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
//4.添加转换方法

 @RequestMapping("StringToDate")
    public String StringToDate(User account){
        System.out.println("执行了");
        System.out.println(account);
        return "success";
    }
//5.表单提交测试
<form action="param/StringToDate" method="post">
    账户<input type="text" name="uname"/><br/>
    密码<input type="text" name="age"/><br/>
   生日 <input type="text" name="birthday"><br/>
    <input type="submit" value="提交">
</form>

运行通过说明你成功啦!

细节:怎么通过SpringMVC获取Servlet原生API
<%--1.编写超链接--%>
    <a href="param/getServlet">获取servlet原生API</a>
 /**
     * 2.获取Servlet原生API
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("getServlet")
    public String getServlet(HttpServletRequest request, HttpServletResponse response){
        System.out.println("执行了");
        System.out.println(request);
    HttpSession session <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>session<span class="token punctuation">)</span><span class="token punctuation">;</span>

    ServletContext servletContext <span class="token operator">=</span> session<span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>servletContext<span class="token punctuation">)</span><span class="token punctuation">;</span>

    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>response<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token string">"success"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

运行结果:

  • 常用注解?
  • @RequestParam()

作用:把请求中指定名称的参数给控制器形参赋值(即网页传参名称与方法中参数名不一致时可用) 属性:

  • value:请求参数中的名称
  • required:请求参数中是否必须提供此参数。默认值为true。表示必须提供,不提供会出错
<a href="anno/testRequestParam?name=haha">testRequestParam</a>
  @RequestMapping("/testRequestParam")
    public String testRequestParam(String username){
        System.out.println(username);
        return "success";
    }

运行结果:

细节:当添加了注解之后,网页传参的参数名称必须与注解中的一致,否则会报错

  • @RequestBody()

作用:用于获取请求体内容。直接使用得到的是 key=value&key=value结构的数据(get方式不适用) 属性:

  • required:是否必须有请求体,默认为true。 当取值为true时,get请求方式会报错。如果取值为false,get请求得到的是null
<form action="anno/testRequestBody" method="post">
    用户:<input type="text" name="username"><br/>
    密码:<input type="text" name="password"><br/>
    <input type="submit" value="提交">
</form>
 @RequestMapping("/testRequestBody")
    public String testRequestBody(@RequestBody String body){
        System.out.println(body);
        return "success";
    }

运行结果:

  • @PathVariable()

作用:用于绑定url中的占位符。例如:请求url中/delete/{id},这个{id}就是占位符。(url支持占位符是spring3.0之后加入的,是springmvc支持restful风格url的一个重要标志) 属性:

  • value:用于指定url中占位符的位置
  • required:是否必须提供占位符
<a href="anno/testPathVariable/10">testPathVariable</a>
 @RequestMapping("/testPathVariable/{sid}")
    public String testPathVariable(@PathVariable(name = "sid") String id){
        System.out.println(id);
        return "success";
    }

运行结果:

  • HiddenHttpMethodFilter过滤器

  • 作用:由于浏览器form表单只支持get与post请求,而default、put等method并不支持,spring3.0添加了一个过滤器,可以将浏览器请求改为指定的请求方式,发送给我们的控制器方法,使得支持get、post、put与delete请求
  • 使用方法:
    • 第一步:在web.xml中设置过滤器
    • 第二步:请求方式必须使用post请求
    • 第三步:按照要求提供_method请求参数,该参数的取值就是我们需要的请求方式
  • @RequestHeader()(用处不大)

作用:用于获取请求消息头 属性:

  • value:提供消息头名称
  • required:是否必须有此消息头

注:使其开发中一般不用

<a href="anno/testRequestHeader">testRequestHeader</a>
 @RequestMapping("/testRequestHeader")
    public String testRequestHeader(@RequestHeader(value = "Accept") String header){
        System.out.println(header);
        return "success";
    }

运行结果

  • @CookieValue()(用的也较少)

作用:用于把指定cookie名称的值传入控制器方法参数 属性:

  • value:指定cookie的名称
  • required:是否必须有cookie
<a href="anno/testCookieValue">testCookieValue</a>
 @RequestMapping("/testCookieValue")
    public String testCookieValue(@CookieValue(value = "JSESSIONID") String cookie){
        System.out.println(cookie);
        return "success";
    }

运行结果:

  • @ModelAttribute()‘’

作用:该注解是springmvc4.3版本之后加入的,他可以用于修饰方法和参数

  • 出现在方法上:表示当前方法会在控制器执行之前执行。它可以修饰没有返回值的方法,也可以修饰有具体返回值的方法
  • 出现在参数上,获取指定的数据给参数赋值

属性:

  • value:用于获取数据的key。key可以是POJO的属性名称,也可以是map结构的key

应用场景:

  • 当表单提交数据不是完整的实体类数据时,保证没有提交数据的字段使用数据库对象原来的数据 例如:我们在编辑一个用户时,用户有一个创建信息的字段,该字段的值是不允许被修改的。在提交表单数据是肯定没有此字段的内容,一旦更新会把该字段内容置为null,此时就可以使用此注解解决问题
<form action="anno/testModelAttribute" method="post">
    用户:<input type="text" name="uname"><br/>
    密码:<input type="text" name="age"><br/>
    <input type="submit" value="提交">
</form>
  @RequestMapping("/testModelAttribute")
        public String testModelAttribute(User user){
            System.out.println(user);
            return "success";
        }
<span class="token comment">/**
 * 该方法会先执行,与表单提交重复的内容会被表单覆盖,表单没有的内容原封不动的传上去
 * @param uname
 * @return
 */</span>
<span class="token annotation punctuation">@ModelAttribute</span>
    <span class="token keyword">public</span> User <span class="token function">showUser</span><span class="token punctuation">(</span>String uname<span class="token punctuation">)</span><span class="token punctuation">{</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"showUser执行了"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//通过用户名查询数据库(模拟)</span>
        User user <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        user<span class="token punctuation">.</span><span class="token function">setUname</span><span class="token punctuation">(</span>uname<span class="token punctuation">)</span><span class="token punctuation">;</span>
        user<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        user<span class="token punctuation">.</span><span class="token function">setBirthday</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> user<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
 @RequestMapping("/testModelAttribute")
        public String testModelAttribute(@ModelAttribute("abc") User user){
            System.out.println(user);
            return "success";
        }

    /**
     * 未带参数的方法需要添加到 map中
     * @param uname
     * @param map
     */
    @ModelAttribute
    public void showUser(String uname, Map<String,User> map){
        System.out.println("showUser执行了");
        //通过用户名查询数据库(模拟)
        User user = new User();
        user.setUname(uname);
        user.setAge(20);
        user.setBirthday(new Date());
        map.put("abc",user);
    }

运行效果:表单只提交了两项,覆盖掉了showUser()方法中的相同参数的两项,方法中未重复的日期参数则会原封不动的传上去

  • @SessionAttributes()

作用:用于多次执行控制器方法间的参数共享 属性:

  • value:用于指定存入的属性名称
  • type:用于指定存入的数据类型
    /**
     * 存入session
     * @param model
     * @return
     */
    @RequestMapping("/testSessionAttributes")
    public String testSessionAttributes(Model model){
        model.addAttribute("msg","东方老赢");
        return "success";
    }
<span class="token comment">/**
 * 获取session
 * @return
 */</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/getSessionAttributes"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> String <span class="token function">getSessionAttributes</span><span class="token punctuation">(</span>ModelMap modelmap<span class="token punctuation">)</span><span class="token punctuation">{</span>
    String msg <span class="token operator">=</span> <span class="token punctuation">(</span>String<span class="token punctuation">)</span> modelmap<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"msg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>msg<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token string">"success"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">/**
 * 删除session
 * @return
 */</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/delSessionAttributes"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> String <span class="token function">delSessionAttributes</span><span class="token punctuation">(</span>SessionStatus status<span class="token punctuation">)</span><span class="token punctuation">{</span>
    status<span class="token punctuation">.</span><span class="token function">setComplete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token string">"success"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<a href="anno/testSessionAttributes">testSessionAttributes</a>
<a href="anno/getSessionAttributes">getSessionAttributes</a>
<a href="anno/delSessionAttributes">delSessionAttributes</a>

运行:

SpringMVC第二篇

  • 响应数据和结果视图
    • 返回值分类
 /**
     * 使用Model
     * @param model
     * @return
     */
    @RequestMapping("/testModel")
    public String testModel(Model model){
        User user = new User();
        user.setUsername("东方老赢");
        user.setPassword("123456");
        user.setAge(20);
        model.addAttribute(user);
        return "success";
    }
Copy
<span class="token comment">/**
 * 使用jsp
 */</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/testVoid"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testVoid</span><span class="token punctuation">(</span>HttpServletRequest request<span class="token punctuation">,</span> HttpServletResponse response<span class="token punctuation">)</span> <span class="token keyword">throws</span> ServletException<span class="token punctuation">,</span> IOException <span class="token punctuation">{</span>
    <span class="token comment">//编写请求转发的程序</span>

//        request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);


Copy
    <span class="token comment">//重定向(重定向无法请求/WEB-INF/pages中的jsp文件)</span>

//        response.sendRedirect(request.getContextPath()+"/response.jsp");


Copy
    <span class="token comment">//设置中文乱码</span>
    response<span class="token punctuation">.</span><span class="token function">setCharacterEncoding</span><span class="token punctuation">(</span><span class="token string">"UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    response<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">"text/html;charset=UTF-8"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">//直接会进行响应</span>
    response<span class="token punctuation">.</span><span class="token function">getWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"东方老赢"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>


<span class="token comment">/**
 * 使用ModelAndView
 */</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/testModelAndView"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> ModelAndView <span class="token function">testModelAndView</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    ModelAndView mv <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ModelAndView</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    User user <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">User</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    user<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span><span class="token string">"东方老赢"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    user<span class="token punctuation">.</span><span class="token function">setPassword</span><span class="token punctuation">(</span><span class="token string">"123456"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    user<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    mv<span class="token punctuation">.</span><span class="token function">addObject</span><span class="token punctuation">(</span><span class="token string">"user"</span><span class="token punctuation">,</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
    mv<span class="token punctuation">.</span><span class="token function">setViewName</span><span class="token punctuation">(</span><span class="token string">"success"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> mv<span class="token punctuation">;</span>
<span class="token punctuation">}</span>


SpringMVC框架提供的转发和重定向
Copy
    @RequestMapping("/testForwardOrRedirect")
    public String testForwardOrRedirect(){
        //请求的转发
//        return "forward:/WEB-INF/pages/success.jsp";
        //重定向
        return "redirect:/response.jsp";
    }
  • SpringMVC框架提供的转发和重定向
 @RequestMapping("/testForwardOrRedirect")
    public String testForwardOrRedirect(){
        //请求的转发
//        return "forward:/WEB-INF/pages/success.jsp";
        //重定向
        return "redirect:/response.jsp";
    }   
  • ResponseBody响应json数据
  • 在webapp下创建js文件夹,导入jquery.min.js
  • 导入json需要的jar包
<!--    json字符串与Javabean对象相互转换过程中,需要用到jackson的jar包-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.0</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.0</version>
    </dependency>
 
  • 编写JavaScript代码
<script src="js/jquery.min.js"></script>
    <script>
        // 页面加载,绑定单击事件
        $(function () {
            $("#btn").click(function () {
                //ajax异步请求
                $.ajax({
                    url:"user/testAjax",
                    contentType:"application/json;charset=UTF-8",
                    data:'{"username":"东方老赢","password":"123456","age":"22"}',
                    dataType:"json",
                    type:"post",
                    success:function (data) {
                        //data服务器端响应的json数据,进行解析
                        alert(data);
                        alert(data.username);
                        alert(data.password);
                        alert(data.age);
                    }
                })
            });
        });
    </script>

  • 编写controller代码,模拟服务器响应
/**
     * @RequestBody:服务器接收到的json格式的数据解析成实体类型
     * @ResponseBody:服务器响应时将返回的数据解析成json格式
     *
     * @param user
     * @return
     */
    @RequestMapping("/testAjax")
    public @ResponseBody User testAjax(@RequestBody User user){
        //客户端发送ajax请求,传的是ajax字符串,后端把json字符串封装到user对象中
        System.out.println(user);
        //做响应,模拟查询数据库
        user.setUsername("东方老赢");
        user.setPassword("654321");
        user.setAge(25);
        return user;
    }
 
  • SpringMVC实现文件上传
    • 文件上传的回顾
    • 文件上传的必要前提:
    1. form表单的enctype取值必须是:multipart/form-data(enctype:是表单请求正文的类型)
    2. method的属性取值必须是Post
    3. 提供一个文件选择域 <input type=“ file ”/>
    • 文件上传的原理分析: 当form表单的enctype取值不是默认值之后,request.getParameter( ) 将失效 enctype = “application/x-www-from-urlencoded”(即默认值)时,form表单的正文内容是:
      • key = value&key = value&key = value

      当form表单的enctype取值为 multipart/form-data 时,请求正文的内容就变成

      • 每一部分都是MIME类型描述的正文 ================================7de1a433602ac 分界符 Content- Disposition:form-data;name=“userName” 协议头 aaaa 协议正文 ================================7de1a433602ac 分界符 Content- Disposition : form-data ; name = “file”;filename = “C:\xxx\xxx\b.txt” Content-Type:text/plain 协议类型(MIME类型) bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ================================7de1a433602ac

    借助第三方组件实现文件上传: commons-fileupload-x.x.x.jar、commons-io-x.x.jar

    • SpringMVC传统方式文件上传
<dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
    </dependency>
    <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
    </dependency>
Copy
 <form action="user/fileupload1" method="post" enctype="multipart/form-data">
        浏览文件<input type="file" name="upload"/><br/>
        <input type="submit" value="上传"/>
    </form>
Copy
  @RequestMapping("/fileupload1")
    public String fileupload1(HttpServletRequest request) throws Exception {
        System.out.println("成功执行 传统文件上传");
Copy
    <span class="token comment">//使用fileupload组件完成文件上传</span>
    <span class="token comment">//上传的位置</span>
    String path <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getServletContext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getRealPath</span><span class="token punctuation">(</span><span class="token string">"/uploads/"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//判断该路径是否存在</span>
    File file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//不存在则创建该文件夹(在工程target文件夹下)</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>file<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        file<span class="token punctuation">.</span><span class="token function">mkdirs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">//解析request对象,获取上传文件项</span>
    DiskFileItemFactory factory <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DiskFileItemFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    ServletFileUpload upload <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ServletFileUpload</span><span class="token punctuation">(</span>factory<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//解析requesth得到一个list集合</span>
    List<span class="token generics function"><span class="token punctuation">&lt;</span>FileItem<span class="token punctuation">&gt;</span></span> items <span class="token operator">=</span> upload<span class="token punctuation">.</span><span class="token function">parseRequest</span><span class="token punctuation">(</span>request<span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>items<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//遍历</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span>FileItem item <span class="token operator">:</span> items<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//进行判断,当前item对象是否是上传文件项</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>item<span class="token punctuation">.</span><span class="token function">isFormField</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment">//为true,书名普通表单项</span>
            System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"普通表单项"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token comment">//为false说明上传文件项</span>
            <span class="token comment">//获取上传文件的名称</span>
            System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"上传文件项"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            String name <span class="token operator">=</span> item<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//完成文件上传</span>
            item<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span>name<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//删除临时文件</span>
            item<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token string">"success"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

  • SpringMVC跨服务器方式文件上传

  • 步骤:
  • 添加jar包
<!--    允许跨服务器上传文件-->
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-core</artifactId>
      <version>1.18.1</version>
    </dependency>
    <dependency>
      <groupId>com.sun.jersey</groupId>
      <artifactId>jersey-client</artifactId>
      <version>1.18.1</version>
    </dependency>
  </dependencies>
添加新表单
  • 添加新表单
<form action="user/fileupload3" method="post" enctype="multipart/form-data">
        浏览文件<input type="file" name="upload"/><br/>
        <input type="submit" value="Springmvc跨服务器文件上传"/>
    </form>
  • 创建新工程模拟图片服务器,并部署tomcat测试是否成功
  • Controller代码
@RequestMapping("/fileupload3")
    public String fileupload3(MultipartFile upload) throws Exception {
        System.out.println("成功执行 Springmvc跨服务器文件上传");
Copy
    String path <span class="token operator">=</span> <span class="token string">"http://localhost:9090/fileupload_war/uploads/"</span><span class="token punctuation">;</span>
    <span class="token comment">//获取上传文件的名称</span>
    String name <span class="token operator">=</span> upload<span class="token punctuation">.</span><span class="token function">getOriginalFilename</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//创建客户端的对象</span>
    Client client <span class="token operator">=</span> Client<span class="token punctuation">.</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//和图片服务器进行连接</span>
    WebResource webResource <span class="token operator">=</span> client<span class="token punctuation">.</span><span class="token function">resource</span><span class="token punctuation">(</span>path<span class="token operator">+</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//完成文件上传</span>
    webResource<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>upload<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token string">"success"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
  • 运行测试即可

细节:Tomcat默认不能进行delete和put,需要修改 readonly 属性;具体教程

  • SpringMVC的异常处理
    • 异常处理思路

    Controller调用service,service调用dao,异常都是向上抛出的,最终有DispatcherServlet找异常处理器进行一场的处理

  • SpringMVC的异常处理
  1. 编写自定义异常类(做提示信息)
package com.mvc.exception;
/**




@Author: 东方老赢




@Date: 2020/4/23 11:02

*/

public class SysException extends Exception {


//存储提示信息

private String message;


@Override

public String getMessage() {

return message;

}


public void setMessage(String message) {

this.message = message;

}


public SysException(String message) {

this.message = message;

}

}
  • 编写异常处理器
package com.mvc.exception;
import org.springframework.web.servlet.HandlerExceptionResolver;

import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

*




异常处理器





@Author: 东方老赢




@Date: 2020/4/23 12:02

*/

public class SysExceptionResolver implements HandlerExceptionResolver {

@Override

public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

//获取到异常对象

SysException sysException = null;

if (e instanceof SysException){

sysException = (SysException) e;

}else {

sysException = new SysException("系统正在维护。。。");

}


Copy
 <span class="token comment">//创建ModelAndView对象</span>
 ModelAndView mv <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ModelAndView</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 mv<span class="token punctuation">.</span><span class="token function">addObject</span><span class="token punctuation">(</span><span class="token string">"errorMsg"</span><span class="token punctuation">,</span>sysException<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 mv<span class="token punctuation">.</span><span class="token function">setViewName</span><span class="token punctuation">(</span><span class="token string">"error"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 <span class="token keyword">return</span> mv<span class="token punctuation">;</span>

}

}


 
  • 配置异常处理器
<!--    配置异常处理器-->
    <bean id="sysExceptionResolver" class="com.mvc.exception.SysExceptionResolver"></bean>
 
  • SpringMVC中的拦截器
    • 拦截器的作用

    SpringMVX中的拦截器类似于Servlet中的过滤器Filter,用于对处理器进行预处理和后处理 谈到拦截器,还要提一个词——拦截器链(Interceptor Chain)。拦截器链就是将拦截器按一定顺序联结成一条链。在访问被拦截的方法和字段之时,拦截器链中的拦截器就会按其之前定义的顺序被调用 拦截器预过滤器的区别:

    • 过滤器是servlet规范中的一部分,任何java web工程都可以用
    • 拦截器是SpringMVC框架自己的,只有使用了SpringMVC框架的工程才能使用
    • 过滤器在url-patten 中配置 /* 之后,可以对所有要访问的资源进行拦截。
    • 拦截器只会拦截访问的控制器的方法,如果访问的是 jsp、html、css、image或者js是不会进行拦截的
    • 细节:他也是AOP思想的具体应用
    • 想要自定义拦截器,必须要求实现:HandlerInterceptor接口
  • 自定义拦截器的步骤
    • 编写自定义拦截器
package com.mvc.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;


import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**




@Author: 东方老赢




@Date: 2020/4/23 18:01

*/

public class MyIntercepter1 implements HandlerInterceptor {


/**



预处理,controller方法执行前

return true:放行,执行下一个拦截器。如果没有,执行conroller方法

return false:不放行


@param request

@param response

@param handler

@return

@throws Exception

*/

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

System.out.println("Interceptor运行成功");

return true;

}

}
  • 配置拦截器(springmvc.xml)
<!--配置拦截器-->
    <mvc:interceptors>
    <!--配置单个拦截器-->
        <mvc:interceptor>
        <!--要拦截的具体方法
                path = /user/* 表示拦截当前类下的所有方法
                path = /**  表示拦截所有方法-->
            <mvc:mapping path="/user/*"/>
                <!--不要拦截的方法-->
            <!--<mvc:exclude-mapping path=""/>-->
                <!--配置拦截器对象-->
            <bean class="com.mvc.interceptor.MyIntercepter1"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

运行测试

  • 拦截器中的方法

1.preHandle:是controller方法执行之前拦截的方法

  • 可以使用request或者response跳转到指定页面
  • return true放行,执行下一个拦截器,如果没有拦截器,则直接执行controller中的方法
  • return false 不放行,不会执行controller中的方法

2.postHandle:是controller方法执行后执行的方法,在JSP视图执行前

  • 可以使用request或者response跳转到指定页面
  • 如果指定了跳转的页面,那么controller方法跳转的页面将不会显示

3.postHandle:是在JSP执行后执行

  • request或者response不能再跳转页面了

第三篇 重头戏:SSM整合

  • 搭建整合环境
    • 整合说明:SSM整合可以用多种形式,这里选用XML+注解方式

1.首先创建一个数据库

CREATE TABLE ssm(
	    id  INT PRIMARY KEY AUTO_INCREMENT,
	    NAME VARCHAR(20),
	    money DOUBLE
	);

2.创建Maven工程,导包

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.version>5.0.2.RELEASE</spring.version>
    <mysql.version>5.1.6</mysql.version>
    <mybatis.version>3.4.5</mybatis.version>
  </properties>
<dependencies>

<!--    spring-->

<!--AOP相关技术-->

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.6.8</version>

</dependency>


Copy
  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>AOPjar包<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>aop<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>$<span class="token punctuation">{</span>spring<span class="token punctuation">.</span>version<span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>context容器<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>context<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>$<span class="token punctuation">{</span>spring<span class="token punctuation">.</span>version<span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>mvc相关jar包<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>web<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>$<span class="token punctuation">{</span>spring<span class="token punctuation">.</span>version<span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>webmvc<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>$<span class="token punctuation">{</span>spring<span class="token punctuation">.</span>version<span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>单元测试<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>test<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>$<span class="token punctuation">{</span>spring<span class="token punctuation">.</span>version<span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>事务<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>tx<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>$<span class="token punctuation">{</span>spring<span class="token punctuation">.</span>version<span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>JDBC技术<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>springframework<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>spring<span class="token operator">-</span>jdbc<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>$<span class="token punctuation">{</span>spring<span class="token punctuation">.</span>version<span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>单元测试<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>junit<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>junit<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span><span class="token number">4.11</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>scope<span class="token punctuation">&gt;</span></span>compile<span class="token operator">&lt;</span><span class="token operator">/</span>scope<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>Mysql驱动jar包<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>mysql<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>mysql<span class="token operator">-</span>connector<span class="token operator">-</span>java<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>$<span class="token punctuation">{</span>mysql<span class="token punctuation">.</span>version<span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>Servlet相关jar包<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>javax<span class="token punctuation">.</span>servlet<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>servlet<span class="token operator">-</span>api<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span><span class="token number">2.5</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>scope<span class="token punctuation">&gt;</span></span>provided<span class="token operator">&lt;</span><span class="token operator">/</span>scope<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>javax<span class="token punctuation">.</span>servlet<span class="token punctuation">.</span>jsp<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>jsp<span class="token operator">-</span>api<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span><span class="token number">2.0</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>scope<span class="token punctuation">&gt;</span></span>provided<span class="token operator">&lt;</span><span class="token operator">/</span>scope<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>页面 el、jstl表达式<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>jstl<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>jstl<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span><span class="token number">1.2</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

<!--      log  strat-->

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-log4j12</artifactId>

<version>1.7.12</version>

</dependency>

<!--      log  end-->


Copy
  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>Mybatis相关jar包组件<span class="token operator">--</span><span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
      <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>mybatis<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
      <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>mybatis<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
      <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span>$<span class="token punctuation">{</span>mybatis<span class="token punctuation">.</span>version<span class="token punctuation">}</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>spring整合mybatis所需jar包<span class="token operator">--</span><span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
      <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>org<span class="token punctuation">.</span>mybatis<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
      <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>mybatis<span class="token operator">-</span>spring<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
      <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span><span class="token number">1.3</span><span class="token number">.0</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

  <span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>数据库连接池:c3p0<span class="token operator">--</span><span class="token operator">&gt;</span>
  <span class="token generics function"><span class="token punctuation">&lt;</span>dependency<span class="token punctuation">&gt;</span></span>
      <span class="token generics function"><span class="token punctuation">&lt;</span>groupId<span class="token punctuation">&gt;</span></span>c3p0<span class="token operator">&lt;</span><span class="token operator">/</span>groupId<span class="token operator">&gt;</span>
      <span class="token generics function"><span class="token punctuation">&lt;</span>artifactId<span class="token punctuation">&gt;</span></span>c3p0<span class="token operator">&lt;</span><span class="token operator">/</span>artifactId<span class="token operator">&gt;</span>
      <span class="token generics function"><span class="token punctuation">&lt;</span>version<span class="token punctuation">&gt;</span></span><span class="token number">0.9</span><span class="token number">.1</span><span class="token number">.1</span><span class="token operator">&lt;</span><span class="token operator">/</span>version<span class="token operator">&gt;</span>
      <span class="token generics function"><span class="token punctuation">&lt;</span>type<span class="token punctuation">&gt;</span></span>jar<span class="token operator">&lt;</span><span class="token operator">/</span>type<span class="token operator">&gt;</span>
      <span class="token generics function"><span class="token punctuation">&lt;</span>scope<span class="token punctuation">&gt;</span></span>compile<span class="token operator">&lt;</span><span class="token operator">/</span>scope<span class="token operator">&gt;</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>dependency<span class="token operator">&gt;</span>

</dependencies>

3.搭建环境

  • Spring框架代码的编写
  • 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--    开启注解扫描 只需要处理service和dao-->

<context:component-scan base-package="com.ssm">

<!--        配置哪些注解不扫描-->

<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

</context:component-scan>

</beans>


 
  • 编写代码测试
package com.ssm.service.impl;
import com.ssm.dao.IAccountDao;

import com.ssm.domain.Account;

import com.ssm.service.IAccountService;

import org.springframework.stereotype.Service;


import java.util.List;


/**



账户业务层


@Author: 东方老赢

@Date: 2020/4/24 9:29

*/


@Service("accountService")

public class AccountServiceImpl implements IAccountService {

@Override

public List<Account> findAll() {

System.out.println("业务层,查询所有。。。");

return null;

}


Copy
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">saveAccount</span><span class="token punctuation">(</span>Account account<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"业务层,保存账户。。。"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}


Copy
package com.ssm.test;

import com.ssm.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author: 东方老赢
 * @Date: 2020/4/24 9:46
 */
public class TestSpring {

    @Test
    public void run1(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("SpringConfig.xml");
        IAccountService accountService = (IAccountService) ac.getBean("accountService");
        accountService.findAll();
    }
}
 

运行结果:通过

  • Spring整合SpringMVC框架
    • 首先编写SpringMVC框架(详情见上)
      • 细节:springmvc.xml配置注解扫描
<!--    开启注解扫描,只扫描Controller注解-->
    <context:component-scan base-package="com.ssm">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

 
  • 然后开始整合
<!--  配置spring的监听器,默认只加载WEB-INF目录下的配置文件-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--  但是由于SpringConfig.xml配置文件不在WEB-INF目录下,所以需要指定路径-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:SpringConfig.xml</param-value>
  </context-param>
Copy
package com.ssm.controller;
import com.ssm.service.IAccountService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;


/**




账户web





@Author: 东方老赢




@Date: 2020/4/24 9:30

*/

@Controller

@RequestMapping("/account")

public class AccountController {


@Autowired

private IAccountService accountService;


@RequestMapping("/findAll")

public String findAll(){

System.out.println("表现层:查询所有。。。");

accountService.findAll();

return "success";

}

}

 
  • 运行结果
--Spring整合Mybatis框架-->
    <!--配置连接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
    <!--配置SqLSessionFactory工厂-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
Copy
<span class="token operator">&lt;</span><span class="token operator">!</span><span class="token operator">--</span>配置AccountDao接口所在包<span class="token operator">--</span><span class="token operator">&gt;</span>
<span class="token operator">&lt;</span>bean id<span class="token operator">=</span><span class="token string">"mapperScannerConfigurer"</span> <span class="token keyword">class</span><span class="token operator">=</span><span class="token string">"org.mybatis.spring.mapper.MapperScannerConfigurer"</span><span class="token operator">&gt;</span>
    <span class="token operator">&lt;</span>property name<span class="token operator">=</span><span class="token string">"basePackage"</span> value<span class="token operator">=</span><span class="token string">"com.ssm.dao"</span><span class="token operator">&gt;</span><span class="token operator">&lt;</span><span class="token operator">/</span>property<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>bean<span class="token operator">&gt;</span>


Copy
package com.ssm.dao;

import com.ssm.domain.Account;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author: 东方老赢
 * @Date: 2020/4/24 9:25
 */
@Repository
public interface IAccountDao {
    @Select("select * from ssm")
    List<Account> findAll();

    @Select("insert into ssm(name,money) values(#{name},#{money})")
    void saveAccount(Account account);
}
Copy
package com.ssm.service.impl;

import com.ssm.dao.IAccountDao;
import com.ssm.domain.Account;
import com.ssm.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 账户业务层
 *
 * @Author: 东方老赢
 * @Date: 2020/4/24 9:29
 */

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao accountDao;

    @Override
    public List<Account> findAll() {
        System.out.println("业务层,查询所有。。。");
        return accountDao.findAll();
    }

    @Override
    public void saveAccount(Account account) {
        System.out.println("业务层,保存账户。。。");
        accountDao.saveAccount(account);
    }
}
Copy
package com.ssm.controller;

import com.ssm.domain.Account;
import com.ssm.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 账户web
 *
 * @Author: 东方老赢
 * @Date: 2020/4/24 9:30
 */
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private IAccountService accountService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        System.out.println("表现层:查询所有。。。");
        List<Account> list = accountService.findAll();
        model.addAttribute("list",list);
        return "success";
    }
}

Copy
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    成功
    <c:forEach items="${list}" var="account">
        ${account.name}
        ${account.money}
    </c:forEach>
</body>
</html>
 

运行结果:

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • SpringMVC 访问controller层出错:No converter found for return value of type: class java.util.HashMap【转载自ht

    最近在搭建SSM框架时候,测试controller方法出现通过url请求不通,报错如下:

    _DIY
  • Java期末复习——主观题

    _DIY
  • C++中与类有关的注意事项(更新中~~~)

    当然了,首先调用基类的构造函数是不容置疑的,不管它在哪里,记住即可,不过关于对象成员的构造函数的调用还需注意, 见 L1, L2, L3, 它们的构造函数的调用...

    _DIY
  • HTML5原生拖放事件的学习与实践

    之前学习了 HTML5 的拖放事件,开发中也用到了拖拽组件。为了厘清整体的逻辑,专门做了一个小例子。

    心谭博客
  • CSS 基础系列:inline-blcok和float

    简单比对一下div+css布局中的inline-block和float的特点,同时附上使用inline-block之后元素之间产生空隙的解决方法。 虽然设置浮...

    Chor
  • Go内存模型

    Go语言中内存分配大致有3种模式:Stack、Heap、Fixed Size Segment。

    后台搬砖鹅
  • 不该缺少的Error体系

    导语: 几乎所有APP在开发过程中都会遇到错误,一些错误是在控制之外,例如磁盘空间不足或网络连接失败。而一些错误是可以预料得到的,例如视频正在处理中无法播放、传...

    MelonTeam
  • 微信企业号(公众号)开发流程汇总

             企业号(公众号)申请需要提前申请,因为有些东西需要备案,审核啥的;

    stys35
  • 相对定位一个例子,仿淘宝商品列表中的简单效果

    deepcc
  • Hortonworks中修改Hive使用postgresql数据库

    在Hive -> Configs -> Advanced中,将数据库改为使用已存在的postgresql数据库,然后修改数据库用户,密码和数据库名称。

    kongxx

扫码关注云+社区

领取腾讯云代金券