1、UserMapper.java

package com.cn.mapper;

import java.util.List;

import com.cn.pojo.User;

public interface UserMapper {
List<User> getUserOrderMap();
}

2、UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.cn.mapper.UserMapper">
<resultMap type="user" id="user_order_map">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="address" column="address"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<!-- collection用于配置一对多关联 -->
<collection property="orderList" ofType="order">
<id property="id" column="oid"/>
<result property="userId" column="id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
</collection>
</resultMap>
<select id="getUserOrderMap" resultMap="user_order_map">
select
u.id,
u.username,
u.birthday,
u.sex,
u.address,
u.uuid2,
o.id oid,
o.number,
o.createtime,
o.note
from
user u
left join
order1 o
on o.user_id = u.id </select>
</mapper>

3、applicationContext.xml

两种配置方式

单独配

<!-- 动态代理 配置方法1-->
<!-- <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> -->
<!-- 配置一个接口 -->
<!-- <bean id="oneMapper" parent="baseMapper">
<property name="mapperInterface" value="com.cn.mapper.UserMapper" />
</bean> -->

包扫描

       <!--  包扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.mapper"></property>
</bean>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 加载配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 连接池的最大数据库连接数 -->
<property name="maxActive" value="10" />
<!-- 最大空闲数 -->
<property name="maxIdle" value="5" />
</bean>
<!-- SqlSessionFactoryBean 配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载 mybatis核心文件sqlMapperConfig配置文件-->
<property name="configLocation" value="classpath:sqlMapperConfig.xml" />
<!-- 别名包扫描-->
<property name="typeAliasesPackage" value="com.cn.pojo"/>
</bean>
<!-- 传统dao开发 -->
<bean class="com.cn.dao.impl.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 动态代理 配置方法1-->
<!-- <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean> -->
<!-- 配置一个接口 -->
<!-- <bean id="oneMapper" parent="baseMapper">
<property name="mapperInterface" value="com.cn.mapper.UserMapper" />
</bean> -->
<!-- 包扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.mapper"></property>
</bean>
</beans>

测试

package com.cn.mapper;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cn.dao.UserDao;
import com.cn.pojo.User; public class UserMapperTest { private ApplicationContext applicationContext; @Before
public void init() {
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
@Test
public void test() {
System.out.println(1);
UserMapper mapper = applicationContext.getBean(UserMapper.class);
List<User> userOrderMap = mapper.getUserOrderMap();
System.out.println(userOrderMap);
}
}

Mybatis-spring 动态代理的更多相关文章

  1. MyBatis学习(三)MyBatis基于动态代理方式的增删改查

    1.前言 上一期讲到MyBatis-Statement版本的增删改查.可以发现.这种代码写下来冗余的地方特别多.写一套没啥.如果涉及到多表多查询的时候就容易出现问题.故.官方推荐了一种方法.即MyBa ...

  2. (十二)mybatis之动态代理

    mybatis之动态代理的应用 在前文(https://www.cnblogs.com/NYfor2018/p/9093472.html)我们知道了,Mybatis的使用需要用到Mapper映射文件, ...

  3. Mybatis mapper动态代理的原理详解

    在开始动态代理的原理讲解以前,我们先看一下集成mybatis以后dao层不使用动态代理以及使用动态代理的两种实现方式,通过对比我们自己实现dao层接口以及mybatis动态代理可以更加直观的展现出my ...

  4. JAVA框架 Spring 和Mybatis整合(动态代理)

    一.使用传统方式的dao的书写方式,不建议.目前采用的是动态代理的方式交给mybatis进行处理. 首先回顾下动态代理要求: 1)子配置文件的中,namespace需要是接口的全路径,id是接口的方法 ...

  5. spring如何管理mybatis(一) ----- 动态代理接口

    问题来源 最近在集成spring和mybatis时遇到了很多问题,从网上查了也解决了,但是就是心里有点别扭,想看看到底怎么回事,所以跟了下源码,终于发现了其中的奥妙. 问题分析 首先我们来看看基本的配 ...

  6. Spring 整合Mybatis Mapper动态代理方法

    先看项目目录结构 很清爽了 最重要的Spring的核心配置文件,看一下 <?xml version="1.0" encoding="UTF-8"?> ...

  7. 【转载】由浅入深分析mybatis通过动态代理实现拦截器(插件)的原理

    转自:http://zhangbo-peipei-163-com.iteye.com/blog/2033832?utm_source=tuicool&utm_medium=referral 我 ...

  8. spring 动态代理

    突然想到AOP,就简单回忆一下动态代理.1.什么是动态代理? 假如有个用户有增删该查4个方法,如果要对用户操作后进行日志记录,可能会有人说直接在增删改查后做日志记录就行. 一旦我想在用户操作之前加一个 ...

  9. Mybatis使用动态代理实现拦截器功能

    1.背景介绍 拦截器顾名思义为拦截某个功能的一个武器,在众多框架中均有“拦截器”.这个Plugin有什么用呢?或者说拦截器有什么用呢?可以想想拦截器是怎么实现的.Plugin用到了Java中很重要的一 ...

  10. Java的三种代理模式(Spring动态代理对象)

    Java的三种代理模式 1.代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩 ...

随机推荐

  1. 2.5 定义FTP工具的各种方法

    用class定义ftp工具的各种方法 import os,sys from ftplib import FTP from mimetypes import guess_type,add_type fr ...

  2. re模块+面向对象

    re模块 re:其实就是带有特殊语法的字符串 语法:单个字符和多个字符 单个字符: \d是匹配所有的数字 \D是匹配所有的非数字 \s是所有的换行符,制表符,空白等,回车符 \S是所有费换行符,空白和 ...

  3. Linux修改日期、时间,系统与硬件时间

    Linux的时间分为两种,硬件时间和系统时间两种: 一.查看与修改系统时间 查看系统时间:date # date Fri Nov 26 15:20:18 CST 1999 用指定的格式显示系统时间:  ...

  4. Django安装遇到的问题

    因为mac一般自带的有python解释器,如果重新安装了新的解释器,并且想默认使用的话,需要配置一下环境变量. 在使用python解释器的时候,可以用命令:which python 来确定当前使用的时 ...

  5. [转] C++ 和 python之间的互相调用

    转载自:https://www.cnblogs.com/apexchu/p/5015961.html 一.Python调用C/C++ 1.Python调用C动态链接库 Python调用C库比较简单,不 ...

  6. PyCharm:ModuleNotFoundError: No module named 'selenium'

    Mac安装PyCharm后,将已有工程导入,之前使用Mac终端执行脚本时正常,现在报错ModuleNotFoundError: No module named 'selenium',解决方法是在PyC ...

  7. mysql与mysqli的区别

    博客搬家了,欢迎大家关注,https://bobjin.com mysqli连接是永久连接,而MySQL是非永久连接. mysql连接:每当第二次使用的时候,都会重新打开一个新的进程. mysqli连 ...

  8. Go Example--自定义排序

    package main import ( "fmt" "sort" ) //定义类型别名 type ByLength []string func (s ByL ...

  9. 列表:remove/del删除方法中的逻辑“误区”

    结果: list_1=["A","B","C","D","E","F",&quo ...

  10. Nginx调试入门

    1.查看nginx.conf配置文件是否有错误 ./nginx -t -c ./nginx.conf   #可以看到,正常情况下语法没问题,配置文件测试成功了,-t测试-c配置文件 如果我故意加入错误 ...