MyBatis integration of Spring's mapperlocations configuration issues

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.