`
y806839048
  • 浏览: 1084431 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

通用mapper生成sql及mybatis使用过程

阅读更多

 

sql装配,自己实现一个,通用mapper和mybatis-generator是两个东西各自功能不同,结合使用而已

通用mapper:

1,省略单表操作的xml

2,不必写实现类(用通用mapper的单表操作方法不用,用xml中sql(mybatis)也不用)

   mybaits      VS         hibernate

mybatis-generator对应hibernate反向工程

通用mapper对应spring-data-jpa

 

 

(一)、通用mapper生成sql过程:

 

生成具体生成sql的地方在相应的操作方法接口中

 

public interface SelectOneMapper<T> {

 

    /**

     * 鏍规嵁瀹炰綋涓殑灞炴�杩涜鏌ヨ锛屽彧鑳芥湁涓�釜杩斿洖鍊硷紝鏈夊涓粨鏋滄槸鎶涘嚭寮傚父锛屾煡璇㈡潯浠朵娇鐢ㄧ瓑鍙�

     *

     * @param record

     * @return

     */

    @SelectProvider(type = BaseSelectProvider.class, method = "dynamicSQL")

    T selectOne(T record);

 

}

 

public class BaseSelectProvider extends MapperTemplate 

 

“V”型调试

 

MapperTemplate中setSqlSource:

 

SqlNode sqlNode = (SqlNode) method.invoke(this, ms);BaseSelectProvider--》dynamicSQL///调用对应操作接口的方法获取对应单表语句

DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode);

setSqlSource(ms, dynamicSqlSource);////自动生成mybatis能用的写在xml中的节点格式语句

 

 

MappedStatement ms这个含有mybatis的1,通用配置,2,基于哪个是实体的单表操作,3,参数,4,哪种操作,5,用什么打印日志

 

sqlNode包含三个段1,select from端,2,表名端,3,where端(由ms得来)

 

 

 

 

 

/**

     * 重新设置SqlSource

     *

     * @param ms

     * @throws java.lang.reflect.InvocationTargetException

     * @throws IllegalAccessException

     */

    public void setSqlSource(MappedStatement ms) throws Exception {

        if (this.mapperClass == getMapperClass(ms.getId())) {

            throw new RuntimeException("请不要配置或扫描通用Mapper接口类:" + this.mapperClass);

        }

        Method method = methodMap.get(getMethodName(ms));

        try {

            //第一种,直接操作ms,不需要返回值

            if (method.getReturnType() == Void.TYPE) {

                method.invoke(this, ms);

            }

            //第二种,返回SqlNode

            else if (SqlNode.class.isAssignableFrom(method.getReturnType())) {

                SqlNode sqlNode = (SqlNode) method.invoke(this, ms);

                DynamicSqlSource dynamicSqlSource = new DynamicSqlSource(ms.getConfiguration(), sqlNode);

                setSqlSource(ms, dynamicSqlSource);

            }

            //第三种,返回xml形式的sql字符串

            else if (String.class.equals(method.getReturnType())) {

                String xmlSql = (String) method.invoke(this, ms);

                SqlSource sqlSource = createSqlSource(ms, xmlSql);

                //替换原有的SqlSource

                setSqlSource(ms, sqlSource);

            } else {

                throw new RuntimeException("自定义Mapper方法返回类型错误,可选的返回类型为void,SqlNode,String三种!");

            }

            //cache

            checkCache(ms);

        } catch (IllegalAccessException e) {

            throw new RuntimeException(e);

        } catch (InvocationTargetException e) {

            throw new RuntimeException(e.getTargetException() != null ? e.getTargetException() : e);

        }

    }

 

    protected void setSqlSource(MappedStatement ms, SqlSource sqlSource)

  {

    MetaObject msObject = SystemMetaObject.forObject(ms);/////////////////////////可知MetaObject其实就是ms

    msObject.setValue("sqlSource", sqlSource);

    

    KeyGenerator keyGenerator = ms.getKeyGenerator();

    if ((keyGenerator instanceof Jdbc3KeyGenerator)) {

      msObject.setValue("keyGenerator", new MultipleJdbc3KeyGenerator());

    }

  }

  

MetaObject:mybatis的类

    public void setValue(String name, Object value)

  {

    PropertyTokenizer prop = new PropertyTokenizer(name);

    if (prop.hasNext())

    {

      MetaObject metaValue = metaObjectForProperty(prop.getIndexedName());

      if (metaValue == SystemMetaObject.NULL_META_OBJECT)

      {

        if ((value == null) && (prop.getChildren() != null)) {

          return;

        }

        metaValue = this.objectWrapper.instantiatePropertyValue(name, prop, this.objectFactory);

      }

      metaValue.setValue(prop.getChildren(), value);

    }

    else

    {

      this.objectWrapper.set(prop, value);

    }

  }

  

查看这几个类:

SqlSourceBuilder

UnpooledDataSourceFactory

Jdbc3KeyGenerator

可知:在检查注入主键的的时候就用MetaObject设置了一次默认的sqlsource(MetaObject是单例)

在具体时候某个通用mapper的单表方法时重置下MetaObject的sqlsource,之后执行sql的时候取到的MetaObject的sqlsource就是覆盖后的()

这个sqlsource的格式就是传统的mybaits.xml手动写的sql节点的格式,之后就交给mybatis去解析这种格式(mybatis使用节点语句都是放到MetaObject中后使用)

 

 

(二)、mybatis中使用ms大致过程:

 

SelectKeyGenerator///使用的地方调用Executor对象

CachingExecutor////////////////这里直接用ms去执行sql

BaseExecutor

MappedStatement

DynamicSqlSource

SqlSourceBuilder

StaticSqlSource

BoundSql

 

 

SelectKeyGenerator:

 

  private void processGeneratedKeys(Executor executor, MappedStatement ms, Object parameter)

  {

    try

    {

      if ((parameter != null) && (this.keyStatement != null) && (this.keyStatement.getKeyProperties() != null))

      {

        String[] keyProperties = this.keyStatement.getKeyProperties();

        Configuration configuration = ms.getConfiguration();

        MetaObject metaParam = configuration.newMetaObject(parameter);

        if (keyProperties != null)

        {

          Executor keyExecutor = configuration.newExecutor(executor.getTransaction(), ExecutorType.SIMPLE);

          List<Object> values = keyExecutor.query(this.keyStatement, parameter, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);

        ...

}

}}}

CachingExecutor:

 @Override

  public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException {

    BoundSql boundSql = ms.getBoundSql(parameterObject);

    CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql);

    return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql);

  }

 

DynamicSqlSource:

  public BoundSql getBoundSql(Object parameterObject) {

    DynamicContext context = new DynamicContext(configuration, parameterObject);

    rootSqlNode.apply(context);

    SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);

    Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();

    SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType, context.getBindings());

    BoundSql boundSql = sqlSource.getBoundSql(parameterObject);

    for (Map.Entry<String, Object> entry : context.getBindings().entrySet()) {

      boundSql.setAdditionalParameter(entry.getKey(), entry.getValue());

    }

    return boundSql;

  }

 

StaticSqlSource:

 public StaticSqlSource(Configuration configuration, String sql, List<ParameterMapping> parameterMappings) {

    this.sql = sql;

    this.parameterMappings = parameterMappings;

    this.configuration = configuration;

  }

 

  @Override

  public BoundSql getBoundSql(Object parameterObject) {

    return new BoundSql(configuration, sql, parameterMappings, parameterObject);

  }

 

 

 

 

参考:

http://blog.csdn.net/isea533/article/details/41892319

 

 

 

 

 

分享到:
评论

相关推荐

    尚硅谷-通用Mapper视频

    通用Mapper是一款非常好用的MyBatis插件,它能够帮助我们自动生成常用增删改查操作的SQL语句,克服MyBatis开发过程中需要大量编写SQL语句的弊端。由于通用Mapper是根据实体类的属性自动生成对应的SQL语句,所以可以...

    尚硅谷-通用Mapper视频(最新)

    通用Mapper是一款非常好用的MyBatis插件,它能够帮助我们自动生成常用增删改查操作的SQL语句,克服MyBatis开发过程中需要大量编写SQL语句的弊端。由于通用Mapper是根据实体类的属性自动生成对应的SQL语句,所以可以...

    尚硅谷_谷粒商城简介_idea_通用mapper.docx

    通用Mapper是一款非常好用的MyBatis插件,它能够帮助我们自动生成常用增删改查操作的SQL语句,克服MyBatis开发过程中需要大量编写SQL语句的弊端。由于通用Mapper是根据实体类的属性自动生成对应的SQL语句,所以可以...

    通用Mapper原理视频讲解

    5.使用MyBatis的XMLLanguageDriver,根据xml形式SQL语句生成SqlSource 6.将生成的SqlSource替换原有的ProviderSqlSource ================================ MyBatis通用Mapper github:...

    springboot+shiro+mybatis+redis+通用mapper+aop+CodeGenerator

    可以直接运行,springboot+shiro+mybatis+redis+通用mapper+aop+CodeGenerator,redis和通用mapper集合在一起,并且代码可以自动生成,

    mybatis_generator通用代码生成器

    tk_mapper 使用springboot+mybatis整合的代码生成器,解决单表创建sql模板

    通俗易懂的通用Mapper教程(含配套资料)

    通用Mapper是一款非常好用的MyBatis插件,它能够帮助我们自动生成常用增删改查操作的SQL语句,克服MyBatis开发过程中需要大量编写SQL语句的弊端。由于通用Mapper是根据实体类的属性自动生成对应的SQL语句,所以可以...

    全面学习Mybatis插件之Mybatis-Plus_Java框架视频教程

    使用原生的Mybatis编写持久层逻辑时,所需要的代码是比较繁琐的,需要定义Mapper接口和Mapper.xml文件,每一个方法都需要编写对应的sql语句,会存在很多大量的重复工作,使用MP之后,对通用的方法做了高度的抽取,...

    mybatis-mapper:mybatis单表通用CURD插件和分页插件

    于是阅读mybatis的源码,设计一个能够自动生成通用sql、又能够将通用sql和手写的sql分离的解决方案。经历过好 几个版本的迭代,当前版本已经在生产环境可用了。(sirenia是一个金属乐队,是Krake

    springmybatis

    mybatis实战教程mybatis in action之九mybatis 代码生成工具的使用 mybatis SqlSessionDaoSupport的使用附代码下载 转自:http://www.yihaomen.com/article/java/302.htm (读者注:其实这个应该叫做很基础的入门...

    Mybatis增强工具包(Mybatis plus).rar

    通用CRUD操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求 多种主键策略:支持多达4种主键策略(内含分布式唯一ID生成器),可自由...

    code-generator

    另外还集成了通用Mapper(tk.mybatis),除了一些特殊的业务逻辑之外不需要写任何 SQL,只需要写好实体类及 Mapper 文件中对应的字段 即可支持相应的增删改查方法,大大提高了开发效率。 功能特点 生成实体类,集成 ...

    mybatis-tool.zip

    mybatis-tool 2.0 版本,之前版本直接上传的工具代码,有些童鞋反映导入idea需要整半天才能运行起来,于是=这次升级为jar包加配置文件的形式, 代码封装不变,修改配置文件和模板文件即可实现数据库单表sql, mapper,...

    还在使用MyBatis Generator?试试这个工具.docx

    代码生成 在企业软件开发过程中,大多数时间都是面向数据库表的增删改查开发。通过通用的增删改查代码生成器,可以有效的...可以帮我们生成表对应的持久化对象(po)、操作数据库的接口(dao)、CRUD sql 的 xml(mapper)。

    Mybatis plus 基于 springBoot 源码

    通用CRUD操作:内置通用 Mapper、通用 Service,仅仅通过少量配置即可实现单表大部分 CRUD 操作,更有强大的条件构造器,满足各类使用需求 多种主键策略:支持多达4种主键策略(内含分布式唯一ID生成器),可自由...

    Spring Boot & MyBatis种子项目,用于快速构建中小型API、RESTful API项目

    - 最佳实践的项目结构、配置...- 集成MyBatis、通用Mapper插件、PageHelper分页插件,实现单表业务零SQL - 提供代码生成器根据表名生成对应的Model、Mapper、MapperXML、Service、ServiceImpl、Controller等基础代码

    mybatis-generator.zip

    可自动生成sql的工具,包括xml(mapper), dao层接口,bean实体;含盖通用的增删改查方法; 本工具的优势:不用集成在spring工程中,即不依赖java工程,不用先建立或导入工程;可直接手动运行,双击bat文件,完全...

    JAVA爬虫Jsoup -Mybatis-Plusaricle-crawling-master.zip

    它提供了很多常用的功能,例如通用 Mapper、分页插件、代码生成器等。同时,它也遵循原生 Mybatis 的 SQL 标准,方便我们快速的进行开发。 Jsoup 是一个 Java 的 HTML 解析器,主要用于从网页中提取数据。通过 ...

    一个基于Spring Boot & MyBatis的种子项目,用于快速构建中小型API、RESTful API项目~

    集成MyBatis、通用Mapper插件、PageHelper分页插件,实现单表业务零SQL 提供代码生成器根据表名生成的Model、Mapper、MapperXML、Service、ServiceImpl、Controller等基础代码,其中Controller模板默认提供POST和...

Global site tag (gtag.js) - Google Analytics