SpringBoot连接MySQL数据库,使用Mybatis框架(入门)

1. 说明

SpringBoot项目,连接MySQL数据库,使用Mybatis框架。

本篇文章作为 SpringBoot 使用 Mybatis 的入门。

2. 依赖

2.1. MySQL驱动依赖

MySQL驱动,使用SpringBoot版本对应的默认版本,不需要手动指定版本。

比如:SpringBoot 版本为 2.7.15,对应的 MySQL 驱动的版本为 8.0.33

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

2.2. Mybatis依赖

MyBatis版本,使用SpringBoot版本推荐的版本(在新建项目时,会根据SpringBoot版本自动生成对应的MyBatis版本)。

比如:SpringBoot 版本为 2.7.15,对应的 MyBatis 的版本为 2.3.1

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.1</version>
        </dependency>

3. 配置数据源

application.yml 文件中,配置数据源(MySQL数据库)。

包括:数据库url,用户名,密码,驱动

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mybatisplus?serverTimeZone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
    username: root
    password: password
    driver-class-name: com.mysql.cj.jdbc.Driver

mysql驱动,使用的是新版驱动 com.mysql.cj.jdbc.Driver;使用旧版驱动,在项目启动的时候,会报错。

4. Controller - Service - Mapper - Entity

4.1. 图示

在这里插入图片描述

4.2. Entity

package com.example.web.entity;

import lombok.Data;

@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

4.3. Mapper

4.3.1. Mapper.java

package com.example.web.mapper;

import com.example.web.entity.User;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface UserMapper {
    List<User> listAll();
}

4.3.2. Mapper.xml

注意:这个 Mapper.xml 文件所在的路径com.example.web.mapper),默认情况,必须和 Mapper.java 文件的路径一致,否则会报错,找不到映射文件(Mapper.xml)。

<?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.example.web.mapper.UserMapper">

    <select id="listAll" resultType="com.example.web.entity.User">
        select * from user
    </select>

</mapper>

4.4. Service

4.4.1. Service接口

package com.example.web.service;

import com.example.web.entity.User;

import java.util.List;

public interface UserService {
    List<User> listAll();
}

4.4.2. Service实现

package com.example.web.service.impl;

import com.example.web.entity.User;
import com.example.web.mapper.UserMapper;
import com.example.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> listAll() {
        return userMapper.listAll();
    }
}

4.5. Controller

package com.example.web.controller;

import com.example.web.entity.User;
import com.example.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> selectAll() {
        return userService.listAll();
    }

}

5. 接口请求与数据

5.1. 接口请求示例

在这里插入图片描述

5.2. 数据库中的表和数据

在这里插入图片描述

6. SpringBoot项目创建时添加mysql驱动和mybatis依赖

这里创建新项目使用的是STS(SpringToolSuite4),因为 IDEA 社区版没法创建SpringBoot项目。
在这里插入图片描述

在这里插入图片描述

MySQL的驱动版本,不需要指定。有和SpringBoot版本适配的默认版本。
在这里插入图片描述

7. 对比:“MySQL驱动”指定版本和不指定版本

7.1. 指定版本

在这里插入图片描述

7.2. 不指定版本(跟随SpringBoot版本)

在这里插入图片描述

8. MySQL驱动的maven仓库坐标变化

MySQL驱动的maven仓库坐标发生了变化。

官方将原来的 mysql-connector-java 改为了 mysql-connector-j

测试时间点为:2023年9月3日

在这里插入图片描述

  • 12
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
好的,我可以为您提供关于使用Spring Boot连接MySQL数据库的一些基本信息和步骤。 首先,您需要在您的Spring Boot项目中添加MySQL连接器的依赖。可以在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>{MySQL版本号}</version> </dependency> ``` 请确保将 {MySQL版本号} 替换为您正在使用MySQL版本号。 接下来,您需要在application.properties文件中配置数据库连接信息。例如: ``` spring.datasource.url=jdbc:mysql://localhost:3306/{数据库名}?useSSL=false&serverTimezone=UTC spring.datasource.username={用户名} spring.datasource.password={密码} spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 请将 {数据库名}、{用户名} 和 {密码} 替换为您的MySQL数据库的实际信息。 最后,您可以通过使用Spring Data JPA或MyBatis等持久化框架来访问数据库。例如,您可以创建一个Person实体类,并使用JPA注解来映射到数据库中的表: ``` @Entity @Table(name = "person") public class Person { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name") private String name; @Column(name = "age") private Integer age; // 省略getter和setter方法 } ``` 然后,您可以创建一个PersonRepository接口来访问数据库中的Person表: ``` @Repository public interface PersonRepository extends JpaRepository<Person, Long> { } ``` 现在,您可以使用PersonRepository接口中提供的方法来访问数据库中的数据了,例如: ``` @Autowired private PersonRepository personRepository; public void savePerson(Person person) { personRepository.save(person); } public List<Person> getAllPeople() { return personRepository.findAll(); } ``` 以上就是使用Spring Boot连接MySQL数据库的一些基本信息和步骤。希望对您有所帮助!
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

宋冠巡

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值