您现在的位置是:首页 > 编程 > 

SpringBoot整合MyBatisPlus

2025-07-19 21:04:15
SpringBoot整合MyBatisPlus SpringBoot整合MyBatisPlus简介:MyBatisPlus是国人开发的更加适合于国人使用的MyBatis。环境准备数据库建表语句代码语言:javascript代码运行次数:0运行复制-- 删除tb_brand表 drop table if exists tb_brand; -- 创建tb_brand表 create table tb_

SpringBoot整合MyBatisPlus

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=".0.0" xmlns:xsi=";
	xsi:schemaLocation=".0.0 .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></groupId>
	<artifactId>SpringBootMybatis-plus</artifactId>
	<version>0.0.1-SAPSHOT</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>.4.</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: jdbc.Driver
    url: jdbc:mysql://localhost:06/mybatis
    username: root
    password: 12456

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

Brand与BrandDao

文件结构

brand

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

public class Brand {
    private Integer id;
    private String brand_name;
    private String companyame;
    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 + '\'' +
                ", companyame='" + companyame + '\'' +
                ", 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 getCompanyame() {
        return companyame;
    }

    public void setCompanyame(String companyame) {
        thispanyame = companyame;
    }

    public Integer getOrdered() {
        return ordered;
    }

    public void setOrdered(Integer 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 .springbootmybatisplus.dao;

import .springbootmybatisplus.domain.Brand;
import org.apache.ibatis.annotati.Mapper;
import org.apache.ibatis.annotati.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 .springbootmybatisplus;

import .springbootmybatisplus.dao.BrandDao;
import .springbootmybatisplus.domain.Brand;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.SpringBootTest;

@SpringBootTest
class SpringBootMybatisPlusApplicationTests {

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

}

运行结果

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

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1145973.html

相关标签:无
上传时间: 2025-07-19 13:27:42
留言与评论(共有 16 条评论)
本站网友 福州ktv团购
28分钟前 发表
06/mybatis username
本站网友 襄阳二手房出售
4分钟前 发表
description='" + description + '\'' + "
本站网友 枫蓝
4分钟前 发表
xsi="; xsi
本站网友 腹部怎样减肥
27分钟前 发表
ordered=" + ordered + "
本站网友 网易结构论坛
24分钟前 发表
status) values ('三只松鼠'
本站网友 正规网上购药网站
4分钟前 发表
-- 企业名称 company_name varchar(20)
本站网友 500克等于多少斤
15分钟前 发表
tb_ # 这里写数据库表的前缀Brand与BrandDao文件结构 brand代码语言:javascript代码运行次数:0运行复制package .springbootmybatisplus.domain; public class Brand { private Integer id; private String brand_name; private String companyame; private Integer ordered; private String description; private boolean status; @java.lang.Override public java.lang.String toString() { return "brand{" + "id=" + id + "
本站网友 22cccc
27分钟前 发表
原始发表:2025-01-20
本站网友 森远
28分钟前 发表
//localhost
本站网友 乙肝用药
8分钟前 发表
description='" + description + '\'' + "
本站网友 专注圣典
18分钟前 发表
('华为'
本站网友 五里店租房
26分钟前 发表
1);创建项目 去:地址mvn(但是可能打不开) pom.xml代码语言:javascript代码运行次数:0运行复制<?xml version="1.0" encoding="UTF-8"?> <project xmlns=".0.0" xmlns
本站网友 义县二手房
2分钟前 发表
mysql
本站网友 林泉
0秒前 发表
-- 状态:0:禁用 1:启用 status int ); -- 添加数据 insert into tb_brand (brand_name
本站网友 江苏单车网
21分钟前 发表
root password