SpringBoot项目 结合 MyBatis使用范例

SpringBoot项目 结合 MyBatis使用范例

1.案例的介绍

1.1 SpringBoot

  • 因为遵循着约定优于配置的规范,所以在快速开发的时候非常方便

1.2 MyBatis

  • MyBatis 是 SSM 项目中的 M,主要负责人项目与数据库之间的相关操作,在项目中作用体现在持久层(有的也称为数据层)

2.IDEA 构建项目

2.1 IDEA 初始化项目

  • 此处我使用的是 ALiBaBa 的国内源:https://start.aliyun.com/
    在这里插入图片描述
  • 指定项目的基本信息
    在这里插入图片描述
  • 添加相关的依赖
    在这里插入图片描述
  • 连接数据库,生成对应的实体对象
    在这里插入图片描述

2.2 进行项目代码的编写

  • 生成数据表对应的实体类
package com.lagou.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * (Comment)实体类
 *
 * @author YunMengGuiYao
 * @since 2022-06-18 09:26:58
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Comment implements Serializable {
    private static final long serialVersionUID = -20796280730949307L;
    /**
     * 评论id
     */
    private Integer id;
    /**
     * 评论内容
     */
    private String content;
    /**
     * 评论作者
     */
    private String author;
    /**
     * 关联的文章id
     */
    private Integer aId;
}
  • 编写对应的mapper接口
package com.lagou.dao;

import com.lagou.entity.Comment;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * (TComment)表数据库访问层
 *
 * @author YunMengGuiYao
 * @since 2022-06-18 09:33:10
 */
@Repository
public interface CommentDao {
    // 根据 id 查询对应的记录
    public Comment findById(Integer id);
}
  • 编写对应的 mapper 映射文件
<?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.lagou.dao.CommentDao">

    <resultMap type="com.lagou.entity.Comment" id="CommentMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="content" column="content" jdbcType="VARCHAR"/>
        <result property="author" column="author" jdbcType="VARCHAR"/>
        <result property="aId" column="a_id" jdbcType="INTEGER"/>
    </resultMap>

    <select id="findById" resultType="Comment">
        select * from t_comment where id = #{id}
    </select>

</mapper>
  • 编写 配置文件
# 端口
server:
  port: 8888
# 应用名称
spring:
  application:
    name: springboot-mybatis
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.159.133:3306/springbootdata?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: liuyunfei
    password: Lyf123456!

# MyBatis 配置
mybatis:
  configuration:
    # 驼峰命名法
    map-underscore-to-camel-case: true
  # 指定映射文件
  mapper-locations: classpath:mapper/*.xml
  # 指定其别名的包
  type-aliases-package: com.lagou.entity
  • 修改启动类
package com.lagou;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.lagou.dao") // mapper 接口扫描
public class SpringbootMybatisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootMybatisApplication.class, args);
    }

}

2.3 进行接口的测试

  • 编写测试用例
package com.lagou;

import com.lagou.dao.CommentDao;
import com.lagou.entity.Comment;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootMybatisApplicationTests {

    @Autowired
    private CommentDao commentDao;

    @Test
    void testFindById(){
        Comment comment = commentDao.findById(1);
        System.out.println("查询结果:" + comment);
    }
}

在这里插入图片描述

2.4 编写 controller,进行 web 形式的访问

  • 编写 controller
package com.lagou.controller;

import com.lagou.dao.CommentDao;
import com.lagou.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 云梦归遥
 * @date 2022/6/18 20:51
 * @description
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentDao commentDao;

    @RequestMapping("/{id}")
    public Comment getCommentById(@PathVariable("id") Integer id){
        Comment comment = commentDao.findById(id);
        return comment;
    }
}

在这里插入图片描述

3.总结

以上就是 SpringBoot 结合 MyBatis 使用的案例,虽然简单,但是基本功能都已经实现了,希望可以帮助到大家

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值