<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
`
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Entity
@Data
@DynamicUpdate
public class University implements Serializable {
/**
* 学校ID
*/
@Id
private String universityId;
/**
* 英文名称
*/
private String englishName;
/**
* 中文名称
*/
private String chineseName;
/**
* 校徽
*/
private String universityBadge;
/**
* 大学位置
*/
private String universityLocation;
/**
* 简介
*/
private String universityIntroduction;
/**
* 创建时间
*/
private Date createTime;
/**
* 修改时间
*/
private Date updateTime;
/**
* 专业信息
*/
@OneToMany(mappedBy = "university")
private List<UniversityMajor> majorList;
}
import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Entity
@Data
@DynamicUpdate
public class UniversityMajor implements Serializable {
@Id
private String universityMajorId;
/**
* 专业方向
*/
private String majorDirection;
/**
* 学时
*/
private Integer studyTime;
/**
* 开学时间
*/
private Integer startTime;
/**
* 开课类型
*/
private String startType;
/**
* 专业学费
*/
private BigDecimal majorTuition;
/**
* 申请费用
*/
private BigDecimal majorApplyMoney;
/**
* optional=false,表示 university不能为空
*/
@ManyToOne()
@JoinColumn(name = "university_id")
private University university;
@OneToOne
@JoinColumn(name = "major_id")
private Major major;
}
import lombok.Data;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
@Entity
@Data
@DynamicUpdate
public class Major implements Serializable {
/**
* 专业ID
*/
@Id
private Integer majorId;
/**
* 专业名称
*/
private String majorName;
/**
* 专业类型
*/
private String type;
/**
* 创建时间
*/
private Date createTime;
/**
* 修改时间
*/
private Date updateTime;
}
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/university")
public class UniversityController {
@Autowired
private UniversityService universityService;
@Autowired
private CommentService commentService;
@GetMapping("/{id}")
public ResultVO<UniversityHomeVO> query(@PathVariable String id) {
UniversityInfoVO universityInfoVO = new UniversityInfoVO();
// 获取大新信息并拷贝
University2UniversityInfoConverter.converter(universityService.findById(id), universityInfoVO);
// 获取五条评论
List<CommentDTO> commentDTOList = commentService.findByCommentTopNumber(id, 0, 5);
universityInfoVO.setCommentVOList(CommentDTO2CommentVOConverter.convert(commentDTOList));
return ResultVOUtil.success(universityInfoVO);
}
}
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class UniversityInfoVO implements Serializable {
private static final long serialVersionUID = -3895834204864685262L;
@JsonProperty("unvId")
private String universityId;
/**
* 英文名称
*/
@JsonProperty("enName")
private String englishName;
/**
* 中文名称
*/
@JsonProperty("chName")
private String chineseName;
/**
* 校徽
*/
@JsonProperty("unvBadge")
private String universityBadge;
/**
* 简介
*/
@JsonProperty("unvIntroduction")
private String universityIntroduction;
/**
* 地理位置
*/
@JsonProperty("unvLocation")
private String universityLocation;
/**
* 专业名称列表
*/
@JsonProperty("supportMajor")
private List<MajorVO> majorVOList;
}
@Data
public class UniversityHomeVO {
/**
* 学校ID
*/
@Id
private String universityId;
/**
* 英文名称
*/
private String englishName;
/**
* 中文名称
*/
private String chineseName;
/**
* 校徽
*/
private String universityBadge;
}
import java.util.List;
import java.util.stream.Collectors;
public class UniversityToUniversityHomeVO {
public static List<UniversityHomeVO> converter(List<University> universityList) {
return universityList.stream()
.map(UniversityToUniversityHomeVO::converter)
.collect(Collectors.toList());
}
public static UniversityHomeVO converter(University university) {
UniversityHomeVO homeVO = new UniversityHomeVO();
BeanUtils.copyProperties(university, homeVO);
return homeVO;
}
}
import java.util.ArrayList;
import java.util.List;
public class University2UniversityInfoConverter {
public static void converter(University university, UniversityInfoVO universityInfoVO) {
universityInfoVO.setUniversityId(university.getUniversityId());
universityInfoVO.setChineseName(university.getChineseName());
universityInfoVO.setEnglishName(university.getEnglishName());
universityInfoVO.setUniversityBadge(university.getUniversityBadge());
universityInfoVO.setUniversityLocation(university.getUniversityLocation());
universityInfoVO.setUniversityIntroduction(university.getUniversityIntroduction());
universityInfoVO.setUniversityLocation(university.getUniversityLocation());
universityInfoVO.setMajorVOList(getMajorList(subList(university.getMajorList(), 0, 6)));
}
}
import java.util.List;
public interface UniversityService {
University findById(String universityId);
List<University> findByChineseNameLike(String chineseName);
List<University> findByEnglishNameLike(String englishName);
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class UniversityServiceImpl implements UniversityService {
@Autowired
private UniversityRepository universityRepository;
@Override
public University findById(String universityId) {
return universityRepository.findById(universityId).orElse(null);
}
@Override
public List<University> findByChineseNameLike(String chineseName) {
return universityRepository.findByChineseNameLike(chineseName);
}
@Override
public List<University> findByEnglishNameLike(String englishName) {
return universityRepository.findByEnglishNameLike(englishName);
}
}
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface UniversityRepository extends JpaRepository<University, String> {
List<University> findByChineseNameLike(String chineseName);
List<University> findByEnglishNameLike(String englishName);
}