溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot如何構(gòu)建ORM框架

發(fā)布時(shí)間:2022-02-21 09:18:43 來源:億速云 閱讀:212 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“SpringBoot如何構(gòu)建ORM框架”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“SpringBoot如何構(gòu)建ORM框架”吧!

目前常用的ORM框架有 Mybatis(batis)、MybatisPlus,Hibernate、Jpa等幾個(gè)框架,今天就簡單介紹一下搭建Mybatisplus框架的流程。

1.增加依賴

<dependencies>
        <!--        第一步:選擇ORM框架,使用springboot整合mybatis-plus依賴包-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!--        第二步:選擇數(shù)據(jù)庫驅(qū)動(dòng),這里是Mysql所以就選擇Mysql的驅(qū)動(dòng),PG的就選擇PG-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <!--        第三步(可選):數(shù)據(jù)庫連接池,可以使用druid的連接池。springboot-jdbc已經(jīng)默認(rèn)依賴了Hikari的連接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
    </dependencies>

2.數(shù)據(jù)庫實(shí)體模型

主要使用@TableName和@TableField,配置屬性類和數(shù)據(jù)庫表的對應(yīng)關(guān)系

@TableName("userinfo")
@Data
public class UserInfo {
 
    @TableId(type = IdType.AUTO)
    private Integer id;
 
    @TableField
    private String name;
 
    private String usernum;
 
    private int sex;
 
    private Date createtime;
 
    private Date updatetime;
}

3.增加Mapper

使用BaseMapper繼承或者IService繼承

BaseMapper 接口中封裝了一系列 CRUD 常用操作

IService 內(nèi)部進(jìn)一步封裝了 BaseMapper 接口的方法(當(dāng)然也提供了更詳細(xì)的方法)。

public interface IUserInfoMapper extends BaseMapper<UserInfo> {
 
}

或者

public interface IUserInfoSevice extends IService<UserInfo> {
 
}

4.@Mapper或者@MapperScan

使用@Mapper或者@MapperScan,將Mapper的接口類編譯成實(shí)現(xiàn)類,才能注入。

@MapperScan:在啟動(dòng)項(xiàng)類上增加@MapperScan,指定掃描的包。指定了變成實(shí)現(xiàn)類的接口所在的包,然后包下面的所有接口在編譯之后都會生成相應(yīng)的實(shí)現(xiàn)類

@Mapper:在接口上增加@Mapper,在編譯之后會生成相應(yīng)的接口實(shí)現(xiàn)類。

@SpringBootApplication
@MapperScan("......")
public class MybatisPlusProgram {
 
    public static void main(String[] args) {
        SpringApplication.run(MybatisPlusProgram.class, args);
    }
}

5.配置連接

默認(rèn)數(shù)據(jù)庫配置連接

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/myboot?useUnicode=true&characterEncoding=utf8
    username: root
    password: root

durid連接池配置連接:

spring:
  datasource:
    #1.JDBC
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/myboot?useUnicode=true&characterEncoding=utf8
    username: root
    password: root
    druid:
      #2.連接池配置
      #初始化連接池的連接數(shù)量 大小,最小,最大
      initial-size: 5
      min-idle: 5
      max-active: 20
      #配置獲取連接等待超時(shí)的時(shí)間
      max-wait: 60000
      #配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
      time-between-eviction-runs-millis: 60000
      # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
      min-evictable-idle-time-millis: 30000
      # 檢查數(shù)據(jù)庫
      validation-query: SELECT 1 FROM DUAL
      test-while-idle: true
      test-on-borrow: true
      test-on-return: false
      # 是否緩存preparedStatement,也就是PSCache  官方建議MySQL下建議關(guān)閉   個(gè)人建議如果想用SQL防火墻 建議打開
      pool-prepared-statements: true
      max-pool-prepared-statement-per-connection-size: 20
      # 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計(jì),'wall'用于防火墻
      filter:
        stat:
          merge-sql: true
          slow-sql-millis: 5000
      #3.基礎(chǔ)監(jiān)控配置
      web-stat-filter:
        enabled: true
        url-pattern: /*
        #設(shè)置不統(tǒng)計(jì)哪些URL
        exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
        session-stat-enable: true
        session-stat-max-count: 100
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        reset-enable: true
        #設(shè)置監(jiān)控頁面的登錄名和密碼
        #監(jiān)控頁訪問:http://localhost:端口號/項(xiàng)目名稱/druid/login.html
        login-username: admin
        login-password: admin
        allow: 127.0.0.1
        #deny: 192.168.1.100

SpringBoot如何構(gòu)建ORM框架

到此,相信大家對“SpringBoot如何構(gòu)建ORM框架”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI