sql模糊查询-记账接口
步骤:
- 填写req
- 填写controller层
- 填写service层
- hashMap存储返回
- 填写两个mapper层
- 填写两个xml层
- xml模糊查询
逻辑:
通过bookId获得图片与名称,还有花销和类型
通过data判断查询几月的数据
req:
通过bookId获得图片与名称,还有花销和类型
通过bookId获得图片与名称,还有花销和类型
通过data判断查询几月数据
所以req层只需要两个字段
req:
package com.Ren.account.controller.req;
import io.swagger.v3.oas.annotations.media.Schema;
import javax.validation.constraints.NotNull;
/**
* @author: Gavin
* @created: 2022/2/13 20:21
* @description:
**/
@Schema(description = "账本")
public class BookReq {
@NotNull(message = "bookId不可为空")
@Schema(description = "账本id")
private String bookId;
@NotNull(message = "日期不可为空")
@Schema(description = "日期")
private String date;
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
controller层:
package com.Ren.account.controller;
import com.Ren.account.controller.req.BookReq;
import com.Ren.account.controller.req.RecordReq;
import com.Ren.account.service.AccountBookService;
import com.Ren.account.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/**
* @author: Gavin
* @created: 2022/2/13 20:17
* @description:
**/
@Tag(name = "账本")
@RestController
@RequestMapping("/book")
public class AccountBookController {
@Resource
private AccountBookService accountBookService;
@Operation(summary = "Todo获取账本")
@PostMapping("/getBook")
public R def(@RequestBody BookReq req, @RequestHeader String token){
return R.ok().put("bookInfo", accountBookService.def(req, token));
}
}
service层:
package com.Ren.account.service;
import com.Ren.account.controller.req.BookReq;
import com.Ren.account.mapper.AccountBookMapper;
import com.Ren.account.mapper.AccountRecordMapper;
import com.Ren.account.util.exception.EmosException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import sun.security.ssl.Record;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
/**
* @author: GULAO
* @created: 2022/2/15 9:33 下午
* @description:
**/
@Service
public class AccountBookService {
@Resource
private AccountBookMapper accountBookMapper;
@Resource
private AccountRecordMapper accountRecordMapper;
public HashMap<String, Object> def(BookReq req, String token) {
if (ObjectUtils.isEmpty(token)) {
throw new EmosException("没有权限");
}
HashMap<String, Object> hashMap = new HashMap<String, Object>();
hashMap.put("userId", token);
hashMap.put("bookId", req.getBookId());
hashMap.put("date", req.getDate());
hashMap.put("type", true);
int inc = accountRecordMapper.getRecord(hashMap);
hashMap.put("type", false);
int exp = accountRecordMapper.getRecord(hashMap);
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("bookInfo", accountBookMapper.getBookInfoById(req.getBookId()));
data.put("inc", inc);
data.put("exp", exp);
return data;
}
}
mapper层:
package com.Ren.account.mapper;
import com.Ren.account.domain.AccountBook;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface AccountBookMapper {
int createBook(HashMap<String, Object> bookMap);
HashMap<String, Object> getBookInfoById(String bookId);
}
mapper层:
package com.Ren.account.mapper;
import com.Ren.account.controller.req.RecordReq;
import com.Ren.account.domain.AccountRecord;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface AccountRecordMapper {
void add(HashMap<String,Object> hashMap);
int getRecord(HashMap<String, Object> hashMap);
}
xml层:
<?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.Ren.account.mapper.AccountBookMapper">
<resultMap id="BaseResultMap" type="com.Ren.account.domain.AccountBook">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="avatar" property="avatar" />
<result column="user_id" property="userId" />
<result column="status" property="status" />
<result column="create_time" property="createTime" />
<result column="update_time" property="updateTime" />
</resultMap>
<select id="getBookInfoById" parameterType="String" resultType="java.util.HashMap">
select name,avatar from account_book where id = #{bookId}
</select>
</mapper>
xml层:
<?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.Ren.account.mapper.AccountRecordMapper">
<resultMap id="BaseResultMap" type="com.Ren.account.domain.AccountRecord">
<id column="id" property="id" />
<result column="type" property="type" />
<result column="sig" property="sig" />
<result column="name" property="name" />
<result column="date" property="date" />
<result column="color" property="color"/>
<result column="amount" property="amount" />
<result column="remark" property="remark" />
<result column="book_id" property="bookId" />
<result column="user_id" jdbcType="INTEGER" property="userId" />
<result column="status" property="status" />
<result column="create_time" property="createTime" />
<result column="update_time" property="updateTime" />
</resultMap>
<select id="getRecord" parameterType="hashmap" resultType="Integer">
select sum(amount) from account_record where date like '%${date}%' and type=#{type} and book_id = #{bookId} and user_id = #{userId}
</select>
</mapper>
注意⚠️:
‘%${date}%’通过这句可以模糊查询



Warning: Undefined variable $aria_req in /www/wwwroot/l.lvovl.cn/wp-content/themes/JieStyle-Two-master/comments.php on line 26
Warning: Undefined variable $aria_req in /www/wwwroot/l.lvovl.cn/wp-content/themes/JieStyle-Two-master/comments.php on line 27
Warning: Undefined variable $aria_req in /www/wwwroot/l.lvovl.cn/wp-content/themes/JieStyle-Two-master/comments.php on line 28