zoukankan      html  css  js  c++  java
  • @mapper注解

    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);
    }
  • 相关阅读:
    columns布局应用场景
    flex速记
    css属性选择器模糊匹配
    ydui的rem适配方案
    vscode搜索失效问题
    ESP8266 超声波测距模块HC-SR04
    树莓派PICO Wifi 无线网卡 esp82666接线图
    查看数据库容量大小
    面试官问我JVM内存结构,我真的是
    深入浅出Java内存模型
  • 原文地址:https://www.cnblogs.com/jthr/p/14142256.html
Copyright © 2011-2022 走看看