mybatis 常用注解

基础增删改查

package cn.xm.exam.mapper.common.custom;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;


public interface MessageCustomMapper {
    
    
    @Insert(" insert into message values (#{messageid,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{idcode,jdbcType=CHAR}, #{sex,jdbcType=VARCHAR}, #{birthday,jdbcType=DATE}, #{emptype,jdbcType=VARCHAR},#{isdispose,jdbcType=VARCHAR})")
    public int insertIntoByAnnotation(Message message) throws SQLException;
    
    @Select("select * from message where messageid = #{messageId}")
    public Message getMessageById(@Param("messageId")String messageId)throws SQLException;
    
    @Update("update message set name=#{name} where messageid=#{messageid}")
    public int updateMessageById(Message message)throws SQLException;
    
    @Select("select * from message where messageid = #{messageId}")
    public Map getMessageMapById(@Param("messageId")String messageId)throws SQLException;
    
    @Delete("delete from message where messageid = #{messageId}")
    public int deleteByMessageId(@Param("messageId")String messageid)throws SQLException;
    

}

高级注解-provider

@SelectProvider

@SelectProvider(type = EmployeeDynalSqlProvider.class , method = "selectWithParam")
List<Employee> selectWithParam(Map<String , Object> param);


@InsertProvider

    @InsertProvider(type = SellerBynalSqlProvider.class,method = "insertSeller")
    void save(Sellers seller);


@UpdateProvider

    @UpdateProvider(type = SellerBynalSqlProvider.class, method = "updateSeller")
    void update(Sellers seller);


@DeleteProvider

@DeleteProvider(type = EmployeeDynalSqlProvider.class , method = "deleteEmployee")
void deleteEmployee(Map<String , Object> param);

具体用法例如

public class UserProvider {

    /**
     * udpate
     * @param UserDO userDO
     * @return
     */
    public String updateSQL(final UserDO userDO) {
        return new SQL() {
            {
                UPDATE("t_user");
                SET("gmt_modified = now()");
                if (userDO.getUserName() != null) {
                    SET("user_name = #{userName}");
                }
                WHERE("id = #{id}}");
            }
        }.toString();
    }
}

很复杂,这种情况还是xml好用
 

高级注解-一对n

@One

    @Select(" select * from stuffs where id = #{id} ")
    @Results(
            value = {
                    @Result(column = "name",property = "name"),
                    @Result(column = "little_type",property = "littleTypes",
                    //one指示我们,查询出来的结果只有一个。
                    one = @One(       
                    //select = sql语句                  select="com.qingyunqifu.dao.LittleTypeDao.findLittleTypeById",
                    //及时加载  
                    fetchType = FetchType.EAGER))


            }
    )
    Stuffs findStuffById(Integer id);


@Many

    @Select(" select * from sellers where id=#{id} ")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "name", property = "name"),
            @Result(column = "manager_name", property = "managerName"),
            @Result(column = "tel", property = "tel"),
            @Result(column = "phone", property = "phone"),
            @Result(column = "qq_num", property = "qqNum"),
            @Result(column = "address", property = "address"),
            @Result(column = "remark", property = "remark"),
            @Result(column = "sell_arrange", property = "sellArrange"),
            @Result(column = "id",property = "stuffsList",
            //many指示我们,查询出来的结果有很多个
            many = @Many(
            //select = sql语句
            select = "com.qingyunqifu.dao.StuffDao.findAllStuffsBySellerId",
            //懒加载
            fetchType = FetchType.LAZY))
    })
    Sellers findAllStuffsBySellerId(Integer id);


何时用@one何时用@many,取决于持久化对象中的属性是单一的还是List< T >。如果是单一的就用@one,是list就用@many。

多对多关系的查询,会用到子查询。

 select * form tb_article where id IN (select article_id form tb_item where order_id = #{id})

其余

@Results @Result 用来配置列名和属性 
@Param 给参数取名字 
@ Options附加配置
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值