首页 > 代码库 > mybatis 注解方式

mybatis 注解方式

 

在spring 的配置文件中添加配置

<!--配置Mapper扫描模式,所有Mapper都继承SqlMapper接口  -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="http://www.mamicode.com/com.lovo.mapper" />
        <property name="markerInterface" value="http://www.mamicode.com/com.lovo.mapper.SqlMapper" />
    </bean>

 

包名为"com.lovo.mapper; 在包中有标志接口SqlMapper

package com.lovo.mapper;

public interface SqlMapper {

}

 

所有的mapper 继承于标志接口

  public interface OutContractMapper extends SqlMapper {

   .....................................

 

}

 

一、查询

1、

    @Select("select * from c_cbdcarport limit #{param1},#{param2}")
    @Results(value=http://www.mamicode.com/{
            @Result(property="id",column="id",id=true),
            @Result(property="address",column="address"),
            @Result(property="carportnumber",column="carportnumber"),
            @Result(property="areanumber",column="areanumber"),
            @Result(property="status",column="status"),
            
    })
    public List<CBDCarportEntity> getAllCdbcarport(int pageNum,int rowNum);

 

2、

     @Select("select count(*) as rows from c_cbdcarport")
      @ResultType(value=http://www.mamicode.com/int.class)
      public int getRows();

3、

        @Select("<script>"
            + " select count(*) as rows from c_contract where 1=1 "
            + "<if test=\"param1 != null and param1 !=‘‘\">"
            + "and contractnumber like concat(‘%‘,#{param1},‘%‘) "
            + "</if>"
            + "<if test=\"param2 != null and param2 !=‘‘\">"
            + "and startTime &gt;= #{param2} "
            + "</if>"
            + "<if test=\"param3 != null and param3 !=‘‘\">"
            + "and endTime &lt;= #{param3} "
            + "</if>"
            + "</script>")
    @ResultType(value = http://www.mamicode.com/int.class)
    public int getRows(String number,String startTime,String endTime);

4、

    @Select("<script>"
            +"select * from c_publishinfo where status=‘0‘"
            + "<if test=\"param1!=null and param1 !=‘‘\">"
            + "and starttime &gt;=#{param1}"
            +"</if>"
            +"<if test=\"param2 !=null and param2 !=‘‘\">"
            +"and endtime &lt;=#{param2}"
            +"</if>"
            + " limit #{param3},#{param4}"
            +"</script>")
    @Results(value=http://www.mamicode.com/{
            @Result(property="id",column="id",id=true),
            @Result(property="address",column="address"),
            @Result(property="carportnum",column="carportnumber"),
            @Result(property="permitnumber",column="permitnumber"),
            @Result(property="carportpic",column="carportpic"),
            @Result(property="starttime",column="starttime"),
            @Result(property="endtime",column="endtime"),
            @Result(property="price",column="price"),
            @Result(property="status",column="status"),
            @Result(property="lender_id",column="lender_id"),
            @Result(property="time",column="time"),
            @Result(property="lender",column="lender_id",
            one=@One(select="com.lovo.mapper.LenderMapper.getLenderEntityById"))
           
    })
    public List<PublishinfoEntity> getAllPublish(String starttime,String endtime,int page,int pageSize);

 

二、添加

  @Insert("insert into c_cbdcarport values(null,#{address},#{carportnumber},#{areanumber},#{status},null,null)")
      @Options(useGeneratedKeys=true,keyProperty="id")
    public void addCdbcarport(CBDCarportEntity Cdbcarport);

 

三、删除

     @Delete("delete from c_cbdcarport where id = #{id}")
    public void delCdbcarportById(int id);

 

四、修改

  @Update("update c_cbdcarport set address=#{address},carportnumber=#{carportnumber}"
        +",areanumber=#{areanumber},status=#{status} where id=#{id}")
    public void updateCdbcarport(CBDCarportEntity Cdbcarport);

 

 


   

mybatis 注解方式