电商项目开发5----代码生成器(3)

model包下 添加 UserCondition.java  继承User.java

package com.rb.model;

public class UserCondition extends User {

}

 更改 (CodeController.java)

        String modelClass = "MenuCondition";
        String model = "Menu";//Menu Controller MenuService
        String modelName = "menu";
     
        VelocityContext ctx = new VelocityContext();
        ctx.put("modelClass", modelClass);
        ctx.put("modelName", modelName);
        ctx.put("model", model);

 将 vm 文件中 类名${modelClass} 改为 ${model}

添加 xml.vm

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--命名空间,xml文件和dao接口对应起来 -->
<mapper namespace="com.rb.dao.${model}Dao">
    
    <resultMap type="com.rb.model.${modelClass}" id="${modelName}Map">
#foreach(${e} in ${columnList})
    <result property="${e.attrName}" column="${e.columnName}"/>
#end
    </resultMap>
    
    <select id="findById" parameterType="integer" resultMap="${modelName}Map">
       select * from ${modelName} where id = #{id} 
    </select>

	<sql id="sqlWhere">
	     <where><!--这样写法会自动去掉第一个and  -->
#foreach(${e} in ${columnList})
#if(${e.columnType}=="String")
         <if test="${e.attrName}!=null and ${e.attrName}!=''">
	           and ${e.columnName} = #{${e.attrName}}
	     </if>   
#else
         <if test="${e.attrName}!=null">
	           and ${e.columnName} = #{${e.attrName}}
	     </if> 
#end
#end
	       
	     </where>
	</sql>
	<!--查询列表  -->
	<select id="list" parameterType="${model}Condition" resultMap="${modelName}Map">
	     select * from ${modelName}
	     <include refid="sqlWhere"></include> 
	 
	</select>
	<!--id不需要,自增  -->
	<insert id="create" parameterType="${model}Condition">
	    insert into
	    ${modelName}(
#foreach(${e} in ${columnList})
    ${e.columnName} #if(${foreach.hasNext}) , #end   
#end
	    )
	    values(
#foreach(${e} in ${columnList})
    #{${e.attrName}} #if(${foreach.hasNext}) , #end      
#end
	    )
	</insert>
	<!--  -->
	<update id="update" parameterType="${model}Condition">
	    update ${modelName} 
	    <set>
#foreach(${e} in ${columnList})
#if(${e.columnType}=="String")
         <if test="${e.attrName}!=null and ${e.attrName}!=''">
	           ${e.columnName} = #{${e.attrName}},
	     </if>   
#else
         <if test="${e.attrName}!=null">
	            ${e.columnName} = #{${e.attrName}},
	     </if> 
#end
#end  
	    </set>
	    where id = #{id}
	    <!-- set 
	    username = #{username},pwd = #{pwd}
	    where id = #{id} -->
	</update>
	<delete id="delete" parameterType="integer">
	    delete from ${modelName} where id = #{id}
	</delete>
	
	<!--批量操作  -->
	<select id="updateBatch" parameterType="list">
	    update user set pwd = '123' where id in
	    <foreach item="item" index="index" collection="list" open="("
	        separator="," close=")">
	        #{item}
	    </foreach>
	</select>
	
	<select id="findByTotal" parameterType="list" resultType="user">
	    select * from ${modelName}
	    <where>
	      <foreach close=" ) " separator=" ) or ( " open=" ( " collection="list" index="index" item="item">
               grade = #{item.grade} - 1 and total >= #{item.total}

          </foreach>
	    </where>
  
	</select>
	
	
	<update id="updateGrade" parameterType="list">
	     update user set grade = grade + 1 where id in
	    <foreach item="item" index="index" collection="list" open="("
	        separator="," close=")">
	        #{item.id}
	    </foreach>
	</update>
	
</mapper>

 CodeBuilder.java 加入如下代码:

  Template xmlVm= ve.getTemplate("/WebContent/WEB-INF/vm/xml.vm");
  merge(xmlVm,ctx,rootPath + "/src/com/rb/dao/" + model + "Dao.xml");

运行代码 

猜你喜欢

转载自blog.csdn.net/Rziyi/article/details/89924967