Mybatis-spring 动态代理

Posted jinyu-helloword

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 动态代理的主要内容,如果未能解决你的问题,请参考以下文章

MapperScannerConfigurer的原理

Mybatis-spring源码分析之注册Mapper Bean

Mybatis源码解析-mybatis-spring原理解析

JAVA之AOP

动态 Rstudio 代码片段

是否可以动态编译和执行 C# 代码片段?