学生和班级的例子:
一个学生在某一时期只能在一个班级:张三在java开发班,一对一
一个班级肯定至少有一个或多个学生:java开发班有6个学生,一对多
那么。对应实际开发时如何处理呢?
表:Clazz和Student表如何设计
主表应该为Clazz,从表应该为Student
image.png
image.png
实体类
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<Student> 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<Student> getStudents() {
return students;
}
public void setStudents(List<Student> 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映射文件
<?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.huhong.dao.ClazzDao" >
<resultMap id="ClazzMap" type="com.huhong.model.Clazz" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<collection property="students" column="id" javaType="ArrayList"
ofType="com.huhong.model.Student"
select="com.huhong.dao.StudentDao.selectStudentByClazzId"
fetchType="lazy">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
</collection>
</resultMap>
<sql id="Base_Column_List" >
id, name
</sql>
<select id="selectByPrimaryKey" resultMap="ClazzMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from tb_clazz
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from tb_clazz
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.huhong.model.Clazz" >
insert into tb_clazz (id, name)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.huhong.model.Clazz" >
insert into tb_clazz
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.huhong.model.Clazz" >
update tb_clazz
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.huhong.model.Clazz" >
update tb_clazz
set name = #{name,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</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.huhong.dao.StudentDao" >
<resultMap id="BaseResultMap" type="com.huhong.model.Student" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="sex" property="sex" jdbcType="VARCHAR" />
<association property="clazz" javaType="com.huhong.model.Clazz">
<id property="id" column="cid"/>
<result property="name" column="cname"/>
</association>
</resultMap>
<sql id="Base_Column_List" >
id, name, sex, clazz_id
</sql>
<select id="selectStudentByClazzId" parameterType="int" resultMap="BaseResultMap">
select * from tb_student where clazz_id = #{id}
</select>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select s.*,c.id cid,c.name cname FROM tb_student s,tb_clazz c where c.id = s.clazz_id and s.id = #{id}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from tb_student
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.huhong.model.Student" >
insert into tb_student (id, name, sex,
clazz_id)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR},
#{clazz_id,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.huhong.model.Student" >
insert into tb_student
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="sex != null" >
sex,
</if>
<if test="clazz_id != null" >
clazz_id,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
#{sex,jdbcType=VARCHAR},
</if>
<if test="clazz_id != null" >
#{clazz_id,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.huhong.model.Student" >
update tb_student
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="sex != null" >
sex = #{sex,jdbcType=VARCHAR},
</if>
<if test="clazz_id != null" >
clazz_id = #{clazz_id,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.huhong.model.Student" >
update tb_student
set name = #{name,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
clazz_id = #{clazz_id,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
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"%>
<html>
<body>
<h2>Clazz Info</h2>
编号: ${clazz.id}<br>
班级名称: ${clazz.name}<br>
<c:forEach items="${students}" var="stu">
${stu.id}<br>
${stu.name}<br>
${stu.sex}<br>
</c:forEach>
</body>
</html>
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"%>
<html>
<body>
<h2>Student Info</h2>
编号: ${student.id}<br>
学生姓名: ${student.name}<br>
学生性别:${student.sex}<br>
班级编号:${student.clazz.id}<br>
班级名称:${student.clazz.name}
</body>
</html>
控制层
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<Student> 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;
}
}
网友评论