Today's attempt to integrate MyBatis with spring is the question of whether to configure mapperlocations when configuring Sqlsessionfactory.
<bean id= "Sessionfactory" class= "Org.mybatis.spring.SqlSessionFactoryBean" > <property name= " DataSource "ref=" DataSource "></property> <property name=" Typealiasespackage "value=" Com.fan.entity "/> <!--when the MyBatis XML file and Mapper interface are not under the same package, you need to specify the path to the XML file with the Mapperlocations attribute. * is a wildcard, representing all files, * * represents all directories-- <property name= "mapperlocations" value= "classpath:com/fan/mapper/*. xml "/> <!--can also introduce mybatis configuration files <property name=" configlocation "value=" classpath:mybatis/ Sqlmapconfig.xml "></property>--></bean><!--scan the directory in Com.lanyuan.mapper directory by scanning mode mapper-- ><bean class= "Org.mybatis.spring.mapper.MapperScannerConfigurer" > <property name= "Basepackage" Value= "Com.fan.mapper" ></property></bean>
Conclusion: If Mapper.xml and Mapper.class are under the same package and have the same name, spring scans the Mapper.class and automatically scans mapper.xml with the same name and assembles to Mapper.class.
If Mapper.xml and Mapper.class are not under the same package or a different name, you must use the configuration mapperlocations to specify the location of the mapper.xml.
Spring is determined by identifying the value of <mapper namespace= "Com.fan.mapper.UserDao" > Namespace in Mapper.xml to determine the corresponding mapper.class.
MyBatis integration of Spring's mapperlocations configuration issues