欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

零基礎搭建boot+MybatisPlus的詳細教程

 更新時間:2022年03月20日 09:14:42   作者:丑陽璐  
這篇文章主要介紹了零基礎搭建boot+MybatisPlus,首先需要創(chuàng)建數(shù)據庫表和創(chuàng)建boot項目使用mybatisplus操作數(shù)據庫,本文通過示例代碼給大家介紹的非常詳細,需要的朋友可以參考下

1.準備工作

1.1 創(chuàng)建數(shù)據庫表

創(chuàng)建表

CREATE  TABLE `login`(
   `id`  INT(4)  primary key auto_increment,
   `login_id`  VARCHAR(50)  UNIQUE,
   `city` VARCHAR(50)  DEFAULT  '富平',
   `password`  VARCHAR(50)
)

在可視化工具中添加數(shù)據(我不太會寫sql)

1.2 創(chuàng)建boot項目

1.3 創(chuàng)建實體類(映射數(shù)據庫表)

2.使用mybatisPlus(操作數(shù)據庫)

2.1 添加mybatisPlus依賴

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.1.2</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

2.2 配置數(shù)據庫信息

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test0314?characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver

2.3 創(chuàng)建mapper接口

該接口中提供了常用的crud方法,我們只需要從容器中獲取mapper操作數(shù)據即可

package com.hand.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hand.demo.entity.User;

/**
 * 用戶數(shù)據訪問層接口
 * */
public interface UserMapper extends BaseMapper<User> {
}

2.4 配置mapper掃描

  • 在啟動類中配置我們的mapper在哪個包
  • 兩種方法:@Mapper注解(麻煩);@MapperScan(在主啟動類上進行配置)
@SpringBootApplication
@MapperScan("com.hand.demo.mapper")
public class Demo0318Application {
    public static void main(String[] args) {
        SpringApplication.run(Demo0318Application.class, args);
    }
}

2.5 test

 <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

在test包下

package com.hand.demo;
import com.hand.demo.entity.User;
import com.hand.demo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class Demo0318ApplicationTests {
    @Autowired
    private UserMapper userMapper;
    /**
     * 獲取UserMapper實現(xiàn)類對象(mybatisPlus容器會使用動態(tài)代理生成該接口的實現(xiàn)類對象,并注入到spring容器中
     * 所以我們只需要在這定義一個成員變量,通過注解自動注入即可)
     * */
    @Test
    public void testQueryAll() {
        List<User> userList = userMapper.selectList(null);
        System.out.println(userList);
    }
}

3. 常用設置

3.1 設置表映射規(guī)則

設置表前綴配置

3.2 主鍵生成策略(默認基于雪花算法)

 @TableId(type = IdType.AUTO)
    private Long id;

3.3 全局設置

mybatis-plus:
  global-config:
    db-config:
      table-prefix:
      id-type: auto

3.4 字段與列名的駝峰映射(默認開啟)

mybatis-plus:
  global-config:
    db-config:
      table-prefix:
      id-type: auto
  configuration:
    map-underscore-to-camel-case: false  

3.5 日志設置

mybatis-plus:
  global-config:
    db-config:
      table-prefix:
      id-type: auto
  configuration:
    map-underscore-to-camel-case: false
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

4.基操

4.1 插入 insert()

4.2 刪除 deleteXxx() map

4.3 更新 updateXxx()

5.Wrapper(條件構造器)

5.1

 Wrapper
           AbstractWrapper    
    QueryWrapper   UpdateWrapper 

QueryWrapper的select可以設置需要查詢的列

6. service層使用

  • 不需要手動注入該泛型內的mapper
  • 如果需要別的mapper手動注入就行
package com.hand.demo.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.hand.demo.entity.User;

public interface UserService extends IService<User> {
    
}
package com.hand.demo.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hand.demo.entity.User;
import com.hand.demo.mapper.UserMapper;
import com.hand.demo.service.UserService;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
}
    @Autowired
    private UserService userService;

    @Test
    public void testService() {
        List<User> list = userService.list();
        System.out.println(list);
    }
  • 也有自己的批量操作等(batch)
  • 自定義方法(多表關聯(lián))

7. 代碼生成器(未完待續(xù))

  • 每個接口都在繼承相同的BaseMapper,IService(代碼冗余,繁瑣)
  • MybatisPlus提供的代碼生成器,一鍵生成mvc三層所有代碼
  • 如何使用,引入下邊的包
 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
        </dependency>

到此這篇關于零基礎搭建boot+MybatisPlus的文章就介紹到這了,更多相關boot+MybatisPlus搭建內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java創(chuàng)建多線程局域網聊天室實例

    Java創(chuàng)建多線程局域網聊天室實例

    這篇文章主要介紹了Java創(chuàng)建多線程局域網聊天室實例,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-07-07
  • 解決swaggerUI頁面沒有顯示Controller方法的坑

    解決swaggerUI頁面沒有顯示Controller方法的坑

    這篇文章主要介紹了解決swaggerUI頁面沒有顯示Controller方法的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • java實現(xiàn)模擬USB接口的功能

    java實現(xiàn)模擬USB接口的功能

    本文主要介紹了java實現(xiàn)模擬USB接口的功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • mybatis關于Criteria使用的小坑

    mybatis關于Criteria使用的小坑

    這篇文章主要介紹了mybatis關于Criteria使用的小坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 如何通過jstack命令dump線程信息

    如何通過jstack命令dump線程信息

    這篇文章主要介紹了如何通過jstack命令dump線程信息,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-07-07
  • java實現(xiàn)順時針打印矩陣

    java實現(xiàn)順時針打印矩陣

    這篇文章主要為大家詳細介紹了java實現(xiàn)順時針打印矩陣的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • Service層異常拋到Controller層處理還是直接處理問題分析

    Service層異常拋到Controller層處理還是直接處理問題分析

    這篇文章主要為大家介紹了Service層異常拋到Controller層處理還是直接處理的問題分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • Linux下Java開發(fā)環(huán)境搭建以及第一個HelloWorld

    Linux下Java開發(fā)環(huán)境搭建以及第一個HelloWorld

    這篇文章主要介紹了Linux下Java開發(fā)環(huán)境搭建以及第一個HelloWorld的實現(xiàn)過程,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-09-09
  • JAVA8 的StringJoiner 使用及原理解析

    JAVA8 的StringJoiner 使用及原理解析

    這篇文章主要介紹了JAVA8 的StringJoiner 使用及原理解析,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Springboot中spring-boot-starter-quartz的使用及說明

    Springboot中spring-boot-starter-quartz的使用及說明

    這篇文章主要介紹了Springboot中spring-boot-starter-quartz的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12

最新評論