首页 > 其他分享 >SpringBoot 整合 MyBatis

SpringBoot 整合 MyBatis

时间:2022-10-03 16:05:29浏览次数:47  
标签:SpringBoot xxx private 整合 mysql MyBatis import com String

本文基于:https://www.bilibili.com/video/BV15b4y1a7yG?p=28&vd_source=cf8900ba33d057d422c588abe5d5290d

pom.xml 中导入坐标

<dependencies>
  ...
  <!-- 引入MyBatis -->
  <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.0</version>
  </dependency>

  <!-- 导入数据库的jar包 -->
  <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
  </dependency>
</dependencies>

这里使用的数据库是 MySql

配置 application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3309/FruitDatabase
    username: root
    password: 123456

上面的代码够语义化了,就不解释了

新建实体类

延续之前的例子,建立一个 Fruit

package com.example.xxx.entity;

public class Fruit {
    private String fruitId;
    private String fruitName;
    private String avatar;
    private Double price;
    private Integer stock;

    // Getter&Setter
    // toString
}

新建 Dao 接口

package com.example.xxx.dao;

import xxx...;

@Mapper
public interface FruitDao {
    @Select("select * from fruit_table where fruit_id = #{id}")
    public Fruit getById(String id);
}

去测试类中测试一下

// TestClass
package com.example.xxx;

import com.example.xxx.dao.FruitDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class TestClass {

    @Autowired
    private FruitDao fruitDao;

    @Test
    void contextLoads() {
        System.out.println(fruitDao.getById("1"));
    }
}

运行 contextLoads 方法:

xxx
Fruit{fruitId='null', fruitName='null', avatar='http://www.xxx.com/pears.jpg', price=74.2, stock=80}
xxx

成功读取到了数据库中的信息,不过这里的 fruitIdfruitNamenull 是由于我们配置实体类的字段名与数据库中的字段名不匹配,数据库中的字段是 fruit_id 这种形式,而 JAVA 实体类中使用的是 小驼峰,后续的博客应该会提到相关的内容

标签:SpringBoot,xxx,private,整合,mysql,MyBatis,import,com,String
From: https://blog.51cto.com/u_15667349/5730390

相关文章

  • 一个 dubbo 和 springboot 的兼容性问题
    背景介绍最近把dubbo的版本从2.7.3升级到2.7.15时,遇到一个报错Noapplicationconfigfoundorit'snotavalidconfig!,对应的异常栈为:Causedby:java.lang.Illega......
  • springboot整合thymeleaf模板引擎和bootstrap实现增删改查和文件上传
     一、参照第八天任务中的栏目表,使用thymeleaf做为前端展现,完成CRUD及分页操作二、使用springboot+mybatis-plus+redis完成用户登录系统,数据库表users字段名称......
  • SpringBoot 整合 MyBatis
    本文基于:https://www.bilibili.com/video/BV15b4y1a7yG?p=28&vd_source=cf8900ba33d057d422c588abe5d5290d在pom.xml中导入坐标<dependencies>...<!--引入MyB......
  • mybatis-plus自动填充功能
    如果需要用到自动填充功能,比如自动填充数据的新增日期,修改日期。先在config包下新增一个配置文件MyMetaObjectHandlerpackagecom.xzit.config;importcom.baomidou.my......
  • springboot常见错误
    错误1:运行项目后报如下错误解决方案报错2:​​Failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile(default-compile)onprojectsb​......
  • Redis入门(四):springboot整合redis
    案例一​​demo​​​为​​chnx/springboot/redis01​​创建springboot项目,导入redis依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>s......
  • 使用mybatis plus常见错误
    错误1:​​代码生成器依赖和mybatis依赖冲突​​启动项目时报错如下2021-12-0313:33:33.927ERROR7228---[main]o.s.b.d.LoggingFailureAnalysisReporter......
  • mybatis plus通过java代码进行权限等全局控制
    在mapper.xml中调用java静态方法,并且传递一些参数在静态方法中进行sql拼接,可以用于用户权限管理、数据权限管理等等一、静态方法拼接sql,可以调用缓存中的用户权限进......
  • MyBatis-Plus修改数据,会不会把其他字段置为null
    前两天在用MyBatis-Plus写了一张单表的增删改查,在写到修改的时候,就突然蹦出一个奇怪的想法。MyBatis-Plus的BaseMapper中有两个关于修改的方法。如下:intupdateById(@Par......
  • SpringBoot--解决子线程无法获得HttpServletRequest的attribute的问题
    ​简介    本文介绍解决SpringBoot子线程无法获得HttpServletRequest的attribute的问题。    在SpringBoot请求中,如果创建了子线程获取request的attribute,......