<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.example.crud.mapper.MyuserMapper"><select id="getuserCount" parameterType="java.util.Map" resultType="java.lang.Integer"><if test="dataShow == 'dev'">
select count(*) from myuser m
where 1=1<if test="classNumTotal!=null and classNumTotal!=''">
AND m.`classnum`=#{classNumTotal}</if></if></select><select id="getUserInfo" parameterType="java.util.Map" resultType="java.util.Map">
select * from myuser m
where 1=1<if test="id!=null and id!=''">
AND m.`id`=#{id}</if></select><insert id="addUser" parameterType="com.example.crud.entity.Myuser">
insert into
myuser
<trim prefix="(" suffix=")" suffixOverrides=","><if test="username!=null">
username,</if><if test="password!=null">
password,</if><if test="name!=null">
name,</if><if test="classNum!=null">
classNum,</if></trim><trim prefix="values(" suffix=")" suffixOverrides=","><if test="username!=null">
#{username},</if><if test="password!=null">
#{password},</if><if test="name!=null">
#{name},</if><if test="classNum!=null">
#{classNum},</if></trim></insert><delete id="deleteUser" parameterType="java.util.Map">
delete from
myuser
where
myuser.id=#{id}</delete><update id="update" parameterType="com.example.crud.entity.Myuser"><if test="id!=null and id!=''">
update myuser m
<set><if test="username!=null and username!=''">
username=#{username},</if><if test="password!=null and password!=''">
password=#{password},</if><if test="name!=null and name!=''">
name=#{name},</if><if test="classNum!=null and classNum!=''">
classnum=#{classNum},</if></set>
where m.id=#{id}</if></update></mapper>
查询
修改
@ApiImplicitParams、ApiImplicitParam的使用
@ApiImplicitParam: 作用在方法上,表示单独的请求参数 参数: 1. name :参数名。 2. value : 参数的具体意义,作用,解释。 3. required : 参数是否必填。 4. dataType :参数的数据类型。(String、int) 5. paramType :查询参数类型,这里有几种形式:
类型 作用 path 以地址的形式提交数据 query 直接跟参数完成自动映射赋值 body 以流的形式提交 仅支持POST header 参数在request headers 里边提交 form 以form表单的形式提交 仅支持POST 这个参数和SpringMvc中的@RequestBody冲突,索性我就去掉了paramType,对接口测试并没有影响。
@PathVariable(name = "id") Long id body @ApiImplicitParams({ @ApiImplicitParam(paramType = "body", dataType = "MessageParam", name = "param", value = "信息参数", required = true) }) @RequestMapping(value = "/findById3", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@RequestBody MessageParam param
提交的参数是这个对象的一个json,然后会自动解析到对应的字段上去,也可以通过流的形式接收当前的请求数据,但是这个和上面的接收方式仅能使用一个(用@RequestBody之后流就会关闭了) header @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "Long", name = "id", value = "信息id", required = true) })
String idstr = request.getHeader("id"); if (StringUtils.isNumeric(idstr)) { id = Long.parseLong(idstr); } Form @ApiImplicitParams({ @ApiImplicitParam(paramType = "form", dataType = "Long", name = "id", value = "信息id", required = true) }) @RequestMapping(value = "/findById5", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) 总结:
(1)对于@ApiImplicitParam的paramType:query、form域中的值需要使用@RequestParam获取, header域中的值需要使用@RequestHeader来获取,path域中的值需要使用@PathVariable来获取,body域中的值使用@RequestBody来获取,否则可能出错;而且如果paramType是body,name就不能是body,否则有问题,与官方文档中的“If paramType is "body", the name should be "body"不符。