学生和班级的例子:
一个学生在某一时期只能在一个班级:张三在java开发班,一对一
一个班级肯定至少有一个或多个学生:java开发班有6个学生,一对多
那么。对应实际开发时如何处理呢?
表:Clazz和Student表如何设计
主表应该为Clazz,从表应该为Student
实体类
package com.huhong.model;
import java.io.Serializable;
import java.util.List;
public class Clazz implements Serializable {
private Integer id;
private String name;
private List students;//duo
private static final long serialVersionUID = 1L;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getStudents() {
return students;
}
public void setStudents(List students) {
this.students = students;
}
}
package com.huhong.model;
import java.io.Serializable;
public class Student implements Serializable {
private Integer id;
private String name;
private String sex;
private Clazz clazz;//1
private static final long serialVersionUID = 1L;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Clazz getClazz() {
return clazz;
}
public void setClazz(Clazz clazz) {
this.clazz = clazz;
}
}
mapper映射文件
id, name
delete from tb_clazz
where id = #{id,jdbcType=INTEGER}
insert into tb_clazz (id, name)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
insert into tb_clazz
id,
name,
#{id,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
update tb_clazz
name = #{name,jdbcType=VARCHAR},
where id = #{id,jdbcType=INTEGER}
update tb_clazz
set name = #{name,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
id, name, sex, clazz_id
delete from tb_student
where id = #{id,jdbcType=INTEGER}
insert into tb_student (id, name, sex,
clazz_id)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR},
#{clazz_id,jdbcType=INTEGER})
insert into tb_student
id,
name,
sex,
clazz_id,
#{id,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
#{clazz_id,jdbcType=INTEGER},
update tb_student
name = #{name,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
clazz_id = #{clazz_id,jdbcType=INTEGER},
where id = #{id,jdbcType=INTEGER}
update tb_student
set name = #{name,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
clazz_id = #{clazz_id,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
clazz.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page isELIgnored ="false" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Clazz Info
编号: ${clazz.id}
班级名称: ${clazz.name}
${stu.id}
${stu.name}
${stu.sex}
student.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page isELIgnored ="false" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Student Info
编号: ${student.id}
学生姓名: ${student.name}
学生性别:${student.sex}
班级编号:${student.clazz.id}
班级名称:${student.clazz.name}
控制层
package com.huhong.controller;
import com.huhong.model.Clazz;
import com.huhong.model.Student;
import com.huhong.service.ClazzService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Tired on 2018/4/11.
*/
@Controller
public class ClazzController {
@Resource
private ClazzService clazzService;
@RequestMapping("/showClazzInfo")
public ModelAndView getClazzInfo()
{
ModelAndView mv = new ModelAndView("clazz");
// this.addClazz();
Clazz clazz = clazzService.selectByPrimaryKey(2);
Clazz clazz1 = clazzService.selectByPrimaryKey(2);
//List students = clazz.getStudents();
mv.addObject("clazz",clazz);
//mv.addObject("students",students);
return mv;
}
public void addClazz()
{
Clazz clazz = new Clazz();
clazz.setId(2);
clazz.setName("机电1班");
clazzService.insertSelective(clazz);
System.out.println(clazz.getName());
}
}
package com.huhong.controller;
import com.huhong.model.Student;
import com.huhong.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
/**
* Created by Tired on 2018/4/12.
*/
@Controller
public class StudentController {
@Resource
private StudentService studentService;
@RequestMapping("/showStudentInfo")
public ModelAndView getStudentInfo()
{
ModelAndView mv = new ModelAndView("student");
Student student = studentService.selectByPrimaryKey(2);
mv.addObject("student",student);
return mv;
}
}