前台往后台传id

js页面

var stuid;
$(function () {
var request = {
QueryString : function(val) {
var uri = window.location.search;
var re = new RegExp("" +val+ "\=([^\&\?]*)", "ig");
return ((uri.match(re))?(uri.match(re)[0].substr(val.length+1)):null);
}
}
stuid =request.QueryString("stuid")

})
layui.use(['layer', 'table', 'jquery', 'form'], function(){
var layer = layui.layer //弹层
,table = layui.table //表格
,$ = layui.jquery
,form = layui.form;

//执行一个 table 实例
table.render({
elem: '#empTable'
,totalRow: true
,id:'bookReload'
,height: 620
,page:true
,url: 'tem/findOne?stuid='+stuid //数据接口
,title: '信息表'
,toolbar: 'default' //开启工具栏,此处显示默认图标,可以自定义模板,详见文档
,cols: [[ //表头
{field: 'id', title: '编号', sort: true, width:180}
,{field: 'createtime', title: '创建时间', width:180}
,{field: 'wd', title: '温度', width:180,templet: function(d) {
var span="";
if (d.wd==null){
return ""
}
if (d.wd>=37.3){
return span += '<span style="color: red">'+d.wd+'</span>';
}
return d.wd;
}}
,{field: 'student', title: '姓名', width:180, templet: function(d){
// var span="";
// if (d.wd>=37.3){
// return span += '<span style="color: red">'+d.student.stuname+'</span>';
// }
return d.student.stuname;
}
}
,{field: 'student', title: '性别', width:180 ,templet: function(d){
return d.student.sex;
}}
]]
});
});
Controller
创建 session对象 转换为 id所在类的对象,通过session对象的get方法获取到前台传来的id,

public String findTem(HttpSession session){

Student stu = (Student) session.getAttribute("stu");
JsonData jsonData = temService.findTem(stu.getStuid());
return JSON.toJSONString(jsonData);
}

添加和修改
public String saveTem(Temperature temperature,HttpSession session){
Student stu = (Student) session.getAttribute("stu");
temperature.setStudent(stu);
String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
temperature.setCreatetime(format);
JsonData jsonData = temService.saveTem(temperature);
return JSON.toJSONString(jsonData);
}

详情查询
@RequestMapping("/findOne")
public String findOne(int stuid){
JsonData jsonData = temService.findTem(stuid);
return JSON.toJSONString(jsonData);
}
 
serviceImpl
(老样子没啥可说了)
package com.hp.service.impl;

import com.hp.mapper.TemMapper;
import com.hp.pojo.Temperature;
import com.hp.service.TemService;
import com.hp.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class TemServiceImpl implements TemService {
@Autowired
private TemMapper temMapper;
@Override
public JsonData findTem(Integer stuid) {
List<Temperature> tem = temMapper.findTem(stuid);
return JsonData.buildSuccess(tem);
}

@Override
public JsonData saveTem(Temperature temperature) {
int i=0;
if (temperature.getId()!=null){
i=temMapper.editTem(temperature);
}else {
i=temMapper.addTem(temperature);
}
if (i>0){
return JsonData.buildSuccess("保存或修改成功");
}
return JsonData.buildError("保存或修改失败");
}

@Override
public JsonData delTem(int id) {
int i = temMapper.delTem(id);
if (i>0){
return JsonData.buildSuccess("del success");
}
return JsonData.buildError("del error");
}

}
mapper.xml
<?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.hp.mapper.TemMapper">
<resultMap id="Tem" type="com.hp.pojo.Temperature">
<id property="id" column="id"></id>
<result property="createtime" column="createtime"/>
<result property="wd" column="wd"/>
<association property="student" column="stuid" javaType="com.hp.pojo.Student">
<id property="stuid" column="stuid"/>
<result property="username" column="username"/>
<result property="pwd" column="pwd"/>
<result property="stuname" column="stuname"/>
<result property="sex" column="sex"/>
<result property="status" column="status"/>
</association>
</resultMap>

<select id="findTem" resultMap="Tem">
SELECT t.*, s.username,s.pwd,s.stuname,s.sex FROM temperature t, student s WHERE s.stuid = t.stuid and s.stuid=#{stuid}
</select>
<insert id="addTem">
insert into temperature (createtime,wd,stuid) VALUES(#{createtime},#{wd},#{student.stuid})
</insert>
<update id="editTem">
update temperature set createtime=#{createtime},wd=#{wd},stuid=#{student.stuid} where id=#{id};
</update>
<delete id="delTem">
delete from temperature where id=#{id}
</delete>
</mapper>


posted @ 2021-03-04 19:06  翘中之楚  阅读(503)  评论(0编辑  收藏  举报