先写 实体类 model
然后mapper层 对应 ssh 的dao层 实现增删改查 分页模糊查询等操作
package com.hdyy.web.dao;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.hdyy.web.bean.WebDoctor;
@Repository
public interface WebDoctorDao {
/**
* 分页查询doctor
* @param params
* @return
*/
public List<WebDoctor> query0(Map<String, Object> params);
public Long getCount(Map<String, Object> params);
/**
* 添加doctor数据
* @param webDoctor
* @return
*/
public int save(WebDoctor webDoctor);
/**
* 更新医生表数据
* @param webDoctor
* @return
*/
public int update(WebDoctor webDoctor);
/**
* 查询课程表和医生表信息
* @return
*/
public List<WebDoctor> query();
/**
* 根据id查询医生表信息
* @param id
* @return
*/
public WebDoctor getDoctor(String id);
/**
* 查询医生表所有信息
* @return
*/
public List<WebDoctor> doctorlist();
}
然后mapper xml 是数据库操作 主要用于写sql 语句
<?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.hdyy.web.dao.WebDoctorDao">
<!-- 这里的webdoctordao名字和dao层名字一致 -->
<sql id="WebCourseSql">
web_doctor.web_doctor_id,web_doctor_name,web_course_id,web_course_title,web_course_date,web_course_time
web_doctor_company,web_doctor_major,
web_doctor_job,web_doctor_introduce
</sql>
<resultMap type="com.hdyy.web.bean.WebDoctor" id="UserResult">
<result property="web_doctor_id" column="web_doctor_id" />
</resultMap>
<!-- id和前面dao层对应的方法一致 -->
<select id="query0" parameterType="map" resultMap="UserResult">
select
<include refid="WebCourseSql"></include>
from web_course inner join web_doctor on web_course.web_doctor_id =
web_doctor.web_doctor_id where web_course_isstart = 0 limit
${startSize},${rows}
</select>
<select id="getCount" parameterType="map" resultType="long">
select
count(0)
from web_doctor
</select>
<!-- resultType介绍 -->
![这里写图片描述](https://img-blog.csdn.net/20170921111442696?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJpdmF0ZV9idWlsZA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
<!-- parameterType介绍 -->
> http://blog.csdn.net/kingjin_csdn_/article/details/53156732
<select id="query" resultType="com.hdyy.web.bean.WebDoctor">
select
<include refid="WebCourseSql"></include>
from web_course inner join web_doctor on web_course.web_doctor_id =
web_doctor.web_doctor_id
</select>
<select id="doctorlist" resultType="com.hdyy.web.bean.WebDoctor">
select
web_doctor_id,web_doctor_name,web_doctor_company,web_doctor_major,web_doctor_job,web_doctor_introduce
from web_doctor
</select>
<insert id="save" parameterType="com.hdyy.web.bean.WebDoctor">
insert into
web_doctor(web_doctor_id,web_doctor_name,web_doctor_company,web_doctor_major,web_doctor_job,web_doctor_introduce)
values
(#{web_doctor_id},#{web_doctor_name},#{web_doctor_company},#{web_doctor_major},#{web_doctor_job},#{web_doctor_introduce})
</insert>
<update id="update" parameterType="com.hdyy.web.bean.WebDoctor">
update web_doctor set web_doctor_name=#{web_doctor_name},web_doctor_company
= #{web_doctor_company},web_doctor_major =
#{web_doctor_major},web_doctor_job=#{web_doctor_job},web_doctor_introduce=#{web_doctor_introduce}
where web_doctor_id = #{web_doctor_id}
</update>
<select id="getDoctor" resultType="com.hdyy.web.bean.WebDoctor">
select
web_doctor_id,
web_doctor_name,
web_doctor_company,web_doctor_major,web_doctor_job,web_doctor_introduce
from web_doctor
where web_doctor_id = #{id}
</select>
</mapper>
然后就是serivice接口 相当于ssh 的biz 业务层
package com.hdyy.web.service;
import java.util.List;
import java.util.Map;
import com.hdyy.web.bean.WebDoctor;
public interface WebDoctorService {
// public List<WebDoctor> query();
/**
* 分页查询所需要的信息
* @param params
* @return
*/
public List<WebDoctor> query0(Map<String, Object> params);
public Long getCount(Map<String, Object> params);
/**
* 添加医生
* @param doctorBean <br>
* 包含: <br>
* web_doctor_name <br>
* web_doctor_company <br>
* web_doctor_major <br>
* web_doctor_job <br>
* web_doctor_introduce
* @return 是否保存成功
*/
public boolean saveDoctor(WebDoctor doctorBean);
/**
* 修改医生
* @param doctorBean <br>
* 包含: <br>
* web_doctor_id <br>
* web_doctor_name <br>
* web_doctor_company <br>
* web_doctor_major <br>
* web_doctor_job <br>
* web_doctor_introduce
* @return 是否修改成功
*/
public boolean updateDoctor(WebDoctor doctorBean);
/**
* 获取医生列表
* @return
*/
public List<WebDoctor> getDoctorlist();
/**
* 根据医生ID 得到医生详细信息
* @return
*/
public WebDoctor getDoctor(String id);
}
然后 写service impl 在里面实现查询到的数据 可以打印你所需要看到查询到的值 打印方法如下
package com.hdyy.web.service.impl;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.annotation.JsonFormat.Value;
import com.hdyy.web.bean.WebCourse;
import com.hdyy.web.bean.WebDoctor;
import com.hdyy.web.dao.WebDoctorDao;
import com.hdyy.web.service.WebDoctorService;
import com.ys.tool.UnixTimeTool;
@Service
public class WebDoctorServiceImpl implements WebDoctorService{
@Resource
private WebDoctorDao webDoctorDao;
// @Transactional
// public List<WebDoctor> query() {
// List<WebDoctor> list =webDoctorDao.query();
// Iterator<WebDoctor> it =list.iterator();
// while(it.hasNext()) {
// WebDoctor bean= it.next();
// String date =bean.getWeb_course_date();
// System.out.println(date);
// String cid= bean.getWeb_course_id();
// System.out.println(cid);
// String did=bean.getWeb_doctor_id();
// System.out.println(did);
// String name =bean.getWeb_doctor_name();
// System.out.println(name);
// String title=bean.getWeb_course_title();
// System.out.println(title);
// }
//
// return list;
// }
@Transactional
public List<WebDoctor> query0(Map<String, Object> params) {
List<WebDoctor> list = webDoctorDao.query0(params);
Iterator<WebDoctor> it = list.iterator();
while (it.hasNext()) {
WebDoctor bean = it.next();
Date date= bean.getWeb_course_date();
String dateStr1 = new SimpleDateFormat("yyyy-MM-dd").format(date);
String datestr= bean.getDatestr();
datestr=dateStr1;
// datestr = datestr.substring(datestr.indexOf("-") + 1, datestr.lastIndexOf("-"));
datestr = datestr.substring(datestr.indexOf("-")+1, datestr.indexOf("-")+3);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s= sdf.format(bean.getWeb_course_date());
String ss=bean.getWeb_course_time();
// int i = Integer.parseInt(s);
}
return list;
}
@Transactional
public Long getCount(Map<String, Object> params) {
return webDoctorDao.getCount(params);
}
@Transactional
public boolean saveDoctor(WebDoctor doctorBean) {
// doctorBean.setWeb_doctor_id("12345");
// doctorBean.setWeb_doctor_name("我蒙");
// doctorBean.setWeb_doctor_company("123");
// doctorBean.setWeb_doctor_job("adf3");
// doctorBean.setWeb_doctor_introduce("111");
// doctorBean.setWeb_doctor_major("我222");
System.out.println("id2:" + doctorBean.getWeb_doctor_id());
System.out.println("name2:" + doctorBean.getWeb_doctor_name());
int i = webDoctorDao.save(doctorBean);
System.out.println(i);
if (i == 1) {
return true;
}else {
return false;
}
// System.out.println(o.getWeb_doctor_name());
// Iterator<WebDoctor> it = list.iterator();
// while (it.hasNext()) {
// WebDoctor bean = it.next();
// System.out.println(bean.getWeb_doctor_name());
// }
}
@Transactional
public boolean updateDoctor(WebDoctor doctorBean) {
System.out.println(doctorBean.getWeb_doctor_id());
System.out.println(doctorBean.getWeb_doctor_name());
int i = webDoctorDao.update(doctorBean);
System.out.println("i" + i);;
if (i == 1) {
return true;
}else {
return false;
}
}
@Transactional
public List<WebDoctor> getDoctorlist() {
List<WebDoctor> list = webDoctorDao.doctorlist();
return list;
}
@Transactional
public WebDoctor getDoctor(String id) {
WebDoctor webDoctor = (WebDoctor) webDoctorDao.getDoctor(id);
System.out.println("id: "+id);
System.out.println("name: "+webDoctor.getWeb_doctor_name());
return webDoctor;
}
}
然后写Controller 主要实现页面展示 和传值 还有ssm 的页面跳转
package com.hdyy.web.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.swing.text.StyledEditorKit.BoldAction;
import org.omg.PortableInterceptor.SUCCESSFUL;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hdyy.web.bean.WebDoctor;
import com.hdyy.web.common.RandomGUID;
import com.hdyy.web.service.WebDoctorService;
@Controller
public class WebDoctorController {
@Resource
WebDoctorService webDoctorService ;
ModelAndView mav = new ModelAndView();
// @RequestMapping("/query")
// @ResponseBody
// public String query(Model model) {
// List<WebDoctor> list = webDoctorService.query();
System.out.println(webDoctorService.query().get(0).getWeb_doctor_id());
System.out.println(webDoctorService.query().get(0).getWeb_doctor_name());
// model.addAttribute("list", list);
// return toJSON(list);
// }
//
public String toJSON(Object object) {
return JSON.toJSONString(object, SerializerFeature.DisableCircularReferenceDetect);
}
@RequestMapping("/query0")
@ResponseBody
public String query0(Integer page, Integer rows, String name) {
page =1;
rows =2;
Map<String, Object> params = new HashMap<String, Object>();
params.put("startSize", (page - 1) * rows);
params.put("rows", rows);
params.put("name", "%" + name + "%");
// System.err.println("----------------" + name);
List<WebDoctor> webDourses = webDoctorService.query0(params);
Long count = webDoctorService.getCount(params);
Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("total", count);
resultMap.put("rows", webDourses);
return toJSON(resultMap);
}
@RequestMapping("/save")
public String save(String id, String name ,String company,String major,String job,String introduce) {
System.out.println("save");
System.out.println("id:" + id);
System.out.println("name:" + name);
WebDoctor bean = new WebDoctor();
bean.setWeb_doctor_id(id);
bean.setWeb_doctor_name(name);
bean.setWeb_doctor_company(company);
bean.setWeb_doctor_major(major);
bean.setWeb_doctor_job(job);
bean.setWeb_doctor_introduce(introduce);
boolean s= webDoctorService.saveDoctor(bean);
System.out.println(s);
if (s==true) {
return "success";
}else {
return "error";
}
}
@RequestMapping("/update")
public String update(String id, String name ,String company,String major,String job,String introduce) {
WebDoctor bean = new WebDoctor();
bean.setWeb_doctor_id(id);
bean.setWeb_doctor_name(name);
bean.setWeb_doctor_company(company);
bean.setWeb_doctor_major(major);
bean.setWeb_doctor_job(job);
bean.setWeb_doctor_introduce(introduce);
boolean s=webDoctorService.updateDoctor(bean);
System.out.println("update" + s);
if (s==true) {
return "success";
}else {
return "error";
}
}
@RequestMapping("/doctorlist")
public ModelAndView doctorlist() {
List list = webDoctorService.getDoctorlist();
System.out.println("list: "+list.size());
mav.addObject("list",list);
mav.setViewName("doctorlist");
return mav;
}
@RequestMapping("/doctor")
public String doctor() {
return "doctor";
}
@RequestMapping("/adddoctor")
public String adddoctor() {
return "adddoctor";
}
@RequestMapping("/updatedoctor")
public ModelAndView updatedoctor(String id) {
WebDoctor bean = webDoctorService.getDoctor(id);
System.out.println("name2: "+bean.getWeb_doctor_name());
mav.addObject("hello", bean);
mav.setViewName("updatedoctor");
return mav;
}
}
然后写jsp页面 在jsp页面获取值的时候
doctorlist 的getAttribute(“list”); 这个list 要和上面mav.addObject(“list”,list);这里的值对应
<%@page import="org.springframework.context.annotation.Bean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.hdyy.web.bean.WebDoctor"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
List list =(List)request.getAttribute("list");
Iterator it = list.iterator();
%>
<!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>
</head>
<body>
<input type=button value=添加 onclick="javascript: window.open('http://127.0.0.1:8083/web/adddoctor.html', 'main')" />
医生列表
<table border=1 cellpadding="0" cellspacing="0">
<% while(it.hasNext()) { WebDoctor bean=(WebDoctor)it.next(); %>
<tr>
<td><%=bean.getWeb_doctor_name() %></td>
<td><%=bean.getWeb_doctor_company() %></td>
<td><%=bean.getWeb_doctor_major() %></td>
<td><%=bean.getWeb_doctor_job() %></td>
<td><%=bean.getWeb_doctor_introduce() %></td>
<td><input type=button value=修改 onclick="javascript: window.open('http://127.0.0.1:8083/web/updatedoctor.html?id=<%=bean.getWeb_doctor_id() %>', 'main')" /></td>
</tr>
<% } %>
</table>
</body>
</html>