亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

開發中經常說到數據入庫,我們在實現時需要知道使用何種技術,連接哪個數據庫,連接數據庫的賬號和密碼等等。

今天要跟大家分享的是spring web項目中使用MyBatis連接到postgresql中。以本機數據庫為例(即數據庫由本機啟動)。

1、安裝postgresql,并安裝postGIS擴展,建議安裝pgadmin方便使用。安裝過程不再贅述,網上教程多且詳細。

2、執行下列代碼可以查看postgresql、postGIS版本信息,執行失敗則安裝不成果

select version();  // 可以查看postgresql的版本信息。
SELECT PostGIS_full_version(); // 查看postGIS版本信息

 

 

 

 

3、在項目的pom.xml文件中配置依賴

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.5</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
</dependency>

4、mApper類、啟動類中添加注解

@Mapper
public interface ShpMapper {
    int insertFeatureList(@Param("featureList") List<FeatureModel> featureModelList);
}
@SpringBootApplication
@MapperScan("com.example.geoservice.mapper")
public class GeoServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GeoServiceApplication.class, args);
    }

}

5、寫配置文件application.properties(前) 或者 application.yml(后)

#配置mapper的掃描路徑
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
# 地址:端口號/數據庫名稱
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres?useSSL=false
spring.datasource.username=postgres
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver
spring:
  datasource:
    driver-class-name: org. postgresql. Driver
    url: jdbc:postgresql://localhost:5432/postgres?useSSL=false
    username: postgres
    password: 123456

mybatis:
  mapper -locations: classpath:mapper/* . xml
  

6、運行時常見錯誤及解決辦法(1)

 Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-08-28 17:06:47.402 ERROR 13520 --- [      main] o.s.b.d.LoggingFailureAnalysisReporter   :

***************************
    APPLICATION FAILED TO START
***************************

    Description:

    Field shpMapper in com.example.geoservice.service.Impl.ShpServiceImpl required a bean of type 'com.example.geoservice.mapper.ShpMapper' that could not be found.

    The injection point has the following annotations:
            - @org.springframework.beans.factory.annotation.Autowired(required=true)

    Action:

    Consider defining a bean of type 'com.example.geoservice.mapper.ShpMapper' in your configuration.

解決辦法:在pom文件中添加依賴,在啟動類中添加掃描路徑

 

 

 

 

6、運行時常見錯誤及解決辦法(2)- 忘記數據庫用戶名和密碼

①查看用戶:在數據庫上右鍵 -> 屬性,在“常規”中可以看到所有者

 

 

 

②修改密碼。忘記密碼查看密碼比較麻煩,這里建議直接修改密碼。

ALTER USER postgres WITH PASSWORD '123456';

 

 

7、mybatis的mapper和xml跳轉,推薦使用插件mybaitisx

點擊file -> setting -> Plugins -> Marketplace ,搜索mybatisx安裝

 

效果,點擊這個黑色的鳥,可以實現相互跳轉

 

 

 

分享完畢。

1、介紹mybaitis的基礎配置,

2、給出了mapper類的基礎樣例,

3、給出無啟動依賴時報錯解決方法,

4、通過pgadmin查看數據庫賬戶,給出修改密碼的方法

5、跳轉插件mybaitisX推薦

分享到:
標簽:數據庫
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定