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就可以了

猜你喜欢

转载自www.cnblogs.com/lix-iao/p/11738385.html
今日推荐