SSM学生健康后台管理系统(小型demo)二完成entity层、dao层、service层
1.entity层
其实我们可以用lombok工具,不需要生成get、set、toString、构造等方法
StudentInfo 实体类
package com.studentheathly.entity;
import org.springframework.stereotype.Component;
@Component
public class StudentInfo{
private int sId;
private String sName;
private int sAge;
private String sIdcard;
private String sPhoneno;
private String sSex;
public StudentInfo() {
}
public StudentInfo(int sId, String sName, int sAge, String sIdcard, String sPhoneno, String sSex) {
this.sId = sId;
this.sName = sName;
this.sAge = sAge;
this.sIdcard = sIdcard;
this.sPhoneno = sPhoneno;
this.sSex = sSex;
}
public int getsId() {
return sId;
}
public void setsId(int sId) {
this.sId = sId;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public int getsAge() {
return sAge;
}
public void setsAge(int sAge) {
this.sAge = sAge;
}
public String getsIdcard() {
return sIdcard;
}
public void setsIdcard(String sIdcard) {
this.sIdcard = sIdcard;
}
public String getsPhoneno() {
return sPhoneno;
}
public void setsPhoneno(String sPhoneno) {
this.sPhoneno = sPhoneno;
}
public String getsSex() {
return sSex;
}
public void setsSex(String sSex) {
this.sSex = sSex;
}
@Override
public String toString() {
return "StudentInfoService{" +
"sId=" + sId +
", sName='" + sName + '\'' +
", sAge=" + sAge +
", sIdcard='" + sIdcard + '\'' +
", sPhoneno='" + sPhoneno + '\'' +
", sSex='" + sSex + '\'' +
'}';
}
}
StudentState 实体类
package com.studentheathly.entity;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class StudentState {
private int bId;
private String bodyInfo;
private int sId;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date addTime;
public StudentState() {
}
@Override
public String toString() {
return "StudentStateMapper{" +
"bId=" + bId +
", bodyInfo='" + bodyInfo + '\'' +
", sId=" + sId +
", addTime=" + addTime +
'}';
}
public StudentState(int bId, String bodyInfo, int sId, Date addTime) {
this.bId = bId;
this.bodyInfo = bodyInfo;
this.sId = sId;
this.addTime = addTime;
}
public int getbId() {
return bId;
}
public void setbId(int bId) {
this.bId = bId;
}
public String getBodyInfo() {
return bodyInfo;
}
public void setBodyInfo(String bodyInfo) {
this.bodyInfo = bodyInfo;
}
public int getsId() {
return sId;
}
public void setsId(int sId) {
this.sId = sId;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
}
2.dao层编写所需要用到的方法
StudentInfoMapper 接口
package com.studentheathly.dao;
import com.studentheathly.entity.StudentInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface StudentInfoMapper {
int addStudentInfo(StudentInfo studentInfo);
List<StudentInfo> getStudentInfo();
int deleteStudentInfoById(@Param("sId") int sId);
}
StudentInfoMapper.xml 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.studentheathly.dao.StudentInfoMapper">
<insert id="addStudentInfo" parameterType="com.studentheathly.entity.StudentInfo">
insert into studentinfo(sname,sage,sidcard,sphoneNo,ssex)
values (#{sName},#{sAge},#{sIdcard},#{sPhoneno},#{sSex})
</insert>
<select id="getStudentInfo" resultType="com.studentheathly.entity.StudentInfo">
select * from studentinfo
</select>
<delete id="deleteStudentInfoById" parameterType="com.studentheathly.entity.StudentInfo">
delete from studentinfo where sid=#{sId}
</delete>
</mapper>
StudentStateMapper 接口
package com.studentheathly.dao;
import com.studentheathly.entity.StudentState;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface StudentStateMapper {
int addStudentState(StudentState studentState);
List<StudentState> getStudentState();
List<StudentState> getStudentStateBySid(@Param("sId") int sId);
}
StudentStateMapper.xml 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.studentheathly.dao.StudentStateMapper">
<insert id="addStudentState" parameterType="com.studentheathly.entity.StudentState">
insert into studentstate(bodyinfo,sid,addtime)
values (#{bodyInfo},#{sId},#{addTime})
</insert>
<select id="getStudentState" resultType="com.studentheathly.entity.StudentState">
select * from studentstate
</select>
<select id="getStudentStateBySid" resultType="com.studentheathly.entity.StudentState">
select * from studentinfo
left join studentstate
on studentinfo.sid=studentstate.sid
where studentinfo.sid=#{sId}
</select>
</mapper>
注意:
1. 接口绑定不要绑定到实体类,是dao层的接口
2. 返回值不要写错
3. 用ResultMap时注意id要与select标签中resultMap一致
3.service层
StudentInfoService 接口
package com.studentheathly.service;
import com.studentheathly.entity.StudentInfo;
import java.util.List;
public interface StudentInfoService {
int addStudentInfo(StudentInfo studentInfo);
List<StudentInfo> getStudentInfo();
int deleteStudentInfoById(int sId);
}
StudentInfoServiceImpl 实现类
package com.studentheathly.service;
import com.studentheathly.dao.StudentInfoMapper;
import com.studentheathly.entity.StudentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentInfoServiceImpl implements StudentInfoService{
@Autowired
private StudentInfoMapper studentInfoMapper;
@Override
public int addStudentInfo(StudentInfo studentInfo) {
return studentInfoMapper.addStudentInfo(studentInfo);
}
@Override
public List<StudentInfo> getStudentInfo() {
return studentInfoMapper.getStudentInfo();
}
@Override
public int deleteStudentInfoById(int sId) {
return studentInfoMapper.deleteStudentInfoById(sId);
}
}
StudentStateService 接口
package com.studentheathly.service;
import com.studentheathly.entity.StudentState;
import java.util.List;
public interface StudentStateService {
int addStudentState(StudentState studentState);
List<StudentState> getStudentState();
List<StudentState> getStudentStateBySid(int sId);
}
StudentStateServiceImpl 实现类
package com.studentheathly.service;
import com.studentheathly.dao.StudentStateMapper;
import com.studentheathly.entity.StudentState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class StudentStateServiceImpl implements StudentStateService{
@Autowired
private StudentStateMapper studentStateMapper;
@Override
public int addStudentState(StudentState studentState) {
return studentStateMapper.addStudentState(studentState);
}
@Override
public List<StudentState> getStudentState() {
return studentStateMapper.getStudentState();
}
@Override
public List<StudentState> getStudentStateBySid(int sId) {
return studentStateMapper.getStudentStateBySid(sId);
}
}
service层基本与dao层方法一致,后期如遇到复杂的业务逻辑,那么service层将完全不会与dao层一致,因此项目作为练习并没有写复杂的业务,如果有兴趣的小伙伴可以去试试一下复杂的业务逻辑处理的编写
三为Controller层及前端展示,完成所有功能