前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >SpringBoot整合MyBatisPlus

SpringBoot整合MyBatisPlus

作者头像
GeekLiHua
发布2025-01-21 18:07:05
发布2025-01-21 18:07:05
10600
代码可运行
举报
文章被收录于专栏:JavaJava
运行总次数:0
代码可运行

SpringBoot整合MyBatisPlus

简介:MyBatisPlus是国人开发的更加适合于国人使用的MyBatis。

环境准备

数据库建表语句

代码语言:javascript
代码运行次数:0
运行
复制
-- 删除tb_brand表
drop table if exists tb_brand;
-- 创建tb_brand表
create table tb_brand
(
    -- id 主键
    id           int primary key auto_increment,
    -- 品牌名称
    brand_name   varchar(20),
    -- 企业名称
    company_name varchar(20),
    -- 排序字段
    ordered      int,
    -- 描述信息
    description  varchar(100),
    -- 状态:0:禁用  1:启用
    status       int
);
-- 添加数据
insert into tb_brand (brand_name, company_name, ordered, description, status)
values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
       ('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),
       ('小米', '小米科技有限公司', 50, 'are you ok', 1);

创建项目

去官网找:官网地址mvn官网(但是可能打不开)

pom.xml

代码语言:javascript
代码运行次数:0
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.6</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>SpringBootMybatis-plus</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>SpringBootMybatis-plus</name>
	<description>SpringBootMybatis-plus</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.4.3</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

application.yml

代码语言:javascript
代码运行次数:0
运行
复制
# 数据库配置信息
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis
    username: root
    password: 123456

# 设置Mp相关配置
mybatis-plus:
  global-config:
    db-config:
      table-prefix: tb_ # 这里写数据库表的前缀

Brand与BrandDao

文件结构

brand

代码语言:javascript
代码运行次数:0
运行
复制
package com.example.springbootmybatisplus.domain;

public class Brand {
    private Integer id;
    private String brand_name;
    private String companyName;
    private Integer ordered;
    private String description;

    private boolean status;

    @java.lang.Override
    public java.lang.String toString() {
        return "brand{" +
                "id=" + id +
                ", brand_name='" + brand_name + '\'' +
                ", companyName='" + companyName + '\'' +
                ", ordered=" + ordered +
                ", description='" + description + '\'' +
                ", status=" + status +
                '}';
    }



    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBrand_name() {
        return brand_name;
    }

    public void setBrand_name(String brand_name) {
        this.brand_name = brand_name;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public Integer getOrdered() {
        return ordered;
    }

    public void setOrdered(Integer ordered) {
        this.ordered = ordered;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isStatus() {
        return status;
    }

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

BrandDao

代码语言:javascript
代码运行次数:0
运行
复制
package com.example.springbootmybatisplus.dao;

import com.example.springbootmybatisplus.domain.Brand;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.awt.print.Book;

@Mapper
public interface BrandDao {
    @Select("select * from tb_brand where id = #{id}")
    public Brand getById(Integer id);
}

测试类

代码语言:javascript
代码运行次数:0
运行
复制
package com.example.springbootmybatisplus;

import com.example.springbootmybatisplus.dao.BrandDao;
import com.example.springbootmybatisplus.domain.Brand;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootMybatisPlusApplicationTests {

	@Autowired
	private BrandDao brandDao;
	@Test
	void contextLoads() {
		System.out.println(brandDao.getById(1));
	}

}

运行结果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • SpringBoot整合MyBatisPlus
    • 环境准备
    • Brand与BrandDao
    • 测试类
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档