一开始,本想着直接从之前的springmvc项目中把mybatisplus的maven依赖直接扒过来,扔进pom文件,在yml文件上配置一下mapper相关xml的路径,然后在application中加上mapperScanner的注解和相关的注解(@Service,@Mapper),就直接去跑了。如果只是mybatis的话,这样是完全可以运行的。但这是mybatis-plus…
一开始我在没有使用到mybatis-plus的封装方法的时候,仅以mybatis的方式是可以正常访问自己写在xml的相关方法的,但当我要使用mybatis-plus封装好的insert方法的时候,问题就出现了:
invalid-bound-statement-not-found ,这个方法未绑定;
为什么会出现这个问题呢?一开始细查了xml的配置路径,相关注解,发现都没错。这时候开始怀疑配置文件出了问题了。
上网百度了一下,发现大部分人的配置是这样写的
mybatis-plus:
mapper-locations:classpath:mapper/**/*Mapper.xml
而我是这样写的
mybatis:
mapper-locations:classpath:mapper/**/*.xml
于是我便按着网友们的方式去更改,结果不用问,当然是不行的=.=
这时候出现了更糟糕的情况:我连在xml内写好的方法也无法访问了,无奈之下只能改回原来的样子,起码我自己写的能访问
既然都怀疑依赖有问题了,那就去mybatisplus的官网找找样例是怎么样的吧。
去了之后,心情很复杂,嗯。他们写的我都copy过来了,但是这次报错了,报的是这个错误
Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required
这个是sqlSessionFactory未注入。。。。他们的依赖是这样的
<dependencies>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>最新版本号version>
dependency>
dependencies>
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<filtering>falsefiltering>
<includes>
<include>**/mapper/*.xmlinclude>
includes>
resource>
resources>
build>
由于项目原因,我不能使用jdk8,所以只能使用MP2.x版本,MP3.x版本jdk需要8或以上才能运行。在苦于怎么解决的时候,我尝试性的把mybatis的依赖重新加入了pom文件,然后。。。。神奇的不报Property ‘sqlSessionFactory’ or ‘sqlSessionTemplate’ are required的错误了。。。。。但是依然是访问不了MP封装好的方法。。。。
因为springboot版本中使用的是mybatis-plus-boot-starter作为依赖的方式,而在mybatis-plus-boot-starter中,据说是包含了jdbc、mybatis、mybatis-plus的封装,那我换个方式,一个个分散来加入进去也行吧,反正我已经有mybaytis依赖了。说不定就是冲突了呢?
于是,我把依赖改成了
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plusartifactId>
<version>2.1.8version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-autoconfigureartifactId>
<version>1.3.2version>
dependency>
然后出现了另一个没有见过的报错,仔细一想,应该是这样方式还需要自己去设置MP的相关配置项。于是便新建了一个配置类:
@Configuration
public class MybatisPlusConfig {
private final DataSource dataSource;
private final MybatisProperties properties;
private ResourceLoader resourceLoader = new DefaultResourceLoader();
@Autowired(required = false)
private Interceptor[] interceptors;
@Autowired(required = false)
private DatabaseIdProvider databaseIdProvider;
@Autowired
public MybatisPlusConfig(ResourceLoader resourceLoader, MybatisProperties properties, @Qualifier("dataSource") DataSource dataSource) {
this.resourceLoader = resourceLoader;
this.properties = properties;
this.dataSource = dataSource;
}
@Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean() {
MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
mybatisPlus.setDataSource(dataSource);
mybatisPlus.setVfs(SpringBootVFS.class);
if (StringUtils.hasText(this.properties.getConfigLocation())) {
mybatisPlus.setConfigLocation(this.resourceLoader.getResource(this.properties.getConfigLocation()));
}
mybatisPlus.setConfiguration(properties.getConfiguration());
if (!ObjectUtils.isEmpty(this.interceptors)) {
mybatisPlus.setPlugins(this.interceptors);
}
MybatisConfiguration mc = new MybatisConfiguration();
mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
mybatisPlus.setConfiguration(mc);
if (this.databaseIdProvider != null) {
mybatisPlus.setDatabaseIdProvider(this.databaseIdProvider);
}
if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
mybatisPlus.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
}
if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
mybatisPlus.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
}
if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
mybatisPlus.setMapperLocations(this.properties.resolveMapperLocations());
}
return mybatisPlus;
}
}
请注意不要导错包了
配置完成了。OK,点击RUN,运行成功。Yes! 调用MP封装好的方法,OK。收工
注:如果@Autowired注入失败,那是因为你没有配置扫描,需要在application上添加注解
@ComponentScan("com.*")