springBoot+mysql+mybatis的基本配置:
多环境
application.properties
spring.profiles.active=dev spring.application.name=resource-make server.port=8095 server.context-path=/resource-make
指定了工程环境为开发环境、指定应用名字、端口号、上下文路径。
application-dev.properties
#XX文件服务器配置 upload.host = test.ruoob.com.cn upload.appKey = upload-file-test upload.appSecret = 8923hjf9f99f9 #XYXY平台服务器上面部署ng地址 xyxy.file.server=http://172.31.12.137:81
常规配置。
application-dev.yml
# DataSource Config spring: datasource: type: org.apache.commons.dbcp2.BasicDataSource driver-class-name: com.mysql.jdbc.Driver #com.mysql.cj.jdbc.Driver #schema: classpath:db/schema-h2.sql #data: classpath:db/data-h2.sql url: jdbc:mysql://127.0.0.1:8080/test_10?useUnicode=true&characterEncoding=UTF-8&useSSL=false&useUnicode=true&serverTimezone=Asia/Shanghai username: root password: 123123 mybatis: mapper-locations: classpath:mapper/*.xml
数据库配置:必须配置在xml,否则会有注解冲突问题。
mapper配置:否则无法引入xml。
=======================================================================================
mybatis报错:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
mybatis配置时出现org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
同时需要指定xml的位置的配置:
1、mybatis的mapper配置:
mybatis-plus: mapper-locations: classpath:mapper/*.xml
2、mybatis-plus的mapper配置:
mybatis: mapper-locations: classpath:mapper/*.xml
注意不要混淆
经验总结:故意写错mybatis的.xml文件,如果项目仍然能够正常启动,说明xml的配置错误,xml没有纳入启动项!
=======================================================================================
mybatis报错:
Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required
SpringBoot+MyBatis整合中的坑以及Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required错误详解
主要是这个包屏蔽了'sqlSessionFactory' 、 'sqlSessionTemplate'这两个,所以最好的方法是用yml来配置
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency>
其他的一些重要的依赖:
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-extension</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> </dependency> </dependencies>
经验总结:springBoot+mybatis框架,尽量把数据库配置放到.yml文件,其它配置可以放到.application文件
=======================================================================================
mybatis报错
autowired找不到对应的bean
报错信息:
Description: Field itemMapper in com.iflytek.qqhy.resourcemake.service.ResourceMakeService required a bean of type 'com.iflytek.qqhy.resourcemake.mapper.ItemMapper' that could not be found. Action: Consider defining a bean of type 'com.iflytek.qqhy.resourcemake.mapper.ItemMapper' in your configuration.
解决方法:
@MapperScan(value = {"com.iflytek.qqhy.resourcemake.mapper","com.iflytek.qqhy.resourcemake.dao","com.iflytek.qqhy.resourcemake.datasource"})
解决方法:XML 文件里只配置了Controller的bean 如果你没有启动项目就因为没有配置service和dao的bean 所以会报错 如果启动了项目 那配置扫描需要修改 要可以扫描到service和dao才可以
=======================================================================================