整合mybatis

1.  导包:在原有的web项目的基础上加上

<!--JDBC连接-->
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
      </dependency>
      <!--mybatis依赖-->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>fastjson</artifactId>
          <version>1.2.46</version>
      </dependency>
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.0</version>
      </dependency>
      <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>1.3.0</version>
      </dependency>
  </dependencies>

2.  其他步骤与普通的ssm步骤类似

spring boot debug mybatis日志 springboot mybatis报错_xml文件

这是dao层的文件的格式。

 

 

一.在这次配置中遇到的问题:

spring boot debug mybatis日志 springboot mybatis报错_xml文件_02

 

报错信息:Loadingclass `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

 

该异常是因为连接驱动的问题,可能com.mysql.jdbc.Driver已经过时,在application配置文件中将连接换位com.mysql.cj.jdbc.Driver即可

spring boot debug mybatis日志 springboot mybatis报错_bc_03

 


 


dao层的接口文件的配置:

spring boot debug mybatis日志 springboot mybatis报错_mysql_04

1.      以前一直以为接口文件是不需要加上Repository的,但是在这次配置中如果接口文件不加上托管注解,在service层直接使用自动注入就会报bean找不到

 


3.

spring boot debug mybatis日志 springboot mybatis报错_bc_05

1.      启动类的位置,一定要放在其他类的上一层,不然就需要加上注解来告诉他扫描的位置,不然是扫描不到Controller类等等的。MapperScan注解是因为下一个问题。

 

 

4.

终极问题:Invalid bound statement (not found)

 

1.       给我的提示是Invalid bound statement (notfound): com.zy100.dao.HouseMapper.findHouseInfo,

 

他的意思是找不到我的findHouseInfo方法,这种错误的原因百度如下:

 

1.             Namespace的值写错了,一定要是包名加上mapper文件名并且不带后缀

 

2.             接口文件的名字一定要和xml文件的名字相同,不然也可能会报错

 

3.             标签的id,这个问题一般不会犯错,因为都是复制的,我重新复制了好几遍,重启之后还是同样的报错,然后我就放弃了,百度上说的大致都是上面这三条

然后我开始怀疑会不会因为mapper文件并没有被扫描到,然后通过百度找到了一个扫描mapper文件的方法,也就是第三步中说过的@MapperScan注解

spring boot debug mybatis日志 springboot mybatis报错_mysql_06

 


这个注解可以设置扫描的路径,放在主方法上面,如果在resources下的话只需要将路径改为classpath:com.zy100.dao即可,原以为一切皆大欢喜,谁知道运行后这个让人头大的问题又出来了!就算是我将路径改成了classpath:com.zy100.dao,并且在resources文件夹下创建与dao层接口文件相同的包名,将xml文件复制下来也不行。


 

4.百度上说xml文件必须放在resources下,但是我目前实现的是放在java文件夹下,因为我想到了idea不会扫描java文件夹下面的xml文件夹,但是可以在pom文件中进行配置让她扫描编译xml文件

 

 

<!--不添加的话java文件夹下Mapper文件不被扫描-->

    <resource>

        <directory>src/main/java</directory>

        <includes>

            <include>**/*.properties</include>

            <include>**/*.xml</include>

        </includes>

        <filtering>false</filtering>

    </resource>
</resources>

 

在加上这个之后,终于换了一个报错,其实在这样做之后就差不多成功了,我最后的包结构就是第一张图,xml文件和接口文件位于同一包下,主方法直接放在了与他们包同级的文件夹下,所以主方法上面那个SpringBootApplication注解中的内容可以加也可以不加,如果是在同一文件夹下或者是在上级包的上级的位置就需要加上。

 

1.       最后两个简单报错, java.sql.SQLException: The server time zone value 'й���׼ʱ��' is unrecognized or represents more than one time zone”

这个是因为编码格式的问题

spring boot debug mybatis日志 springboot mybatis报错_xml文件_07

 


在application.properties中连接数据库的时候拼接

?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
1.      粗心的报了一错误,大家估计不会遇到

 

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

这个错误是

spring boot debug mybatis日志 springboot mybatis报错_bc_08

 


这个是因为当时账号密码都是填写的root,后来想起来是123456

 

 

 

 

 

1.      

报错信息:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

该异常是因为连接驱动的问题,可能com.mysql.jdbc.Driver已经过时,在application配