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>