SpringBoot項目的pom雜記

1、加載SpringBoot

  SpringBoot可以在pom文件中使用繼承parent的方式,這種方式簡便易用。

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.1.RELEASE</version>
		<relativePath/>
</parent>

  但是這種方式parent就只能有一個,如果項目需要引進其他的parent,就不能用這種方式。可以參考以下的方式。

<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>2.1.1.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
</dependencyManagement>

  departmentManagment,表示申明,並不會把jar包包進來。需要用到具體的依賴纔會加載進來。

2、SpringBoot 數據庫連接池

  SpringFrameWork JDBC 默認使用的是HikariCP連接池,這個是默認的。

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

  如果想換成其他的鏈接池可以用一下的方式,如使用alibaba的druid

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>HikariCP</artifactId>
            <groupId>com.zaxxer</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

  但這隻限於使用spring-boot-starter-jdbc,如果只是使用spring-jdbc那就不需要。spring-jdbc內部自動化配置連接池,會使用到druid。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.1.3.RELEASE</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

3、自定義SQL的錯誤碼

  各個廠商SQL出錯碼是各不相同的,Spring整合了各個廠商的錯誤碼,定義在sql-error-codes.xml文件中。根據spring-jdbc的SQLErrorCodesFactory中的定義。先導入Spring集成的錯誤碼xml文件(org/springframework/jdbc/support/sql-error-codes.xml),然後再導入項目定義在 the root of the class path 的 sql-error-codes.xml。自定義的codes會覆蓋Spring提供的codes中bean id相同的配置。
  如下是自定義sql-error-codes.xml的文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <bean id="H2" class="org.springframework.jdbc.support.SQLErrorCodes">
        <property name="badSqlGrammarCodes">
            <value>42000,42001,42101,42102,42111,42112,42121,42122,42132</value>
        </property>
        <property name="duplicateKeyCodes">
            <value>23001,23505</value>
        </property>
        <property name="dataIntegrityViolationCodes">
            <value>22001,22003,22012,22018,22025,23000,23002,23003,23502,23503,23506,23507,23513</value>
        </property>
        <property name="dataAccessResourceFailureCodes">
            <value>90046,90100,90117,90121,90126</value>
        </property>
        <property name="cannotAcquireLockCodes">
            <value>50200</value>
        </property>
        <property name="customTranslations">
            <bean class="org.springframework.jdbc.support.CustomSQLErrorCodesTranslation">
                <property name="errorCodes" value="23001,23505" />
                <property name="exceptionClass"
                          value="com.demo.errorcodedemo.CustomDuplicatedKeyException" />
            </bean>
        </property>
    </bean>
</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章