spring cloud 整合配置mybatis


做新項目時總結的 , 親測可用
1 , 在pom文件添加依賴 :
  <dependency>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-core</artifactId>
  <version>1.3.2</version>
</dependency>
<!-- mybatis -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 使用數據源 -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
<version>1.0.14</version>
</dependency>
<!-- mysql -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.34</version>
  <scope>runtime</scope>
</dependency>
<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.3.2</version>
</dependency>
<!-- mybatis 分頁插件 -->
<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>4.1.6</version>
</dependency>
<!-- mybatis-generator自動生成代碼 -->
<dependency>
  <groupId>org.mybatis.generator</groupId>
  <artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
2 , 在application.yml添加配置 :
spring:
  datasource:
    url: jdbc:mysql://192.168.00.00:3306/xxxx?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
   password: xxxxxx
   driver-class-name: com.mysql.jdbc.Driver
   druid:
      initialSize: 5 # 初始化大小
      minIdle: 5 # 最小
      maxActive: 20 # 最大
      maxWait: 60000 # 獲取連接等待超時的時間
      timeBetweenEvictionRunsMillis: 60000 # 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒
      minEvictableIdleTimeMillis: 300000 # 配置一個連接在池中最小生存的時間,單位是毫秒
      validationQuery: SELECT 1 FROM DUAL
     testWhileIdle: true
     testOnBorrow: false
     testOnReturn: false
     poolPreparedStatements: true # 打開PSCache
     maxPoolPreparedStatementPerConnectionSize: 20 # 指定每個連接上PSCache的大小
     filters: stat,wall,log4j # 配置監控統計攔截的filters,去掉后監控界面sql無法統計,'wall'用於防火牆
     connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
#mybatis config
mybatis:
  typeAliasesPackage: xxx.xxx.xxxx.domain #實體類包路徑
  mapperLocations: classpath:mapper/*.xml #mapper.xml路徑
3 , 在啟動類添加注解 :
@MapperScan("xx.xx.xx.dao")//mapper接口包路徑
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableAutoConfiguration//解決報錯 : Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required 不要@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})自動注入
 
最后創建對應的類和xml就可以了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM