Spring Boot xml映射
实体层
@Data 里面包含get()与set()以级toString方法等等,可以大大提高写代码的效率
@TableName 是指向那个数据表
package com.zhdj.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.time.LocalDateTime;
@Data
//@Getter
//@Setter
@Accessors(chain = true)
@TableName("t_learning")
public class Learning implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id",type = IdType.AUTO)
private Long id;
@TableField("title")
private String title;
@TableField("chapter")
private String chapter;
@TableField("directory")
private String directory;
@TableField("cover")
private String cover;
@TableField("type")
private String type;
@TableField("vide")
private String vide;
@TableField("like_num")
private int likeNum;
@TableField("watch")
private int watch;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
@TableField("time")
private LocalDateTime time;
}
}
持久层
package com.zhdj.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhdj.pojo.Learning;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface LearningMapper extends BaseMapper<Learning> {
}
xml映射
column对映的是数据库数据名称,property对映的是实体类取的名称
<?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.zhdj.dao.LearningMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.zhdj.pojo.Learning">
<id column="id" property="id"/>
<result column="title" property="title"/>
<result column="chapter" property="chapter"/>
<result column="directory" property="directory"/>
<result column="cover" property="cover"/>
<result column="type" property="type"/>
<result column="vide" property="vide"/>
<result column="like_num" property="likeNum"/>
<result column="watch" property="watch"/>
<result column="time" property="time"/>
</resultMap>
<select id="findAllType" resultType="com.zhdj.pojo.Learning">
SELECT * from t_learning
</select>
</mapper>
package com.zhdj.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhdj.pojo.Learning;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface LearningMapper extends BaseMapper<Learning> {
List<Learning> findAllType();
}
工具类
前端响应状态码就是这工具类生成的,例如成功状态码200、失败状态码500
package com.zhdj.utilts;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 公共返回对象
*
* @author wj
* @since 1.0.0
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RespUtil {
private long code;
private String message;
private Object obj;
public RespUtil(long code, String message) {
this.code=code;
this.message=message;
}
/**
* 返回成功的信息
* @param message
* @return
*/
public static RespUtil success(String message){
return new RespUtil(200,message,null);
}
/**
* 返回成功信息且带数据
* @param message
* @param obj
* @return
*/
public static RespUtil success(String message, Object obj){
return new RespUtil(200,message,obj);
}
public static RespUtil success( Object obj){
return new RespUtil(200,"查询成功!!!",obj);
}
/**
* 返回失败信息
* @param message
* @return
*/
public static RespUtil error(String message){
return new RespUtil(500,message,null);
}
/**
* 返回失败信息并带数据
* @param message
* @param obj
* @return
*/
public static RespUtil error(String message, Object obj){
return new RespUtil(500,message,obj);
}
public static RespUtil error(){
return new RespUtil(500,"查询失败!!!");
}
public static RespUtil errorTime(String message, Object obj){
return new RespUtil(304,message,obj);
}
public static RespUtil errorTime(String message){
return new RespUtil(304,message,null);
}
}
业务层
service 接口
package com.zhdj.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zhdj.pojo.Learning;
import com.zhdj.utilts.RespUtil;
import java.util.List;
public interface ILearningService extends IService<Learning> {
RespUtil findAllType();
}
serviceIpmI
package com.zhdj.service.serviceIpmI;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhdj.dao.LearningMapper;
import com.zhdj.dao.SwiperMapper;
import com.zhdj.pojo.Learning;
import com.zhdj.pojo.Swiper;
import com.zhdj.service.ILearningService;
import com.zhdj.service.ISwiperService;
import com.zhdj.utilts.RespUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class ILearningServiceIpmI extends ServiceImpl<LearningMapper, Learning> implements ILearningService {
@Resource LearningMapper learningMapper;
@Override
public RespUtil findAllType() {
if (learningMapper.findAllType().size()!=0)
return RespUtil.success(learningMapper.findAllType());
return RespUtil.error();
}
}
控制层
package com.zhdj.controller;
import com.alibaba.fastjson.JSONObject;
import com.zhdj.pojo.Learning;
import com.zhdj.service.ILearningService;
import com.zhdj.utilts.RespUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;
@RestController
@RequestMapping("/learning")
public class LearningController {
@Resource
ILearningService iLearningService;
@GetMapping("/findAllType")
public RespUtil findAllType(){
return iLearningService.findAllType();
}
}