抽丝剥茧JavaWeb应用之扯扯springMVC常用注解

本文主要扯一扯工作中经常使用的SpringMVC注解和mybatis批量更新Oracle。本文使用的springmvc的版本是4.1.4.RELEASE

1.项目经常性使用的SpringMVC注解有:@Controller (标识这个类是一个控制类,用于映射外部的HTTP请求,只能作用ElementType.TYPE上)和 @RequestMapping(用于标识匹配方法的路径) 这两个都是MVC基于DispatcherServlet分发HTTP请求的基础。参见

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-introduction

 @RestController注解 = @Controller + @ResponseBody

参见源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {

	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any
	 * @since 4.0.1
	 */
	String value() default "";

}

 @RequestParam 用于标识请求参数名称(value())及是否需要(required()),它们均有默认值。

 @RequestMapping 可以作用ElementType.METHOD, ElementType.TYPE上。可设置的元素如下所示:

	@ResponseBody
	@RequestMapping(name = "abc" ,
			value = "/abc" , // 映射路径
			method = { RequestMethod.GET , RequestMethod.POST},  //请求方法
			headers = "content-type=text/html, //请求头的文档类型,不能设置编码类型,否则会报错
			params="!myParam", //方法接受的参数中不能存在myParam参数名
			consumes = {"text/html", "application/json"},  //方法接受的数据格式
			produces = "application/json" //方法返回json格式
			)
	public ResultMessageDTO demo1(){
		return ResultCodeUtil.genResult(ReturnCodeEnum.SUCC , new MapData("hello", "springmvc"));
	}

 @RequestBody:主要参见官方文档 :

@RequestBody 
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-ann-requestbody

 指示方法参数应绑定到HTTP请求正文的值。通过使用HttpMessageConverter将请求体转换为method参数。 HttpMessageConverter负责将HTTP请求消息转换为对象,并从对象转换为HTTP响应体。在项目中可以这样配置

	<bean id="mappingJackson2HttpMessageConverter"
		class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
		<property name="supportedMediaTypes">
			<list>
				<value>text/html;charset=UTF-8</value>
				<value>text/json;charset=UTF-8</value>
				<value>application/json;charset=UTF-8</value>
			</list>
		</property>
	</bean>
	<!-- 采用SpringMVC自带的JSON转换工具,支持@ResponseBody注解 -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJackson2HttpMessageConverter" />    <!-- JSON转换器 -->
			</list>
		</property>
	</bean>

 HttpMessageConverter 是一个策略接口,实现此接口的方法使将HTTP请求到响应的一个转换器。

 

2.mybatis批量更新的一种方法:

<update id="updateAA" parameterType="java.util.List">
	<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">  
                update t_aa   
                <set>  
                  a = #{item.a , jdbcType=VARCHAR} ,
                  m = sysdate ,
                  b = #{item.b , jdbcType=VARCHAR}
                </set>  
                where c = #{item.c ,jdbcType=VARCHAR}  
                and d = #{item.d,jdbcType=VARCHAR}
         </foreach>  
</update

 以上。

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值