springboot 使用 mybatis 快速上手

创建数据库表对应的实体类

@Data
public class Template {

    private int id;

    private String name;

    private String type;

    private int productId;

    private Timestamp createTime;

    private Timestamp updateTime;

    private Timestamp deleteTime;
}

创建 TemplateMapper.java

@Mapper
public interface TemplateMapper {

    List<Template> getTemplateList(String productId);
}

创建 TemplateMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.vazquez.devops.mapper.TemplateMapper">

    <resultMap id="TemplateMap" type="com.vazquez.devops.model.Template">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="type" property="type"/>
        <result column="product_id" property="productId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="delete_ime" property="deleteTime"/>
    </resultMap>

    <select id="getTemplateList" resultMap="TemplateMap">
        select * from template where product_id = #{productId}
    </select>
</mapper>

创建 TemplateService.java

public interface TemplateService {

    Response getTemplateList(String productId);
}

创建 TemplateServiceImpl.java

@Service
public class TemplateServiceImpl implements TemplateService {

    @Autowired
    TemplateMapper templateMapper;

    @Override
    public Response getTemplateList(String productId) {
        List<Template> templateList = templateMapper.getTemplateList(productId);

        return Response.success(HttpStatus.OK.toString(), "查询所有模板成功", templateList);
    }
}

创建 TemplateController.java

@RestController
@RequestMapping("/api/template")
public class TemplateController {

    @Autowired
    TemplateService templateService;

    @GetMapping("/getTemplateList/{productId}")
    public Response getTemplateList(@PathVariable String productId) {
        return templateService.getTemplateList(productId);
    }
}

创建 mybaits 的配置文件 mybatisConf.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!--开启驼峰命名,不然会查询数据为空-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>

创建统一返回对象

public class Response<T> {

    private String status;

    private String message;

    private T data;

    public Response(String status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Response<T> success(String code, String message, T data) {
        return new Response<>(code, message, data);
    }
}

修改 application.yml

具体的 springboot 整合 mybatis 参考历史文章

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://xxx.xxx.xxx.xxx:3306/xxx
    username: root
    password: xxx
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis:
  # 全局配置文件
  config-location: classpath:mybatis/mybatisConf.xml
  # sql 映射文件
  mapper-locations: classpath:mybatis/mapper/*.xml

代码目录结构

在这里插入图片描述

效果截图

在这里插入图片描述

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快,把我桶也提着

如果对您有帮助欢迎支持哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值