日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Mybatis-plus的快速使用

作者:宣布無人罪 更新時間: 2023-12-19 編程語言

01 7項前期準備

  • 引入依賴

  • 配置yml文件

  • 配置啟動類

  • 創建實體類

  • 創建mapper接口繼承BaseMapper類

  • 創建service接口繼承IService類

  • 創建ServiceImpl實現類繼承ServiceImp類和實現service接口

02 引入依賴

  • 在pom.xml中配置依賴

 ? ?<dependencies>
<!--其他要用的依賴-->
 ? ? ? ?<!--mybatis-plus需要的依賴-->
 ? ? ? ?<dependency>
 ? ? ? ? ? ?<groupId>com.baomidou</groupId>
 ? ? ? ? ? ?<artifactId>mybatis-plus-boot-starter</artifactId>
 ? ? ? ? ? ?<version>3.5.1</version>
 ? ? ? ?</dependency>
 ? ?</dependencies>

03 配置yml文件

# 配置數據源
spring:
  datasource:
 ?  driver-class-name: com.mysql.cj.jdbc.Driver
 ?  url: jdbc:mysql://localhost:3306/需要連接的數據庫?userSSL=false;serverTimezone=Asia/Shanghai
 ? ?#你的賬號
 ?  username: root
 ? ?#你的密碼
 ?  password: 123456
mybatis-plus:
 ?# mapper文件夾配置文件,在resource資源文件夾內建一個mapper的文件夾
  mapper-locations: classpath:mapper/*.xml
 ?# resultType別名,沒有這個配置resultType包名要寫全,配置后只要寫類名
  type-aliases-package: com.example.springboot01.entity
  configuration:
 ? ?# 日志的配置,直接添加即可,內部有自帶配置項
 ?  log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
 ? ?#數據庫下劃線自動轉駝峰配置
 ?  map-underscore-to-camel-case: true

04 配置啟動類

  • springboot自帶一個啟動類,在啟動類配置注解進行掃描@MapperScan("com.mashang.demo.mapper")

  • 括號內為掃描路徑,填入java文件夾中數據操作層的路徑(就接下去要繼承BaseMapper類的類所在的文件夾)

@SpringBootApplication
@MapperScan("com.mashang.demo.mapper")
public class DemoApplication {
?
 ? ?public static void main(String[] args) {
 ? ? ? ?SpringApplication.run(DemoApplication.class, args);
 ?  }
?
}

05 創建實體類

  • 創建與數據庫內表對應的實體類,這里我配置了在yml文件中配置了數據庫下劃線自動轉駝峰,所以數據庫中的下劃線自動轉成下一個單詞首字母大寫

public class Category {
 ? ?//@TableId用于標記數據庫中的主鍵
 ? ?@TableId
 ? ?private Long categoryId;
 ? ?private String categoryName;
 ? ?private String categoryPicture1;
 ? ?private String categoryPicture2;
 ? ?//get和set方法,toString方法,空參構造器和全參數構造器記得重寫,我使用了額外的插件,這里就不重寫了
 ? ?
}

06 創建mapper接口繼承BaseMapper類

  • 創建一個接口繼承mybatis的BaseMapper<>類

  • 括號內的泛型要填需要操作的數據庫對應的實體類也就是第5步創建實體類

@Repository//記得注入sprig
public interface CategoryMapper extends BaseMapper<Category> {
?
}

07 創建service接口繼承IService類

  • 創建一個接口繼承mybatis的IService<Category>

  • 括號內的泛型要填需要操作的數據庫對應的實體類也就是第5步創建實體類

public interface ICategoryService extends IService<Category> {
}

08 創建ServiceImpl實現類繼承ServiceImp類和實現service接口

  • 創建一個實體繼承mybatis的ServiceImpl<Category>類

  • 括號內的泛型要填是第5步創建實體類和第6步創建的mapper接口

  • 還要實現(implements)第7部創建的service接口

@Service//同樣的,要注入spring
public class CategoryServiceImp extends ServiceImpl<CategoryMapper, Category>
 ? ?implements ICategoryService {
}

09 測試

  • 完成了7項前期準備后就可以開始使用了

  • 這里使用springboot自帶的測試類test文件夾內DemoApplicationTests類進行測試

@SpringBootTest
class DemoApplicationTests {
    //首先注入第7步service接口
 ? ?@Autowired
 ? ?private ICategoryService iCategoryService;
?
 ? ?@Test
 ? ?void contextLoads() {
 ? ? ? ?//這時候mybatis-plus自動實現接口中的方法,可以直接調用
 ? ? ? ?List<Category> categories=iCategoryService.list();
 ?  }
?
}
  • 將鼠標移到contextLoads()方法內, 右鍵啟動Run'contextLoads()'

  • 當操作臺打印出數據庫對應的實體類數據時,說明成功

JDBC Connection [HikariProxyConnection@1958242673 wrapping com.mysql.cj.jdbc.ConnectionImpl@1f9d4b0e] will not be managed by Spring
==> ?Preparing: SELECT category_id,category_name,category_picture1,category_picture2 FROM category
==> Parameters: 
<== ? ?Columns: category_id, category_name, category_picture1, category_picture2
<== ? ? ? ?Row: 1, 手機, null, null
<== ? ? ? ?Row: 2, 電視機, null, null
<== ? ? ? ?Row: 3, 空調, null, null
<== ? ? ? ?Row: 4, 洗衣機, null, null
<== ? ? ? ?Row: 5, 保護套, null, null
<== ? ? ? ?Row: 6, 保護膜, null, null
<== ? ? ? ?Row: 7, 充電器, null, null
<== ? ? ? ?Row: 8, 充電寶, null, null
<== ? ? ? ?Row: 11, 手辦, null, null
<== ? ? ?Total: 9
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@58c1da09]

原文鏈接:https://blog.csdn.net/2302_77182979/article/details/134405306

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新