V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Braisdom
V2EX  ›  Java

对比 MyBatis,你看出什么端倪?

  •  2
     
  •   Braisdom ·
    braisdom · 2020-12-11 16:34:54 +08:00 · 9877 次点击
    这是一个创建于 1475 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目地址: https://github.com/braisdom/ObjectiveSql

    1 简单查询

    @DomainModel
    public class Blog {
        private Long id;
        private String title;
        private Integer state;
        @Relation(relationType = RelationType.BELONGS_TO)
        private Author author;
    }
    

    根据单字段查询

    MyBatis ObjectiveSQL
    public interface BlogMapper {
      @Select("SELECT * FROM blog WHERE id = #{id}")
      Blog selectBlog(int id);
    }
    ...
    BlogMapper mapper = session.getMapper(BlogMapper.class); 
    Blog blog = mapper.selectBlog(i);
    
    Blog blog = Blog.queryByPrimaryKey(1);
    

    稍微复杂一点的查询

    MyBatis ObjectiveSQL
    <?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="org.mybatis.example.BlogMapper">
      <select id="findByTitle" resultType="Blog">
        select * from Blog where title like  '%#{title}%'
      </select>
    </mapper>
    ...
    BlogMapper mapper = session.getMapper(BlogMapper.class); 
    Blog blog = mapper.findByTitle("标题");
    
    public static Blog queryByTitle(String title) {
         Query<Blog> query = createQuery();
         query.where("title like ?",  "%" + title + "%");
         return query.queryFirst();
    }
    ...
    Blog blog = Blog.queryByTitle(1);
    

    2 复杂查询

    动态查询

    MyBatis ObjectiveSQL
    <select id="findActiveBlogLike"
         resultType="Blog">
      SELECT * FROM BLOG
      WHERE
      <if test="state != null">
        state > #{state}
      </if>
      <if test="title != null">
        AND title like #{title}
      </if>
      <if test="author != null and author.name != null">
        AND author_name like #{author.name}
      </if>
    </select>
    ...
    BlogMapper mapper = session.getMapper(BlogMapper.class); 
    Blog blog = mapper.findActiveBlogLike(...);
    
    Blog.Table blog = Blog.asTable();
    Select select = new Select();
    LogicalExpression eternalExpression = new EternalExpression();
    if(state != null)
         eternalExpression = eternalExpression.and(blog.state > state);
    if(title != null)
         eternalExpression = eternalExpression.and(blog.title.like(title));
    if(author != null && author.name != null)
         eternalExpression = eternalExpression.and(blog.author.like(author.name));
    return select.from(blog).where(eternalExpression).execute();
    

    关联查询

    MyBatis ObjectiveSQL
    <mapper>
      <resultMap type="Blog" id="blog">
        <id column="id" property="id" />
        ...
        <association property="author" javaType="Author">
          ...
        </association>
      </resultMap>
      <select id="findBlog" parameterType="int" resultMap="blog">
        ...
      </select>
    </mapper>
    ...
    BlogMapper mapper = session.getMapper(BlogMapper.class); 
    Blog blog = mapper. findBlog(...);
    
    Blog blog = Blog.queryByPrimaryKey(1, 
                        Blog.BELONGS_TO_AUTHOR)
    
    103 条回复    2020-12-14 14:47:03 +08:00
    1  2  
    Braisdom
        101
    Braisdom  
    OP
       2020-12-14 12:40:58 +08:00
    join, sub query 这些都是传统 ORM 框架的弱势,有兴趣去这里,https://github.com/braisdom/ObjectiveSql
    话题太长,这里讨论不方便
    liuhuan475
        102
    liuhuan475  
       2020-12-14 14:32:38 +08:00   ❤️ 1
    @Braisdom 为什么要用你这个框架 而不用 mybatis-plus
    Braisdom
        103
    Braisdom  
    OP
       2020-12-14 14:47:03 +08:00
    @liuhuan475 这个问题很好,是我后续要出更多文档进行比较的方向,至于为什么我目前先介绍几点:

    1 )动态代码生成:对于简单查询我只需要一个模型定义就可以,而 MyBatis 则需要一个多余的 Mapper 。
    2 )复杂表达式(算术,比较,逻辑):我可能直接将 Java 的表达式转换成 SQL 表达式,例如:aTable.column + bTable.column...,而 MyBatis 只能通过字符串。
    3 )关联查询,分页查询:我只需要一个 Annotation,而 MyBatis 则需要 Mapper + XML

    上面只是简单的描述,这些是我针对 MyBatis 的弱点进行的挑战。
    1  2  
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   6023 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 02:03 · PVG 10:03 · LAX 18:03 · JFK 21:03
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.