团队开发一个项目,由老大架了一个框架,遇到了DAO层不用写接口了,我也是用了2次才记住这个事的,因为自己一直都是习惯于写DAO层的实现类,所以,习惯性的还是写了个实现类。于是遇到错误了。

找不到那个方法。问了团队的人才知道,方法名和Mapper中配置的id名必须一样。

实现:

一、配置Spring集成MyBatis:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"...
  4. -------------------------------------------------
  5. <!-- 配置数据源 -->
  6. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
  7. ...
  8. -------------------------------------------------
  9. <!-- 产生sqlsessionfactory -->
  10. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  11. <property name="dataSource" ref="dataSource" />
  12. <property name="configLocation" value="classpath:mybatis-config.xml" />
  13. </bean>
  14. --------------------------------------------------

要实现对数据库的操作必须要有sqlSession,而sqlSession是由sqlSessionFactory创建的。我们可以在spring配置好bean。

  1. <!-- 自动扫描mapper接口-->
  2. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
  3. p:basePackage="com.xiaojuzi.chaojijuhui.**.dao"
  4. p:sqlSessionFactoryBeanName="sqlSessionFactory" />

这个配置就是配置映射文件的路径,这样做的好处就是不用再写Dao的实现类了,也就是说,我们写好接口,写好配置文件,会自动映射到对应的方法和sql语句。

二、开发mapper.xml映射文件

  1. <mapper namespace="com.xiaojuzi.chaojijuhui.user.dao.UserDao">
  2. -------------------------------------------------------

在这里只有一个UserDao(被代理的接口)。 
user.mapper.xml–namespace配置的就是UserDao的包全名。

三、开发mapper.java的接口

  1. /**
  2. * 根据用户的用户名查询用户
  3. * @param user
  4. * @return
  5. */
  6. User queryUserByLoginName (String loginName);
  7. /**
  8. * 用户通过手机号码去修改密码
  9. * @param userModel
  10. * @return
  11. */
  12. Boolean updatePasswordByMobile(UserModel userModel);

如果需要特定类型的参数,就自己再造一个POJO类(例如:UserModel)。

  1. <sql id="userColumns">
  2. u.id,
  3. u.login_name as "loginName",
  4. u.head_img as "headImg",
  5. ...
  6. ---------------------------------------------
  7. <select id="queryUserByLoginName" resultType="User" parameterType="User">
  8. SELECT <include refid="userColumns" />
  9. FROM juhui_user u
  10. WHERE u.login_name = #{loginName}
  11. and u.del_flag = #{DEL_FLAG_NORMAL}
  12. </select>
  13. <update id="updatePasswordByMobile" parameterType="UserModel">
  14. update juhui_user set
  15. update_date=DATE_FORMAT(#{updateDate}, '%Y-%m-%d %H:%i:%S'),
  16. salt = #{salt},
  17. password = #{password}
  18. where mobile = #{mobile}
  19. </update>

这里mapper.xml的(select、insert、update..)标签的id必须和DAO接口的方法名一样!

Mapper开发规则

1、 在mapper.xml中将namespace设置为mapper.Java的全限定名 
2、 将mapper.java接口的方法名和mapper.xml中statement的id保持一致。 
3、 将mapper.java接口的方法输入参数类型和mapper.xml中statement的parameterType保持一致 
4、 将mapper.java接口的方法输出 结果类型和mapper.xml中statement的resultType保持一致

注意遵循上边四点规范!

这样抛弃Dao实现类的写法: 
具有更好的可扩展性,提高了灵活度。

原理

再根据网上的一些知识点,讲一下原理:

mybatis通过JDK的动态代理方式,在启动加载配置文件时,根据配置mapper的xml去生成Dao的实现。

session.getMapper()使用了代理,当调用一次此方法,都会产生一个代理class的instance,看看这个代理class的实现.

  1. public class MapperProxy implements InvocationHandler {
  2. ...
  3. public static <T> T newMapperProxy(Class<T> mapperInterface, SqlSession sqlSession) {
  4. ClassLoader classLoader = mapperInterface.getClassLoader();
  5. Class<?>[] interfaces = new Class[]{mapperInterface};
  6. MapperProxy proxy = new MapperProxy(sqlSession);
  7. return (T) Proxy.newProxyInstance(classLoader, interfaces, proxy);
  8. }
  9. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  10. if (!OBJECT_METHODS.contains(method.getName())) {
  11. final Class<?> declaringInterface = findDeclaringInterface(proxy, method);
  12. final MapperMethod mapperMethod = new MapperMethod(declaringInterface, method, sqlSession);
  13. final Object result = mapperMethod.execute(args);
  14. if (result == null && method.getReturnType().isPrimitive()) {
  15. throw new BindingException("Mapper method '" + method.getName() + "' (" + method.getDeclaringClass() + ") attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
  16. }
  17. return result;
  18. }
  19. return null;
  20. }

这里是用到了JDK的代理Proxy。 newMapperProxy()可以取得实现interfaces 的class的代理类的实例。

当执行interfaces中的方法的时候,会自动执行invoke()方法,其中public Object invoke(Object proxy, Method method, Object[] args)中 method参数就代表你要执行的方法.

MapperMethod类会使用method方法的methodName 和declaringInterface去取 sqlMapxml 取得对应的sql,也就是拿declaringInterface的类全名加上 sql-id..

总结: 
这个就是利用JDK的代理类实现的。

[MyBatis]DAO层只写接口,不用写实现类的更多相关文章

  1. MyBatis Dao层的编写

    传统的dao层编写 以前编写dao层,先新建一个包com.chy.dao,再写接口StudentDao: public interface StudentDao { public void inser ...

  2. MyBatis dao层 方法传参

    MyBatis dao层 方法传参有三种方法. 1. 以下标的方法获取参数. <update id="insertSuccessKilled">       INSER ...

  3. 数据库Dao层编增删改查写,数据库事务,数据库升级

    数据库事务 有两个特点 1.安全性 情景:正常的转账行为,这个时候如果出现停电等异常,已经扣钱但是没有加钱:这个时候就可用数据库事务解决问题 2.高效性: 使用数据库事务添加享受同数量的数据,对比耗时 ...

  4. mybatis dao 层开发简易版 非整合 spring

    同样老习惯,先上项目结构截图 首先 补充上篇文中缺失的 mysql demo 用的 小脚本 drop database if exists mybatis; CREATE DATABASE `myba ...

  5. MyBatis DAO层传递参数到mapping.xml 几种方式

    Dao层传递参数到mapping.xml文件的几种方式:(Mybatis传值总结) 第一种:传递单个参数 Dao层Code片段: /** * 根据articleId查询XXXX详情. * * @par ...

  6. 基于dbunit进行mybatis DAO层Excel单元测试

    DAO层测试难点 可重复性,每次运行单元测试,得到的数据是重复的 独立性,测试数据与实际数据相互独立 数据库中脏数据预处理 不能给数据库中数据带来变化 DAO层测试方法 使用内存数据库,如H2.优点: ...

  7. Mybatis Dao层注解及XML组合Dao的开发方式

    mybatis可以用xml进行数据操作,也可以在dao层用注解的方式,也可以采取xml和dao层接口组合使用的方法.显然 ,后者更加简单. 实体类Student   package com.zhao. ...

  8. 黑马MyBatisday2 MyBatis Dao层实现 接口代理实现&传统实现 动态SQL和SQL抽取 自定义类型处理 分页插件PageHelper

    package com.itheima.mapper; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelp ...

  9. mybatis之DAO层自动实现接口

    * 使用mybatis举例,使用DAO接口方式实现* 不需要针对接口去编写具体的实现类代码,编写映射XML文件,直接拿来使用即可.* 1.导入jar包:mybatis和mysql-connector* ...

随机推荐

  1. 从一个实例学习----FLASK-WTF

    本案例通过实现一个注册页面的编写,来带你了解FLASK-WTF的运用. 主要功能为表单基础的功能--手机号码必须为11位数,且通过数据库查找不能有已经注册的了,密码要求输入两遍且必须一样,且所有内容不 ...

  2. mysql-connector-java 6.x 时区设置

    最近遇到了一个问题,java从mysql从读出的时间与本地时间有14个小时的时间差,经查证.测试解决了此问题,在此总结一下: jdbc:mysql:&allowMultiQueries=tru ...

  3. 流式计算与计算抽象化------《Designing Data-Intensive Applications》读书笔记15

    上篇的内容,我们探讨了分布式计算中的MapReduce与批处理.所以本篇我们将继续探索分布式计算优化的相关细节,并且分析MapReduce与批处理的局限性,看看流式计算是否能给我们在分布式计算层面提供 ...

  4. solr6.6教程-从mysql数据库中导入数据(三)

    整理多半天的solr6.6,终于算是把solr6.6中配置数据库弄出来了,网上的文章千篇一律,各说个的,没有一篇统一覆盖solr6.6版本配置的 帖子,本章节我会把配置的一些注意事项一一列举,由于时间 ...

  5. PHP opcache扩展安装

    下面是我在PHP 5.4下的安装方法: https://pecl.php.net/get/zendopcache-7.0.5.tgz tar xzf zendopcache-7.0.5.tgz cd ...

  6. CodeForces - 796C Bank Hacking

    思路:共有n-1条边连接n个点,即形成一棵树.一开始需要选择一个点hack--将这个点视为根结点,与它相邻的点防御值加1,与它相隔一个在线点的点的防御也加1.当根节点被hack,即这个点被删除,又变成 ...

  7. hdu4825 01字典树+贪心

    从高位向低位构造字典树,因为高位得到的数更大. AC代码: #include<cstdio> using namespace std; typedef long long LL; cons ...

  8. .net Winfrom 僵尸窗口 的问题

    执行顺序如下: Form1 form1 =new Form1(); form1.ShowDialog(); Form2 form2= Application.OpenForms["Form2 ...

  9. python 爬取糗事百科 gui小程序

    前言:有时候无聊看一些搞笑的段子,糗事百科还是个不错的网站,所以就想用Python来玩一下.也比较简单,就写出来分享一下.嘿嘿 环境:Python 2.7 + win7 现在开始,打开糗事百科网站,先 ...

  10. linux系统 initrd.img中init启动脚本分析

    概述:这篇文章主体内容来源于网上转载.前面几篇文章倾向于制作initrd.img,这篇文章更倾向于initrd.img的运行过程:加载framebuff驱动 ide驱动和文件系统驱动,最后进入到真正的 ...