项目地址: 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) |
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.