The common annotations of springMVC in Java Web applications

This article mainly talks about SpringMVC annotations and mybatis batch update Oracle that are often used in work. The version of springmvc used in this article is 4.1.4.RELEASE

1. The SpringMVC annotations frequently used by the project are: @Controller (to identify this class as a control class, used to map external HTTP requests, which can only be used on ElementType.TYPE) and @RequestMapping (used to identify the path of the matching method) Both of these are the basis for MVC to distribute HTTP requests based on DispatcherServlet. see

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

 @RestController注解 = @Controller + @ResponseBody

See the source code:

@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 is used to identify the request parameter name (value()) and whether it is required (required()). They all have default values.

 @RequestMapping can act on ElementType.METHOD, ElementType.TYPE. The elements that can be set are as follows:

	@ResponseBody
	@RequestMapping(name = "abc" ,
			value = "/abc" , // map path
			method = { RequestMethod.GET , RequestMethod.POST}, //Request method
			headers = "content-type=text/html, //The document type of the request header, the encoding type cannot be set, otherwise an error will be reported
			params="!myParam", //The parameter name of myParam cannot exist in the parameters accepted by the method
			consumes = {"text/html", "application/json"}, //Data format accepted by the method
			produces = "application/json" //The method returns json format
			)
	public ResultMessageDTO demo1(){
		return ResultCodeUtil.genResult(ReturnCodeEnum.SUCC , new MapData("hello", "springmvc"));
	}

 @RequestBody: Mainly refer to the official documentation:

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

 A value indicating that the method parameter should be bound to the HTTP request body. Convert the request body to method parameters by using HttpMessageConverter. HttpMessageConverter is responsible for converting HTTP request messages to objects and from objects to HTTP response bodies. It can be configured like this in the project

	<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>
	<!-- Using SpringMVC's own JSON conversion tool, support @ResponseBody annotation -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<ref bean="mappingJackson2HttpMessageConverter" /> <!-- JSON Converter-->
			</list>
		</property>
	</bean>

 HttpMessageConverter is a strategy interface whose methods implement a converter that converts HTTP requests to responses.

 

2. A method of batch update of 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

 above.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326676936&siteId=291194637