table of Contents
Two, a configuration properties
problem
The generator using mybatis automatically becomes reverse engineering code, dao, model, and mapping xml files. The following error has been reported when calling the interface
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.ccb.market.dao.UserMapper.selectByPrimaryKey
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48) ~[mybatis-3.4.5.jar:3.4.5]
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65) ~
analysis
According to the error content Invalid bound statement (not found): com.ccb.market.dao.UserMapper.selectByPrimaryKey. The initial analysis program did not find the corresponding xml mapping file when running to the dao layer. What is the reason for not finding it? Wave search. It is concluded that the xml file is not scanned when the program is running, that is to say, there are three reasons
1. There is no such xml file in the compiled file
2. The file of the dao layer does not match the name of the mapping file xml
3. The path does not match.
Finally, after some attempts to locate the following configuration is missing in the application.protites. This configuration is very important, this configuration is very important, this configuration is very important
#指定mapper.xml的位置,如果mapper.xml和接口类在一个包下就不需要指定了
mybatis.mapper-locations=classpath:com/ccb/market/mapper/*.xml
to sum up
Springboot meets the following two conditions in turn when integrating mybatis;
One or two notes
1. Annotate @SpringBootApplication
//配置要扫描的包目录
@SpringBootApplication(scanBasePackages = "com.ccb.market")
Equivalent to
<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
<context:component-scan base-package="com.ccb.market" />
2. Annotate @MapperScan("com.ccb.market.dao")//Configure the directory to scan the dao layer
//配置要扫描dao层的目录
@MapperScan("com.ccb.market.dao")
Equivalent to
<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="core.common.springmvc.PackagesSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:config/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/ccb/market/mapper/*.xml" />
<property name="typeAliasesPackage" value="com.**.model" />
</bean>
Two, a configuration properties
#指定mapper.xml的位置,如果mapper.xml和接口类在一个包下就不需要指定了
mybatis.mapper-locations=classpath:com/ccb/market/mapper/*.xml