配置接着上一篇文章

新建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的更多相关文章

  1. Java Maven:spring boot + Mybatis连接MySQL,通用mapper的增删改查,映射实现多表查询

    1. MySQL自带库test添加表user.role 角色表role 用户表user 2. 添加依赖,配置属性 相关依赖:百度即可,此处略 application.properties spring ...

  2. (转)springMVC+mybatis+ehcache详细配置

    一. Mybatis+Ehcache配置 为了提高MyBatis的性能,有时候我们需要加入缓存支持,目前用的比较多的缓存莫过于ehcache缓存了,ehcache性能强大,而且位各种应用都提供了解决方 ...

  3. Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建(转)

    这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 如果还没有搭建好环境( ...

  4. Eclipse+Spring+SpringMVC+Maven+Mybatis+MySQL+Tomcat项目搭建

    ---恢复内容开始--- 1. 建表语句及插入数据 CREATE TABLE `book_user` ( user_id INT(11) NOT NULL AUTO_INCREMENT, user_n ...

  5. Eclipse Maven Mybatis的使用

    关于maven的使用网上有太多教程,这里就不再介绍.本篇文章只用来记录 在Eclipse中使用maven创建含有mybatis的程序的配置,及注意事项. 使用Eclipse创建Maven项目 创建后的 ...

  6. eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建

    这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 接下来马上进入项目搭建 ...

  7. Mybatis逆向工程生成po、mapper接口、mapper.xml

    Mybatis逆向工程生成po.mapper接口.mapper.xml 一.新建一个maven工程 请查看我的另一篇博客:<使用idea创建一个maven工程> 二.引入所需依赖 需要my ...

  8. Maven+Mybatis一些简单例子

    一.创建maven工程 把依赖的包写在pom.xml中.保存后,工程会有错,需要在工程上右键选择“Maven-->Update Project” pom.xml内容为 <project x ...

  9. Maven+MyBatis 初试

    工作中一直使用的都是Hibernate,总是听见有人拿Mybatis和Hibernate做比较,今天尝试来看看. 一.用Maven建立web项目 此处参见 http://www.cnblogs.com ...

随机推荐

  1. Android Resource介绍和使用

    1. 相关文件夹介绍 文件 取值方式 string.xml getResource().getString(resourceId)或者getResource().getText(resourceId) ...

  2. B树、B-树、B+树、B*树都是什么(转)

    B树        即二叉搜索树:        1.所有非叶子结点至多拥有两个儿子(Left和Right):        2.所有结点存储一个关键字:        3.非叶子结点的左指针指向小于 ...

  3. curl返回值写入内存的场景

    直接上代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <cur ...

  4. JavaScript之arguments.callee

    arguments.callee 在哪一个函数中运行,它就代表哪个函数. 一般用在匿名函数中. 在匿名函数中有时会需要自己调用自己,但是由于是匿名函数,没有名子,无名可调. 这时就可以用argumen ...

  5. SPFile上传文件到文档库

    , dataLen);            SPSite sps = SPControl.GetContextSite(Context);            sps.AllowUnsafeUpd ...

  6. Mvc--Html.ActionLink()用法

    },new{ target="_blank"})会生成 <a href="Products/Detail/1" target="_blank&q ...

  7. SGU 187.Twist and whirl - want to cheat( splay )

    维护一个支持翻转次数M的长度N的序列..最后输出序列.1<=N<=130000, 1<=M<=2000 splay裸题... ------------------------- ...

  8. javascript块级作用域

    在c/java中,拥有块级作用域的概念,大括号内就是一个块级作用域,在块级作用域内声明的变量,块以外不可见. C语音的块级作用域示例如下: ,two = ; if(one < two){ ; t ...

  9. sqlplus命令手册

    show errorshow allshow usersqlplus show和set命令是两条用于维护SQLPlus系统变量的命令 : SQL> show all --查看所有系统变量值 SQ ...

  10. ExtJS 修改load paging时的参数

    ExtJS 的pagingToolbar 在翻页的时候传入的参数是固定的  分别是start 和 limit(其中limit的值就是store.pageSize的值) 如何在每次翻页的时候传入自己的参 ...