代码生成器(4)

上节课我们完成了90%的代码,这节课我们将此部分完全完成。


主要任务:添加Category表,完成jsp.vm


  1. 创建jsp.vm
    ①修改,添加foreach
<!-- 列表 -->
		<div id="right">
			<button type="button" class="btn btn-primary" onclick="showAddDiv()">新增</button>
			<table class="table table-bordered">
				<thead>
					<tr>
#foreach(${e} in ${columnList})
						<th>${e.columnComment}</th>
#end
						<th>删除</th>
						<th>编辑</th>
					</tr>
				</thead>
				<tbody>

②新增,添加froeach

<div class="box">
		<div id="loginDiv">
			<p class="logTitle">新增</p>
			<div>
				<form id="saveForm">
					<input type="hidden" name="id" id="id" value="0" />
#foreach(${e} in ${columnList})
#if(${e.attrName}!='id')
					<div class="formInput">
						<span>${e.columnComment}:</span> 
						<input type="text" name="${e.attrName}" id="${e.attrName}" />
					</div>	
#end
#end
					<input class="formButton" type="button" value="保存"
						onClick="create()" />
				</form>
			</div>
		</div>
	</div>	

③故我们的jsp.vm全部代码如下

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!-- 引入jstl自定义的c标签 -->
<!-- 自定义标签的一个用法,设置一个变量 格式:c:setc:if   c:foreach -->
<c:set var="ctx" value="#literal()${#end pageContext.request.contextPath}" /> 

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>用户列表</title>
<link href="#literal()${#end ctx }/css/bootstrap.min.css" type="text/css" rel="stylesheet" />
<link href="#literal()${#end ctx }/css/bootstrap-theme.min.css" type="text/css" rel="stylesheet" />
<script src="#literal()${#end ctx }/js/jquery.min.js" type="text/javascript"></script>
<script src="#literal()${#end ctx }/js/bootstrap.min.js" type="text/javascript"></script> 
<script src="#literal()${#end ctx }/js/bootstrap-paginator.min.js"></script>
<!-- 引入自己的css -->
<link href="#literal()${#end ctx }/css/public.css" type="text/css" rel="stylesheet" />
<script type="text/javascript">
function showAddDiv(){
	$(".box").show();//显示div
	//document.getElementById("divId").style.display="block";
}

function edit(id){
	$.ajax({
		type:"get",
		url:"#literal()${#end ctx}/${modelName}/findById.do",
		data:{"id":id},
		success:function(data){
#foreach(${e} in ${columnList})
			$("#${e.attrName}").val(data.${e.attrName});
#end
			$(".box").show();
		}
	});
}


function deleteUser(id){
	if(confirm("您确定要删除吗?")){
		$.post("#literal()${#end ctx}/${modelName}/delete.do",{"id":id},
				function(data){
			if(data){
				alert("删除成功");
				window.location.reload();
			}else{
				alert("删除失敗");
			}	
		})
	}
}

//保存
function create(){
	alert("新增提交的数据:"+$("#saveForm").serialize());
	$.post("#literal()${#end ctx}/${modelName}/create.do",
			$("#saveForm").serialize(),function(data){//form表单参数可以直接通过这种提交
				if(data){
					alert("保存成功");
					window.location.reload();
				}else{
					alert("失敗");
				}		
	});
}

</script>
</head>
<body>
	<!-- 划分区域 div -->
	<div class="banner"><!-- 上面那个图片部分 -->
		<div class="title">
			欢迎,<span id="user">赵琦</span><a href="#">退出</a>
		</div>
	</div>
	
	<!-- 下面主体部分 -->
	<div id="main">
		<div id="left">
			<ul>
				<c:forEach items="#literal()${#end menuList }" var="row">
					<li>
						<img class="icon1" src="#literal()${#end ctx }/#literal()${#end row.icon}" />
						<a href="#literal()${#end ctx }#literal()${#end row.url}">#literal()${#end row.name }</a>
					</li>
				</c:forEach>
				
				<%-- <li>
					<img class="icon1" src="#literal()${#end ctx }/img/icon02.png" />
					<a href="#">修改密码</a>
				</li> --%>
			</ul>
		</div>
		<!-- 列表 -->
		<div id="right">
			<button type="button" class="btn btn-primary" onclick="showAddDiv()">新增</button>
			<table class="table table-bordered">
				<thead>
					<tr>
#foreach(${e} in ${columnList})
						<th>${e.columnComment}</th>
#end
						<th>删除</th>
						<th>编辑</th>
					</tr>
				</thead>
				<tbody>
					<!-- EL表达式-翻课本 -->
					<!-- List<User> row.id 这个id就是后台传来的User里面的属性名  -->
					<c:forEach items="#literal()${#end pageInfo.list }" var="row">
						<tr>
#foreach(${e} in ${columnList})
							<td>#literal()${#end row.${e.attrName} }</td>
#end
							<td><a href="#" class="btn btn-primary" onclick="deleteUser(#literal()${#end row.id})">删除</a></td>
							<td><a href="#" class="btn btn-primary" onclick="edit(#literal()${#end row.id})">编辑</a></td>
						</tr>
					</c:forEach>
				</tbody>
			</table>
			
			<div style="text-align:center;"> 
				<ul id="pageLimit"></ul> 
			</div>
			<script>		      
			    $('#pageLimit').bootstrapPaginator({
				    currentPage: "#literal()${#end pageInfo.pageNum }",//当前的请求页面。
				    totalPages: "#literal()${#end pageInfo.pages }",//一共多少页。
				    size:"normal",//应该是页码打下 :默认正常noramal  small larger
				    bootstrapMajorVersion: 3,//bootstrap的版本要求。
				    numberOfPages:"#literal()${#end pageInfo.pageSize }",//一页列出多少数据。
				    itemTexts: function (type, page, current) {//如下的代码是将页眉显示的中文显示我们自定义的中文。
				        switch (type) {
					        case "first": return "首页";
					        case "prev": return "上一页";
					        case "next": return "下一页";
					        case "last": return "末页";
					        case "page": return page;
				        }
				    },
				    onPageClicked:function(event,originalEvent,type,page){//分页按钮点击事件
			           // var zhanghao = $("#searchrole").val();   //这是搜索时的参数,根据实际情况加或者不要
			            var url = '#literal()${#end ctx}/${modelName}/list.do?pageNum=' + page;
			            //如果查询条件不为空,
			            //url = url + '&username=' + 'zq';
			            window.location.href = encodeURI(url);
                    }
				});
			</script> 
		</div>
	</div>
	
	<div class="box">
		<div id="loginDiv">
			<p class="logTitle">新增</p>
			<div>
				<form id="saveForm">
					<input type="hidden" name="id" id="id" value="0" />
#foreach(${e} in ${columnList})
#if(${e.attrName}!='id')
					<div class="formInput">
						<span>${e.columnComment}:</span> 
						<input type="text" name="${e.attrName}" id="${e.attrName}" />
					</div>	
#end
#end
					<input class="formButton" type="button" value="保存"
						onClick="create()" />
				</form>
			</div>
		</div>
	</div>	
</body>
</html>
  1. CodeBuilder.java添加jsp的vm
		// jsp的
		Template jspVm = ve.getTemplate("/WebContent/WEB-INF/vm/jsp.vm");
		CodeBuilder.merge(jspVm, ctx, rootPath +"WebContent/jsp/" + modelName +".jsp");
		System.out.println("成功");
  1. 创建Category表
    在这里插入图片描述
    在这里插入图片描述
    ①CategoryController.java——controller层
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;
import com.lf.model.MenuCondition;
import com.lf.service.MenuService;
import com.lf.model.CategoryCondition;
import com.lf.service.CategoryService;

@RequestMapping("/category")
@Controller
public class CategoryController {

	@Autowired
	private CategoryService categoryService;
	@Autowired
	private MenuService menuService;
	
	@ResponseBody
	@RequestMapping("/delete.do")
	public boolean delete(Integer id){
		try{
			categoryService.delete(id);
		}catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
		return true;
	}
	
	@ResponseBody
	@RequestMapping("/findById.do")
	public CategoryCondition findById(Integer id){
		return categoryService.findById(id);
	}
	
	@ResponseBody
	@RequestMapping("/create.do")
	public boolean create(CategoryCondition category){
		try{
			categoryService.create(category);
		}catch(Exception e){
			System.out.println(e.getMessage());
			return false;
		}
		return true;
	}
	
	@RequestMapping("/list.do")
	public String list(CategoryCondition category,Model model,
			@RequestParam(required=true,value="pageNum",defaultValue="1") Integer pageNum,
			@RequestParam(required=true,value="pageSize",defaultValue="3") Integer pageSize
			){
		PageInfo<CategoryCondition> categorys  = categoryService.list(pageNum,pageSize,category);
		model.addAttribute("pageInfo", categorys);
		
		List<MenuCondition> menuList = menuService.list(null);
		model.addAttribute("menuList", menuList);
		return "category";
	}
	
	@ResponseBody
	@RequestMapping("/listData.do")
	public List<CategoryCondition> list(CategoryCondition category,Model model){
		return categoryService.list(category);
	}
}

②CategoryDao.java——dao层

package com.lf.dao;

import java.util.List;
import com.github.pagehelper.PageInfo;
import com.lf.model.CategoryCondition;// MenuCondition

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

③CategoryDao.xml——dao层

<?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.CategoryDao">
	<resultMap type="com.lf.model.CategoryCondition" id="categoryMap">
		<result property="id" column="id"/>
		<result property="parentid" column="parentId"/>
		<result property="catname" column="catName"/>
		<result property="catstatus" column="catStatus"/>
		<result property="sort" column="sort"/>
	</resultMap>

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

	<sql id="sqlWhere">
		<where><!-- 这种写法会自动去掉第一个and -->
			<if test="id!=null">
				and id = #{id}
			</if>
			<if test="parentid!=null">
				and parentId = #{parentid}
			</if>
			<if test="catname!=null and catname!=''">
				and catName = #{catname}
			</if>
			<if test="catstatus!=null">
				and catStatus = #{catstatus}
			</if>
			<if test="sort!=null">
				and sort = #{sort}
			</if>
		</where>
	</sql>
	<!-- 查询列表 -->
	<select id="list" parameterType="CategoryCondition" resultMap="categoryMap">
		select * from category
		<include refid="sqlWhere" />
	</select>

	<!-- id不需要,自增 -->
	<insert id="create" parameterType="CategoryCondition">
		insert into
		category(
	id   , 	parentId   , 	catName   , 	catStatus   , 	sort  		)
		values(
	#{id}  , 	#{parentid}  , 	#{catname}  , 	#{catstatus}  , 	#{sort} 		)
	</insert>

	<update id="update" parameterType="CategoryCondition">
		update category
		<set>
			<if test="id!=null">
				id = #{id},
			</if>
			<if test="parentid!=null">
				parentId = #{parentid},
			</if>
			<if test="catname!=null and catname!=''">
				catName = #{catname},
			</if>
			<if test="catstatus!=null">
				catStatus = #{catstatus},
			</if>
			<if test="sort!=null">
				sort = #{sort},
			</if>
		</set>
		where id = #{id}
	</update>

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

④Category.java——model层

package com.lf.model;

public class Category {
	private Integer id;// ID
	private Integer parentid;// 父类ID
	private String catname;// 类别名称
	private Integer catstatus;// 分类状态
	private Integer sort;// 排序
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public Integer getParentid() {
		return parentid;
	}
	public void setParentid(Integer parentid) {
		this.parentid = parentid;
	}
	public String getCatname() {
		return catname;
	}
	public void setCatname(String catname) {
		this.catname = catname;
	}
	public Integer getCatstatus() {
		return catstatus;
	}
	public void setCatstatus(Integer catstatus) {
		this.catstatus = catstatus;
	}
	public Integer getSort() {
		return sort;
	}
	public void setSort(Integer sort) {
		this.sort = sort;
	}

}

⑤CategoryCondition.java——model层

package com.lf.model;

public class CategoryCondition extends Category {
	
}

⑥CategoryService.java——service层

package com.lf.service;

import java.util.List;
import com.github.pagehelper.PageInfo;
import com.lf.model.CategoryCondition;

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

⑦CategoryServiceImpl.java——service层

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.CategoryDao;
import com.lf.model.CategoryCondition;

@Service
public class CategoryServiceImpl implements CategoryService {
	
	@Autowired
	private CategoryDao categoryDao;

	@Override
	public PageInfo<CategoryCondition> list(Integer pageNum,Integer pageSize,CategoryCondition category) {
		PageHelper.startPage(pageNum, pageSize);
		List<CategoryCondition> list =  categoryDao.list(category);
		PageInfo<CategoryCondition> pageInfo = new PageInfo<CategoryCondition>(list);
		
		return pageInfo;
	}
	
	@Override
	public List<CategoryCondition> list(CategoryCondition category) {
		List<CategoryCondition> list =  categoryDao.list(category);
		return list;
	}

	@Override
	public void create(CategoryCondition category) {
		if(category.getId()!=null && category.getId()!=0){
			categoryDao.update(category);
		}else{
			categoryDao.create(category);
		}
	}

	@Override
	public void update(CategoryCondition category) {
		categoryDao.update(category);
	}

	@Override
	public void delete(Integer id) {
		categoryDao.delete(id);
	}

	@Override
	public CategoryCondition findById(Integer id) {
		return categoryDao.findById(id);
	}
}

运行 Run As——Java Application,可以将category表的相关代码自动生成。

最后我们Run As——Run On Server,在地址栏输入

http://localhost:8080/shop/user/list.do

运行结果如图:在这里插入图片描述
到此我们的代码生成器就写完了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值