@Data public class Blog { private String id; private String title; private String author; private Date createTime;//属性名字和字段名字不一致 使用驼峰命名把A_COLUMN 变成 aColumn private int views; }
获得一个随机id的方法
public static String getId(){ return UUID.randomUUID().toString().replaceAll("-",""); }
接口
public interface BlogMapper { //插入数据 int addBlog(Blog blog); //查询博客 List<Blog> queryBlogIf(Map map); List<Blog> queryBlogChoose(Map map); int updateBlog(Map map); //查询一二三号的博客 List<Blog> queryBlogForEach(Map map); }
驼峰命名
<settings> <setting name="logImpl" value="STDOUT_LOGGING"/><!-- 标准的日志工厂,可以直接用,其他的需要导入包--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>
sql标签,1=1,if标签的使用
<!--引用sql片段--> <sql id="if-title-author"> <if test="title != null"> and title =#{title} </if> <if test="author !=null"> and author=#{author} </if> </sql> <select id="queryBlogIf" parameterType="map" resultType="com.wu.pojo.Blog"> select * from blog where 1=1 <include refid="if-title-author"></include> </select>
拼接的sql
select * from blog where 1=1 and title =?
where,choose,when,otherwise标签的使用
<select id="queryBlogChoose" parameterType="map" resultType="com.wu.pojo.Blog"> select * from blog <where> <choose> <when test="title!=null"> and title =#{title} </when> <when test="author !=null"> and author=#{author} </when> <otherwise> and views=#{views} </otherwise> </choose> </where> </select>
拼接的sql
select * from blog WHERE views=?
set标签的使用
<!--set标签自动删除无关逗号--> <update id="updateBlog" parameterType="map"> update blog <set> <if test="title!=null"> title =#{title}, </if> <if test="author!=null"> author=#{author} </if> </set> where id=#{id} </update>
拼接完成的sql:
update blog SET author=? where id=?
for each标签的使用
<!--for each的集合--> <select id="queryBlogForEach" parameterType="map" resultType="com.wu.pojo.Blog"> select * from blog <where> <foreach collection="ids" item="id" open="and (" close=")" separator="or"> id=#{id} </foreach> </where> </select> </mapper>
拼接完成的sql
select * from blog WHERE ( id=? or id=? or id=? )
原文:https://www.cnblogs.com/wuyimin/p/14860825.html