SpringMVC的学习总结

SpringMVC
1.什么是SpringMVC?
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的SpringMVC框架或集成其他WEB MVC开发框架,如Struts(现在一般不用),Struts2(一般老项目使用)等。
Spring MVC是属于Spring 框架的WEB模块中的一个部分。
参考Spring的体系结构图
在这里插入图片描述
SpringMVC是web层的mvc开发框架,属于Spring框架的WEB模块中的一个部分。
2.SpringMVC的执行流程
在这里插入图片描述
1.启动服务器的时候配置在web.xml文件中的中央控制器【DispatcherServlet】被初始化完成,并且加载配置的springMVC的配置文件。
2.客户端浏览器发送http请求。
3.http请求被中央控制器【DispatcherServlet】拦截,转交给url解析器解析。
4.Url解析器解析http请求,得到具体的请求路径。
5.Url解析器将解析得到的具体的请求路径返回给中央控制器【DispatcherServlet】。
6.中央控制器【DispatcherServlet】将得到的具体的请求路径转交给控制器适配器。
7.控制器适配器根据具体的请求路径查找与之对应的请求处理类。
8.请求处理类就执行具体的请求处理,得到ModelAndView对象【1.数据。2.跳转地址】,将ModelAndView对象交给控制器适配器,控制器适配器将ModelAndView对象返回给中央控制器【DispatcherServlet】。
9.中央控制器【DispatcherServlet】将ModelAndView对象转交给视图解析器去解析。
10.视图解析器解析ModelAndView对象,得到一个具体的数据显示路径,将这个具体的数据显示路径返回给中央控制器【DispatcherServlet】。
11.中央控制器【DispatcherServlet】得到具体的数据显示路径之后,将路径所代表的资源转换执行成一个html数据。
将转换执行后的html数据返回给客户端浏览器。
3.请求的访问路径配置
1.web.xml文件中DispatcherServlet配置的
1.1 /
对应 http://127.0.0.1:8080/springmvc1/add
1.2 .do
对应 http://127.0.0.1:8080/springmvc1/add.do
1.3 / 这是一个错误的方式

2.springmvc配置文件中的控制器的name属性值
控制器的name属性值设置要与web.xml文件中DispatcherServlet配置的
的配置形式一致
2.1 web.xml文件中DispatcherServlet配置的/那么springmvc配置文件中的控制器的name属性值应该为“/xxxx”
2.2 web.xml文件中DispatcherServlet配置的*.do那么springmvc配置文件中的控制器的name属性值应该为“/xxxx.do”
以后控制器的name属性值会被@RequestMapper注解的value属性代替
5.请求处理的控制器类
5.1创建一个普通的java类,实现org.springframework.web.servlet.mvc.Controller
接口
5.2.重写handleRequest方法【处理用请求的方法】
5.3在springmvc的配置文件中配置

<!-- 控制器 -->
<!-- 与web.xml文件中的中央处理器DispatcherServlet的url-pattern的配置形式匹配-->
<bean name="/hello" class="com.click369.springmvc.controller.HelloController"></bean>

例如:

package com.click369.springmvc.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloController implements Controller{
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("HelloController---请求处理方法");
        ModelAndView  mav=new ModelAndView();
        mav.addObject("username","zhangsan");
        mav.setViewName("test");
        return mav;
    }
}

4.ModelAndView类
1.请求处理方法的返回值是ModelAndView类
2.组成ModelAndView类的第一部分是一个Model【模型】封装数据
第二部分是一个View【视图】展示数据的页面元素
3.ModelAndView类是用来封装数据,展示数据的页面元素。
4.构造方法ModelAndView()来创建ModelAndView类的对象
ModelAndView mav=new ModelAndView();
5.封装数据的方法addObject(attributeName, attributeValue)相当于setAttribute(key,value)
mav.addObject(“username”,“zhangsan”);
6.设置展示数据的页面元素的名称setViewName(viewName)
mav.setViewName(“test”);

mav.setViewName("test.jsp");
mav.setViewName("test.html");
mav.setViewName("控制器对应的请求处理路径");

mav.setViewName("test");---forword跳转[转发]
mav.setViewName("forward:test.jsp");
mav.setViewName("redirect:test.jsp");----sendRedirect跳转[重定向]

5.视图解析器
InternalResourceViewResolver------解析展示数据的页面元素

<!-- 视图解析器 -->
<!-- 利用请求处理类中的得到的视图名,通过视图解析器的前缀和后缀合并得到一个完整的页面元素的访问路径。 -->
	 <!-- prefix+视图名称+suffix : 完整的页面元素的访问路径 -->
	 <!-- prefix==http://localhost:8080/demo1/ -->
	 <!-- 视图名称:ModelAndView中setViewName("test")的参数test -->
	 <!-- suffix:后缀[.jsp] -->
	 <!-- http://localhost:8080/demo1/test.jsp -->
	 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 	<property name="prefix" value="/"></property>
	 	<property name="suffix"  value=""></property>
	 </bean> 

6**.DispatcherServlet中央处理器设置加载SpringMVC配置文件的设置**
我们在启动程序的时候需要加载SpringMVC配置文件,此时这个SpringMVC配置文件的位置可能有所不同,SpringMVC配置文件在不同的位置是DispatcherServlet中央处理器如何配置SpringMVC配置文件的位置。
1.通过init-param元素配置加载SpringMVC配置文件
2.SpringMVC配置文件在src/main/resources文件夹下的时候:
src/main/resources----->springmvcconfig.xml

<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvcconfig.xml</param-value>
</init-param>

3.SpringMVC配置文件在WEB-INF文件夹下的时候:
src/main/webapp/WEB-INF----->springmvcconfig.xml

<init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/myspringmvc.xml</param-value>
</init-param>

4.如果我们不在web.xml文件中配置init-param元素配置加载SpringMVC配置文件
1.将SpringMVC配置文件放放置在WEB-INF文件夹下
2.SpringMVC配置文件,名称“-servlet.xml”
例如:myspringmvc-servlet.xml

<servlet>
	    <servlet-name>springmvc</servlet-name>
	    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<servlet>

SpringMVC配置文件
src/main/webapp/WEB-INF-------[springmvc-servlet.xml]
注意:通常情况下我们把SpringMVC配置文件/资源文件【jsp/html】放置在WEB-INF文件夹
7.@Controller/@RequestMapping/@Service/@RestController/@Component/@Autowired/
@Resource/@RequestBody/@ResponseBody

步骤1:开启SpringMVC注解配置 <mvc:annotation-driven />
可以不用在SpringMVC的配置文件中配置

org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,

自己编写的处理用户请求的控制器类也不用配置
但是不能省略视图解析器的配置,因为视图解析器中前缀名和后缀名都是需要自己手动配置的。
步骤2.需要在处理用户请求的控制器类上添加@Controller,在请求处理方法上添加@RequestMapping配置请求处理方法的路径
步骤3:SpringMVC配置文件中通过
<context:component-scan base-package="包名"></context:component-scan>设置Spring中的自动扫描的包
步骤4.如果需要视图解析器就配置,如果不需要就不用配置。

<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix"  value="/WEB-INF/"></property>
         <property name="suffix"  value=".jsp"></property>
</bean>

使用注解开发程序,减少配置

例如:
p

ackage com.click369.springmvc.handler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestHandler{
	@RequestMapping("/hello.test")
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("处理用户请求1111111111..............");
		return null;
	}
}
<?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"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
	http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 开启Springmvc的注解驱动 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!-- 设置Spring中的自动扫描的包 -->
    <context:component-scan base-package="com.click369.springmvc.handler"></context:component-scan>
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix"  value="/WEB-INF/"></property>
         <property name="suffix"  value=".jsp"></property>
    </bean>
</beans>

SpringMVC中常用的注解
1.@Controller—表示我们所编写的java类是一个处理请求的控制器类。
只能作用在java类。
可以使用@Component去代替,在javaweb程序中是分层出来的为了表名java类是一个控制器,我们才使用@Controller

2.@RequestMapping–设置控制器类/请求处理方法的访问路径的。
可以作用在java类,表示配置这个java类的访问路径;
例如:

package com.click369.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value="/test")
public class HelloController {
	@RequestMapping(value="/hello")
	public  ModelAndView  sayHello(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("test","hello");
		mav.setViewName("test.jsp");
		return mav;
	}
}

访问路径:http://localhost:8080/demo1/test/hello

也可以作用在请求处理方法上,表示配置这个请求处理方法的访问路径。
例如:

package com.click369.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
	@RequestMapping(value="/hello")
	public  ModelAndView  sayHello(){
		ModelAndView mav=new ModelAndView();
		mav.addObject("test","hello");
		mav.setViewName("test.jsp");
		return mav;
	}
}

访问路径:http://localhost:8080/springmvcdemo1/hello

@Controller
@RequestMapping("/test")
public class TestHandler{
	@RequestMapping("/hello.test")
	public ModelAndView handleRequest() throws Exception {
		System.out.println("处理用户请求1111111111..............");
		return null;
	}
}

访问路径:http://localhost:8080/springmvcdemo1/test/hello.test

设置请求路径时@RequestMapping("/test")与@RequestMapping(“test”)与@RequestMapping(value="/test")都是一样的,习惯使用那一个就用那一个,@RequestMapping(value="/test")这个写法是标准操作。

@RequestMapping的属性
1.value表示设置访问路径 @RequestMapping(value="/test")
设置访问路径的时候可以设置通配符
? : 匹配任何单字符

例如:@RequestMapping("/?hello.test")
http://localhost:8080/springmvcdemo1/test/ahello.test
http://localhost:8080/springmvcdemo1/test/Mhello.test
http://localhost:8080/springmvcdemo1/test/Bhello.test
http://localhost:8080/springmvcdemo1/test/ahello.test
http://localhost:8080/springmvcdemo1/test/Mhello.test
错误:
http://localhost:8080/springmvcdemo1/test/hello.test
http://localhost:8080/springmvcdemo1/test/aahello.test
  • : 匹配任意数量的字符
例如:@RequestMapping("/*/hello.test")
http://localhost:8080/springmvcdemo1/test/asfasddf/hello.test
http://localhost:8080/springmvcdemo1/test/user/hello.test
http://localhost:8080/springmvcdemo1/test/my/hello.test

** : 匹配多个路径
例如:@RequestMapping("/**/hello.test")
http://localhost:8080/springmvcdemo1/test/user/my/dox/hello.test

2.method–限制请求的访问方式

      @RequestMapping(value="/my",method=RequestMethod.GET)
      Test.jsp
        <form action="test/my" method="get">
       <input type="submit" value="测试">
       </form>
http://localhost:8080/springmvcdemo1/test/my?

      @RequestMapping(value="/my",method=RequestMethod.POST)
      Test.jsp
        <form action="test/my" method="post">
           <input type="submit" value="测试">
       </form>
http://localhost:8080/springmvcdemo1/test/myS

8.请求处理方法接收请求参数值
http请求:http://localhost:8080/mvc/user/login/zhangsan/123456
请求参数:/zhangsan/123456
1.@PathVariable 定义在方法上获取请求url路径上的参数数据
例如:

package com.click369.springmvc.handler;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value="/user")
public class UserHandler {
    //处理注册请求的方法
	@RequestMapping(value="/register1/{username}/{mypass}",method=RequestMethod.GET)
	public  ModelAndView  registerMethod1(@PathVariable("username")String name,
			@PathVariable("mypass")String pass)throws Exception{
		System.out.println("name=="+name);
		System.out.println("pass=="+pass);
		ModelAndView  mav=new ModelAndView();
		mav.addObject("myname",name);
		mav.addObject("mypass",pass);
		mav.setViewName("test");
		return mav;
	}
}

测试:http://localhost:8080/mvc/user/register1/zhangsan/123456

http请求:http://localhost:8080/springmvcdemo1/test/my?name=zhangsan&password=123456
请求参数:name=zhangsan&password=123456
2.@RequestParam 定义在方法上,获取请求中通过key=value方式传递的参数数据
例如:get方式提交请求

 //处理注册请求的方法
	@RequestMapping(value="/register2",method=RequestMethod.GET)
	public  ModelAndView  registerMethod2(@RequestParam("username")String name,
			@RequestParam("password")String pass)throws Exception{
			System.out.println("name=="+name);
			System.out.println("pass=="+pass);
			ModelAndView  mav=new ModelAndView();
			mav.addObject("myname",name);
			mav.addObject("mypass",pass);
			mav.setViewName("test");
			return mav;
		}

测试:http://localhost:8080/mvc/user/register2?username=lisi&password=000000

post方式提交请求

//处理注册请求的方法
		@RequestMapping(value="/register3",method=RequestMethod.POST)
		public  ModelAndView  registerMethod3(@RequestParam("username")String name,
				@RequestParam("password")String pass)throws Exception{
				System.out.println("name=="+name);
				System.out.println("pass=="+pass);
				ModelAndView  mav=new ModelAndView();
				mav.addObject("myname",name);
				mav.addObject("mypass",pass);
				mav.setViewName("test");
				return mav;
			}

测试:http://localhost:8080/mvc/user/register3

3.HttpServletRequest对象接收数据
例如:

//处理注册请求的方法
@RequestMapping(value="/register4",method=RequestMethod.POST)
public ModelAndView registerMethod4(HttpServletRequest req)throws Exception{
String name=req.getParameter(“username”);
String pass=req.getParameter(“password”);
ModelAndView mav=new ModelAndView();
mav.addObject(“myname”,name);
mav.addObject(“mypass”,pass);
mav.setViewName(“test”);
return mav;
}

  1. 在请求处理方法中定义对应参数的变量,变量的名称与页面元素的name属性值相同
    //处理注册请求的方法

    @RequestMapping(value="/register5",method=RequestMethod.POST)
    public ModelAndView registerMethod5(String username,String password)throws Exception{
    //String name=req.getParameter(“username”);
    //String pass=req.getParameter(“password”);
    ModelAndView mav=new ModelAndView();
    mav.addObject(“myname”,username);
    mav.addObject(“mypass”,password);
    mav.setViewName(“test”);
    return mav;
    }

9.SpringMVC的文件上传和下载
SpringMVC的文件上传
1.需要使用Apache的commons-fileupload组件【文件上传组件】
2.需要配置一个文件上传的视图解析器CommonsMultipartResolver
3.创建上传页面
1.表单的提交方式method一定是post
2.表单中的enctype属性一定是multipart/form-data
3.表单中的文件上传元素
4.创建文件上传的控制器
例如:
Pom.xml

<!-- 配置commons-fileupload的依赖 -->
		<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>

springmvc.xml
<!-- 上传文件的视图解析器 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
     <property name="maxUploadSize" value="104857600" />
     <property name="maxInMemorySize" value="4096" />
     <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

Upload.jsp
<body>
    <center>
       <form action="upload" method="post" enctype="multipart/form-data">
           <input type="file"  name="myfile"/><br>
           <input type="submit" value="上传文件">
       </form>
    </center>
</body>
TestUploadHandler.java
package com.click369.controller;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UploadController {
	@RequestMapping(value="/upload")
	public ModelAndView uploadMethod(HttpServletRequest request)throws Exception{
		ModelAndView  mav=new ModelAndView();
		//将当前类中的ServletContext对象转换成CommonsMultipartResolver
		ServletContext servletContext=request.getSession().getServletContext();
		CommonsMultipartResolver  commonsMultipartResolver =new CommonsMultipartResolver(servletContext);
		//检查form中是否有enctype="multipart/form-data"
		if(commonsMultipartResolver.isMultipart(request)){
			//将HttpServletRequest对象变成多部分MultipartHttpServletRequest 
			MultipartHttpServletRequest multipartreq=(MultipartHttpServletRequest)request;
			//从MultipartHttpServletRequest对象中得到文件名称
			Iterator<String>  itname=multipartreq.getFileNames();
			//变量Iterator
			while(itname.hasNext()){
				//得到文件名称
				 String nameshuxing=itname.next().toString();
				//根据文件名称得到具体文件
				MultipartFile multipartfile=multipartreq.getFile(nameshuxing);
				String newfilename=System.currentTimeMillis()+multipartfile.getOriginalFilename().substring(multipartfile.getOriginalFilename().lastIndexOf("."));
				if(multipartfile!=null){
					// 获取项目的根目录
					String realPath = servletContext.getRealPath("/uploadpic");
					File uploadpicdir = new File(realPath);
					if(!uploadpicdir.exists()){
						//创建保存图片的文件夹路径
						uploadpicdir.mkdirs();
					}
					String path=uploadpicdir.getAbsolutePath()+File.separator+newfilename;
					//开始上传
					multipartfile.transferTo(new File(path));
				}
				String reqURL=request.getRequestURL().toString();
				reqURL=reqURL.substring(0,reqURL.lastIndexOf("/"));
				reqURL=reqURL+"/uploadpic/"+newfilename;
				}
			mav.setViewName("success.html");
		}else{
			mav.setViewName("error.html");
		}
		return mav;
	}
}

SpringMVC的文件下载
例如:

<a  href="dowload?filename=Java学习之路.txt">下在Java学习之路.txt</a>
@RequestMapping(value="/dowload")
	public ResponseEntity<byte[]> dowloadMethod(HttpServletRequest req)throws Exception{
		String filename=req.getParameter("filename");
		String realPath = req.getSession().getServletContext().getRealPath("/uploadpic");
		File uploadpicdir = new File(realPath);
		File file=new File(uploadpicdir,filename);
		System.out.println(file.getAbsolutePath());
		HttpHeaders headers = new HttpHeaders();          
	    headers.setContentDispositionFormData("attachment",filename); 
	    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); 
	   return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);  
	}

10.SSM
S-Spring [业务]
S-SpingMVC [web]
M-MyBatis [dao]

1.创建数据库表

DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(20) DEFAULT NULL,
  `user_pass` varchar(20) DEFAULT NULL,
  `user_age` int(11) DEFAULT NULL,
  `user_address` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

2.创建javaee项目
3.导入依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<!-- spring-jdbc -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>
<!-- MyBatis依赖 -->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.6</version>
</dependency>
<!-- mybatis-spring 整合包 -->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.1</version>
</dependency>
<!-- mysql数据库驱动 -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.38</version>
</dependency>
<!--druid 阿里的连接池-->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.9.8</version>
</dependency>

4.完善项目结构
5.利用MyBatis的逆向工程,生成javabean和数据访问接口,以及SQL映射文件
6.将生成的javabean和数据访问接口,以及SQL映射文件复制到我们的项目中
7.为生成的javabean添加序列化接口
8.在src/main/resources创建mapper文件夹,将SQL映射文件移动到该文件夹中。
9.创建业务访问接口及其实现类
10.创建控制器类和请求处理方法
11.创建数据库连接文件mydata.properties

mydriver = com.mysql.jdbc.Driver
myurl = jdbc:mysql://127.0.0.1:3306/test
myusername = root
mypassword = 123456

12.创建springmvc/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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 加载mydata.properties -->
   <context:property-placeholder location="classpath:mydata.properties"></context:property-placeholder>
    <!--配置阿里的数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${mydriver}"></property>
        <property name="url" value="${myurl}"></property>
        <property name="username" value="${myusername}"></property>
        <property name="password" value="${mypassword}"></property>
    </bean>
    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入数据源进入SqlSessionFactory -->
        <property name="dataSource" ref="dataSource"></property>
        <!--注入sql映射文件-->
        <property name="mapperLocations" value="classpath:mapper/UserBeanMapper.xml"></property>
    </bean>
    <!--配置扫描数据库访问接口包,创建数据库访问接口对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.click369.ssm.mapper"></property>
    </bean>
    <!--开启注解功能-->
    <context:annotation-config></context:annotation-config>
    <!-- 配置自动扫描service包 -->
    <context:component-scan base-package="com.click369.ssm"></context:component-scan>
    <!-- 开启mvc注解 -->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

13.配置web.xml

<!-- 配置中央处理器-->
<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <!--http://127.0.0.1:8080/springmvc1/add.do-->
  <url-pattern>*.do</url-pattern>
</servlet-mapping>

14.创建页面
15.部署测试

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值