spring day03笔记 | 乐文库-凯发k8官网下载客户端
spring事务:
1.在业务层接口上使用@transactional注解
2.spring配置类中,启用事务@enabletransactionmanagement
3.在jdbc配置类中,定义事务管理类
@beanpublic platformtransactionmanager transactionmanager(datasource datasource){ datasourcetransactionmanager tm = new datasourcetransactionmanager(); tm.setdatasource(datasource); return tm;}
spring事务角色:
事务管理员:发起事务者,开启事务的方法。下图中事务t。
事务协调员:参与事务者,数据层或业务层方法。下图事务t1和t2
spring的事务默认只回滚err和运行时异常,对于io异常这类直接继承于exception的编译时异常是不回滚的,但可以通过rollbackfor属性属性设置回滚类型
@transactional(rollbackfor = ioexception.class)
事务传播行为:业务要求转账操作执行失败后,日志不回滚,但现在的情况是三个事务都加入了同一个事物。要回滚一起回滚,如下图。可在日志接口使用propagation属性,设置属性传播行为
@transactional(propagation = propagation.requires_new)
springmvc:
1.导入相关jar包
javax.servlet servlet-api 2.5 provided org.springframework spring-webmvc 5.2.10.release
注意,servlet一定要加provided作用范围,否则报错a child container failed during start
org.apache.tomcat.maven tomcat7-maven-plugin 2.1 90 /
2.创建控制器类
package com.supersql.controller;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.responsebody;@controllerpublic class usercontroller { //设置访问路径 @requestmapping("/save") //设置当前操作的返回值类型 @responsebody public string save(){ return "{name:'张三'}"; }}
3.初始化springmvc配置类
package com.supersql.config;import org.springframework.context.annotation.componentscan;import org.springframework.context.annotation.configuration;@configuration@componentscan("com.supersql.controller")public class springmvcconfig {}
4.初始化servlet容器,加载mvc环境
package com.supersql.config;import org.springframework.web.context.webapplicationcontext;import org.springframework.web.context.support.annotationconfigwebapplicationcontext;import org.springframework.web.servlet.support.abstractdispatcherservletinitializer;public class servletcontainersinitconfig extends abstractdispatcherservletinitializer { //加载springmvc容器的配置 @override protected webapplicationcontext createservletapplicationcontext() { annotationconfigwebapplicationcontext ctx = new annotationconfigwebapplicationcontext(); ctx.register(springmvcconfig.class); return ctx; } //设置哪些请求归属mvc @override protected string[] getservletmappings() { return new string[]{"/"}; } //加载spring容器配置 @override protected webapplicationcontext createrootapplicationcontext() { return null; }}
简化方案:
package com.supersql.config;import org.springframework.web.servlet.support.abstractannotationconfigdispatcherservletinitializer;public class servletcontainersinitconfig extends abstractannotationconfigdispatcherservletinitializer { @override protected class[] getrootconfigclasses() { return new class[]{ springconfig.class }; } @override protected class[] getservletconfigclasses() { return new class[]{ springmcvconfig.class }; } @override protected string[] getservletmappings() { return new string[]{ "/" }; }}
工作流程:
加载spring控制的bean时,排除springmvc控制的bean
按注解过滤:
@componentscan(value = "com.supersql",excludefilters = @componentscan.filter( type = filtertype.annotation, classes = controller.class))public class springconfig {}
设置请求映射路径:
@requestmapping,可用于类注解,方法注解
post请求处理中文乱码:
//servlet容器中@overrideprotected filter[] getservletfilters() { characterencodingfilter cf = new characterencodingfilter(); cf.setencoding("utf-8"); return new filter[]{cf};}
五种类型的参数传递:@requestparams适用于url、表单传参,@requestbody适用于json传参
普通参数:默认按参数名接参数,参数名不同的可用@requestparam绑定请求参数
pojo类型:如果请求参数和实体类中的参数名一样,就自动装配
嵌套pojo型:user类型中嵌套address类型,参数传递时应为address.city,参数名.属性
数组类型:传递多个同名参数,用同名数组接收
集合类型: 不加@requestparams注解,会将集合类型当成pojo类型,尝试构造对象。
json数据类型传递:
1.导入jackson坐标
2.springmvc配置类中开启@enablewebmvc
日期类型参数传递:
对于yyyy/mm/dd格式的日期,可直接用date类型接受,其他格式需要用@datatimeformat进行格式化
@requestmapping("/time")@responsebodypublic string time(@datetimeformat(pattern = "yyyy-mm-dd") date date){ system.out.println(date); return "test";}
响应:
响应页面:不加@responsebody,返回值是string,就是响应页面。
响应文本:加@responsebody,返回值是string。
响应json:加@responsebody,返回pojo,pojo转json是由(httpmessageconverter)jackson完成的
rest风格:从路径取参数@pathvariable,如果全类都是响应数据的,可以把@responsebody放到类上面,从而简化书写。
@controller @responsebody = @restcontroller
@requestmapping(method = requestmethod.post) = @postmapping
@requestmapping(value = "/save/{username}",method = requestmethod.post)//@postmapping("/save/{username}") ,简化开发@responsebody public string save(@pathvariable string username){ system.out.println(username); return username;}
放行静态资源:
此时如果想访问:/pages/test.jsp,会报错因为servlet容器配置类中已经把所有请求都给springmvc处理了,实际上上述页面应该给tomcat处理的。
定义配置类,springmvcconfig里扫描带他上,即可
package com.supersql.config;import org.springframework.context.annotation.configuration;import org.springframework.web.servlet.config.annotation.resourcehandlerregistry;import org.springframework.web.servlet.config.annotation.webmvcconfigurationsupport;@configurationpublic class springmvcsupport extends webmvcconfigurationsupport { @override protected void addresourcehandlers(resourcehandlerregistry registry) { //当访问pages/下的资源,不要走mvc registry.addresourcehandler("/pages/**","/css/**").addresourcelocations("/pages/",""/css/); }}
本文来自网络,不代表乐文库立场,如若转载,请注明出处:https://www.lewenku.com/?p=495248