前言
涉及到的环境配置文件问题较多
记录一下我在学习springboot的mybatis的xml模式下做demo时遇到的问题,以及对应的解决方案,以此为戒。
application.properties配置如下
mybatis.config-location=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
spring.datasource.url=jdbc:mysql://localhost:3306/study?
serverTimezone=UTC&useUnicode=true
&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
一、bug汇总
Question1
Question2
Question3:数据库字段有值,但是查询部分没有值
二、解决方法
问题1
出现这类错误的原因一般是由于资源文件位置不对所引起的,例如此时我的mybatis.xml等mapper下的系列xml文件并不在resource文件目录下(左图),因此将他们放到资源文件就可以了(右图)。
问题2
这种问题肯定是配置文件的问题,但是不知道具体哪里的问题,此时我的mybatis.xml文件内容如下图1所示,我发现内容并不完整,于是加上了一些少的配置后成功了。
图1:
图2:
问题3
数据库中字段并非空值
这肯定是查询语句那里出毛病了,因为其他地方都是正确的,查询语句如下:
<select id="getAllStudents" resultType="Student">
select *from student
</select>
但是语句并没有错啊,而且并不是所有字段都为空,后来锁定到了数据库表的字段和我实体类的字段并不完全相同,于是加上as
<select id="getAllStudents" resultType="Student">
select id,student_name as studentName,student_sex as studentSex,grade,major
from student
</select>
查询后发现已经可以找到了,OK
总结
多试错,多总结,这些环境和语法类的问题都是能够解决的。