Spring+MyBatis uses dynamic proxy to automatically generate DAO interface implementation class

Using the mybatis dynamic proxy method can automatically generate the dao interface implementation class, without having to implement the dao interface class by yourself, simplifying programming
1. Prerequisite:
(1) Ensure that the namespace of the mapper.xml file and the class path of the dao interface are always
(2) mapper.xml The id
configured
in the file is consistent with the name of the method defined by the dao interface. Configure the MapperScannerConfigurer in the applicationContext.xml file.

 <!-- 配置sessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:sqlMapConfig.xml"/><!--org.mybatis.spring.SqlSessionFactoryBean-->
        <property name="dataSource" ref="dataSource"/>
        <!--如果dao接口和.xml文件不在同一目录-->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
 <!--mybatis动态代理,实现Dao接口-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 注意:从上下文找到sqlSessionFactory对象 -->
        <property name="basePackage" value="org.lieying.dao"/>
    </bean>

Project directory screenshot
Insert picture description here

Guess you like

Origin blog.csdn.net/rj2017211811/article/details/108347740