Mybatis-spring 动态代理
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 动态代理的更多相关文章
- MyBatis学习(三)MyBatis基于动态代理方式的增删改查
1.前言 上一期讲到MyBatis-Statement版本的增删改查.可以发现.这种代码写下来冗余的地方特别多.写一套没啥.如果涉及到多表多查询的时候就容易出现问题.故.官方推荐了一种方法.即MyBa ...
- (十二)mybatis之动态代理
mybatis之动态代理的应用 在前文(https://www.cnblogs.com/NYfor2018/p/9093472.html)我们知道了,Mybatis的使用需要用到Mapper映射文件, ...
- Mybatis mapper动态代理的原理详解
在开始动态代理的原理讲解以前,我们先看一下集成mybatis以后dao层不使用动态代理以及使用动态代理的两种实现方式,通过对比我们自己实现dao层接口以及mybatis动态代理可以更加直观的展现出my ...
- JAVA框架 Spring 和Mybatis整合(动态代理)
一.使用传统方式的dao的书写方式,不建议.目前采用的是动态代理的方式交给mybatis进行处理. 首先回顾下动态代理要求: 1)子配置文件的中,namespace需要是接口的全路径,id是接口的方法 ...
- spring如何管理mybatis(一) ----- 动态代理接口
问题来源 最近在集成spring和mybatis时遇到了很多问题,从网上查了也解决了,但是就是心里有点别扭,想看看到底怎么回事,所以跟了下源码,终于发现了其中的奥妙. 问题分析 首先我们来看看基本的配 ...
- Spring 整合Mybatis Mapper动态代理方法
先看项目目录结构 很清爽了 最重要的Spring的核心配置文件,看一下 <?xml version="1.0" encoding="UTF-8"?> ...
- 【转载】由浅入深分析mybatis通过动态代理实现拦截器(插件)的原理
转自:http://zhangbo-peipei-163-com.iteye.com/blog/2033832?utm_source=tuicool&utm_medium=referral 我 ...
- spring 动态代理
突然想到AOP,就简单回忆一下动态代理.1.什么是动态代理? 假如有个用户有增删该查4个方法,如果要对用户操作后进行日志记录,可能会有人说直接在增删改查后做日志记录就行. 一旦我想在用户操作之前加一个 ...
- Mybatis使用动态代理实现拦截器功能
1.背景介绍 拦截器顾名思义为拦截某个功能的一个武器,在众多框架中均有“拦截器”.这个Plugin有什么用呢?或者说拦截器有什么用呢?可以想想拦截器是怎么实现的.Plugin用到了Java中很重要的一 ...
- Java的三种代理模式(Spring动态代理对象)
Java的三种代理模式 1.代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩 ...
随机推荐
- Cordova编译报AAPT错误的解决方法
因为项目中同时使用cordova-hot-code-push-plugin和phonegap-plugin-barcodescanner,编译时报错:AAPT: error: resource and ...
- leetcode题解 1.TwoSum
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- clearfix 用法
如果有一个DIV作为外部容器,内部的DIV如果设置了float样式,则外部的容器DIV因为内部没有 clear,导致不能被撑开.看下面的例子:Div布局如下:Css代码如下:.out{border:1 ...
- redis 分布式锁实现
我们实现的分布式锁,使用redis提供的SET NX功能,由于redis server的单线程模型,保证了天然并发安全. https://stackoverflow.com/questions/116 ...
- xshell连接linux主机时,出现错误:Could not connect to '***.***.***.***' (port 22)
xshell连接linux主机时,会出现错误:Could not connect to '192.168.89.144' (port 22): Connection failed. 但是这时能ping ...
- Jquery仿百度经验左右滚动切换效果(转)
http://www.xwcms.net/webAnnexImages/fileAnnex/201608/61342/index.html
- POJ1821 Fence
题意 Language:Default Fence Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6478 Accepted: ...
- JDBC执行SQL语句以及Date对象和字符串之间的相互转换(关键是那张标准表)
只要能分隔数字就行,老外没有11月这个概念 以前看见被人这么写,你觉得可以写成yyYY这样吗,可以mm这样吗,可以mM这样吗,不要有这种想法 都是大神们都写好了,只需要参考手册,然后调用API就行了 ...
- Python shutil 模块
高级的文件.文件夹.压缩包 处理模块 http://www.cnblogs.com/wupeiqi/articles/4963027.html
- python, ImageFont
ImageFont模块定义了相同名称的类,即ImageFont类.这个类的实例存储bitmap字体,用于ImageDraw类的text()方法. PIL可以配置是否支持TrueType和OpenTyp ...