代码生成器(3)

本文详细介绍了一个代码生成器的实现过程,包括modelCondition.vm、xml.vm等模板的创建与使用,以及如何通过Velocity模板引擎自动生成Java代码,涵盖Model、DAO、Service、Controller等层的代码生成,特别聚焦于Menu表的代码生成。

上节课完成了45%的代码生成器,接下来我们来写关于Menu表其他代码


主要任务:完成modelCondition.vm,xml.vm


  1. 创建modelCondition.vm
    ① 在model层创建UserCondition.java并且要继承User类
    在这里插入图片描述
package com.lf.model;

public class UserCondition extends User {
	
}

② 在vm下建立modelCondition.vm

package com.lf.model;

public class ${model}Condition extends ${model} {
	
}

③ 在code层中的CodeBuilder.java中添加关于modelCondition的代码

		// modelCondition的
		Template modelConditionVm = ve.getTemplate("/WebContent/WEB-INF/vm/modelCondition.vm");
		CodeBuilder.merge(modelConditionVm, ctx, rootPath +"src/com/lf/model/" + model +"Condition.java");
  1. 添加字段,修改
    ①将CodeBuilder中的modelClass改为model,其相应的值为
		String modelClass = "MenuCondition";
		String model = "Menu";// Menu Controller  Menu Service
		String modelName = "menu";
		VelocityContext ctx = new VelocityContext();
		ctx.put("modelClass", modelClass);
		ctx.put("modelName", modelName);
		ctx.put("model", model);

故此时的CodeBuilder代码为

package com.lf.code;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import com.google.common.base.CaseFormat;

public class CodeBuilder {

	public static void main(String[] args) {
		// new一下他的模版引擎
		VelocityEngine ve = new VelocityEngine();
		// 设置模版和输出的代码文件的编码方式
		Properties p = new Properties();
		p.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
		p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
		p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
		ve.init(p);// 引擎初始化
		
		// 引入一个模版,通过模版路径
		Template serviceVm = ve.getTemplate("/WebContent/WEB-INF/vm/service.vm");
		
		// 定义占位符变量,给个值
		String modelClass = "MenuCondition";
		String model = "Menu";// Menu Controller  Menu Service
		String modelName = "menu";
		// 生成的代码放置的目录==项目目录
		String rootPath = "E:/EClass/shop/"; 
		
		// 变量放到上下文对象里
		VelocityContext ctx = new VelocityContext();
		ctx.put("modelClass", modelClass);
		ctx.put("modelName", modelName);
		ctx.put("model", model);
				
		// 将占位符数据和模版合并,输出代码文件
		CodeBuilder.merge(serviceVm, ctx, rootPath +"src/com/lf/service/" + model +"Service.java");
		
		// service实现类的
		Template serviceImplVm = ve.getTemplate("/WebContent/WEB-INF/vm/serviceImpl.vm");
		CodeBuilder.merge(serviceImplVm, ctx, rootPath +"src/com/lf/service/" + model +"ServiceImpl.java");
		
		// dao的
		Template daoVm = ve.getTemplate("/WebContent/WEB-INF/vm/dao.vm");
		CodeBuilder.merge(daoVm, ctx, rootPath +"src/com/lf/dao/" + model +"Dao.java");
		
		// controller的
		Template controllerVm = ve.getTemplate("/WebContent/WEB-INF/vm/controller.vm");
		CodeBuilder.merge(controllerVm, ctx, rootPath +"src/com/lf/controller/" + model +"Controller.java");
		
		// model的
		// 从数据库查询表的字段:字段名称,字段类型,字段注释 good_name goodName setGoodName
		List<CodeBean> list = selectColumn(modelName);
		ctx.put("columnList", list);
		Template modelVm = ve.getTemplate("/WebContent/WEB-INF/vm/model.vm");
		CodeBuilder.merge(modelVm, ctx, rootPath +"src/com/lf/model/" + model +"Condition.java");
		
		// modelCondition的
		Template modelConditionVm = ve.getTemplate("/WebContent/WEB-INF/vm/modelCondition.vm");
		CodeBuilder.merge(modelConditionVm, ctx, rootPath +"src/com/lf/model/" + model +"Condition.java");	
			
		System.out.println("成功");
	}
	
	// jdbc连接数据库
	private static List<CodeBean> selectColumn(String modelName) {
		List<CodeBean> list = new ArrayList<CodeBean>();
		// java.sql这个包里的类
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			// 注册数据库驱动
			Class.forName("com.mysql.jdbc.Driver");
			// 创建数据库
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "");
			// sql语句
			String sql = "SELECT t.COLUMN_NAME,t.DATA_TYPE,t.COLUMN_COMMENT "
					+ " FROM information_schema.COLUMNS t "
					+ " WHERE t.TABLE_NAME = '"+modelName+"' "
					+ " AND t.TABLE_SCHEMA = 'shop' ";
			// 执行sql语句
			ps = conn.prepareStatement(sql);
			// 获取查询结果
			rs = ps.executeQuery();
			// 循环遍历查询结果,塞到columnList里面
			while(rs.next()){
				CodeBean bean = new CodeBean();
				bean.setColumnName(rs.getString(1));
				bean.setColumnComment(rs.getString(3));
				//类型 
				String type = rs.getString(2);
				if(type.equalsIgnoreCase("varchar") || type.equalsIgnoreCase("varchar2")){//字符串
					bean.setColumnType("String");
				}else if(type.equalsIgnoreCase("int") || type.equalsIgnoreCase("integer")){//整数
					bean.setColumnType("Integer");
				}else if(type.equalsIgnoreCase("float")
						|| type.equalsIgnoreCase("double")
						|| type.equalsIgnoreCase("decimal")){//小数:float double decimal
					bean.setColumnType("Double");
				}else{
					bean.setColumnType("String");
				}
				
				//good_name goodName GoodName
				bean.setAttrName(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, bean.getColumnName()));
				bean.setUperName(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, bean.getColumnName()));
				
				list.add(bean);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				ps.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
		}
		
		return null;
	}

	private static void merge(Template template,VelocityContext ctx,String path){
		PrintWriter writer = null;
		try{
			writer = new PrintWriter(path);
			//合并数据和模版,输出文件
			template.merge(ctx, writer);
			writer.flush();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			writer.close();
		}
	}
}

②controller.vm为

package com.lf.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageInfo;

#if(${modelName}=='menu')
import com.lf.model.${modelClass};//MenuCondition
import com.lf.service.${model}Service;//Menu
#else
import com.lf.model.${modelClass};
import com.lf.service.${model}Service;
import com.lf.model.Menu;
import com.lf.service.MenuService;
#end

@RequestMapping("/${modelName}")
@Controller
public class ${model}Controller {
	
#if(${modelName}=='menu')
	@Autowired
	private ${model}Service ${modelName}Service;
#else
	@Autowired
	private ${model}Service ${modelName}Service;
	@Autowired
	private MenuService menuService;
#end
	
	
	
	@ResponseBody
	@RequestMapping("/delete.do")
	public boolean delete(Integer id){
		try{
			${modelName}Service.delete(id);
		}catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
		return true;
	}
	
	@ResponseBody
	@RequestMapping("/findById.do")
	public ${modelClass} findById(Integer id){
		return ${modelName}Service.findById(id);
	}
	
	@ResponseBody
	@RequestMapping("/create.do")
	public boolean create(${modelClass} ${modelName}){
		try{
			${modelName}Service.create(${modelName});
		}catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
		return true;
	}
	
	@RequestMapping("/list.do")
	public String list(${modelClass} ${modelName},Model model,
			@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum,
			@RequestParam(required=true,value="pageSize",defaultValue="3") Integer pageSize
			){
		PageInfo<${modelClass}> ${modelName}s  = ${modelName}Service.list(pageNum,pageSize,${modelName});
		model.addAttribute("pageInfo", ${modelName}s);
		
		List<MenuCondition> menuList = menuService.list(null);
		model.addAttribute("menuList",menuList);
		
		return "${modelName}";
	}
	
	@ResponseBody
	@RequestMapping("/list.do")
	public List<${modelClass}> list(${modelClass} ${modelName},Model model){
		return ${modelName}Service.list(${modelName});
	}
}

③dao.vm

package com.lf.dao;

import java.util.List;
import com.github.pagehelper.PageInfo;
import com.lf.model.${modelClass};

public interface ${model}Dao {
	/**
	 * 没有分页的列表查询
	 * @param pageNum
	 * @param pageSize
	 * @param ${modelClass}
	 * @return
	 */
	public List<${modelClass}> list(${modelClass} ${modelName});
	public void create(${modelClass} ${modelName});
	public void update(${modelClass} ${modelName});
	public void delete(Integer id);
	public ${modelClass} findById(Integer id);
}

④service.vm


package com.lf.service;

import java.util.List;
import com.github.pagehelper.PageInfo;
import com.lf.model.${modelClass};//MenuCondition

public interface ${model}Service {
	/**
	 * 带有分页的列表查询
	 * @param pageNum
	 * @param pageSize
	 * @param ${modelClass}
	 * @return
	 */
	public PageInfo<${modelClass}> list(Integer pageNum,Integer pageSize,${modelClass} ${modelName});
	
	/**
	 * 没有分页的列表查询
	 * @param pageNum
	 * @param pageSize
	 * @param ${modelClass}
	 * @return
	 */
	public List<${modelClass}> list(${modelClass} ${modelName});
	public void create(${modelClass} ${modelName});
	public void update(${modelClass} ${modelName});
	public void delete(Integer id);
	public ${modelClass} findById(Integer id);
}

⑤serviceImpl.vm

package com.lf.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lf.dao.${model}Dao;
import com.lf.model.${modelClass};

@Service
public class ${model}ServiceImpl implements ${model}Service {
	
	@Autowired
	private ${model}Dao ${modelName}Dao;

	@Override
	public PageInfo<${modelClass}> list(Integer pageNum,Integer pageSize,${modelClass} ${modelName}) {
		PageHelper.startPage(pageNum, pageSize);
		List<${modelClass}> list =  ${modelName}Dao.list(${modelName});
		PageInfo<${modelClass}> pageInfo = new PageInfo<${modelClass}>(list);
		
		return pageInfo;
	}
	
	@Override
	public List<${modelClass}> list(${modelClass} ${modelName}) {
		List<${modelClass}> list =  ${modelName}Dao.list(${modelName});
		return list;
	}

	@Override
	public void create(${modelClass} ${modelName}) {
		if(${modelName}.getId()!=null && ${modelName}.getId()!=0){
			${modelName}Dao.update(${modelName});
		}else{
			${modelName}Dao.create(${modelName});
		}
	}

	@Override
	public void update(${modelClass} ${modelName}) {
		${modelName}Dao.update(${modelName});
	}

	@Override
	public void delete(Integer id) {
		${modelName}Dao.delete(id);
	}

	@Override
	public ${modelClass} findById(Integer id) {
		return ${modelName}Dao.findById(id);
	}
}

OK,修改完后,Run As一下。

  1. 写xml.vm
    ① 在vm下建立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.lf.dao.${model}Dao">
	<resultMap type="com.lf.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" />
	</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}
	</update>

	<delete id="delete" parameterType="integer">
		delete from ${modelName} where id =	#{id}
	</delete>
</mapper>

②在code层中的CodeBuilder.java中添加关于xml的代码

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

则在dao层下生成的MenuDao.xml:

<?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.lf.dao.MenuDao">
	<resultMap type="com.lf.model.MenuCondition" id="menuMap">
		<result property="id" column="id"/>
		<result property="name" column="name"/>
		<result property="icon" column="icon"/>
		<result property="sort" column="sort"/>
		<result property="url" column="url"/>
	</resultMap>

	<select id="findById" parameterType="integer" resultMap="menuMap">
		select * from menu where id = #{id}
	</select>

	<sql id="sqlWhere">
		<where><!-- 这种写法会自动去掉第一个and -->
			<if test="id!=null">
				and id = #{id}
			</if>
			<if test="name!=null and name!=''">
				and name = #{name}
			</if>
			<if test="icon!=null and icon!=''">
				and icon = #{icon}
			</if>
			<if test="sort!=null">
				and sort = #{sort}
			</if>
			<if test="url!=null and url!=''">
				and url = #{url}
			</if>
		</where>
	</sql>
	<!-- 查询列表 -->
	<select id="list" parameterType="MenuCondition" resultMap="menuMap">
		select * from menu
		<include refid="sqlWhere" />
	</select>

	<!-- id不需要,自增 -->
	<insert id="create" parameterType="MenuCondition">
		insert into
		menu(
	id   , 	name   , 	icon   , 	sort   , 	url  		)
		values(
	#{id}  , 	#{name}  , 	#{icon}  , 	#{sort}  , 	#{url} 		)
	</insert>

	<update id="update" parameterType="MenuCondition">
		update menu
		<set>
			<if test="id!=null">
				id = #{id},
			</if>
			<if test="name!=null and name!=''">
				name = #{name},
			</if>
			<if test="icon!=null and icon!=''">
				icon = #{icon},
			</if>
			<if test="sort!=null">
				sort = #{sort},
			</if>
			<if test="url!=null and url!=''">
				url = #{url},
			</if>
		</set>
		where id = #{id}
	</update>

	<delete id="delete" parameterType="integer">
		delete from menu where id =	#{id}
	</delete>
</mapper>

综上,现在我们相当于完成了90%的代码,我们下节课将jsp.vm完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值