1.标记这是一个映射接口,这样子写还是需要写xml文件
package com.atguigu.springcloud.dao; import com.atguigu.springcloud.entities.Payment; import com.atguigu.springcloud.entities.PaymentExample; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; @Mapper //使用了@mapper 标记这是一个映射接口 public interface PaymentMapper { int countByExample(PaymentExample example); int deleteByExample(PaymentExample example); int deleteByPrimaryKey(Long id); int insert(Payment record); int insertSelective(Payment record); List<Payment> selectByExample(PaymentExample example); Payment selectByPrimaryKey(Long id); int updateByExampleSelective(@Param("record") Payment record, @Param("example") PaymentExample example); int updateByExample(@Param("record") Payment record, @Param("example") PaymentExample example); int updateByPrimaryKeySelective(Payment record); int updateByPrimaryKey(Payment record); }
2:向下面这样子写的话,把mapper这个DAO交給Spring管理 ,不用再写mapper映射xml文件,自动根据这个添加@Mapper注解的接口生成一个实现类
//UserDAO import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import entity.User; /** * 添加了@Mapper注解之后这个接口在编译时会生成相应的实现类 * * 需要注意的是:这个接口中不可以定义同名的方法,因为会生成相同的id * 也就是说这个接口是不支持重载的 */ @Mapper public interface UserDAO { @Select("select * from user where name = #{name}") public User find(String name); @Select("select * from user where name = #{name} and pwd = #{pwd}") /** * 对于多个参数来说,每个参数之前都要加上@Param注解, * 要不然会找不到对应的参数进而报错 */ public User login(@Param("name")String name, @Param("pwd")String pwd); }
分类:
spring in action
, springboot
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek “源神”启动!「GitHub 热点速览」
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器