其次在application.properties文件中配置mybatis的mapper文件位置,和實體類的包路徑,還有最好加上駝峰命名規范
####mybatis配置 # 注意注意 mybatis.mapper-locations=classpath:edu/hohai/chapter1/gt/mapper/*.xml #mybatis.mapper-locations=classpath:mapper/*.xml #這種方式需要自己在resources目錄下創建mapper目錄然后存放xml mybatis.type-aliases-package=edu.hohai.chapter1.gt.entity # 駝峰命名規范 如:數據庫字段是 order_id 那么 實體字段就要寫成 orderId mybatis.configuration.map-underscore-to-camel-case=true
看下項目結構:這里我把Mapper類和Mapper映射文件放在了一起,這時候我們還需要做下面設置,讓springboot能在java包中掃描xml文件(springboot默認掃描的是包中的*.java文件),如果使用resources目錄下面的xml,這打開上面的
mybatis.mapper-locations=classpath:mapper/*.xml
設置springboot在包中掃描xml文件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> <!--下面主要是設置在java包里面也讓springboot掃描xml類型的配置文件--> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>