码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis 注解方式

时间:2017-04-23 14:59:56      阅读:434      评论:0      收藏:0      [点我收藏+]

标签:column   marker   sep   number   com   out   ges   rtp   scan   

 

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

<!--配置Mapper扫描模式,所有Mapper都继承SqlMapper接口  -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lovo.mapper" />
        <property name="markerInterface" value="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={
            @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=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 = 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={
            @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 注解方式

标签:column   marker   sep   number   com   out   ges   rtp   scan   

原文地址:http://www.cnblogs.com/JimCalark/p/6752438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!