9.动态mysql

@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=? ) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值