项目地址: https://github.com/braisdom/ObjectiveSql
@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); |
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) |
101
Braisdom OP join, sub query 这些都是传统 ORM 框架的弱势,有兴趣去这里,https://github.com/braisdom/ObjectiveSql
话题太长,这里讨论不方便 |
102
liuhuan475 2020-12-14 14:32:38 +08:00 1
@Braisdom 为什么要用你这个框架 而不用 mybatis-plus
|
103
Braisdom OP @liuhuan475 这个问题很好,是我后续要出更多文档进行比较的方向,至于为什么我目前先介绍几点:
1 )动态代码生成:对于简单查询我只需要一个模型定义就可以,而 MyBatis 则需要一个多余的 Mapper 。 2 )复杂表达式(算术,比较,逻辑):我可能直接将 Java 的表达式转换成 SQL 表达式,例如:aTable.column + bTable.column...,而 MyBatis 只能通过字符串。 3 )关联查询,分页查询:我只需要一个 Annotation,而 MyBatis 则需要 Mapper + XML 上面只是简单的描述,这些是我针对 MyBatis 的弱点进行的挑战。 |