为什么Appraise 服务接口和MyBatis 的 Mapper接口要放一样的方法
时间: 2023-12-10 10:06:14 浏览: 29
Appraise 服务接口和 MyBatis 的 Mapper 接口放一样的方法,是因为它们的目的都是对 Appraise 数据库表进行操作。但是它们所处的层次不同,有着不同的职责和作用。
MyBatis 的 Mapper 接口是用来定义与数据库交互的方法,具体实现是由 MyBatis 框架根据 SQL 配置文件自动生成的。Mapper 接口中的方法名和 SQL 配置文件中的 SQL 语句名称是一一对应的。
而 Appraise 服务接口是应用程序中定义的服务接口,位于服务层。它的作用是封装业务逻辑,提供给控制器或其他服务使用。服务接口中的方法名通常和业务功能相关联,不一定要和数据库操作一一对应。
在实际开发中,服务接口中的方法通常会调用 MyBatis 的 Mapper 接口来完成具体的数据库操作。这样可以将数据访问与业务逻辑分离,提高代码的可维护性和可扩展性。
相关问题
这串代码是什么意思public interface AppraiseMapper { Appraise getAppraiseById(Integer id); // 查询所有 List<Appraise> getAllAppraises(); List<Appraise> getAll(); // 根据id删除 int deleteAppraiseById(Integer id); // 添加 int addAppraise(Appraise appraise); // 更新 int updateAppraise(Appraise appraise); List<Appraise> query(String name); }
这段代码是一个 MyBatis 的 Mapper 接口,用来对数据库中的 Appraise 表进行操作。
其中包含了一些方法:
- getAppraiseById(Integer id):根据 ID 查询 Appraise 记录。
- getAllAppraises() 和 getAll():查询 Appraise 表中所有记录。
- deleteAppraiseById(Integer id):根据 ID 删除 Appraise 记录。
- addAppraise(Appraise appraise):添加一条 Appraise 记录。
- updateAppraise(Appraise appraise):更新一条 Appraise 记录。
- query(String name):根据 Appraise 的 name 字段进行模糊查询。
这些方法的实现是由 MyBatis 框架根据 SQL 配置文件自动生成的。在实际使用时,需要在 XML 文件中定义 SQL 语句,并将其与对应的方法进行关联。
这串代码是什么意思public interface IAppraiseService { Appraise getAppraiseById(Integer id); List<Appraise> getAllAppraises(); List<Appraise> getAll(); int deleteAppraiseById(Integer id); int addAppraise(Appraise appraise); int updateAppraise(Appraise appraise); List<Appraise> query(String name); }
这段代码是一个 Appraise 服务接口,定义了对 Appraise 数据库表的一些基本操作。
该接口包含了一些方法:
- getAppraiseById(Integer id):根据 ID 查询 Appraise 记录。
- getAllAppraises() 和 getAll():查询 Appraise 表中所有记录。
- deleteAppraiseById(Integer id):根据 ID 删除 Appraise 记录。
- addAppraise(Appraise appraise):添加一条 Appraise 记录。
- updateAppraise(Appraise appraise):更新一条 Appraise 记录。
- query(String name):根据 Appraise 的 name 字段进行模糊查询。
这些方法的具体实现需要在服务层进行,服务层通常调用 DAO 层提供的方法来完成具体的数据库操作。这里的接口只定义了方法的声明,具体的实现需要在实现类中进行。