SpringBoot+mybatis+mysql自学笔记

mybatis-plugin-generator

mybatis反向生成代码,xml文件中配置generator依赖

    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.6</version>
首先domainObjectRenamingRule是在1.3.6之后的版本才有的属性,下列配置的功能为,生成表实体,去掉表名的前缀ds_,其中_可以省略不写,表名的第一个字母要大写,domainObjectRenamingRule的重名发生在生成了默认实体名之后。
        <!--生成对应表及类名-->
        <table tableName="%">
            <generatedKey column="id" sqlStatement="Mysql" identity="true"/>
            <domainObjectRenamingRule searchString="^Ds" replaceString="" />
            <!--<columnRenamingRule searchString="^[^_]+" replaceString=""/>-->
        </table>

mybatis-映射文件xml-sql语句

deleteList是一个Map的集合:
批量删除

<delete id="batchDelPrice" parameterType="java.util.ArrayList">
     <foreach collection="deleteList" item="deleteItem" separator=";">
        delete from xxx
        where user_code = #{deleteItem.userCode} 
        and product_id in 
        <foreach collection="deleteItem.delete" item="item" index="index" open="("             close=")" separator=",">
          #{item}
        </foreach>
    </foreach>
</delete>


批量添加

<insert id="batchAddPrice" parameterType="java.util.ArrayList">
    insert into xxx(
    user_code,product_id,price,efftime,index_num,pubtime
    )values
       <foreach collection="addList" item="addItem" separator="," >
        <foreach collection="addItem.priceList" item="priceItem" index="priceIndex" separator=",">
           (
             #{addItem.userCode},#{priceItem.priceId},#{priceItem.defPrice},now(),1,now()
           )
        </foreach>
       </foreach>
</insert>


当参数为List,去掉外层foreach嵌套,为单循环

@Param("addList")指定参数存入map的key

普通的查询:
//resultType指定返回值类型,当返回值类型为List时,resultType为List内存储数据的类型,当返回值类型为Map时,返回值为map

<select id="queryProductDefPrice" resultType="java.util.HashMap">
    select product_id,default_price from xxx
</select>

mybatis <if>标签判断==

外层使用单引号,判断的值用双引号包围

类似 <if test='a != null and a == "0" '></if>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值