You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
<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 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(...);
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
1 简单查询
根据单字段查询
稍微复杂一点的查询
2 复杂查询
动态查询
关联查询
Beta Was this translation helpful? Give feedback.
All reactions