spring book t 2.11.mybatis&Druid接続池を整合する.
12943 ワード
環境
idea 2019 jdk 1.8 maven 3.6 spring book t t 2.1.1(現在のspringbootの最高バージョンは2.1.8の場合、2.0以上のテストがあり、いくつかのバージョンが正常に動作します)プロジェクトタイプ:jar
注意:spring bootは自動的に配置される前提は>>すべてのコンポーネントがコア起動クラスのあるカバンとそのサブバッグの下にあるので、本編のすべてのjava類はコアクラスのあるカバンとそのサブパッケージの下で作成されます.
インポートの依存
1、耐久層インターフェースに@Mapperインターフェースを追加します.
2,構成類に@MapperScan(「comp.wn.demo.sprigboot.mapper」)の注釈を追加して、自動スキャンをオンにしてもいいです.パラメータはパッケージ名です.
XML開発:
コアプロファイルに対応するプロファイルを追加します.
コア設定ファイルで開く
XML方式で設定すると@Mapperと@MapperScanの注釈が無効になります.mybatis設定ファイルでスキャンするカバンを指定する必要があります.
idea 2019 jdk 1.8 maven 3.6 spring book t t 2.1.1(現在のspringbootの最高バージョンは2.1.8の場合、2.0以上のテストがあり、いくつかのバージョンが正常に動作します)プロジェクトタイプ:jar
注意:spring bootは自動的に配置される前提は>>すべてのコンポーネントがコア起動クラスのあるカバンとそのサブバッグの下にあるので、本編のすべてのjava類はコアクラスのあるカバンとそのサブパッケージの下で作成されます.
インポートの依存
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.17version>
dependency>
コアプロファイルにDruidデータソースを配置するspring:
datasource:
driver-class-name: com.mysql.jdbc.Driver #
url: jdbc:mysql://localhost:3306/test # url
username: root #
password: 159357 #
type: com.alibaba.druid.pool.DruidDataSource # , Druid
注釈開発:1、耐久層インターフェースに@Mapperインターフェースを追加します.
2,構成類に@MapperScan(「comp.wn.demo.sprigboot.mapper」)の注釈を追加して、自動スキャンをオンにしてもいいです.パラメータはパッケージ名です.
XML開発:
コアプロファイルに対応するプロファイルを追加します.
# mybatis
mybatis:
# mybatis
config-location: classpath:mybatis/mybatis-config.xml
# mybatis mapper
mapper-locations: classpath:mybatis/mapper/*.xml
ラクダのピークの命名法を設定する2つの方法:コア設定ファイルで開く
mybatis:
configuration:
map-underscore-to-camel-case: true
カスタムConfigrationCustoomizer構成により、次の種類を追加します.package com.wn.demo.springboot.config;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.wn.demo.springboot.mapper")
public class MyBatisConfig {
/**
* mybatis , ConfigurationCustomizer
* @return
*/
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return new ConfigurationCustomizer() {
@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
//
configuration.setMapUnderscoreToCamelCase(true);
}
};
}
}
注意XML方式で設定すると@Mapperと@MapperScanの注釈が無効になります.mybatis設定ファイルでスキャンするカバンを指定する必要があります.