码迷,mamicode.com
首页 > 移动开发 > 详细

sping+maven+mybatis+ehcache续之实现mapper

时间:2015-06-16 18:39:37      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

配置接着上一篇文章

新建UserMapper.java和UserMapper.xml

其中UserMapper.xml的namespace以及文件名要和UserMapper.java一致

<mapper namespace="com.alibaba.mapper.UserMapper">

技术分享

spring增加配置

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
        <property name="mapperInterface" value="com.alibaba.mapper.UserMapper"/> 
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/> 
</bean>

测试类

package com.alibaba.dao;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.alibaba.mapper.UserMapper;
import com.alibaba.po.User;

public class UserMapperTest {
    
    private ApplicationContext applicationContext;

    @Before
    public void setup() throws Exception{
        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
    }
    
    @Test
    public void test() throws Exception{
        UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
        User user = userMapper.getUser(1);
        System.out.println(user);
    }

}

 



配置方式2

启用spring扫描器,将原先的UserMapper Bean注释掉

<!-- mapper配置 MapperFactoryBean:根据mapper接口生成代理对象 -->
    <!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
        <property name="mapperInterface" value="com.alibaba.mapper.UserMapper"/> 
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/> 
    </bean> -->
        
    <!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册 遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录 
        中 自动扫描出来的mapper的bean的id为mapper类名(首字母小写) -->
    <!-- 指定扫描的包名 如果扫描多个包,每个包中间使用半角逗号分隔 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
        <property name="basePackage" value="com.alibaba.mapper"/> 
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 
    </bean>

将sqlMapConfig.xml中的扫描包注释掉

<!-- <package name="com.alibaba.mapper"/>  -->

测试文件同上。。。。

 

sping+maven+mybatis+ehcache续之实现mapper

标签:

原文地址:http://www.cnblogs.com/tuifeideyouran/p/4581053.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!