任务-JPA-Competition
代码目录
实体类代码
/**
* 竞赛实体
*/
@Entity
@Table(name = "competition_table")
@EntityListeners(AuditingEntityListener.class)
public class Competition implements Serializable {
/**
* 数据库Id
*/
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
@Column(length = 36,name = "id")
private String id;
/**
* 年度
*/
@Column(name = "year")
private String year;
/**
* 赛事名称
*/
@Column(name = "name")
private String name;
/**
* 主办单位
*/
@Column(name = "organizer")
private String organizer;
/**
* 所属专业
*/
@Column(name = "major")
private String major;
/**
* 赛事的描述
*/
@Column(name = "description")
private String description;
Repo层
package com.itsen.Repo;
import com.itsen.Pojo.Competition;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
@Repository
public interface CompetitionRepository extends JpaRepository<Competition,String> , JpaSpecificationExecutor<Competition> {
//JPQL
@Query(value = "from Competition where id=?1")
Competition queryCompetitionById1(String id);
//
//native SQl
@Query(value = "select * from competition_table where id=?1",nativeQuery = true)
Competition queryCompetitionById2(String id);
}
Service层代码(实现类)
@Service
public class CompetitionServiceIml implements CompetitionService {
@Resource
private CompetitionRepository competitionRepository;
/*
新增赛事
*/
@Override
public Competition insertCompetition(Competition competition) {
return competitionRepository.save(competition);
}
/*
删除赛事
*/
@Override
public void deleteCompetition(String id) {
competitionRepository.deleteById(id);
}
/*
修改赛事
*/
@Override
public Competition updateCompetition(Competition competition) {
return competitionRepository.save(competition);
}
@Override
public Competition queryCompetition(String id) {
//JPQL
// return competitionRepository.queryCompetitionById1(id);
//native SQL
// return competitionRepository.queryCompetitionById2(id);
//JPA Specification
Specification<Competition> spc=new Specification<Competition>() {
@Override
public Predicate toPredicate(Root<Competition> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
Path<Object> path = root.get(Competition_.ID);
Predicate predicate = criteriaBuilder.equal(path, id);
return predicate;
}
};
return competitionRepository.findOne(spc).orElse(null);
}
}
Controller层代码
@RestController
@RequestMapping("/competitions")
public class CompetitionController {
@Resource
private CompetitionService competitionService;
/*
新增赛事
*/
@PostMapping("")
public Competition addCompetition(@RequestBody Competition competition)
{
return competitionService.insertCompetition(competition);
}
/*
删除赛事
*/
@DeleteMapping("/{id}")
public String deleteCompetition(@PathVariable("id") String id)
{
competitionService.deleteCompetition(id);
return "success";
}
/*
修改赛事
*/
@PutMapping("")
public Competition updateCompetition(@RequestBody Competition competition)
{
return competitionService.updateCompetition(competition);
}
/**
* 根据id查询赛事
*/
@GetMapping("/{id}")
public Competition queryCompetition(@PathVariable String id)
{
return competitionService.queryCompetition(id);
}
}
Service层代码(接口)
@Service
public interface CompetitionService {
/*
新增赛事
*/
Competition insertCompetition(Competition competition);
/*
删除赛事
*/
void deleteCompetition(String id);
/*
修改赛事
*/
Competition updateCompetition(Competition competition);
Competition queryCompetition(String id);
}
实体类JPa元模型
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Competition.class)
public abstract class Competition_ {
public static volatile SingularAttribute<Competition, String> major;
public static volatile SingularAttribute<Competition, String> year;
public static volatile SingularAttribute<Competition, String> organizer;
public static volatile SingularAttribute<Competition, String> name;
public static volatile SingularAttribute<Competition, String> description;
public static volatile SingularAttribute<Competition, String> id;
public static final String MAJOR = "major";
public static final String YEAR = "year";
public static final String ORGANIZER = "organizer";
public static final String NAME = "name";
public static final String DESCRIPTION = "description";
public static final String ID = "id";
}
模拟请求响应测试,以及数据库图
数据库
-
新增
-
删除
-
修改
-
查询