java 文件上传,三级连动,分页

 

package com.zhuzg.doc.service;

import java.util.List;

import com.github.pagehelper.PageInfo;
import com.zhuzg.doc.pojo.Area;
import com.zhuzg.doc.pojo.Depart;
import com.zhuzg.doc.pojo.Doc;
import com.zhuzg.doc.pojo.DocVo;
import com.zhuzg.doc.pojo.Skill;

public interface DocService {
    
    // 关于医生的增删改查
    PageInfo<Doc> list(DocVo docVo);
    int add(Doc doc);
    int update(Doc doc);
    int del(int[] ids);
    Doc getById(int id);
    
    
    // 根据父id 查询直接子节点
    List<Area> listArea(int pid);
    
    // 列出擅长
    List<Skill> listSkills();
    
    //列出科室
    List<Depart> listDepart();
    
    
    
    

}
package com.zhuzg.doc.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.zhuzg.doc.pojo.Area;
import com.zhuzg.doc.pojo.Depart;
import com.zhuzg.doc.pojo.Doc;
import com.zhuzg.doc.pojo.DocVo;
import com.zhuzg.doc.pojo.Skill;

public interface DocDao {

    List<Doc> list(DocVo docVo);

    int add(Doc doc);

    int addDocSkill(@Param("docId") int docId, @Param("skillId") int skillId);

    List<Depart> listDepart();

    Doc getById(int id);

    List<Skill> listSkills();

    int delDoc(int[] ids);

    List<Area> listAea(int pid);

    int delDocSkillByDoc(int ...ids);

    int udate(Doc doc);

}

 

package com.zhuzg.doc.service.impl;

import java.util.Iterator;
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.zhuzg.doc.dao.DocDao;
import com.zhuzg.doc.pojo.Area;
import com.zhuzg.doc.pojo.Depart;
import com.zhuzg.doc.pojo.Doc;
import com.zhuzg.doc.pojo.DocVo;
import com.zhuzg.doc.pojo.Skill;
import com.zhuzg.doc.service.DocService;

@Service(interfaceClass =  DocService.class)
public class DocServiceImpl implements DocService {
    
    @Autowired
    DocDao docDao;
    

    @Override
    public PageInfo<Doc> list(DocVo docVo) {
        // TODO Auto-generated method stub
        PageHelper.startPage(docVo.getPage(),docVo.getPageSize());
        
        return new PageInfo<Doc>(docDao.list(docVo));
    }

    @Override
    public int add(Doc doc) {
        // TODO Auto-generated method stub
        //添加主表
        int result = docDao.add(doc);
        //添加子表
        List<Skill> skills = doc.getSkills();
        for (int i = 0; i < skills.size(); i++) {
            //插入中间表
            result+=docDao.addDocSkill(doc.getId(),skills.get(i).getId());
        }
        
        return result;
    }

    @Override
    public int update(Doc doc) {
        // TODO Auto-generated method stub
        //添加主表
        int result = docDao.udate(doc);
        
        //删除中间表    delDocSkillByDoc
        result += docDao.delDocSkillByDoc(doc.getId());
        
        //添加子表
        List<Skill> skills = doc.getSkills();
        for (int i = 0; i < skills.size(); i++) {
            //插入中间表
            result+=docDao.addDocSkill(doc.getId(),skills.get(i).getId());
        }
        
        return result;
    }

    @Override
    public int del(int[] ids) {
        // TODO Auto-generated method stub
        //删除子表
        int result = docDao.delDocSkillByDoc(ids);
        //删除主表
        result += docDao.delDoc(ids);
        return result;
    }

    @Override
    public List<Area> listArea(int pid) {
        // TODO Auto-generated method stub
        return docDao.listAea(pid);
    }

    @Override
    public List<Skill> listSkills() {
        // TODO Auto-generated method stub
        return docDao.listSkills();
    }

    @Override
    public List<Depart> listDepart() {
        // TODO Auto-generated method stub
        return docDao.listDepart();
    }

    @Override
    public Doc getById(int id) {
        // TODO Auto-generated method stub
        return docDao.getById(id);
    }
    
    

}
<?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.zhuzg.doc.dao.DocDao">

    <!-- List<Doc> list(DocVo docVo); -->
    <resultMap type="doc" id="docMapper">
        <id column="id" property="id"></id>
        <association property="province" column="provinceId" select="findAreaByid"></association>
        <association property="city" column="cityId" select="findAreaByid"></association>
        <association property="county" column="countyId" select="findAreaByid"></association>
        <association property="depart" column="departId" select="findDepartByid"></association>
        <collection property="skills" column="id"  select="findSkillsByDocId" ></collection>
    </resultMap>
    
    <select id="list" resultMap="docMapper">
        select * from tb_doc 
    </select>
    
    <select id="findAreaByid" resultType="area">
        select * from tb_area where id=#{value}
    </select>
    <select id="findDepartByid" resultType="depart">
        select * from tb_depart where id=#{value}
    </select>
    <select id="findSkillsByDocId" resultType="skill">
        SELECT s.* from tb_doc_skill ds LEFT JOIN  tb_skill s ON s.id=ds.skillId 
             WHERE ds.docId=#{value}
    </select>
    <!-- int add(Doc doc); -->
    <insert id="add" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
            insert into tb_doc(name,cardno,birthday,provinceId,cityId,countyId,mobile,enterDate,img)
                values (#{name},#{cardno},#{birthday},#{provinceId},#{cityId},#{countyId},#{mobile},#{enterDate},#{img})     
    </insert>
    <!-- int addDocSkill(Integer id, Integer id2); -->
    <insert id="addDocSkill">
        insert into tb_doc_skill(docId,skillId) values(#{docId},#{skillId})
    </insert>
        
    <!-- List<Depart> listDepart(); -->
    <select id="listDepart" resultType="depart">
        select * from tb_depart
    </select>

    <!-- Doc getById(); -->
    <select id="getById" resultMap="docMapper">
    select * from tb_doc where id=#{value}
    </select>

    <!-- List<Skill> listSkills(); -->
    <select id="listSkills" resultType="skill">
        select * from tb_skill
    </select>

    <!-- int delDoc(int[] ids); -->
    <delete id="delDoc">
        delete from tb_doc where id in 
        <foreach collection="array" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <!-- List<Area> listAea(int pid); -->
    <select id="listAea" resultType="area">
        select * from tb_area where pid=#{value}
    </select>

    <!-- int delDocSkillByDoc(int ...ids); -->
    <delete id="delDocSkillByDoc">
        delete from tb_doc_skill where  docId in
        <foreach collection="array" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <!-- int udate(Doc doc); -->
    <update id="udate">
        update tb_doc
        set name=#{name},
            cardno=#{cardno},
            birthday=#{birthday},
            provinceId=#{provinceId},
            cityId=#{cityId},
            countyId=#{countyId},
            mobile=#{mobile},
            enterDate=#{enterDate},
            img=#{img}
        where id=#{id}
    </update>

</mapper>

 

 

package com.zhuzg.doc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.zhuzg.doc.common.FileUtils;

import com.zhuzg.doc.pojo.Area;
import com.zhuzg.doc.pojo.Depart;
import com.zhuzg.doc.pojo.Doc;
import com.zhuzg.doc.pojo.Skill;
import com.zhuzg.doc.service.DocService;

@Controller
public class DocController {
    
    @Reference
    DocService docService;
    
    /** 
     * 
     * @return
     */
    @RequestMapping("toadd")
    public String toAdd(HttpServletRequest request) {
        // 获取所有的省份
        List<Area> listProince = docService.listArea(0);
        request.setAttribute("proinces", listProince);
        //
        List<Skill> listSkills = docService.listSkills();
        request.setAttribute("skills", listSkills);
        
        List<Depart> listDepart = docService.listDepart();
        request.setAttribute("departs", listDepart);
        
        return "add";
        
    }
    
    /** 
     * 
     * @return
     */
    @RequestMapping("toupdate")
    public String toUpdate(HttpServletRequest request,int id) {
        
        Doc doc = docService.getById(id);
        request.setAttribute("doc", doc);
        
        System.out.println("doc is " + doc);
        
        // 获取所有的省份
        List<Area> listProince = docService.listArea(0);
        request.setAttribute("proinces", listProince);
        //
        List<Skill> listSkills = docService.listSkills();
        request.setAttribute("skills", listSkills);
        
        List<Depart> listDepart = docService.listDepart();
        request.setAttribute("departs", listDepart);
        
        //获取医生的所在省的id 根据这个获取市的列表
        if(doc.getProvince()!=null) {
            List<Area> listcities = docService.listArea(doc.getProvince().getId());
            request.setAttribute("cities", listcities);
        }
        
        //获取医生的所在市的id 根据这个获取县的列表
        if(doc.getCity()!=null) {
            List<Area> listcounties = docService.listArea(doc.getCity().getId());
            request.setAttribute("counties", listcounties);
        }
        return "update";
        
    }
    
    @RequestMapping("listChild")
    @ResponseBody
    public List<Area> listChild(HttpServletRequest request,int pid) {
        List<Area> listArea = docService.listArea(pid);
        return listArea ;
    }
    
    /** 
     * 
     * @return
     */
    @RequestMapping("add")
    public String add(HttpServletRequest request,Doc doc,int[] selSkillId,MultipartFile file) {
        try {
            String path = FileUtils.processFile(file);
            doc.setImg(path);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<Skill> skills= new ArrayList<Skill>();
        for (int i = 0; i < selSkillId.length; i++) {
            Skill skill = new Skill();
            skill.setId(selSkillId[i]);
            skills.add(skill);
        }
        doc.setSkills(skills);
        docService.add(doc);
        return "rediect:list";
    }
    
    /** 
     * 
     * @return
     */
    @RequestMapping("update")
    public String update(HttpServletRequest request,Doc doc,int[] selSkillId,MultipartFile file) {
        try {
            String path = FileUtils.processFile(file);
            doc.setImg(path);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List<Skill> skills= new ArrayList<Skill>();
        for (int i = 0; i < selSkillId.length; i++) {
            Skill skill = new Skill();
            skill.setId(selSkillId[i]);
            skills.add(skill);
        }
        doc.setSkills(skills);
        docService.update(doc);
        return "rediect:list";
    }
    
    
}

<%@ 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>
<html>
<head>
<meta charset="UTF-8">
    <script type="text/javascript" src="./resource/jquery/jquery-3.4.1.js"></script>
<title>添加</title>
</head>
<body>
    <form action="./add" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td>姓名</td>
            <td><input name="name"></td>
        </tr>
        <tr>
            <td>身份证</td>
            <td><input name="cardno"></td>
        </tr>
        <tr>
            <td>电话</td>
            <td><input name="mobile"></td>
        </tr>
        <tr>
            <td>性别</td>
            <td> <input type="radio" name="gender" value="1">男  
                    &nbsp;&nbsp;&nbsp;&nbsp;
                  <input type="radio" name="gender" value="2">女
            </td>
        </tr>
        <tr>
            <td>生日</td>
            <td><input  type="date" name="birthday"></td>
        </tr>
        <tr>
            <td>省</td>
            <td>
            <select name="provinceId" id="provinceId" onchange="changeSub('provinceId','cityId')">
                <option value="-1">---请选择---</option>     
                <c:forEach items="${proinces}" var="p">
                    <option value="${p.id}">${p.name}</option>     
                </c:forEach>
            </select> </td>
        </tr>
        <tr>
            <td>市</td>
            <td>
            <select name="cityId" id="cityId"  onchange="changeSub('cityId','countyId')">
                <option value="-1">---请选择---</option>     
            </select> </td>
        </tr>
        <tr>
            <td>县</td>
            <td>
            <select name="countyId" id="countyId">
                <option value="-1">---请选择---</option>     
            </select> </td>
        </tr>
        <tr>
            <td>科室</td>
            <td>
            <select name="departId" id="departId">
                <option value="-1">---请选择---</option>     
                <c:forEach items="${departs}" var="d">
                    <option value="${d.id}">${d.name}</option>     
                </c:forEach>
            </select> </td>
        </tr>
        
        <tr>
            <td>擅长</td>
            <td>
                <c:forEach items="${skills}" var="s">
                    <input type="checkbox" name="selSkillId" value="${s.id}">${s.name} &nbsp;&nbsp;&nbsp;&nbsp; 
                </c:forEach>
            </td>
        </tr>
        <tr>
            <td>头像</td>
            <td>
                <input type="file" name="file">
            </td>
        </tr>
        <tr>
            <td>入职日期</td>
            <td><input  type="date" name="enterDate"></td>
        </tr>
        
        <tr>
            <td></td>
            <td><button type="submit" >提交</button></td>
        </tr>
    </table>
    </form>
    <script type="text/javascript">
    
        function changeSub(parId,childId){
            var pid=$("#"+parId).val();
            //获取到被影响的下拉框的对象
            var childObj = $("#"+childId)
            $.post("./listChild",{pid,pid},function(data){
                //遍历data 
                //清空下一个级别
                childObj.empty();
                //
                childObj.append('<option value="-1">---请选择---</option> ')            
                for ( var i in data) {
                    childObj.append('<option value="'+data[i].id+'">'+data[i].name+'</option> ')    
                }
            })
        }
    </script>
</body>
</html>
package com.zhuzg.doc.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.multipart.MultipartFile;

public class FileUtils {
    
    //存放图片的路径
    static String rootPath;
    
    //windows存放图片的路径
    static String winRootPath;
    
    //linux存放图片的路径
    static String linuxRootPath;
    
    //当前的操作系统
    static String currentOs="";
    
    
    static {
        String os = System.getProperty("os.name").toLowerCase();  
        
        //linux 
        if(os.indexOf("linux")>=0 ) {
            currentOs="linux";
        }
        //windows 
        if(os.indexOf("windows")>=0 ) {
            currentOs="windows";
        }
    }
    
    public static String getRootPath() {
        return rootPath;
    }


    /**
     * 保存文件的具体位置
     * @param rootPath
     */
    @Value("${win.pic.savepath}")
    public void setWinRootPath(String rootPath) {
        if(currentOs.equals("windows")) {
            winRootPath=rootPath;
            FileUtils.rootPath=rootPath;
            createPath();
        }
        
        
    }
    
    @Value("${linux.pic.savepath}")
    public void setLinuxRootPath(String rootPath) {
        if(currentOs.equals("linux")) {
            linuxRootPath=rootPath;
            FileUtils.rootPath=rootPath;
            createPath();
        }
    }
    
    /**
     * 创建目录
     */
    static private void createPath() {
        //不存在则创建该目录
            File file = new File(FileUtils.rootPath);
            if(!file.exists()) {
                file.mkdirs();
            }
    }


    /**
     * 
     * @param response
     * @param file
     * @throws FileNotFoundException 
     */
    public static void downLoad(HttpServletResponse response, String filename) throws FileNotFoundException {
         /* // 下载本地文件
        String fileName = "Operator.doc".toString(); // 文件的默认保存名
*/        // 读到流中
        InputStream inStream = new FileInputStream(rootPath+ "/"+ filename);// 文件的存放路径
        // 设置输出的格式
        response.reset();
        response.setContentType("bin");
        response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
       
        // 循环取出流中的数据
        byte[] b = new byte[1024];
        int len;
        try {
          while ((len = inStream.read(b)) > 0)
            response.getOutputStream().write(b, 0, len);
          inStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
    
    }
    
    
    /**
     *  上传文件
     * @param file
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
    public static String processFile(MultipartFile file) throws IllegalStateException, IOException {

        // 原来的文件名称
        System.out.println("file.isEmpty() :" + file.isEmpty()  );
        System.out.println("file.name :" + file.getOriginalFilename());
        
        if(file.isEmpty()||"".equals(file.getOriginalFilename()) || file.getOriginalFilename().lastIndexOf('.')<0 ) {
            return "";
        }
            
        String originName = file.getOriginalFilename();
        String suffixName = originName.substring(originName.lastIndexOf('.'));
        SimpleDateFormat sdf=  new SimpleDateFormat("yyyyMMdd");
        String path = rootPath + "/" + sdf.format(new Date());
        File pathFile = new File(path);
        if(!pathFile.exists()) {
            pathFile.mkdir();
        }
        String destFileName =         path + "/" +  UUID.randomUUID().toString() + suffixName;
        File distFile = new File( destFileName);
        file.transferTo(distFile);//文件另存到这个目录下边
        return destFileName.substring(rootPath.length()+1);
        
        
    }

}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
    <script type="text/javascript" src="./resource/jquery/jquery-3.4.1.js"></script>
<title>修改</title>
</head>
<body>
    <form action="./update" method="post" enctype="multipart/form-data">
    <table>
        <tr>
            <td>姓名</td>
        
            <td>
                <input type="hidden" name="id" value="${doc.id}">
            <input name="name" value="${doc.name}"></td>
        </tr>
        <tr>
            <td>身份证</td>
            <td><input name="cardno" value="${doc.cardno}"></td>
        </tr>
        <tr>
            <td>电话</td>
            <td><input name="mobile" value="${doc.mobile}"></td>
        </tr>
        <tr>
            <td>性别</td>
            <td> <input type="radio" name="gender" value="1">男  
                    &nbsp;&nbsp;&nbsp;&nbsp;
                  <input type="radio" name="gender" value="2">女
            </td>
        </tr>
        <tr>
            <td>生日</td>
            <td><input  type="date" name="birthday" value='<fmt:formatDate value="${doc.birthday}" pattern="yyyy-MM-dd"/>'></td>
        </tr>
        <tr>
            <td>省</td>
            <td>
            <select name="provinceId" id="provinceId" onchange="changeSub('provinceId','cityId')">
                <option value="-1">---请选择---</option>     
                <c:forEach items="${proinces}" var="p">
                    <option value="${p.id}" ${doc.province.id==p.id?'selected':''}>${p.name}</option>     
                </c:forEach>
            </select> </td>
        </tr>
        <tr>
            <td>市</td>
            <td>
            <select name="cityId" id="cityId"  onchange="changeSub('cityId','countyId')">
                <option value="-1">---请选择---</option>     
                <c:forEach items="${cities}" var="c">
                    <option value="${c.id}" ${doc.city.id==c.id?'selected':''}>${c.name}</option>     
                </c:forEach>
            </select> </td>
        </tr>
        <tr>
            <td>县</td>
            <td>
            <select name="countyId" id="countyId">
                <option value="-1">---请选择---</option>     
                <c:forEach items="${counties}" var="c">
                    <option value="${c.id}" ${doc.county.id==c.id?'selected':''}>${c.name}</option>     
                </c:forEach>
            </select> </td>
        </tr>
        <tr>
            <td>科室</td>
            <td>
            <select name="departId" id="departId">
                <option value="-1">---请选择---</option>     
                <c:forEach items="${departs}" var="d">
                    <option value="${d.id}">${d.name}</option>     
                </c:forEach>
            </select> </td>
        </tr>
        
        <tr>
            <td>擅长</td>
            <td>
                <c:forEach items="${skills}" var="s">
                    <input type="checkbox" name="selSkillId" 
                        <c:forEach items="${doc.skills}" var="selSkill">
                            <c:if test="${selSkill.id==s.id}">checked</c:if>
                        </c:forEach>
                    value="${s.id}">${s.name} &nbsp;&nbsp;&nbsp;&nbsp; 
                </c:forEach>
            </td>
        </tr>
        <tr>
            <td>头像</td>
            <td>
                <input type="file" name="file">
                <img src="/pic/${doc.img}" width="150" height="150">
            </td>
        </tr>
        <tr>
            <td>入职日期</td>
            
            <td><input  type="date" name="enterDate"  value='<fmt:formatDate value="${doc.enterDate}" pattern="yyyy-MM-dd"/>'></td>
        </tr>
        
        <tr>
            <td></td>
            <td><button type="submit" >提交</button></td>
        </tr>
    </table>
    </form>
    <script type="text/javascript">
    
        function changeSub(parId,childId){
            var pid=$("#"+parId).val();
            //获取到被影响的下拉框的对象
            var childObj = $("#"+childId)
            $.post("./listChild",{pid,pid},function(data){
                //遍历data 
                //清空下一个级别
                childObj.empty();
                //
                childObj.append('<option value="-1">---请选择---</option> ')            
                for ( var i in data) {
                    childObj.append('<option value="'+data[i].id+'">'+data[i].name+'</option> ')    
                }
            })
        }
    </script>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

后补

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  3 <mapper namespace="com.wls.dao.DoctorDao">
  4 
  5      <select id="selects" resultMap="doctorMapper">
  6         SELECT d.*,k.kname,datediff(now(), rutime)as ageWork  FROM `doctor` d   
  7         LEFT JOIN   keshi k on d.kid = k.kid
  8          
  9          <where>
 10             <if test="dname!=null and dname!=''">
 11               and d.dname like concat('%',#{dname},'%')
 12             </if>
 13             <if test="sex!=null and sex!=''">
 14               and d.sex =#{sex}
 15             </if>
 16             <if test="shengfen!=null and shengfen!=0">
 17               and d.shengfen =#{shengfen}
 18             </if>
 19             <if test="shi!=null and shi!=0">
 20               and d.shi =#{shi}
 21             </if>
 22             <if test="xian!=null and xian!=0">
 23               and d.xian =#{xian}
 24             </if>
 25              <if test="kid!=null and kid!=0">
 26               and d.kid =#{kid}
 27             </if>
 28             <if test="age1!=null ">
 29               and d.age &gt;=#{age1}
 30             </if>
 31             <if test="age2!=null ">
 32               and d.age &lt;=#{age2}
 33             </if>
 34          </where>  
 35      </select>
 36      <resultMap type="Doctor" id="doctorMapper">
 37           <id property="did" column="did"/>
 38           <result property="dname" column="dname"/>
 39           <result property="dbianhao" column="dbianhao"/>
 40           <result property="sex" column="sex"/>
 41           <result property="createtime" column="createtime"/>
 42           <result property="kid" column="kid"/>  
 43           <result property="telephone" column="telephone"/>
 44           <result property="rutime" column="rutime"/>
 45           <result property="pic" column="pic"/>
 46           <result property="age" column="age"/>
 47           <result property="ageWork" column="ageWork"/>
 48           <association property="sf" column="shengfen" select="findDiQuByid"></association>
 49           <association property="si" column="shi" select="findDiQuByid"></association>
 50           <association property="xia" column="xian" select="findDiQuByid"></association>
 51           
 52           <association property="ks" javaType="KeShi">
 53                <id property="kid" column="kid"/>
 54                 <result property="kname" column="kname"/>
 55           </association>
 56           <collection property="sc" column="did" select="selectBySid" >
 57              
 58           </collection>
 59      </resultMap>
 60       <select id="selectBySid" resultType="ShanChang">
 61            SELECT sc.* from  sss LEFT JOIN shanchang sc  on sss.ssid =  sc.sid where sss.ddid =#{did}
 62       </select>
 63       
 64       <select id="findDiQuByid" resultType="DiQu">
 65          select * from diqu where id=#{value}
 66          
 67           
 68       </select>
 69       
 70       <select id="selectOne" resultType="DiQu">
 71         select * from diqu where zid=#{zid}
 72       </select>
 73       
 74       <select id="selectSc" resultType="ShanChang">
 75         select * from shanchang
 76       </select>
 77       
 78       
 79       <insert id="add" useGeneratedKeys="true" keyProperty="did">
 80          insert into doctor values(null,#{dname},#{dbianhao},
 81          #{sex},#{createtime},#{shengfen},#{shi},#{xian},
 82          #{kid},#{telephone},#{rutime},#{pic},#{age})
 83       </insert>
 84       
 85       <insert id="addSss"> 
 86           insert into sss values
 87           <foreach collection="ssids" item="sid" separator=",">
 88             (#{did},#{sid})
 89           </foreach>
 90       </insert>
 91       
 92       <select id="selectByDid" resultMap="doctorMapper">
 93               SELECT * from doctor where did=#{value}
 94       </select>
 95       
 96       <update id="update">
 97          update doctor set dname=#{dname},dbianhao=#{dbianhao},sex=#{sex},createtime=#{createtime},
 98          shengfen=#{shengfen},shi=#{shi},xian=#{xian},kid=#{kid},telephone=#{telephone},
 99          rutime=#{rutime},pic=#{pic},age=#{age} where did =#{did}
100       </update>
101       
102       <delete id="delSss">
103           delete from sss where ddid=#{did}
104       </delete>
105 </mapper>
  1 package com.wls.controller;
  2 
  3 import java.io.File;
  4 import java.io.IOException;
  5 import java.util.List;
  6 import java.util.UUID;
  7 
  8 import org.apache.dubbo.config.annotation.Reference;
  9 import org.springframework.stereotype.Controller;
 10 import org.springframework.ui.Model;
 11 import org.springframework.web.bind.annotation.RequestMapping;
 12 import org.springframework.web.bind.annotation.RequestParam;
 13 import org.springframework.web.bind.annotation.ResponseBody;
 14 import org.springframework.web.multipart.MultipartFile;
 15 
 16 import com.github.pagehelper.PageInfo;
 17 import com.wls.pojo.DiQu;
 18 import com.wls.pojo.Doctor;
 19 import com.wls.pojo.ShanChang;
 20 import com.wls.pojo.Vo;
 21 import com.wls.service.DoctorService;
 22 
 23 
 24 @Controller
 25 public class DoctorController {
 26   
 27     @Reference
 28     DoctorService doctorService;
 29 
 30     @RequestMapping("selects")
 31     public String selects(Model m,Vo vo,@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="2")Integer pageSize){
 32         
 33         PageInfo<Doctor> info = doctorService.selects(vo, pageNum, pageSize);
 34         m.addAttribute("info", info);
 35         m.addAttribute("vo", vo);
 36         return "selects";
 37     }
 38     
 39     @RequestMapping("selectOne")
 40     @ResponseBody
 41     public Object selects(Integer zid){
 42         List<DiQu>  list=doctorService.selectOne(zid);
 43         return list;
 44     }
 45     
 46     
 47     @RequestMapping("toAdd")
 48     public String toAdd(Model m){
 49         List<ShanChang> sc = doctorService.selectSc();
 50         m.addAttribute("sc", sc);
 51         return "add";
 52     }
 53     
 54     
 55     
 56     @RequestMapping("add")
 57     @ResponseBody
 58     public boolean add(Doctor doc,MultipartFile file){
 59         
 60         System.err.println(doc);
 61         if(file!=null && !file.isEmpty()){
 62             String path="d:/pic/";
 63             String oldFilename = file.getOriginalFilename();
 64             String fileName=UUID.randomUUID()+oldFilename.substring(oldFilename.lastIndexOf("."));
 65             try {
 66                 file.transferTo(new File(path,fileName));
 67             } catch (IllegalStateException e) {
 68                 // TODO Auto-generated catch block
 69                 e.printStackTrace();
 70             } catch (IOException e) {
 71                 e.printStackTrace();
 72             }
 73             doc.setPic(fileName);
 74         }
 75         
 76         int i = doctorService.add(doc);
 77         return i>0;
 78     }
 79     
 80     
 81     @RequestMapping("toUpdate")
 82     public String toUpdate(Model m,Integer did){
 83         List<ShanChang> sc = doctorService.selectSc();
 84         m.addAttribute("sc", sc);
 85         
 86         Doctor doc=doctorService.selectByDid(did);
 87         m.addAttribute("doc", doc);
 88         System.err.println(doc);
 89         return "update";
 90     }
 91     
 92     
 93     
 94     @RequestMapping("update")
 95     @ResponseBody
 96     public boolean update(Doctor doc,MultipartFile file){
 97         
 98         System.err.println(doc);
 99         if(file!=null && !file.isEmpty()){
100             String path="d:/pic/";
101             String oldFilename = file.getOriginalFilename();
102             String fileName=UUID.randomUUID()+oldFilename.substring(oldFilename.lastIndexOf("."));
103             try {
104                 file.transferTo(new File(path,fileName));
105             } catch (IllegalStateException e) {
106                 // TODO Auto-generated catch block
107                 e.printStackTrace();
108             } catch (IOException e) {
109                 e.printStackTrace();
110             }
111             doc.setPic(fileName);
112         }
113         
114         int i = doctorService.update(doc);
115         return i>0;
116     }
117     
118     
119     
120     
121     
122 }

 

  1 <%@ page language="java" contentType="text/html; charset=UTF-8"
  2     pageEncoding="UTF-8"%>
  3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5 <html>
  6 <head>
  7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8 <link href="/resource/index3.css" rel="stylesheet">\
  9 <script type="text/javascript" src="/resource/jquery-3.2.1.js"></script>
 10 <script type="text/javascript">
 11    function goPage(pageNum){
 12         location.href="/selects?pageNum="+pageNum;  
 13    } 
 14    $(function(){
 15        $.post("/selectOne",{zid:55},function(arr){
 16              for ( var i in arr) {
 17                  $("#s1").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
 18             }
 19              
 20        })
 21    })
 22    
 23    function sj(val){
 24         $("#s2").text("")
 25        $.post("/selectOne",{zid:val},function(arr){
 26              for ( var i in arr) {
 27                  $("#s2").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
 28             }
 29              
 30        })
 31    }
 32    function sj3(val){
 33        $("#s3").text("")
 34        $.post("/selectOne",{zid:val},function(arr){
 35              for ( var i in arr) {
 36                  $("#s3").append("<option value='"+arr[i].id+"'>"+arr[i].zname+"</option>")
 37             }
 38              
 39        })
 40    }
 41    
 42    function toAdd(){
 43        location.href="/toAdd";
 44    }
 45 </script>
 46 <title>页面</title>
 47 
 48 </head>
 49 <body>
 50 <button onclick="toAdd()">添加</button>
 51 <form action="/selects" method="get">
 52       姓名:<input type="text" name="dname" value="${vo.dname }">
 53       性别:<select name="sex">
 54         <option ></option>
 55         <option value="男" ${vo.sex=='男'?"selected":"" }></option>
 56         <option value="女" ${vo.sex=='女'?"selected":"" }></option>
 57       </select>   
 58    省:<select id="s1" onchange="sj(this.value)" name="shengfen"><option value="0">请选择</option></select>
 59    市:<select id="s2" onchange="sj3(this.value)" name="shi"></select> 
 60    县:<select id="s3" name="xian"></select>
 61      科室:<select name="kid">
 62         <option value="0" ${vo.kid==0?"selected":"" }>请选择</option>
 63         <option value="1" ${vo.kid==1?"selected":"" }>内科</option>
 64         <option value="2" ${vo.kid==2?"selected":"" }>外科</option>
 65         <option value="3" ${vo.kid==3?"selected":"" }>骨科</option>
 66       </select>   
 67    年龄:<input type="text" name="age1" value="${vo.age1 }"><input type="text" name="age2" value="${vo.age2 }"> 
 68    <button>查看</button>
 69 </form>
 70  <table>
 71    <tr>
 72      <td>姓名</td>
 73      <td>性别</td>
 74      <td>生日</td>
 75      <td>年龄</td>
 76      <td></td>
 77      <td></td>
 78      <td></td>
 79      <td>科室</td>
 80      <td>擅长</td>
 81      <td>工龄</td>
 82      <td>头像</td>
 83      <td>操作</td>
 84    </tr>
 85      <c:forEach items="${info.list }" var="li">
 86          <tr>
 87          <td>${li.dname }</td>
 88          <td>${li.sex }</td>
 89          <td>${li.createtime }</td>
 90          <td>${li.age }</td>
 91           <td>${li.sf.zname }</td>
 92           <td>${li.si.zname }</td>
 93           <td>${li.xia.zname }</td>
 94          <td>${li.ks.kname }</td>
 95          <td>
 96            <c:forEach items="${li.sc }" var="s">
 97              ${s.sname }
 98            </c:forEach>
 99          </td>
100          <td>${li.ageWork }</td>
101          <td>
102            <img alt="" src="/pic/${li.pic }" style="width: 50px;height: 50v">
103          </td>
104           <td>
105                  <a href="/toUpdate?did=${li.did }">修改</a>
106          </td>
107    </tr>
108      </c:forEach>
109  </table>
110  <jsp:include page="/WEB-INF/view/pages.jsp"></jsp:include>
111 </body>
112 </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}

 

posted @ 2020-05-12 09:24  小单唉~  阅读(162)  评论(0)    收藏  举报