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); }