第四节:springboot整合Mybatis(controller+service+mapper)完整过程
【代码】第四节:springboot整合Mybatis(controller+service+mapper)完整过程。
·
文章目录
一、SpringBoot中统一全局返回格式
package com.gzgs.study01.common.utils.results;
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class Result<T> {
public static final String SUCCESS_CODE = "OK";
private String code;
private String message;
private T data;
private long timestamp;
}
package com.gzgs.study01.common.utils.results;
public class ResultHelper {
public static <T> Result<T> success(T data) {
return new Result<T>()
.setCode("200")
.setData(data)
.setTimestamp(System.currentTimeMillis());
}
public static <T> Result<T> fail(String message) {
return new Result<T>()
.setCode("500")
.setMessage(message)
.setTimestamp(System.currentTimeMillis());
}
}
二、数据层-mapper
1、UserMapper类
package com.gzgs.study01.web.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gzgs.study01.web.entity.User;
import com.gzgs.study01.web.param.UserParam;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
//我们使用的@Mapper和@MapperScan并不是spring框架的注解,未在spring容器中注册为bean;
//@Repository标签可加可不加,不加的话@Autowired的时候会爆红而已,因为@MapperScan不是spring框架的注解
public interface UserMapper extends BaseMapper<User> {
//通过用户名查询,为什么会加limit 1,因为我这里的返回类是User,如果查询到多条数据会报错
@Select({"select * from t_user where user_name = #{userName} limit 1"})
User getUserByName(String userName);
//模糊查询
@Select({"select * from t_user where user_name like concat('%',#{userName},'%') "})
List<User> selectUserByName(String userName);
/**
* 复杂查询:自己搜索mapper@Select复杂查询
* @param param
* @return
*/
@Select({"<script>"+
" select t.* from t_user t"+
" <where>"+
" <if test='param.userName!= null and param.userName !=\"\" '>"+
" and t.user_name like concat('%',#{param.userName},'%')"+
" </if>"+
" <if test='param.age!=null'>"+
" and t.age = #{param.age} "+
" </if>"+
" </where>"+
"</script>"
})
List<User> selectUserByParam(@Param("param") UserParam param);
}
三、业务层-service
1、UserService类
package com.gzgs.study01.web.service;
import com.gzgs.study01.web.param.UserParam;
import com.gzgs.study01.web.vo.UserVo;
import java.util.List;
public interface UserService {
List<UserVo> selectUserByParam(UserParam param);
UserVo getUserByName(String userName);
}
2、UserServiceImpl类
package com.gzgs.study01.web.service.impl;
import com.gzgs.study01.common.utils.MyBeanUtils;
import com.gzgs.study01.web.entity.User;
import com.gzgs.study01.web.mapper.UserMapper;
import com.gzgs.study01.web.param.UserParam;
import com.gzgs.study01.web.service.UserService;
import com.gzgs.study01.web.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List<UserVo> selectUserByParam(UserParam param) {
List<User> list = userMapper.selectUserByParam(param);
//entity转vo
List<UserVo> voList = MyBeanUtils.listCopy(UserVo.class, list);
return voList;
}
@Override
public UserVo getUserByName(String userName) {
User user = userMapper.getUserByName(userName);
//entity转vo
UserVo userVo = new UserVo();
if(Objects.nonNull(user)){//判空处理,如果不判空会报异常
BeanUtils.copyProperties(user,userVo);
}
return userVo;
}
}
四、控制层-Controller
1、UserController类
package com.gzgs.study01.web.controller;
import com.gzgs.study01.common.utils.results.Result;
import com.gzgs.study01.common.utils.results.ResultHelper;
import com.gzgs.study01.web.param.UserParam;
import com.gzgs.study01.web.service.UserService;
import com.gzgs.study01.web.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/v1/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/selectUserByParam")
public Result<List<UserVo>> selectUserByParam(@RequestBody UserParam param){
List<UserVo> voList = userService.selectUserByParam(param);
return ResultHelper.success(voList);
}
}
五、Postman请求
六、其他
1、一般来说,返回给前端的是vo数据
2、list的entity转vo需要自己封装
package com.gzgs.study01.common.utils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
public class MyBeanUtils {
public static <T,F> List<F> listCopy(Class<F> target,List<T> sourceList){
if(!CollectionUtils.isEmpty(sourceList)){
List<F> targetList = new ArrayList<>();
for (T t : sourceList){
try {
F f = target.newInstance();
BeanUtils.copyProperties(t,f);
targetList.add(f);
}catch (Exception e){
}
}
return targetList;
}else {
return null;
}
}
}
3、项目结构
更多推荐
已为社区贡献2条内容
所有评论(0)