有2个注解,一个@PathVariable,一个@RequestParam,后一个是?问号后带的参数,比如pn翻页,另外接收json,在入参时,加入@RequestBody,。另外需要在配置文件中启用 @EnableWebMvc
@RestController public class IndexController { @Autowired private UserService userService; @RequestMapping("/getone") public User getone(){ User byId = this.userService.getById(1); return byId; } @RequestMapping("/listpage") public List<User> listpage(@RequestParam(value="pn",defaultValue = "1") Integer pn){ // 这里可以将?后的参数,自定义为入参的名称,如果这里@requestParam也是pn,也可以省略, IPage page = new Page(pn,3); List<User> users = this.userService.page(page,null).getRecords(); return users; }
public String listpage2(@RequestParam List<String> likes){ // 如果传参是个list集合,需在前面加上@requestParam,可以了。因为他会自动把list当作pojo转换,而不是当作数组往里添加
}
public String parampage(User user){ // 如果user有级联的属性,比如有address,在传参的时候,?name=xiaoming&age=19&address.name=beijing&address.province=haidian 只要写点,往后加,级联就可以自动包装进去pojo
}
public String jsonpage(@RequestBody List<String> likes){ } // 接收json
public String datePage(@DateTimeFormat(pattern="yyyy-MM-dd") Date date){} // 接收日期,默认yyyy/MM/dd,其他的需要写格式转换 @RequestMapping("/list/{id}") public User list(@PathVariable Long id){ return this.userService.getById(id); }
postman 发送json
json pojo
///////////////////////////////////////////////////////////////////////////////////////////////////////////
另外一个注解 @Param
在 xml中,他是用来对于井号后的参数的,对应注解@Param后的参数
<select id="getDescriptionUpOthers" resultType="cn.taotao.bean.Description"> select * from tbl_description where id > #{id ,jdbcType=INTEGER} and categoryId = #{cat,jdbcType=INTEGER} and isPublish =1 order by id asc limit #{num,jdbcType=INTEGER} </select>
对应的dao接口
public List<Description> getDescriptionUpOthers(@Param("cat")Integer cat,@Param("num")Integer num,@Param("id") Integer id);
另外的一个例子
有#的注释,写在dao层中
@Select("select * from tbl_user inner join tbl_depart using(departId) where id =#{value}")
@Results({
@Result(property = "depart", javaType = Depart.class, column = "departid", one = @One(select = "cn.taotao.dao.DepartDao.selectByPrimaryKey"))
, @Result(property = "departid", column = "departid")})
User selectByPrimaryKey(Long id);
里面引用的其他dao类
@Select("select * from tbl_depart where departid=#{value}")
Depart selectByPrimaryKey(Integer departid);