UJCMS后端二次开发教程08-Mapper接口

2023-08-30 13:54 阅读

MyBatis使用Mapper接口操作数据库。

Mapper接口

com.ujcms.cms.ext.mapper.ExampleMapper

@Mapper
@Repository
public interface ExampleMapper {
    /**
     * 插入数据
     *
     * @param bean 实体对象
     * @return 插入条数
     */
    int insert(Example bean);

    /**
     * 更新数据
     *
     * @param bean 实体对象
     * @return 更新条数
     */
    int update(Example bean);

    /**
     * 删除数据
     *
     * @param id 主键ID
     * @return 删除条数
     */
    int delete(Integer id);

    /**
     * 根据主键获取数据
     *
     * @param id 主键ID
     * @return 实体对象。没有找到数据,则返回 {@code null}
     */
    @Nullable
    Example select(Integer id);

    /**
     * 根据查询条件获取列表
     *
     * @param queryInfo 查询条件
     * @return 数据列表
     */
    List<Example> selectAll(@Nullable @Param("queryInfo") QueryInfo queryInfo);
}

这里的接口和普通MyBatis接口并无区别。其中QueryInfo查询解析器解析查询字符串后,得到的信息,用于生成查询语句。

这个类代码生成器只生成一次,只要类文件存在,再次运行代码生成器不会再次生成或修改这个类。可以随意进行修改。

Spring扫描Mapper接口

mapper接口所在的包,主要使用Spring注解进行扫描。

com.ujcms.cms.ext.ContextConfig

@Configuration
@MapperScan("com.ujcms.cms.ext.mapper")
public class ContextConfig implements InitializingBean {
    ...
}
QQ咨询
电话
微信
微信扫码咨询