Mapper代理模式开发Dao

1.配置Mapper

a.单个接口配置MapperFactoryBean:

<!-- 动态代理Dao开发,第一种方式 -MapperFactoryBean -->

  <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">

     <property name="sqlSessionFactory" ref="sqlSessionFactory" />

    </bean>

    <!-- 用户动态代理扫描 -->

    <bean parent="baseMapper">

      <property name="mapperInterface" value="com.itheima.mybatis.mapper.UserMapper" />

   </bean>

b.配置包扫描器(推荐使用):

<!-- 动态代理Dao开发,第一种方式,包扫描器(推荐使用) -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

         <!-- basePackage:配置映射包装扫描,多个包时用","";"分隔 ,有了该包扫描器就可以加载出该包下所有的mapper-->

<property name="basePackage" value="com.itheima.mybatis.mapper" />

   </bean>

2.测试

public class UserMapperTest {

   

    private ApplicationContext applicationContext;

   

    @Before

    public void init(){

       applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

    }

 

    @Test

    public void testGetUserById() {

       UserMapper userMapper = applicationContext.getBean(UserMapper.class);

       User user = userMapper.getUserById(10);

       System.out.println(user);

 

    }

 

猜你喜欢

转载自blog.csdn.net/qq_37706228/article/details/81335900