三级联动核心

Controller

package com.wls.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.apache.dubbo.config.annotation.Reference;
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 org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wls.pojo.DiQu;
import com.wls.pojo.Doctor;
import com.wls.pojo.ShanChang;
import com.wls.pojo.Vo;
import com.wls.service.DoctorService;


@Controller
public class DoctorController {
  
	@Reference
	DoctorService doctorService;

	@RequestMapping("selects")
	public String selects(Model m,Vo vo,@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="2")Integer pageSize){
		
		PageInfo<Doctor> info = doctorService.selects(vo, pageNum, pageSize);
		m.addAttribute("info", info);
		m.addAttribute("vo", vo);
		return "selects";
	}
	
	@RequestMapping("selectOne")
	@ResponseBody
	public Object selects(Integer zid){
		List<DiQu>  list=doctorService.selectOne(zid);
		return list;
	}
	
	
	@RequestMapping("toAdd")
	public String toAdd(Model m){
		List<ShanChang> sc = doctorService.selectSc();
		m.addAttribute("sc", sc);
		return "add";
	}
	
	
	
	@RequestMapping("add")
	@ResponseBody
	public boolean add(Doctor doc,MultipartFile file){
		
		System.err.println(doc);
		if(file!=null && !file.isEmpty()){
			String path="d:/pic/";
			String oldFilename = file.getOriginalFilename();
			String fileName=UUID.randomUUID()+oldFilename.substring(oldFilename.lastIndexOf("."));
			try {
				file.transferTo(new File(path,fileName));
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			doc.setPic(fileName);
		}
		
		int i = doctorService.add(doc);
		return i>0;
	}
	
	
	@RequestMapping("toUpdate")
	public String toUpdate(Model m,Integer did){
		List<ShanChang> sc = doctorService.selectSc();
		m.addAttribute("sc", sc);
		
		Doctor doc=doctorService.selectByDid(did);
		m.addAttribute("doc", doc);
		System.err.println(doc);
		return "update";
	}
	
	
	
	@RequestMapping("update")
	@ResponseBody
	public boolean update(Doctor doc,MultipartFile file){
		
		System.err.println(doc);
		if(file!=null && !file.isEmpty()){
			String path="d:/pic/";
			String oldFilename = file.getOriginalFilename();
			String fileName=UUID.randomUUID()+oldFilename.substring(oldFilename.lastIndexOf("."));
			try {
				file.transferTo(new File(path,fileName));
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			doc.setPic(fileName);
		}
		
		int i = doctorService.update(doc);
		return i>0;
	}
	
}

Mapper

<?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" >
<mapper namespace="com.wls.dao.DoctorDao">

     <select id="selects" resultMap="doctorMapper">
        SELECT d.*,k.kname,datediff(now(), rutime)as ageWork  FROM `doctor` d   
        LEFT JOIN   keshi k on d.kid = k.kid
         
         <where>
            <if test="dname!=null and dname!=''">
              and d.dname like concat('%',#{dname},'%')
            </if>
            <if test="sex!=null and sex!=''">
              and d.sex =#{sex}
            </if>
            <if test="shengfen!=null and shengfen!=0">
              and d.shengfen =#{shengfen}
            </if>
            <if test="shi!=null and shi!=0">
              and d.shi =#{shi}
            </if>
            <if test="xian!=null and xian!=0">
              and d.xian =#{xian}
            </if>
             <if test="kid!=null and kid!=0">
              and d.kid =#{kid}
            </if>
            <if test="age1!=null ">
              and d.age &gt;=#{age1}
            </if>
            <if test="age2!=null ">
              and d.age &lt;=#{age2}
            </if>
         </where>  
     </select>
     <resultMap type="Doctor" id="doctorMapper">
          <id property="did" column="did"/>
          <result property="dname" column="dname"/>
          <result property="dbianhao" column="dbianhao"/>
          <result property="sex" column="sex"/>
          <result property="createtime" column="createtime"/>
          <result property="kid" column="kid"/>  
          <result property="telephone" column="telephone"/>
          <result property="rutime" column="rutime"/>
          <result property="pic" column="pic"/>
          <result property="age" column="age"/>
          <result property="ageWork" column="ageWork"/>
          <association property="sf" column="shengfen" select="findDiQuByid"></association>
          <association property="si" column="shi" select="findDiQuByid"></association>
          <association property="xia" column="xian" select="findDiQuByid"></association>
          
          <association property="ks" javaType="KeShi">
               <id property="kid" column="kid"/>
         	   <result property="kname" column="kname"/>
          </association>
          <collection property="sc" column="did" select="selectBySid" >
             
          </collection>
     </resultMap>
      <select id="selectBySid" resultType="ShanChang">
           SELECT sc.* from  sss LEFT JOIN shanchang sc  on sss.ssid =  sc.sid where sss.ddid =#{did}
      </select>
      
      <select id="findDiQuByid" resultType="DiQu">
         select * from diqu where id=#{value}
         
          
      </select>
      
      <select id="selectOne" resultType="DiQu">
        select * from diqu where zid=#{zid}
      </select>
      
      <select id="selectSc" resultType="ShanChang">
        select * from shanchang
      </select>
      
      
      <insert id="add" useGeneratedKeys="true" keyProperty="did">
         insert into doctor values(null,#{dname},#{dbianhao},
         #{sex},#{createtime},#{shengfen},#{shi},#{xian},
         #{kid},#{telephone},#{rutime},#{pic},#{age})
      </insert>
      
      <insert id="addSss"> 
          insert into sss values
          <foreach collection="ssids" item="sid" separator=",">
            (#{did},#{sid})
          </foreach>
      </insert>
      
      <select id="selectByDid" resultMap="doctorMapper">
              SELECT * from doctor where did=#{value}
      </select>
      
      <update id="update">
         update doctor set dname=#{dname},dbianhao=#{dbianhao},sex=#{sex},createtime=#{createtime},
         shengfen=#{shengfen},shi=#{shi},xian=#{xian},kid=#{kid},telephone=#{telephone},
         rutime=#{rutime},pic=#{pic},age=#{age} where did =#{did}
      </update>
      
      <delete id="delSss">
          delete from sss where ddid=#{did}
      </delete>
</mapper>

前台列表页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="/resource/index3.css" rel="stylesheet">\
<script type="text/javascript" src="/resource/jquery-3.2.1.js"></script>
<script type="text/javascript">
   function goPage(pageNum){
	    location.href="/selects?pageNum="+pageNum;  
   } 
   $(function(){
	   $.post("/selectOne",{zid:55},function(arr){
		     for ( var i in arr) {
		    	 $("#s1").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
			}
		     
	   })
   })
   
   function sj(val){
	    $("#s2").text("")
	   $.post("/selectOne",{zid:val},function(arr){
		     for ( var i in arr) {
		    	 $("#s2").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
			}
		     
	   })
   }
   function sj3(val){
	   $("#s3").text("")
	   $.post("/selectOne",{zid:val},function(arr){
		     for ( var i in arr) {
		    	 $("#s3").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
			}
		     
	   })
   }
   
   function toAdd(){
	   location.href="/toAdd";
   }
</script>
<title>页面</title>

</head>
<body>
<button onclick="toAdd()">添加</button>
<form action="/selects" method="get">
      姓名:<input type="text" name="dname" value="${vo.dname }">
      性别:<select name="sex">
        <option ></option>
        <option value="男" ${vo.sex=='男'?"selected":"" }></option>
        <option value="女" ${vo.sex=='女'?"selected":"" }></option>
      </select>   
   省:<select id="s1" onchange="sj(this.value)" name="shengfen"><option value="0">请选择</option></select>
   市:<select id="s2" onchange="sj3(this.value)" name="shi"></select> 
   县:<select id="s3" name="xian"></select>
     科室:<select name="kid">
        <option value="0" ${vo.kid==0?"selected":"" }>请选择</option>
        <option value="1" ${vo.kid==1?"selected":"" }>内科</option>
        <option value="2" ${vo.kid==2?"selected":"" }>外科</option>
        <option value="3" ${vo.kid==3?"selected":"" }>骨科</option>
      </select>   
   年龄:<input type="text" name="age1" value="${vo.age1 }"><input type="text" name="age2" value="${vo.age2 }"> 
   <button>查看</button>
</form>
 <table>
   <tr>
     <td>姓名</td>
     <td>性别</td>
     <td>生日</td>
     <td>年龄</td>
     <td></td>
     <td></td>
     <td></td>
     <td>科室</td>
     <td>擅长</td>
     <td>工龄</td>
     <td>头像</td>
     <td>操作</td>
   </tr>
     <c:forEach items="${info.list }" var="li">
         <tr>
    	 <td>${li.dname }</td>
    	 <td>${li.sex }</td>
    	 <td>${li.createtime }</td>
    	 <td>${li.age }</td>
     	 <td>${li.sf.zname }</td>
     	 <td>${li.si.zname }</td>
     	 <td>${li.xia.zname }</td>
    	 <td>${li.ks.kname }</td>
    	 <td>
    	   <c:forEach items="${li.sc }" var="s">
    	     ${s.sname }
    	   </c:forEach>
    	 </td>
    	 <td>${li.ageWork }</td>
    	 <td>
    	   <img alt="" src="/pic/${li.pic }" style="width: 50px;height: 50v">
    	 </td>
     	 <td>
                 <a href="/toUpdate?did=${li.did }">修改</a>
         </td>
   </tr>
     </c:forEach>
 </table>
 <jsp:include page="/WEB-INF/view/pages.jsp"></jsp:include>
</body>
</html>

前台添加页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="/resource/jquery/jquery-3.4.1.js"></script>
<script type="text/javascript">
   

     
$(function(){
	   $.post("/selectOne",{zid:55},function(arr){
		     for ( var i in arr) {
		    	 $("#s1").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
			}
		     
	   })
})

function sj(val){
	    $("#s2").text("")
	   $.post("/selectOne",{zid:val},function(arr){
		     for ( var i in arr) {
		    	 $("#s2").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
			}
		     
	   })
}
function sj3(val){
	   $("#s3").text("")
	   $.post("/selectOne",{zid:val},function(arr){
		     for ( var i in arr) {
		    	 $("#s3").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
			}
		     
	   })
}

     
     function add(){
    	 
        var formData = new FormData($("#form1")[0]);
            alert(formData)
   		   $.ajax({
   			   url:"/add",
   			   type:"post",
   			   data:formData,
   		       processData:false,
   		       contentType:false,
   		       success:function(flag){
   		    	  if(flag){
   	   			   alert("添加成功!");
   	   			   location.href="/selects";
   	   		      }else{
   	   			   alert("添加失败!");
   	   		      }
   		       }
   		   })

     }
     
     
</script>
</head>

<body>
<form id="form1">
   <table>
     <tr>
       <td>姓名:<input type="text" name="dname"></td>
     </tr>
     
     <tr>
      
       <td>身份证编号:<input type="text" name="dbianhao"></td>
     </tr>
     
     <tr>
       <td><input type="radio" name="sex" value="男"><input type="radio" name="sex" value="女"></td>
     </tr>
     
      <tr>  
       <td>生日日期:<input type="date" name="createtime"></td>
     </tr>
     
     <tr>       
       <td>年龄:<input type="text" name="age" > </td>
     </tr>
      <tr>
     <td>
                        省:<select id="s1" onchange="sj(this.value)" name="shengfen"><option value="0">请选择</option></select>
   		市:<select id="s2" onchange="sj3(this.value)" name="shi"></select> 
                        县:<select id="s3" name="xian"></select>
    </td>
     </tr>
     
      <tr>
       <td>科室:
       <select name="kid">
        <option value="1" }>内科</option>
        <option value="2" }>外科</option>
        <option value="3" }>骨科</option>
      </select>
      </td>
     </tr>
  
     <tr>
      <td>擅长:
       <c:forEach items="${sc }" var="s">
           <input type="checkbox" name="ssids" value="${s.sid }">${s.sname }&nbsp;&nbsp;&nbsp; 
       </c:forEach>
       
      </td>
     </tr>
     
  
     <tr>       
       <td>电话:<input type="text" name="telephone" > </td>
     </tr>
     
     <tr>       
       <td>入职日期:<input type="text" name="rutime" > </td>
     </tr>
     
      <tr>       
       <td>图片:<input type="file" name="file" > </td>
       </tr>
     
      <tr>
        
       <td><input type="button" onclick="add()" value="提交"> </td>
     </tr>
   </table>
   </form>
</body>
</html>

前台修改页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改</title>
<script type="text/javascript" src="/resource/jquery/jquery-3.4.1.js"></script>
<script type="text/javascript">
     
$(function(){
	   $.post("/selectOne",{zid:55},function(arr){
		     for ( var i in arr) {
		    	 $("#s1").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
			}
		     
	   })
})

function sj(val){
	    $("#s2").text("")
	   $.post("/selectOne",{zid:val},function(arr){
		     for ( var i in arr) {
		    	 $("#s2").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
			}
		     
	   })
}
function sj3(val){
	   $("#s3").text("")
	   $.post("/selectOne",{zid:val},function(arr){
		     for ( var i in arr) {
		    	 $("#s3").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
			}
		     
	   })
}

     
     function update(){
    	 
        var formData = new FormData($("#form1")[0]);
            alert(formData)
   		   $.ajax({
   			   url:"/update",
   			   type:"post",
   			   data:formData,
   		       processData:false,
   		       contentType:false,
   		       success:function(flag){
   		    	  if(flag){
   	   			   alert("添加成功!");
   	   			   location.href="/selects";
   	   		      }else{
   	   			   alert("添加失败!");
   	   		      }
   		       }
   		   })

     }
     
     
</script>
</head>

<body>
<form id="form1">
   <table>
     <tr>  
       <td>
       <input type="hidden" name="did"  value="${doc.did }" >
                      姓名:<input type="text" name="dname"  value="${doc.dname }" >
       </td>
     </tr>
     
     <tr>
      
       <td>身份证编号:<input type="text" name="dbianhao"  value="${doc.dbianhao }"></td>
     </tr>
     
     <tr>
       <td><input type="radio" name="sex" value="男" ${doc.sex=='男'?"checked":"" }><input type="radio" name="sex" value="女"  ${doc.sex=='女'?"checked":"" } ></td>
     </tr>
     
      <tr>  
       <td>生日日期:<input type="date" name="createtime" value="${doc.createtime }" ></td>
     </tr>
     
     <tr>       
       <td>年龄:<input type="text" name="age" value="${doc.age }"> </td>
     </tr>
      <tr>
     <td>
                        省:<select id="s1" onchange="sj(this.value)" name="shengfen"><option value="0">请选择</option></select>
   		市:<select id="s2" onchange="sj3(this.value)" name="shi"></select> 
                        县:<select id="s3" name="xian"></select>
    </td>
     </tr>
     
      <tr>
       <td>科室:
       <select name="kid">
        <option value="1" ${doc.kid==1?"selected":"" } }>内科</option>
        <option value="2" ${doc.kid==2?"selected":"" } }>外科</option>
        <option value="3" ${doc.kid==3?"selected":"" }}>骨科</option>
      </select>
      </td>
     </tr>
  
    
     <tr>
      <td>擅长:
       <c:forEach items="${sc }" var="s">
           <input type="checkbox" name="ssids" 
                 <c:forEach items="${doc.sc}" var="ss">
							<c:if test="${ss.sid==s.sid}">checked</c:if>
				</c:forEach>
           value="${s.sid }">${s.sname }&nbsp;&nbsp;&nbsp; 
       </c:forEach>
       
      </td>
     </tr>
     
  
     <tr>       
       <td>电话:<input type="text" name="telephone" value="${doc.telephone }"> </td>
     </tr>
     
     <tr>       
       <td>入职日期:<input type="date" name="rutime" value="${doc.rutime }"> </td>
     </tr>
     
      <tr>       
       <td>图片:<input type="file" name="file" >
        <img alt="" src="/pic/${doc.pic }" style="width: 50px;height: 50v">
        </td>
       </tr>
     
      <tr>
        
       <td><input type="button" onclick="update()" value="提交"> </td>
     </tr>
   </table>
   </form>
</body>
</html>

分页

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<input type="button" onclick="goPage(1)"
	value="首页">
<input type="button"
	onclick="goPage(${info.prePage==0?1:info.prePage })" value="上一页">
<input type="button"
	onclick="goPage(${info.nextPage ==0?info.pages:info.nextPage})"
	value="下一页">
<input type="button" onclick="goPage(${info.pages})"
	value="尾页">
	${info.pageNum}/${info.pages}


Dao

package com.wls.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.wls.pojo.DiQu;
import com.wls.pojo.Doctor;
import com.wls.pojo.ShanChang;
import com.wls.pojo.Vo;

public interface DoctorDao {
	
	List<Doctor> selects(Vo vo);
	
	List<DiQu> selectOne(Integer zid);
	
	List<ShanChang> selectSc();
	
	int add(Doctor doc);
	
	Doctor selectByDid(Integer did);
	
	int addSss(@Param("did")Integer did,@Param("ssids") Integer[] ssids);

	int update(Doctor doc);

	int delSss(Integer did);

}

service接口

package com.wls.service;

import java.util.List;

import com.github.pagehelper.PageInfo;
import com.wls.pojo.DiQu;
import com.wls.pojo.Doctor;
import com.wls.pojo.ShanChang;
import com.wls.pojo.Vo;

public interface DoctorService {

//列表
PageInfo<Doctor> selects(Vo vo,Integer pageNUm,Integer pageSize);

//添加
int add(Doctor doc);

//查询擅长表
List<ShanChang> selectSc();

//三级联动查询
List<DiQu> selectOne(Integer zid);

Doctor selectByDid(Integer did);

int update(Doctor doc);

}


service实现类

package com.wls.service.impl;


import java.util.List;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wls.dao.DoctorDao;
import com.wls.pojo.DiQu;
import com.wls.pojo.Doctor;
import com.wls.pojo.ShanChang;
import com.wls.pojo.Vo;
import com.wls.service.DoctorService;

@Service
public class DoctorServiceImpl implements DoctorService{

	@Autowired
	DoctorDao doctorDao;
	
	@Override
	public PageInfo<Doctor> selects(Vo vo, Integer pageNUm, Integer pageSize) {
		 PageHelper.startPage(pageNUm,pageSize);
		 List<Doctor> list = doctorDao.selects(vo);
		return new PageInfo<>(list);
	}

	@Override
	public List<DiQu> selectOne(Integer zid) {
		// TODO Auto-generated method stub
		return doctorDao.selectOne(zid);
	}

	@Override
	public int add(Doctor doc) {
		int i=doctorDao.add(doc);
		int j=0;
		if(i>0){
			 j = doctorDao.addSss(doc.getDid(),doc.getSsids());
		}
		return j;
	}

	@Override
	public List<ShanChang> selectSc() {
		
		return doctorDao.selectSc();
	}

	@Override
	public Doctor selectByDid(Integer did) {
		
		return doctorDao.selectByDid(did);
	}

	@Override
	public int update(Doctor doc) {
		int i = doctorDao.update(doc);
		int j=0;
		if(i>0){
			j=doctorDao.delSss(doc.getDid());
			doctorDao.addSss(doc.getDid(), doc.getSsids());
		}
		return j;
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值