ALearner

  • 首页
  • Python
  • Linux
  • Java
  • 工具
  • 前端
  • 算法
  • My World
HelloStranger
每个人都是初学者
  1. 首页
  2. Java
  3. SpringMvc
  4. 正文

SpringMVC的两个细碎知识点

2017年8月15日 7788点热度 0人点赞 0条评论

本文主要解释两件事情:

  1. 注解 @RequestParam 和@Param的区别;
  2. pom依赖解决编译项目时java1.5版本警告的信息

一、@RequestParam 与@Param

对于注解@RequestParam和@Param其实谈不上比较,因为他们不属于同一个范畴,现在分别介绍其用法。

1、@RequestParam

在SpringMVC后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取。

基本使用:

 @ResponseBody

@RequestMapping(value = "/welcome",method = RequestMethod.GET)

public List welcome(@RequestParam(value = "welcome",required = false)String welcome){

List<String> list = new ArrayList<>();

if (welcome==null){

list.add("welcome page");

}else {

list.add(welcome);

}

return list;

}

 

 

当我们传参的时候,例如下面的链接:

 

http://localhost:8080/manage/welcome?welcome=hello

 

返回值为:

 

["hello"]

 

当我们不传参的时候,例如下面的链接:

http://localhost:8080/manage/welcome

返回值为:

["welcome page"]

 

原因是当我们required = false,意味着这个这个参数可选,当没有传递该参数的时候,默认值为 null。

如果用@RequestMapping注解的参数是int基本类型,但是required=false,这时如果不传参数值会报错,因为不传值,会赋值为null给int,这个不可以

 

2、@Param

这一注解属于mybatis范畴,属于dao层向Mapper.xml传递参数的问题,现在简单介绍一下Mybatis传递参数的三种方式:

第一种:

Dao层方法:

List<DepartmentModel> getDepartment(int page,int pageSize);

Mapper.xml内容:
<select id="getDepartment" resultType="com.sdumzg.WebModel.model.DepartmentModel"> 
        <![CDATA[ SELECT * FROM department order by id LIMIT #{0},#{1} ]]> 
</select>

 

第二种

Dao层方法:

List<DepartmentModel> getDepartment(Map pageMap);
Mapper.xml内容:
<select id="getDepartment" resultType="com.sdumzg.WebModel.model.DepartmentModel"> 
        <![CDATA[ SELECT * FROM department order by id LIMIT #{page},#{pageSize} ]]> 
</select>

Service层调用

Map<String,Object> pageMap=new HashMap<String,Object>(); pageMap.put("page",1); pageMap.put("pageSize",5); 调用: getDepartment(pageMap);

 

第三种

Dao层方法:

List<DepartmentModel> getDepartment(@Param("page") int page,@Param("pageSize") int pageSize);

Mapper.xml内容:
<select id="getDepartment"  resultType="com.sdumzg.WebModel.model.DepartmentModel">
    <![CDATA[
    SELECT * FROM department order by id LIMIT #{page},#{pageSize}
    ]]>
</select>

二、项目编译时Java1.5版本警告pom

当我们运行编译某个项目的时候,有可能会 出现下面的的警告:

 

解决方法:

在pom中添加下边的plugin:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

 

 

相关

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: 暂无
最后更新:2017年8月15日

iquantumer

你现在的态度决定你十年后是人物还是废物。

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022 alearner.top. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

鲁ICP备16024047号