myBatis---接口代理开发(demo)
一、概述
使用接口代理开发,可以不用写接口的实现类,而采用的是MapperFactoryBean代理的实现类。
* 接口代理方式开发,遵循四大原则
* 1.方法名 == mapper.xml的id名
* 2.返回值类型 == mapper.xml文件的resultType,或resultMap
* 3.形参 == mapper.xml的入参 parameterType
* 4.接口名 == mapper.xml的namespace
二、文件概况

三、代码
1.applicationContext.xml
<?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"> <!-- 数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="orcl"/>
</bean> <!-- sqlSessionFactoryBean工厂 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="sqlMapConfig.xml"/>
</bean> <!-- DeptDao接口的代理类 -->
<bean id="deptDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.cissst.it.dept.dao.DeptDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
</bean> <!-- EmpDao接口的代理类 -->
<bean id="empDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.cissst.it.emp.dao.EmpDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>
</bean> </beans>
2.sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases>
<typeAlias type="com.cissst.it.emp.entity.Emp" alias="Emp"/>
</typeAliases>
<mappers>
<mapper resource="com/cissst/it/dept/dao/DeptDao.xml"/>
<mapper resource="com/cissst/it/emp/dao/EmpDao.xml"/>
</mappers>
</configuration>
3.接口.java
package com.cissst.it.emp.dao;
import java.util.List;
import com.cissst.it.emp.entity.Emp;
public interface EmpDao {
public List<Emp> findEmpByDeptno(Integer deptno);
}
4.mapper.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.cissst.it.emp.dao.EmpDao">
<select id="findEmpByDeptno" parameterType="Integer" resultType="Map">
select * from Emp where deptno = #{id}
</select>
</mapper>
5.测试类
package com.cissst.it.test; import java.util.List; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.cissst.it.dept.dao.DeptDao;
import com.cissst.it.dept.entity.Dept;
import com.cissst.it.emp.dao.EmpDao;
import com.cissst.it.emp.entity.Emp;
public class Main {
@SuppressWarnings("resource")
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//查找10号部门信息
DeptDao obj1 = (DeptDao) context.getBean("deptDao");
Dept dept = obj1.findByDeptno(10);
System.out.println(dept); //查询部门为10号的所有员工信息
EmpDao obj2 = (EmpDao) context.getBean("empDao");
List<Emp> list = obj2.findEmpByDeptno(10);
System.out.println(list);
}
}
myBatis---接口代理开发(demo)的更多相关文章
- Mybatis学习(2)原始dao开发和使用mapper接口代理开发
基础知识: 1).SqlSessionFactoryBuilder: 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory.将SqlSessionFact ...
- Mybatis 接口代理的实现(BeanDefinitionRegistryPostProcessor+FactoryBean)
相信在开发中,尤其是mybatis 配置操作中,我们只需要提供一个mapper 接口,然后注入到service 中,就可以进行调用. 按我们的一般逻辑来说,我们并没有进行接口的实现,应该会报空指针异常 ...
- MyBatis 动态代理开发
MyBatis 动态代理开发 § Mapper.xml文件中的namespace与mapper接口的类路径相同. § Mapper接口方法名和Mapper.xml中定义的每个statement的i ...
- MyBatis之代理开发模式
1 mybatis-Dao的代理开发模式 Dao:数据访问对象 原来:定义dao接口,在定义dao的实现类 dao的代理开发模式 只需要定义dao接口,由mybatis产生dao接口的实现类. 1.1 ...
- MyBatis的接口式编程Demo
很久没细看过MyBatis了,时间一长就容易忘记. 下面是一个接口式编程的例子. 这里的例子一共分为4步: 1 首先要有一个namespace为接口的全类名的映射文件,该例中是 IMyUser.xml ...
- MyBatis进阶--接口代理方式实现Dao 和动态SQL
MyBatis接口代理方式实现Dao层 接口代理方式-实现规则 传统方式实现Dao层,我们既要写接口.还要写实现类.而MyBatis框架可以帮助我们省略写Dao层接口实现类的步骤.程序员只需要编写接口 ...
- 【mybatis深度历险系列】深入浅出mybatis中原始dao的开发和mapper代理开发
使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.mybatis在进行dao开发的时候,涉及到三姐妹,分别是SqlSessionFactoryBuilder ...
- Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合
Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合 标签: mybatisSpringbeanApplicationContextMapper 2015-12-31 1 ...
- mybatis入门基础(二)----原始dao的开发和mapper代理开发
承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...
随机推荐
- from jobscrawler_qianchengwuyou.items import JobscrawlerQianchengwuyouItem
-- coding: utf-8 -- import scrapy from jobscrawler_qianchengwuyou.items import JobscrawlerQianchengw ...
- selemiun 自动化测试登录验证码处理
selemiun 自动化测试登录验证码处理 一.软件及插件的安装 1.火狐浏览器版本(55.0(x64 zh-CN):https://www.cnblogs.com/sandysun/p/783811 ...
- MySQL行转列、列转行
一.行转列 有如图所示的表,现在希望查询的结果将行转成列 建表语句如下: CREATE TABLE `TEST_TB_GRADE` ( `ID` int(10) NOT NULL AUTO_INCRE ...
- 【SoftwareTesting】Homework1
The errors I will mention after are from the project in the last semester. The project is a Java pro ...
- pytest自动化3:fixture之conftest.py实现setup
出处:https://www.cnblogs.com/yoyoketang/p/9390073.html 前言: 前面一篇讲到用例加setup和teardown可以实现在测试用例之前或之后加入一些操作 ...
- my first homepage
<!DOCTYPE html><html><head><style type="text/css">p{ text-indent:2 ...
- MySQL最基本的概念梳理
本文根据<MySQL必知必会>(Ben Forta著,2009)整理,基于MySQL4.1-5,可作为深入研究MySQL之前的漱口篇.(基本语句.正则表达式.联结.全文本搜索.增删改查.存 ...
- IPC 简说
IPC(inter-process communication)进程间通信 多进程分为两种情况 1. 同一个应用,使用android:process属性启动的四大组件 2. 多应用 通过android ...
- Python字符串练习
1. 确定一个字符串中有多少个元音字母 def getCount(inputStr): return len([a for a in inputStr if a in "aeiou" ...
- css+jquery 实现图片局部放大预览
今天有时间开始动手,使用css+jquery实现了图片局部放大的组件,首先看看效果图: 界面设计思路如下: 1.两个div,左边放图片的缩略图 2.在左边缩略图鼠标移动的时候,区域(效果图中的网格) ...