大家好;
经过两天的练手,相信大家开始了解SpringMVC了吧!下面开始写我项目的功能了,其中也涉及到了一些问题,给大家共享一下!
1) 首先当然是要在benmu-core/src/main/resources/mybatisGenerate/mybatorConfig.xml 做配置了
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- classPathEntry:数据库的JDBC驱动的jar包地址 -->
<classPathEntry location="D:\jar\mysql.jar" />
<context id="benmu" targetRuntime="MyBatis3"
defaultModelType="conditional">
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://117.121.26.70:3305/benmu" userId="kaifa"
password="99754106633f94d350db34d548d6091a">
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer
true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal
-->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- targetProject:自动生成代码的位置 -->
<javaModelGenerator targetPackage="<span style="color:#CC0000;">com.benmu.medical.system.weixin.entity</span>"
targetProject="benmu-core/src/main/java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="true" />
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<sqlMapGenerator targetPackage="<span style="color:#CC0000;">com.benmu.medical.system.weixin.mapper</span>"
targetProject="benmu-core/src/main/java">
<property name="enableSubPackages" value="false" />
</sqlMapGenerator>
<!-- 生成dao -->
<javaClientGenerator targetPackage="<span style="color:#FF0000;">com.benmu.medical.system.weixin.mapper</span>"
targetProject="benmu-core/src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 数据库表跟实体类的对应 -->
<table tableName="<span style="color:#FF0000;">t_hosp_dept</span>" domainObjectName="<span style="color:#FF0000;">WxHospDept</span>"
enableInsert="true" enableSelectByPrimaryKey="true"
enableUpdateByPrimaryKey="true" enableDeleteByPrimaryKey="true"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="true" >
</table>
</context>
</generatorConfiguration>
这里需要注意的就是生成的XML了,因为我使用到了一个判断查询
<strong><select id="getWxHospDeptParentList" resultMap="<span style="color:#FF9900;">BaseResultMap</span>" parameterType="<span style="color:#FF6600;">com.benmu.medical.system.weixin.entity.WxHospDept</span>" >
select
<include refid="Base_Column_List" />
from t_hosp_dept <span style="color:#FF0000;">where</span> 1=1
<span style="color:#CC0000;"> <choose> </span>
<span style="color:#FF0000;"><when test="pid!=null and pid!=''"> </span>
and pid = #{pid,jdbcType=VARCHAR}
<span style="color:#FF0000;"></when>
<otherwise></span>
and pid = '0'
<span style="color:#CC0000;"></otherwise>
</choose></span>
and hosp_id = #{hospId,jdbcType=VARCHAR}
</select></strong>
2)service
package com.benmu.medical.system.weixin.service;
import java.util.List;
import com.benmu.medical.system.weixin.entity.WxHospDept;
public interface WxHospDeptService {
/*
* 查询科室预览 一级菜单
*/
public List<WxHospDept> getWxHospDeptParentList(WxHospDept wxHospDept);
}
3)serviceIMPL
package com.benmu.medical.system.weixin.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.benmu.medical.system.weixin.entity.WxHospDept;
import com.benmu.medical.system.weixin.mapper.WxHospDeptMapper;
import com.benmu.medical.system.weixin.service.WxHospDeptService;
@Service
public class WxHospDeptServiceImpl implements WxHospDeptService {
@Autowired
private WxHospDeptMapper wxHospDeptMapper;
/*
* 查询科室预览 一级菜单
*/
@Override
public List<WxHospDept> getWxHospDeptParentList(WxHospDept wxHospDept){
return wxHospDeptMapper.getWxHospDeptParentList(wxHospDept);
}
}
4)Mapper
package com.benmu.medical.system.weixin.mapper;
import java.util.List;
import com.benmu.medical.system.weixin.entity.WxHospDept;
public interface WxHospDeptMapper {
/*
* 查询科室预览 一级菜单
*/
public List<WxHospDept> getWxHospDeptParentList(WxHospDept wxHospDept);
}
5)Controller
package com.benmu.medical.system.weixin.web;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.benmu.basic.mybatis.PageInfo;
import com.benmu.medical.system.weixin.entity.WxHospDept;
import com.benmu.medical.system.weixin.service.WxHospDeptService;
@Controller
public class WxHospDeptController {
@Autowired
private WxHospDeptService wxHospDeptService;
/*
* 科室预览页面
*/
private final static String WX_HOSP_DEPT = "/system/hoospdept/WxHospDept";
/**
*查询科室预览 一级菜单 (页面跳转)
*/
@RequestMapping(value = "/pageJump", method = RequestMethod.GET)
public String pageJump(
HttpServletRequest request,
@RequestParam(value = "pid", required = true) String pid,
@RequestParam(value = "hospId", required = true) String hospId
) throws Exception {
WxHospDept wxHospDept = new WxHospDept();
wxHospDept.setPid(pid);
wxHospDept.setHospId(hospId);
List<WxHospDept> entity =wxHospDeptService.getWxHospDeptParentList(wxHospDept);
request.setAttribute("entity",entity);
return WX_HOSP_DEPT;
}
/*
* 查询科室预览 二级菜单
*/
@RequestMapping(value = "/wx/wxHospDeptController", method = RequestMethod.POST)
@ResponseBody
public Object wxHospDeptController(
PageInfo<WxHospDept> pageInfo,
@RequestParam(value = "pid", required = true) String pid,
@RequestParam(value = "hospId", required = true) String hospId
)
throws Exception {
WxHospDept wxHospDept = new WxHospDept();
wxHospDept.setPid(pid);
wxHospDept.setHospId(hospId);
List<WxHospDept> entity =wxHospDeptService.getWxHospDeptParentList(wxHospDept);
Map<String,Object> modelMap =new HashMap<String,Object>();
modelMap.put("entity",entity);
return modelMap;
}
}
6)JSP
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/view/common/tags.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>选择科室列表</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<%@ include file="/view/common/resource.jsp"%>
<script language="javascript" type="text/javascript" src="<%=request.getContextPath()%>/resources/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/resources/ztree/js/jquery.ztree.core-3.5.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/resources/ztree/js/jquery.ztree.excheck-3.5.js"></script>
<link rel="stylesheet" href="<%=request.getContextPath()%>/resources/ztree/css/zTreeStyle/zTreeStyle_3.5.css" type="text/css"/>
</head>
<body>
<div>
<input type="hidden" id="pName" name="PName" value=""/>
</div>
<!-- 一级目录 -->
<table>
<c:forEach items="${entity}" var="entity">
<tr>
<td>
<input type="hidden" id="fdid" name="fdid" value="${entity.fdid}"/>
<input type="hidden" id="pid" name="pid" value="${entity.pid}"/>
<input type="hidden" id="hospId" name="hospId" value="${entity.hospId}"/>
<input type="text" id="name" name="name" value="${entity.name}" οnclick="show(${entity.fdid},${entity.hospId},this.value)"/>
</td>
</tr>
</c:forEach>
</table>
<!--二级目录 方法实现 -->
<script type="text/javascript">
function show(fdid,hospId,name){
var name =name;
var pid = fdid; //obj 当前科室的fdID作二级目录的Pid
var hospId = hospId;
$("#pName").val(name);
$.post("/benmu/wx/wxHospDeptController",
{
"pid" : pid,
"hospId" : hospId
},
function(data){
//拼接Html
if(data.entity!=null){
//var splitHtml = "<table> ";
var pName = $("#pName").val();
var splitHtml = "";
for(var i = 0; i<data.entity.length;i++ ){
splitHtml += pName+"--"+data.entity[i].name;
splitHtml +=" ";
/* splitHtml+="<input type='hidden' id='fdid' name='fdid' value="+data.entity[i].fdid+"/>";
splitHtml+="<input type='hidden' id='pid' name='pid' value="+data.entity[i].pid+"/>";
splitHtml+="<input type='hidden' id='hospId' name='hospId' value="+data.entity[i].hospId+"/>";
splitHtml+="<input type='text' id='name' name='name' value="+data.entity[i].name+"/>";
splitHtml+="</td></tr>"; */
}
//splitHtml+="</table>";
alert(splitHtml);
}
});
}
</script>
</body>
</html>
注意:一定要注意路径问题!