文章目录

  • 文件上传
    • 准备一个文件上传的表单
    • 导入文件上传需要的jar包
    • 配置文件上传解析器
    • 编写文件上传的Controller控制器中的代码:
  • 使用ResponseEntity返回值处理文件下载
  • 使用@ResponseBody将返回的数据转成json
    • 使用的步骤如下:
    • Controller中的代码:
  • 使用@RequestBody接收请求体数据
  • HttpMessageConverter原理 (了解)
  • SpringMVC中异常处理
    • 使用@ExceptionHandler注解处理异常
    • 使用@ControllerAdvice注解处理异常
    • 异常处理优先顺序
    • 使用SimpleMappingExceptionResolver类映射异常跳转
  • 三大框架ssm(Spring+SpringMVC+Mybatis)的整合
    • 测试数据库
    • 创建一个动态Web工程
    • 然后导入整合Spring+SpringMVC+Mybatis的所有jar包
    • 各种配置文件

文件上传

文件上传在SpringMVC中如何实现:
1、准备一个文件上传的表单
2、导入文件上传需要的jar包
commons-fileupload-1.2.1.jar、commons-io-1.4.jar

3、配置文件上传解析器 CommonsMultipartResolver
4、配置Controller控制器的代码

准备一个文件上传的表单

文件上传的表单 <br/>
<form action="${pageContext.request.contextPath}/upload"enctype="multipart/form-data"method="post">用户名: <input type="text" name="username" /> <br>头像: <input type="file" name="photo" /> <br><input type="submit" value="上传喽">
</form>

导入文件上传需要的jar包

commons-fileupload-1.2.1.jar
commons-io-1.4.jar

配置文件上传解析器

<!-- 配置文件上传的解析器id值必须是 multipartResolver , 否则无效-->
<bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 防止中文乱码 --><property name="defaultEncoding" value="UTF-8" />
</bean>

编写文件上传的Controller控制器中的代码:

@RequestMapping(value = "/upload")
public String upload(@RequestParam(name = "username") String username,@RequestParam(name = "photo") MultipartFile photo){System.out.println(" 用户名 : " + username );// 获取上传文件名String filename = photo.getOriginalFilename();System.out.println("上传的文件名: " + filename);try {// transferTo() 将文件保存到某个位置photo.transferTo(new File("e:/" + filename));} catch (IOException e) {e.printStackTrace();}return "redirect:/index.jsp";
}

使用ResponseEntity返回值处理文件下载

  /*** 文件下载*/@RequestMapping(value = "/download")public ResponseEntity<byte[]> download(HttpSession httpSession) throws IOException {ServletContext servletContext = httpSession.getServletContext();// 文件的下载的步骤:
//          1 读取下载文件的内容 ( 通过ServletContext对象 )<br/>InputStream resourceAsStream = servletContext.getResourceAsStream("/file/d.jpg");System.out.println(resourceAsStream);// 将流里的数据转换为byte字节数组byte[] bytes = IOUtils.toByteArray(resourceAsStream);// 响应头的内容MultiValueMap headers = new HttpHeaders();
//          3 设置下载的文件的数据类型 <br/>headers.add("Content-Type", servletContext.getMimeType("/file/d.jpg"));
//          4 设置响应头,告诉客户端收到的文件做下载使用. <br/>headers.add("Content-disposition", "attachement; filename=d.jpg");/*** ResponseEntity表示返回给客户端的内容<br/>*  响应行 (响应状态码.第三个参数)<br/>*  响应头 (第二个参数)<br/>*  响应体 (第一个参数)<br/>*/ResponseEntity<byte[]> responseEntity = new ResponseEntity<byte[]>(bytes,headers, HttpStatus.OK);return responseEntity;}

使用@ResponseBody将返回的数据转成json

使用的步骤如下:

1、导入json相关的包到web工程中
jackson-annotations-2.1.5.jar
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar
2、编写一个请求的方式接收请求,并返回数据对象
3、在方法上添加注解@ResponseBody自动将返回值json化

Controller中的代码:

@Controller
public class PersonController {/*** 希望返回的是json数据 <br/>* @ResponseBody是将返回的对象转换为json数据 <br/>*/@ResponseBody@RequestMapping(value = "/queryPerson")public Person queryPersonById(){// 根据id查询一个person对象返回return new Person(100,"国哥爱你们");}}

返回一个对象的测试:
http://localhost:8080/25_springmvc_last/queryPerson

@RequestMapping(value = "/queryPersons")
@ResponseBody
public List<Person> queryPersons(){List<Person> list = new ArrayList<>();list.add(new Person(1, "张飞"));list.add(new Person(2, "关羽"));list.add(new Person(3, "赵云"));return list;
}

测试的地址:

http://localhost:8080/25_springmvc_last/queryPersons

返回Map数据测试

@ResponseBody
@RequestMapping(value = "/queryForMap")
public Map<String,Object> queryForMap(){Map<String,Object> map = new HashMap<>();map.put("key1", "没想到 这么帅的老师 语文好的不得了。。");map.put("key2", "国哥号称 海南彭于晏");map.put("key3", true);map.put("key4", new Integer(100));return map;
}

测试地址:

http://localhost:8080/25_springmvc_last/queryForMap

使用@RequestBody接收请求体数据

jsp页面的代码:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html><head><title>springmvc</title><script type="text/javascript" src="${pageContext.request.contextPath}/script/jquery-1.7.2.js" ></script><script type="text/javascript">// 页面加载完成之后$(function () {// 查询一个Person对象返回$("#queryOne").click(function () {// 发Ajax请求$.getJSON("${pageContext.request.contextPath}/queryPerson","",function (data) {console.log(data);});});// 查询多个Person对象$("#queryMany").click(function () {$.getJSON("${pageContext.request.contextPath}/queryPersons","",function (data) {console.log(data);});});$("#queryMap").click(function () {$.getJSON("${pageContext.request.contextPath}/queryForMap","",function (data) {console.log(data);});});/*** 给服务器发json数据*/$("#addPerson").click(function () {var personJson = {"id":1,"name":"zza"};// contentType 设置发送给服务器的数据格式类型// 一般情况下. contentType属性和 @RequestBody 一级组合使用.完成json操作/*** application/x-www-form-urlencoded ==>>> 表示发送的格式是: name=value&name=value <br/>* application/json ===>>>   表示发送的格式是: json字符串 <br/>*/$.ajax({url:"${pageContext.request.contextPath}/addPerson",data:JSON.stringify(personJson),//把json对象转换为json字符串.发送给服务器type:"POST",//发json数据,必须是post请求success:function (data) {console.log(data);},dataType:"json",contentType:"application/json"});});});</script></head><body><a id="queryOne" href="#">查询一个对象</a><a href="#" id="queryMany">查询多对象</a><a href="#" id="queryMap">查询Map对象</a><a href="#" id="addPerson">添加用户</a></body>
</html>

服务器端Controller的代码

package com.atguigu.controller;import com.atguigu.pojo.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;@Controller
public class PersonController {/*** 希望返回的是json数据 <br/>* @ResponseBody是将返回的对象转换为json数据 <br/>*/@ResponseBody@RequestMapping(value = "/queryPerson")public Person queryPersonById(){// 根据id查询一个person对象返回return new Person(100,"国哥爱你们");}@RequestMapping(value = "/queryPersons")@ResponseBodypublic List<Person> queryPersons(){List<Person> list = new ArrayList<>();list.add(new Person(1, "张飞"));list.add(new Person(2, "关羽"));list.add(new Person(3, "赵云"));return list;}@ResponseBody@RequestMapping(value = "/queryForMap")public Map<String,Object> queryForMap(){Map<String,Object> map = new HashMap<>();map.put("key1", "没想到");map.put("key2", "彭于晏");map.put("key3", true);map.put("key4", new Integer(100));return map;}/*** @ResponseBody 是把返回的对象转换为json字符串<br/>* @RequestBody 是把请求体中的json字符串转换为对象*/@ResponseBody@RequestMapping(value = "/addPerson")public Map<String, Object> addPerson(@RequestBody Person person){System.out.println("服务器收到的数据是=>>>" + person);Map<String,Object> map = new HashMap<>();map.put("isOk", true);return map;}
}

HttpMessageConverter原理 (了解)

HttpMessageConverter< T >
1) HttpMessageConverter< T > 是 Spring3.0 新添加的一个接口,负责将请求信息转换为一个对象(类型为 T),将对象(类型为 T)输出为响应信息
2) HttpMessageConverter接口定义的方法:
① Boolean canRead(Class<?> clazz,MediaType mediaType): 指定转换器可以读取的对象类型,即转换器是否可将请求信息转换为 clazz 类型的对象,同时指定支持 MIME 类型(text/html,applaiction/json等)
② Boolean canWrite(Class<?> clazz,MediaType mediaType):指定转换器是否可将 clazz 类型的对象写到响应流中,响应流支持的媒体类型在MediaType 中定义。
③ List< MediaType > getSupportMediaTypes():该转换器支持的媒体类型。
④ T read(Class<? extends T> clazz,HttpInputMessage inputMessage):将请求信息流转换为 T 类型的对象。
⑤ void write(T t,MediaType contnetType,HttpOutputMessgae outputMessage):将T类型的对象写到响应流中,同时指定相应的媒体类型为 contentType。

3) 如果需要自定义HttpMessageConverter消息转换器,可以通过继承 org.springframework.http.converter.AbstractHttpMessageConverter 类来实现
a) T readInternal(Class<? extends Person> clazz, HttpInputMessage inputMessage) 将客户端消息转换成为服务器需要的类型
b) boolean supports(Class<?> clazz) 判断是否支持对指定类型的处理
c) void writeInternal(Person person, HttpOutputMessage outputMessage) 将Controller控制器返回类型转换为客户端需要的数据

SpringMVC中异常处理

使用@ExceptionHandler注解处理异常

@Controller
public class HelloController {/*** @ExceptionHandler 注解表示当前方法用于捕获异常 <br/>*/@ExceptionHandlerpublic String exceptionHandler1(Exception e) {System.out.println(" HelloController#exceptionHandler1( Exception e ) 被调用了");System.out.println(e);return "/error1.jsp";}/*** @ExceptionHandler 注解表示当前方法用于捕获异常 <br/>* 异常越精确,越优先选择*/@ExceptionHandlerpublic String exceptionHandler2(RuntimeException e) {System.out.println(" HelloController#exceptionHandler2( RuntimeException e ) 被调用了");System.out.println(e);return "/error1.jsp";}/*** @ExceptionHandler 注解表示当前方法用于捕获异常 <br/>* 异常越精确,越优先选择*/@ExceptionHandlerpublic String exceptionHandler3(ArithmeticException e) {System.out.println(" HelloController#exceptionHandler3( ArithmeticException e ) 被调用了");System.out.println(e);return "/error1.jsp";}@RequestMapping(value = "/hello")public String hello() {System.out.println(" hello() 方法被调用了 ");int i = 12 / 0;return "redirect:/index.jsp";}}

使用@ControllerAdvice注解处理异常

/*** @ControllerAdvice 注解表示全局异常处理*/
@ControllerAdvice
@Controller
public class HelloController {/*** @ExceptionHandler 注解表示当前方法用于捕获异常 <br/>*/@ExceptionHandlerpublic String exceptionHandler1(Exception e) {System.out.println(" HelloController#exceptionHandler1( Exception e ) 被调用了");System.out.println(e);return "/error1.jsp";}/*** @ExceptionHandler 注解表示当前方法用于捕获异常 <br/>* 异常越精确,越优先选择*/@ExceptionHandlerpublic String exceptionHandler2(RuntimeException e) {System.out.println(" HelloController#exceptionHandler2( RuntimeException e ) 被调用了");System.out.println(e);return "/error1.jsp";}/*** @ExceptionHandler 注解表示当前方法用于捕获异常 <br/>* 异常越精确,越优先选择*/@ExceptionHandlerpublic String exceptionHandler3(ArithmeticException e) {System.out.println(" HelloController#exceptionHandler3( ArithmeticException e ) 被调用了");System.out.println(e);return "/error1.jsp";}@RequestMapping(value = "/hello")public String hello() {System.out.println(" hello() 方法被调用了 ");int i = 12 / 0;return "redirect:/index.jsp";}}

异常处理优先顺序

在局部异常处理和全局异常处理同时存在的时候,优先顺序是:
1、局部优先 ---->>>> 2、精确优化

使用SimpleMappingExceptionResolver类映射异常跳转

<!-- 配置异常跳转映射信息 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><!-- 不同异常.有不同的跳转路径 --><property name="exceptionMappings"><props><!--prop 配置一个异常和一个跳转路径key 属性指定异常全类名越精确越优先--><prop key="java.lang.Exception">/error1.jsp</prop><prop key="java.lang.RuntimeException">/error2.jsp</prop><prop key="java.lang.ArithmeticException">/error3.jsp</prop></props></property>
</bean>

三大框架ssm(Spring+SpringMVC+Mybatis)的整合

测试数据库

drop database if exists ssm;create database ssm;use ssm; ##创建图书表
create table t_book(`id` int(11) primary key auto_increment,  ## 主键`name` varchar(50) not null,             ## 书名 `author` varchar(50) not null,              ## 作者`price` decimal(11,2) not null,              ## 价格`sales` int(11) not null,                    ## 销量`stock` int(11)                              ## 库存
);## 插入初始化测试数据
insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , 'java从入门到放弃' , '国哥' , 80 , 9999 , 9 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , '数据结构与算法' , '严敏君' , 78.5 , 6 , 13 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , '怎样拐跑别人的媳妇' , '龙伍' , 68, 99999 , 52 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , '木虚肉盖饭' , '小胖' , 16, 1000 , 50 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , 'C++编程思想' , '刚哥' , 45.5 , 14 , 95 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , '蛋炒饭' , '周星星' , 9.9, 12 , 53 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , '赌神' , '龙伍' , 66.5, 125 , 535 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , 'Java编程思想' , '阳哥' , 99.5 , 47 , 36 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , 'JavaScript从入门到精通' , '婷姐' , 9.9 , 85 , 95 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , 'cocos2d-x游戏编程入门' , '国哥' , 49, 52 , 62 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , 'C语言程序设计' , '谭浩强' , 28 , 52 , 74 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , 'Lua语言程序设计' , '雷丰阳' , 51.5 , 48 , 82 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , '西游记' , '罗贯中' , 12, 19 , 9999 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , '水浒传' , '华仔' , 33.05 , 22 , 88 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , '操作系统原理' , '刘优' , 133.05 , 122 , 188 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , '数据结构 java版' , '封大神' , 173.15 , 21 , 81 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , 'UNIX高级环境编程' , '乐天' , 99.15 , 210 , 810 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , 'javaScript高级编程' , '国哥' , 69.15 , 210 , 810 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , '大话设计模式' , '国哥' , 89.15 , 20 , 10 );insert into t_book(`id` , `name` , `author` , `price` , `sales` , `stock`)
values(null , '人月神话' , '刚哥' , 88.15 , 20 , 80 ); ## 查看表内容
select id,name,author,price,sales,stock from t_book;

创建一个动态Web工程

然后导入整合Spring+SpringMVC+Mybatis的所有jar包

数据库连接池:
druid-1.1.9.jar

切入点表达式解析:
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

Junit测试包:
junit_4.12.jar
org.hamcrest.core_1.3.0.jar

Mybatis的jar包:
mybatis-3.5.1.jar

Mybatis整合Spring的包:
mybatis-spring-2.0.4.jar

mysql数据库驱动jar:
mysql-connector-java-5.1.37-bin.jar

Spring核心层包:
spring-beans-5.2.5.RELEASE.jar
spring-context-5.2.5.RELEASE.jar
spring-core-5.2.5.RELEASE.jar
spring-expression-5.2.5.RELEASE.jar

Spring的日记桥接包( 是核心包的依赖包必须导 ):
spring-jcl-5.2.5.RELEASE.jar

Spring切面包:
spring-aop-5.2.5.RELEASE.jar
spring-aspects-5.2.5.RELEASE.jar

Spring数据库访问以及事务:
spring-jdbc-5.2.5.RELEASE.jar
spring-orm-5.2.5.RELEASE.jar
spring-tx-5.2.5.RELEASE.jar

Spring的测试包:
spring-test-5.2.5.RELEASE.jar

SpringMVC需要的jar包:
spring-web-5.2.5.RELEASE.jar
spring-webmvc-5.2.5.RELEASE.jar

各种配置文件

Mybatis核心配置文件

mybatis-config.xml配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><settings><!-- 打开延迟加载的开关 --><setting name="lazyLoadingEnabled" value="true" /><!-- 将积极加载改为消极加载 按需加载 --><setting name="aggressiveLazyLoading" value="false" /></settings>
</configuration>

SpringMVC需要的配置文件

springmvc.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsdhttp://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><!-- 扫描springMVC的类和异常处理 --><context:component-scan base-package="com.atguigu" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/><context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/></context:component-scan><!-- 视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/book/" /><property name="suffix" value=".jsp"/></bean><!-- SpringMVC标签的两个mvc标签 --><mvc:default-servlet-handler/><mvc:annotation-driven/>
</beans>

web.xml中的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID" version="2.5"><display-name>ssm</display-name><!--    解决乱码的Filter过滤器   --><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><init-param><param-name>forceEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>CharacterEncodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 支持restful风格的Filter --><filter><filter-name>HiddenHttpMethodFilter</filter-name><filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> </filter><filter-mapping><filter-name>HiddenHttpMethodFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- 整合Spring到Web的监听器  --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- SpringMVC的前端控制器 --><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

Spring需要的配置文件 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"xsi:schemaLocation="http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><!-- 扫描除SpringMVC之外所有组件 --><context:component-scan base-package="com.atguigu"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /><context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" /></context:component-scan><!-- 加载jdbc.properties属性配置文件 --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 配置数据库连接池对象 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="user" value="${jdbc.user}" /><property name="password" value="${jdbc.password}" /><property name="url" value="${jdbc.url}" /><property name="driverClassName" value="${jdbc.driver}" /><property name="initialSize" value="${jdbc.initialSize}" /><property name="maxActive" value="${jdbc.maxActive}" /></bean><!-- 配置事务管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource" /></bean><!-- Mybatis整合Spring的核心配置之一 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:mybatis-config.xml" /><property name="mapperLocations" value="classpath:com/atguigu/dao/*.xml" /></bean><!-- Mybatis整合Spring的核心配置之二      老式的将Mapper接口注入到SpringIOC容器中<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.atguigu.dao"></property></bean>--><!-- Mybatis整合Spring的核心配置之二 扫描并将Mapper接口注入到SpringIOC容器中 --><mybatis:scan base-package="com.atguigu.mapper" /><!-- 配置事务属性 --><tx:advice id="tx_ssm" transaction-manager="transactionManager"><tx:attributes><tx:method name="add*" propagation="REQUIRED" /><tx:method name="save*" propagation="REQUIRED" /><tx:method name="update*" propagation="REQUIRED" /><tx:method name="delete*" propagation="REQUIRED" /><tx:method name="*" read-only="true" /></tx:attributes></tx:advice><!-- 配置事务切面 --><aop:config><aop:advisor advice-ref="tx_ssm"pointcut="execution(* com..service..*.*(..))" /></aop:config></beans>

SpringMVC中的文件上传与下载,json转换,及三大框架的整合相关推荐

  1. SpringMVC中的文件上传与下载

    文件上传: apache上传组件方案 添加依赖 <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileup ...

  2. (转)SpringMVC学习(九)——SpringMVC中实现文件上传

    http://blog.csdn.net/yerenyuan_pku/article/details/72511975 这一篇博文主要来总结下SpringMVC中实现文件上传的步骤.但这里我只讲单个文 ...

  3. SpringMVC之实现文件上传与下载

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  4. SpringMVC中的文件上传

    1. 配置图片服务器 一般图片会单独保存在图片服务器上, 本文为简化处理, 在Tomcat中配置一个路劲用于专门存放图片 在tomcat上配置图片虚拟目录,在tomcat下conf/server.xm ...

  5. 最全面的SpringMVC教程(五)——文件上传与下载

    前言 本文为 [SpringMVC教程]文件上传与下载 相关知识,具体将对使用MultipartResolver处理文件上传的步骤,两种文件下载方式(直接向response的输出流中写入对应的文件流. ...

  6. Java Web项目中遇到的文件上传与下载问题

    (转发自:https://www.cnblogs.com/xdp-gacl/p/4200090.html)   在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中 ...

  7. java spring文件下载_SpringMVC实现文件上传和下载的工具类

    本文主要目的是记录自己基于SpringMVC实现的文件上传和下载的工具类的编写,代码经过测试可以直接运行在以后的项目中. 开发的主要思路是对上传和下载文件进行抽象,把上传和下载的核心功能抽取出来分装成 ...

  8. 一篇文章教你学会使用SpringBoot实现文件上传和下载

    文章目录 一.搭建SpringBoot开发环境 1.创建项目 2.配置application.properties参数 3.实体响应类和异常信息类 4.创建FileController 二.接口测试 ...

  9. JavaWeb学习总结(五十)——文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

最新文章

  1. JetBrains产品永久破解
  2. 寒假作业--微信小程序开发1
  3. 使用证书保护网站--兼谈证书服务器吊销列表的使用
  4. redis-cluster集群搭建
  5. Python中的协程
  6. Python中的模块和包:模块的(动态)导入、__future__模块使用
  7. [js] ajax请求地址只支持http/https吗?能做到让它支持rtmp://等其它自定义协议吗 ?
  8. 解决方案:加盐加密算法BCrypt
  9. 2021年NBA附加赛第一轮预测
  10. 转载-jmeter进阶功能
  11. 如何正确看待LeCun工作调整?听听FAIR研究员们现身说法
  12. try catch 对于循环体,应放在外面还是里面?
  13. 【挨踢人物传】向立天:从电视编导到技术总监,只要努力,你也能铸就传奇(第七期)...
  14. 解决谷歌浏览器最新chrome9+ 版本CORS跨域问题
  15. 考研计算机软件与理论院校排名,计算机软件与理论专业考研院校排名
  16. 天池-淘宝用户行为数据分析(python+Tableau)
  17. Vue Whitelabel Error Page错误
  18. 加密市场熊市最后的曙光——Treasure Project(藏宝计划)
  19. DS1302时钟程序解读
  20. webpack打包工具不会用,那是因为你没看过这篇

热门文章

  1. Git Github 学习
  2. 使用video.js播放手机本地视频
  3. 今日芯声 | 微软 Xbox 老大:关闭游戏直播平台 Mixer,我没有遗憾
  4. linux系统教程_【笔记】windows10安装linux双系统教程(可能是现今最简单方法)...
  5. 关于程序新手入行的分析与看法
  6. 如何快速有效的从零开始学习3d建模?
  7. 衣服裤子染色了怎么办
  8. 核苷酸(evolution)
  9. Sigmastar 方案的相机开发流程和注意点
  10. 如何给电脑重装系统--一点通