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);
    }
  • 相关阅读:
    Java Web
    Tomcat学习笔记
    Java Web学习笔记(2)
    Java Web学习笔记(1)
    2017-2018-1 Java演绎法 小组会议及交互汇总
    【Alpha版本】冲刺阶段
    【Alpha版本】冲刺阶段
    【Alpha版本】冲刺阶段
    【Alpha版本】冲刺阶段
    【Alpha版本】冲刺阶段
  • 原文地址:https://www.cnblogs.com/jthr/p/14142256.html
Copyright © 2011-2022 走看看