最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot數(shù)據(jù)訪問(wèn)的實(shí)現(xiàn)

 更新時(shí)間:2023年11月27日 10:26:56   作者:程序猿進(jìn)階  
本文主要介紹了SpringBoot數(shù)據(jù)訪問(wèn)的實(shí)現(xiàn),引入各種xxxTemplate,xxxRepository來(lái)簡(jiǎn)化我們對(duì)數(shù)據(jù)訪問(wèn)層的操作,感興趣的可以了解一下

對(duì)于數(shù)據(jù)訪問(wèn)層,無(wú)論是SQL還是NoSQL,SpringBoot默認(rèn)采用整合Spring Data的方式進(jìn)行統(tǒng)一處理,添加大量自動(dòng)配置,屏蔽了很多設(shè)置。引入各種xxxTemplate,xxxRepository來(lái)簡(jiǎn)化我們對(duì)數(shù)據(jù)訪問(wèn)層的操作。對(duì)我們來(lái)說(shuō)只需要進(jìn)行簡(jiǎn)單的設(shè)置即可。

一、整合基本的 JDBC 與數(shù)據(jù)源

【1】引入jdbc starter [spring-boot-starter-jdbc] 和MySQL驅(qū)動(dòng)。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

【2】在application.yml中配置數(shù)據(jù)源相關(guān)信息:

spring:
  datasource:
    username: root
    password: 123
    url: jdbc:mysql://127.0.0.1:3306/jdbc
    driver-class-name: com.mysql.jdbc.Driver

【3】測(cè)試:默認(rèn)使用的是org.apache.tomcat.jdbc.pool.DataSource作為數(shù)據(jù)源。數(shù)據(jù)源的相關(guān)配置都在DataSourceProperties里面。自動(dòng)配置原理:org.springframework.boot.autoconfigure.jdbc包中的DataSourceConfiguration,根據(jù)配置創(chuàng)建數(shù)據(jù)源,默認(rèn)使用Tomcat連接池;可以通過(guò)spring.datasource.type指定自定義數(shù)據(jù)源類(lèi)型;SpringBoot默認(rèn)支持一下數(shù)據(jù)源:DataSource、HikariDataSourceBasicDataSource。用戶(hù)也可以自定義數(shù)據(jù)源:如下可知是通過(guò)build創(chuàng)建數(shù)據(jù)源的。利用反射創(chuàng)建type類(lèi)型的數(shù)據(jù)源,并綁定相關(guān)屬性。

@ConditionalOnMissingBean({DataSource.class})
@ConditionalOnProperty(
    name = {"spring.datasource.type"}
)
static class Generic {
    Generic() {
    }
        //通過(guò) build 創(chuàng)建數(shù)據(jù)源的。利用反射創(chuàng)建 type 類(lèi)型的數(shù)據(jù)源,并綁定相關(guān)屬性。
    @Bean
    public DataSource dataSource(DataSourceProperties properties) {
        return properties.initializeDataSourceBuilder().build();
    }
}

【4】第二個(gè)比較重要的類(lèi)DataSourceAutoConfiguration自動(dòng)配置類(lèi)中的dataSourceInitializer繼承了ApplicationListener。

public class DataSourceAutoConfiguration {
    private static final Log logger = LogFactory.getLog(DataSourceAutoConfiguration.class);

    public DataSourceAutoConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean
    public DataSourceInitializer dataSourceInitializer(DataSourceProperties properties, ApplicationContext applicationContext) {
        return new DataSourceInitializer(properties, applicationContext);
    }
    //......
}

class DataSourceInitializer implements ApplicationListener<DataSourceInitializedEvent> {
//......
}

DataSourceInitializer的兩個(gè)主要作用:①、運(yùn)行建表語(yǔ)句;②、運(yùn)行操作數(shù)據(jù)的sql語(yǔ)句;

//運(yùn)行建表語(yǔ)句
private void runSchemaScripts() {
    List<Resource> scripts = this.getScripts("spring.datasource.schema", this.properties.getSchema(), "schema");
    if(!scripts.isEmpty()) {
        String username = this.properties.getSchemaUsername();
        String password = this.properties.getSchemaPassword();
        this.runScripts(scripts, username, password);

        try {
            this.applicationContext.publishEvent(new DataSourceInitializedEvent(this.dataSource));
            if(!this.initialized) {
                this.runDataScripts();
                this.initialized = true;
            }
        } catch (IllegalStateException var5) {
            logger.warn("Could not send event to complete DataSource initialization (" + var5.getMessage() + ")");
        }
    }

}
//運(yùn)行操作數(shù)據(jù)的 sql語(yǔ)句
private void runDataScripts() {
    List<Resource> scripts = this.getScripts("spring.datasource.data", this.properties.getData(), "data");
    String username = this.properties.getDataUsername();
    String password = this.properties.getDataPassword();
    this.runScripts(scripts, username, password);
}

【5】進(jìn)行數(shù)據(jù)表創(chuàng)建時(shí),默認(rèn)只需要將文件命名為:schema-*.sql;進(jìn)行數(shù)據(jù)操作時(shí),默認(rèn)只需要將文件命令為:data-*.sql;如果自定義sql文件時(shí),可以在application.yml中通過(guò)如下方式指定sql文件位置:傳入的是一個(gè)list列表

spring:
  datasource:
    schema:
      - classpath: student.sql

【6】JdbcTemplateAutoConfiguration自動(dòng)配置類(lèi)給容器中注入了JdbcTemplate組件,幫組我們操作數(shù)據(jù)庫(kù)。

@AutoConfigureAfter({DataSourceAutoConfiguration.class})
public class JdbcTemplateAutoConfiguration {
    @Bean
    @Primary
    @ConditionalOnMissingBean({JdbcOperations.class})
    public JdbcTemplate jdbcTemplate() {
        return new JdbcTemplate(this.dataSource);
    }
}

【7】高級(jí)配置:使用druid數(shù)據(jù)源,首先需要引入依賴(lài):

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>

【8】在yml配置文件中加入Druid

spring:
  datasource:
    username: root
    password: 123
    url: jdbc:mysql://127.0.0.1:3306/jdbc
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

二、整合 Mybatis 數(shù)據(jù)源(注解版)

【1】創(chuàng)建項(xiàng)目:選中web、mybatismysqljdbc模塊的starts;在pom.xml中會(huì)發(fā)現(xiàn)mybatisspringboot的整合依賴(lài):這個(gè)依賴(lài)并不是以spring-boot開(kāi)始的,說(shuō)明并不是spring提供的依賴(lài),而是由第三方mybatis提供的依賴(lài)包;

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>

【2】定義個(gè)接口類(lèi),用來(lái)操作目標(biāo)表的增刪改查:通過(guò)@Mapper表示該接口類(lèi)是一個(gè)MybatisMapper類(lèi),通過(guò)增刪改查注解@Select @Update @Insert @Delete對(duì)數(shù)據(jù)表進(jìn)行操作;

//指定這是一個(gè)操作數(shù)據(jù)庫(kù)的 mapper
@Mapper
public interface DepartmentMapper {

    @Select("select * from department where id=#{id}")
    public Department getDeptById(Integer id);
}

【3】通過(guò)Controller層調(diào)用Server繼而調(diào)用Mapper對(duì)數(shù)據(jù)完成操作:

@Controller
public class DepartmentController {

    @Autowired
    private DepartmentMapper mapper;

    @GetMapping("/dept/{id}")
    public Department getDeptById(@PathVariable("id") Integer id){
        return mapper.getDeptById(id);
    }
}

到此這篇關(guān)于SpringBoot數(shù)據(jù)訪問(wèn)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot數(shù)據(jù)訪問(wèn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 關(guān)于Idea的Invalidate Caches/Restart使用

    關(guān)于Idea的Invalidate Caches/Restart使用

    項(xiàng)目類(lèi)導(dǎo)入爆紅可能因Idea緩存異常導(dǎo)致Maven依賴(lài)識(shí)別失敗,解決方法為通過(guò)Invalidate Caches/Restart清除緩存,等待重新構(gòu)建索引后重新進(jìn)入項(xiàng)目
    2025-07-07
  • 客戶(hù)端設(shè)置超時(shí)時(shí)間真的很重要

    客戶(hù)端設(shè)置超時(shí)時(shí)間真的很重要

    今天小編就為大家分享一篇關(guān)于客戶(hù)端設(shè)置超時(shí)時(shí)間真的很重要,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • Java調(diào)用Pytorch模型實(shí)現(xiàn)圖像識(shí)別

    Java調(diào)用Pytorch模型實(shí)現(xiàn)圖像識(shí)別

    這篇文章主要為大家詳細(xì)介紹了Java如何調(diào)用Pytorch實(shí)現(xiàn)圖像識(shí)別功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2023-06-06
  • Java中InputStream流的多次讀取的實(shí)現(xiàn)示例

    Java中InputStream流的多次讀取的實(shí)現(xiàn)示例

    本文主要介紹了Java中InputStream流的多次讀取的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2026-01-01
  • 一個(gè)Java工程師超實(shí)用的17個(gè)日常效率工具

    一個(gè)Java工程師超實(shí)用的17個(gè)日常效率工具

    這篇文章主要總結(jié)介紹了一個(gè)Java工程師超實(shí)用的17個(gè)日常效率工具,中包含了大多數(shù)開(kāi)發(fā)人員已經(jīng)使用、正在使用或?qū)?lái)一定會(huì)用到的高效工具,每個(gè)工具都給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2026-03-03
  • 最新評(píng)論

    陆丰市| 碌曲县| 原平市| 威远县| 大兴区| 防城港市| 肥城市| 庆安县| 丽水市| 蒲江县| 南溪县| 张家川| 广德县| 确山县| 石渠县| 广宁县| 西充县| 河北区| 泸水县| 井陉县| 会同县| 育儿| 石嘴山市| 岳西县| 衡东县| 乌鲁木齐市| 阆中市| 凌源市| 阿鲁科尔沁旗| 东丰县| 铁力市| 张家港市| 合川市| 鄢陵县| 玉溪市| 肥乡县| 雅安市| 广灵县| 娱乐| 若尔盖县| 耒阳市|