zoukankan      html  css  js  c++  java
  • sping+maven+mybatis+ehcache续之实现mapper

    配置接着上一篇文章

    新建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"/>  -->

    测试文件同上。。。。

  • 相关阅读:
    C语言资料分享
    vue子组件添加事件无效解决
    vue-cli3 vue动画 打包后不生效解决
    setTimeOut定时器实现数组内容循环获取
    echart 之仪表盘 动态分段颜色实现
    elementUI table树默认箭头修改
    《二》打包发布工程-README.md编辑
    《一》打包发布工程--npm 打包发布js库篇
    npm打包发布js库包npm ERR! 403 Forbidden
    安装nrm 后执行报错TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/4581053.html
Copyright © 2011-2022 走看看