接下来说的是springboot+jpa+mysql中的基本的层
一、entity
这里也可以是domain,也就是定义域或者说实体类,与数据库中的表相对应,通常在类的上方有@Entity注解,@Table(name=“表的名字,如果没有这个注解表名默认是类的名字”)。 也可以在这个包中写一些普通的返回类或者参数对象类,就不需要注解。
二、repository
这里也可以是dao, 数据库访问对象,主要的功能就是用于进行数据操作的,在程序的标准开发架构中属于数据访问层的操作,类的上方有@Repository,这个包中的类都是接口,它们 都extends CrudRepository<Object,Object> 第一个Object是对应的实体类,第二个是id的类型(注意是封装类,比如Long、Integer),其中的方法有简单的增删改查,也可以书写自己的sql查询语句只要在方法上使用@Query(value=sql,nativeQuery = true)即可注意返回值
@Repository
public interface RecordRepository extends CrudRepository<Record, Long> {
List<Record> findByUid(long uid);
Record findByBookCopyIdAndName(int copyId, String name);
Record findByBookCopyIdAndReturnTime(int id, String time);
//找到了借阅和预约的总数量
@Query(value = "select count(id) from record where uid=?1 and return_time is null",nativeQuery = true)
int findCountByUid(Long uid);
三、service
方法层,使用repository中的方法,处理数据,一般还有一个serviceImp接口层,便于维护管理,@Service 注解
四、controller
接收前端传的数据,调用service层中的方法,返回所需的数据,有@RestController注解
另外还有security层进行安全管理,config层进行配置如跨域问题等,exception层处理异常等