转载自https://blog.csdn.net/hanpenghu/article/details/83897618
springboot maven资源路径配置 resource路径配置, 解决mybatis的xml放在java路径而没有放在resource路径下报错的问题
我是这样配置的
-
<build>
-
<!--resources配置解决mybatis 的mapperXml配置在java路径不被扫描的问题 -->
-
<resources>
-
<resource>
-
<directory>src/main/java</directory>
-
</resource>
-
<resource>
-
<directory>src/main/resources</directory>
-
</resource>
-
</resources>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
</plugin>
-
<!--跳过测试 -->
-
<plugin>
-
<groupId>org.apache.maven.plugins</groupId>
-
<artifactId>maven-surefire-plugin</artifactId>
-
<configuration>
-
<skipTests>true</skipTests>
-
</configuration>
-
</plugin>
-
</plugins>
-
</build>
-
-
-
</project>
然后在资源文件 application.properites里面配置mybatis的xml路径
-
server.port=8081
-
-
#必须有
-
mybatis.mapper-locations=classpath*:com/hanhan/dao/xmlMapper/*.xml
-
#必须有
-
logging.config=classpath:logback.xml
-
#必须有
-
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-
#必须有
-
spring.datasource.url=jdbc:mysql://127.0.0.1:3307/ipace?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT
-
#必须有
-
spring.datasource.username=root
-
#必须用
-
spring.datasource.password=root
-
-
-
#32位随机字符串
-
#dd9008e5ec8608c9eb9d57ab764651eb
-
rand.str = ${random.value}
-
#36位,比32位的多了4个-
-
#0cf88531-9569-44c4-926e-2528dd5948e2
-
rand.uuid = ${random.uuid}
-
#随机int类型,主意有负数
-
rand.intid = ${random.int}
-
#随机long类型
-
rand.longid = ${random.long}
-
#100以内的随机int类型
-
rand.number = ${random.int(100)}
-
#0-10亿范围内的随机int类型
-
rand.range = ${random.int[0,1000000000]}
-
java的接口mapper路径扫描在启动类上配置
-
import org.mybatis.spring.annotation.MapperScan;
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.scheduling.annotation.EnableScheduling;
-
-
@SpringBootApplication(scanBasePackages = {"com.hanhan","hanhan"})
-
@MapperScan({"com.hanhan.dao"})
-
@EnableScheduling
-
public class BeetltestApplication {
-
-
-
public static void main(String[] args) {
-
SpringApplication.run(BeetltestApplication.class, args);
-
}
-
贴完别人的 说下我的问题,
首先是引入springboot集成的mybatis的jar包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
其次是applicatioin的配置文件配置数据库的连接等
spring:
datasource:
name: feibi
url: jdbc:mysql://****/i***
username: ***
password: ***
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20
applicatioin里还要配置mybatis的扫描xml文件位置
mybatis:
mapper-locations: classpath:mybatis/*.xml
config-location: classpath:conf/mybatis-config.xml
check-config-location: true
最后是启动类需要加入mapperscan
@MapperScan("com.*.*.dao")