SpringBoot整合MyBatisPlus

水深无声 2024-03-30 06:33 292阅读 0赞

SpringBoot整合MyBatisPlus

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

环境准备

数据库建表语句

  1. -- 删除tb_brand
  2. drop table if exists tb_brand;
  3. -- 创建tb_brand
  4. create table tb_brand
  5. (
  6. -- id 主键
  7. id int primary key auto_increment,
  8. -- 品牌名称
  9. brand_name varchar(20),
  10. -- 企业名称
  11. company_name varchar(20),
  12. -- 排序字段
  13. ordered int,
  14. -- 描述信息
  15. description varchar(100),
  16. -- 状态:0:禁用 1:启用
  17. status int
  18. );
  19. -- 添加数据
  20. insert into tb_brand (brand_name, company_name, ordered, description, status)
  21. values ('三只松鼠', '三只松鼠股份有限公司', 5, '好吃不上火', 0),
  22. ('华为', '华为技术有限公司', 100, '华为致力于把数字世界带入每个人、每个家庭、每个组织,构建万物互联的智能世界', 1),
  23. ('小米', '小米科技有限公司', 50, 'are you ok', 1);

创建项目
在这里插入图片描述
在这里插入图片描述
去官网找:官网地址mvn官网(但是可能打不开)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.7.6</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>SpringBootMybatis-plus</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>SpringBootMybatis-plus</name>
  15. <description>SpringBootMybatis-plus</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>com.baomidou</groupId>
  22. <artifactId>mybatis-plus-boot-starter</artifactId>
  23. <version>3.4.3</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>mysql</groupId>
  27. <artifactId>mysql-connector-java</artifactId>
  28. <scope>runtime</scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-test</artifactId>
  33. <scope>test</scope>
  34. </dependency>
  35. </dependencies>
  36. <build>
  37. <plugins>
  38. <plugin>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-maven-plugin</artifactId>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. </project>

在这里插入图片描述
在这里插入图片描述
application.yml

  1. # 数据库配置信息
  2. spring:
  3. datasource:
  4. driver-class-name: com.mysql.cj.jdbc.Driver
  5. url: jdbc:mysql://localhost:3306/mybatis
  6. username: root
  7. password: 123456
  8. # 设置Mp相关配置
  9. mybatis-plus:
  10. global-config:
  11. db-config:
  12. table-prefix: tb_ # 这里写数据库表的前缀

Brand与BrandDao

文件结构
在这里插入图片描述

brand

  1. package com.example.springbootmybatisplus.domain;
  2. public class Brand {
  3. private Integer id;
  4. private String brand_name;
  5. private String companyName;
  6. private Integer ordered;
  7. private String description;
  8. private boolean status;
  9. @java.lang.Override
  10. public java.lang.String toString() {
  11. return "brand{" +
  12. "id=" + id +
  13. ", brand_name='" + brand_name + '\'' +
  14. ", companyName='" + companyName + '\'' +
  15. ", ordered=" + ordered +
  16. ", description='" + description + '\'' +
  17. ", status=" + status +
  18. '}';
  19. }
  20. public Integer getId() {
  21. return id;
  22. }
  23. public void setId(Integer id) {
  24. this.id = id;
  25. }
  26. public String getBrand_name() {
  27. return brand_name;
  28. }
  29. public void setBrand_name(String brand_name) {
  30. this.brand_name = brand_name;
  31. }
  32. public String getCompanyName() {
  33. return companyName;
  34. }
  35. public void setCompanyName(String companyName) {
  36. this.companyName = companyName;
  37. }
  38. public Integer getOrdered() {
  39. return ordered;
  40. }
  41. public void setOrdered(Integer ordered) {
  42. this.ordered = ordered;
  43. }
  44. public String getDescription() {
  45. return description;
  46. }
  47. public void setDescription(String description) {
  48. this.description = description;
  49. }
  50. public boolean isStatus() {
  51. return status;
  52. }
  53. public void setStatus(boolean status) {
  54. this.status = status;
  55. }
  56. }

BrandDao

  1. package com.example.springbootmybatisplus.dao;
  2. import com.example.springbootmybatisplus.domain.Brand;
  3. import org.apache.ibatis.annotations.Mapper;
  4. import org.apache.ibatis.annotations.Select;
  5. import java.awt.print.Book;
  6. @Mapper
  7. public interface BrandDao {
  8. @Select("select * from tb_brand where id = #{id}")
  9. public Brand getById(Integer id);
  10. }

测试类

  1. package com.example.springbootmybatisplus;
  2. import com.example.springbootmybatisplus.dao.BrandDao;
  3. import com.example.springbootmybatisplus.domain.Brand;
  4. import org.junit.jupiter.api.Test;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.context.SpringBootTest;
  7. @SpringBootTest
  8. class SpringBootMybatisPlusApplicationTests {
  9. @Autowired
  10. private BrandDao brandDao;
  11. @Test
  12. void contextLoads() {
  13. System.out.println(brandDao.getById(1));
  14. }
  15. }

运行结果
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,292人围观)

还没有评论,来说两句吧...

相关阅读