教师实体基础功能的开发
1、一键生成batis实体类
参考https://blog.csdn.net/yangqinfeng1121/article/details/80183516
//1.1建立Teacher实体
package com.sdu.postgraduate.entity;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Date;
@Entity
public class Teacher {
@Id
private Integer teaId;
private String teaName;
private String teaGender;
private Date teaBirthday;
private Integer teaTelNum;
private String mailAddress;
private String position;
private String highestEdu;
private String finalEdu;
private Date teaDocTime;
private Date teaMasterTime;
private String teaDname;
private String personIntro;
private String searchDirection;
private String workAddress;
private byte[] teaPhoto;
public Integer getTeaId() {
return teaId;
}
public void setTeaId(Integer teaId) {
this.teaId = teaId;
}
public String getTeaName() {
return teaName;
}
public void setTeaName(String teaName) {
this.teaName = teaName == null ? null : teaName.trim();
}
public String getTeaGender() {
return teaGender;
}
public void setTeaGender(String teaGender) {
this.teaGender = teaGender == null ? null : teaGender.trim();
}
public Date getTeaBirthday() {
return teaBirthday;
}
public void setTeaBirthday(Date teaBirthday) {
this.teaBirthday = teaBirthday;
}
public Integer getTeaTelNum() {
return teaTelNum;
}
public void setTeaTelNum(Integer teaTelNum) {
this.teaTelNum = teaTelNum;
}
public String getMailAddress() {
return mailAddress;
}
public void setMailAddress(String mailAddress) {
this.mailAddress = mailAddress == null ? null : mailAddress.trim();
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position == null ? null : position.trim();
}
public String getHighestEdu() {
return highestEdu;
}
public void setHighestEdu(String highestEdu) {
this.highestEdu = highestEdu == null ? null : highestEdu.trim();
}
public String getFinalEdu() {
return finalEdu;
}
public void setFinalEdu(String finalEdu) {
this.finalEdu = finalEdu == null ? null : finalEdu.trim();
}
public Date getTeaDocTime() {
return teaDocTime;
}
public void setTeaDocTime(Date teaDocTime) {
this.teaDocTime = teaDocTime;
}
public Date getTeaMasterTime() {
return teaMasterTime;
}
public void setTeaMasterTime(Date teaMasterTime) {
this.teaMasterTime = teaMasterTime;
}
public String getTeaDname() {
return teaDname;
}
public void setTeaDname(String teaDname) {
this.teaDname = teaDname == null ? null : teaDname.trim();
}
public String getPersonIntro() {
return personIntro;
}
public void setPersonIntro(String personIntro) {
this.personIntro = personIntro == null ? null : personIntro.trim();
}
public String getSearchDirection() {
return searchDirection;
}
public void setSearchDirection(String searchDirection) {
this.searchDirection = searchDirection == null ? null : searchDirection.trim();
}
public String getWorkAddress() {
return workAddress;
}
public void setWorkAddress(String workAddress) {
this.workAddress = workAddress == null ? null : workAddress.trim();
}
public byte[] getTeaPhoto() {
return teaPhoto;
}
public void setTeaPhoto(byte[] teaPhoto) {
this.teaPhoto = teaPhoto;
}
}
//1.2建立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.sdu.postgraduate.dao.TeacherMapper" >
<resultMap id="BaseResultMap" type="com.sdu.postgraduate.entity.Teacher" >
<id column="tea_id" property="teaId" jdbcType="INTEGER" />
<result column="tea_name" property="teaName" jdbcType="CHAR" />
<result column="tea_gender" property="teaGender" jdbcType="CHAR" />
<result column="tea_birthday" property="teaBirthday" jdbcType="DATE" />
<result column="tea_tel_num" property="teaTelNum" jdbcType="INTEGER" />
<result column="mail_address" property="mailAddress" jdbcType="CHAR" />
<result column="position" property="position" jdbcType="CHAR" />
<result column="highest_edu" property="highestEdu" jdbcType="CHAR" />
<result column="final_edu" property="finalEdu" jdbcType="CHAR" />
<result column="tea_doc_time" property="teaDocTime" jdbcType="DATE" />
<result column="tea_master_time" property="teaMasterTime" jdbcType="DATE" />
<result column="tea_dname" property="teaDname" jdbcType="CHAR" />
<result column="person_intro" property="personIntro" jdbcType="CHAR" />
<result column="search_direction" property="searchDirection" jdbcType="CHAR" />
<result column="work_address" property="workAddress" jdbcType="CHAR" />
</resultMap>
<resultMap id="ResultMapWithBLOBs" type="com.sdu.postgraduate.entity.Teacher" extends="BaseResultMap" >
<result column="tea_photo" property="teaPhoto" jdbcType="LONGVARBINARY" />
</resultMap>
<sql id="Base_Column_List" >
tea_id, tea_name, tea_gender, tea_birthday, tea_tel_num, mail_address, position,
highest_edu, final_edu, tea_doc_time, tea_master_time, tea_dname, person_intro, search_direction,
work_address
</sql>
<sql id="Blob_Column_List" >
tea_photo
</sql>
<select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from teacher
where tea_id = #{teaId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from teacher
where tea_id = #{teaId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.sdu.postgraduate.entity.Teacher" >
insert into teacher (tea_id, tea_name, tea_gender,
tea_birthday, tea_tel_num, mail_address,
position, highest_edu, final_edu,
tea_doc_time, tea_master_time, tea_dname,
person_intro, search_direction, work_address,
tea_photo)
values (#{teaId,jdbcType=INTEGER}, #{teaName,jdbcType=CHAR}, #{teaGender,jdbcType=CHAR},
#{teaBirthday,jdbcType=DATE}, #{teaTelNum,jdbcType=INTEGER}, #{mailAddress,jdbcType=CHAR},
#{position,jdbcType=CHAR}, #{highestEdu,jdbcType=CHAR}, #{finalEdu,jdbcType=CHAR},
#{teaDocTime,jdbcType=DATE}, #{teaMasterTime,jdbcType=DATE}, #{teaDname,jdbcType=CHAR},
#{personIntro,jdbcType=CHAR}, #{searchDirection,jdbcType=CHAR}, #{workAddress,jdbcType=CHAR},
#{teaPhoto,jdbcType=LONGVARBINARY})
</insert>
<insert id="insertSelective" parameterType="com.sdu.postgraduate.entity.Teacher" >
insert into teacher
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="teaId != null" >
tea_id,
</if>
<if test="teaName != null" >
tea_name,
</if>
<if test="teaGender != null" >
tea_gender,
</if>
<if test="teaBirthday != null" >
tea_birthday,
</if>
<if test="teaTelNum != null" >
tea_tel_num,
</if>
<if test="mailAddress != null" >
mail_address,
</if>
<if test="position != null" >
position,
</if>
<if test="highestEdu != null" >
highest_edu,
</if>
<if test="finalEdu != null" >
final_edu,
</if>
<if test="teaDocTime != null" >
tea_doc_time,
</if>
<if test="teaMasterTime != null" >
tea_master_time,
</if>
<if test="teaDname != null" >
tea_dname,
</if>
<if test="personIntro != null" >
person_intro,
</if>
<if test="searchDirection != null" >
search_direction,
</if>
<if test="workAddress != null" >
work_address,
</if>
<if test="teaPhoto != null" >
tea_photo,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="teaId != null" >
#{teaId,jdbcType=INTEGER},
</if>
<if test="teaName != null" >
#{teaName,jdbcType=CHAR},
</if>
<if test="teaGender != null" >
#{teaGender,jdbcType=CHAR},
</if>
<if test="teaBirthday != null" >
#{teaBirthday,jdbcType=DATE},
</if>
<if test="teaTelNum != null" >
#{teaTelNum,jdbcType=INTEGER},
</if>
<if test="mailAddress != null" >
#{mailAddress,jdbcType=CHAR},
</if>
<if test="position != null" >
#{position,jdbcType=CHAR},
</if>
<if test="highestEdu != null" >
#{highestEdu,jdbcType=CHAR},
</if>
<if test="finalEdu != null" >
#{finalEdu,jdbcType=CHAR},
</if>
<if test="teaDocTime != null" >
#{teaDocTime,jdbcType=DATE},
</if>
<if test="teaMasterTime != null" >
#{teaMasterTime,jdbcType=DATE},
</if>
<if test="teaDname != null" >
#{teaDname,jdbcType=CHAR},
</if>
<if test="personIntro != null" >
#{personIntro,jdbcType=CHAR},
</if>
<if test="searchDirection != null" >
#{searchDirection,jdbcType=CHAR},
</if>
<if test="workAddress != null" >
#{workAddress,jdbcType=CHAR},
</if>
<if test="teaPhoto != null" >
#{teaPhoto,jdbcType=LONGVARBINARY},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.sdu.postgraduate.entity.Teacher" >
update teacher
<set >
<if test="teaName != null" >
tea_name = #{teaName,jdbcType=CHAR},
</if>
<if test="teaGender != null" >
tea_gender = #{teaGender,jdbcType=CHAR},
</if>
<if test="teaBirthday != null" >
tea_birthday = #{teaBirthday,jdbcType=DATE},
</if>
<if test="teaTelNum != null" >
tea_tel_num = #{teaTelNum,jdbcType=INTEGER},
</if>
<if test="mailAddress != null" >
mail_address = #{mailAddress,jdbcType=CHAR},
</if>
<if test="position != null" >
position = #{position,jdbcType=CHAR},
</if>
<if test="highestEdu != null" >
highest_edu = #{highestEdu,jdbcType=CHAR},
</if>
<if test="finalEdu != null" >
final_edu = #{finalEdu,jdbcType=CHAR},
</if>
<if test="teaDocTime != null" >
tea_doc_time = #{teaDocTime,jdbcType=DATE},
</if>
<if test="teaMasterTime != null" >
tea_master_time = #{teaMasterTime,jdbcType=DATE},
</if>
<if test="teaDname != null" >
tea_dname = #{teaDname,jdbcType=CHAR},
</if>
<if test="personIntro != null" >
person_intro = #{personIntro,jdbcType=CHAR},
</if>
<if test="searchDirection != null" >
search_direction = #{searchDirection,jdbcType=CHAR},
</if>
<if test="workAddress != null" >
work_address = #{workAddress,jdbcType=CHAR},
</if>
<if test="teaPhoto != null" >
tea_photo = #{teaPhoto,jdbcType=LONGVARBINARY},
</if>
</set>
where tea_id = #{teaId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKeyWithBLOBs" parameterType="com.sdu.postgraduate.entity.Teacher" >
update teacher
set tea_name = #{teaName,jdbcType=CHAR},
tea_gender = #{teaGender,jdbcType=CHAR},
tea_birthday = #{teaBirthday,jdbcType=DATE},
tea_tel_num = #{teaTelNum,jdbcType=INTEGER},
mail_address = #{mailAddress,jdbcType=CHAR},
position = #{position,jdbcType=CHAR},
highest_edu = #{highestEdu,jdbcType=CHAR},
final_edu = #{finalEdu,jdbcType=CHAR},
tea_doc_time = #{teaDocTime,jdbcType=DATE},
tea_master_time = #{teaMasterTime,jdbcType=DATE},
tea_dname = #{teaDname,jdbcType=CHAR},
person_intro = #{personIntro,jdbcType=CHAR},
search_direction = #{searchDirection,jdbcType=CHAR},
work_address = #{workAddress,jdbcType=CHAR},
tea_photo = #{teaPhoto,jdbcType=LONGVARBINARY}
where tea_id = #{teaId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.sdu.postgraduate.entity.Teacher" >
update teacher
set tea_name = #{teaName,jdbcType=CHAR},
tea_gender = #{teaGender,jdbcType=CHAR},
tea_birthday = #{teaBirthday,jdbcType=DATE},
tea_tel_num = #{teaTelNum,jdbcType=INTEGER},
mail_address = #{mailAddress,jdbcType=CHAR},
position = #{position,jdbcType=CHAR},
highest_edu = #{highestEdu,jdbcType=CHAR},
final_edu = #{finalEdu,jdbcType=CHAR},
tea_doc_time = #{teaDocTime,jdbcType=DATE},
tea_master_time = #{teaMasterTime,jdbcType=DATE},
tea_dname = #{teaDname,jdbcType=CHAR},
person_intro = #{personIntro,jdbcType=CHAR},
search_direction = #{searchDirection,jdbcType=CHAR},
work_address = #{workAddress,jdbcType=CHAR}
where tea_id = #{teaId,jdbcType=INTEGER}
</update>
</mapper>
//1.3建立mapper接口
@Mapper
public interface TeacherMapper {
int deleteByPrimaryKey(Integer teaId);
int insert(Teacher record);
int insertSelective(Teacher record);
Teacher selectByPrimaryKey(Integer teaId);
int updateByPrimaryKeySelective(Teacher record);
int updateByPrimaryKeyWithBLOBs(Teacher record);
int updateByPrimaryKey(Teacher record);
}
2、业务逻辑的编写
//2.1建立TeacherSerive
@Service
public class TeacherService {
@Resource
private TeacherMapper teacherMapper;
@Resource
private StudyProjectMapper studyProjectMapper;
@Resource
private StudentMapper studentMapper;
/**
* 根据teaID查询老师信息
* @param teaId
* @return
*/
public Teacher selectTeacherByPrimaryKey(Integer teaId){
return teacherMapper.selectByPrimaryKey(teaId);
}
/**
* 通过teaID加载该教师的全部学生 返回学生信息列表
* @param request
* @return
*/
@RequestMapping("/loadStudent")
public Result LoadStudent(@RequestBody Map<String, Object> request) {
int tea_id = (int) request.get("tea_id");
// List<Student> stu = teacherService.loadStudent(tea_id);
// Student s1 = stu.get(0);
// System.out.println(stu.size());
return Result.succ(teacherService.loadStudent(tea_id));
}
/**
* 根据包含查询条件(教师id、姓名、研究方法)的对象teacher加载教师列表
* 空对象则为全部导师
* @param request
* @return
*/
@RequestMapping("/loadTeacher")
public Result LoadTeacher(@RequestBody Map<String, Object> request) {
Integer tea_id = (int) request.get("tea_id");
String tea_name = (String) request.get("tea_name");
String search_direction = (String) request.get("search_direction");
Teacher a = new Teacher();
a.setTeaId(tea_id);
a.setTeaName(tea_name);
a.setSearchDirection(search_direction);
return Result.succ(teacherService.loadTeacher(a));
}
}
//2.2建立TeacherController
@RestController
@RequestMapping(value = "/teacher")
public class TeacherController {
@Resource
private TeacherService teacherService;
@RequestMapping("/test")
public Result test(@RequestBody Map<String, Object> request) {
int tea_id = (int) request.get("tea_id");
return Result.succ(teacherService.selectTeacherByPrimaryKey(tea_id));
}
}
使用postman进行测试