Mybtis注解

增加接口CategoryMapper ,并在接口中声明的方法上,加上注解
增加对CategoryMapper 的映射:
<mappers>
//com.shop.mapper包下的CategoryMapper接口
        <mapper class="com.shop.mapper.CategoryMapper"/> 
</mappers>
以后mapper内每增加一个接口,需要在mappers标签内增加对应的映射

或者直接映射包:(只要接口在该包下,就不用再写映射了)
<mappers>
//com.shop.mapper包
        <package name="com.shop.mapper"/>
</mappers>

基于注解的简单增删改查:
public interface CategoryMapper {
  
    @Insert(" insert into category ( name ) values (#{name}) ") 
    public int add(Category category); 
        
    @Delete(" delete from category where id= #{id} ") 
    public void delete(int id); 
      
    @Update("update category set name=#{name} where id=#{id} ") 
    public int update(Category category);  
        
    @Select(" select * from category ") 
    public List<Category> list(); 
}

多个参数:例如分页
需要在参数前加上注释@Param("参数名")
@Select("select * from comment where cbid=#{cbid} limit #{begin},#{pageSize}")
public List<Comment> Listpage(@Param("pageSize") int pageSize,@Param("begin") int begin,@Param("cbid") int cbid);

注解方式:一对多
查询所有Category,及Category下的Product
新增接口ProductMapper
注解@Select用于根据分类id获取产品集合
public interface ProductMapper {
    @Select(" select * from product where cid = #{cid}")
    public List<Product> listByCategory(int cid);
}
@Select注解获取Category类本身,@Results 通过@Result和@Many中调用ProductMapper.listByCategory()方法相结合,来获取一对多关系
public interface CategoryMapper {
    @Select(" select * from category ")
    @Results({ 
//下句可以省略
                @Result(property = "id", column = "id"),
//column = "id"对应Category的id的字段,select = "com.shop.mapper.ProductMapper.listByCategory")写全包名+接口名+方法名
//一对多用many=@Many(),一对多需要添加属性:javaType = List.class
                @Result(property = "products", javaType = List.class, column = "id", many = @Many(select = "com.shop.mapper.ProductMapper.listByCategory") )
            })
    public List<Category> list();
}

注解方式:多对一
CategoryMapper接口,提供get方法
public interface CategoryMapper {
    @Select(" select * from category where id = #{id}")
    public Category get(int id);
}
ProductMapper接口,提供list方法
public interface ProductMapper {
    @Select(" select * from product ")
    @Results({ 
//column="cid"对应product表中的cid,多对一用one=@One()
        @Result(property="category",column="cid",one=@One(select="com.how2java.mapper.CategoryMapper.get")) 
    })
    public List<Product> list();
}

注解方式:多对多
订单项与商品是多对一关系,订单与订单项是一对多关系
ProductMapper接口,提供 get方法:
public interface ProductMapper {
    @Select("select * from product_ where id = #{id}")
    public Product get(int id);
}
OrderItemMapper,提供listByOrder方法:
public interface OrderItemMapper {
    @Select(" select * from order_item_ where oid = #{oid}")
    @Results({ 
        @Result(property="product",column="pid",one=@One(select="com.how2java.mapper.ProductMapper.get")) 
    }) 
    public List<OrderItem> listByOrder(int oid);
}
OrderMapper,提供list方法
public interface OrderMapper {
    @Select("select * from order_")
     @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "orderItems", javaType = List.class, column = "id", 
                    many = @Many(select = "com.how2java.mapper.OrderItemMapper.listByOrder"))
            })      
    public List<Order> list();
}

注解方式:动态SQL
用script标签包围,然后像xml语法一样书写
例子:
@Select("<script>select * from comment <where>"
            + "<if test='cbid!=0'> and cbid=#{cbid}</if>"
            + "<if test='cuid!=0'> and cuid=#{cuid}</if>"
            + "</where></script>")

注解方式:返回insert语句的自增长id
例如:
@Insert("insert into discuss values(null,now(),#{dresult},#{event.eveid},1)")
@Options(useGeneratedKeys=true,keyProperty="did",keyColumn="did")
public int addDiscuss(Discuss dis);
返回的自增长id被储存到参数Discuss对象中
int did = dis.getDid();

转载于:https://www.cnblogs.com/snzd9958/p/10099042.html

MyBatis基于注解的使用相关推荐

  1. mybatis基于注解的入门案例

    mybatis基于注解的入门案例:             把IUserDao.xml移除,在dao接口的方法上使用@Select注解,并且指定SQL语句             同时需要在SqlMa ...

  2. Mybatis基于注解实现增删查改和多参数列表查询

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 在[Mybatis]Mybatis入门概述及第一个Mybatis实例实现增删改查 和[Myba ...

  3. spring整合mybatis基于注解

    数据库 /* Navicat MySQL Data Transfer Source Server         : mysql Source Server Version : 50549 Sourc ...

  4. mybatis基于注解(三)

    定义操作 user 表的注解接口 UserMapper.java package com.ys.annocation;import org.apache.ibatis.annotations.Dele ...

  5. 使用基于注解的mybatis时,利用反射和注解生成sql语句

    在开发时遇到一个问题,在使用基于注解的mybatis插入一个对象到mysql时,在写sql语句时需要列出对象的所有属性,所以在插入一个拥有10个以上属性的对象时sql语句就会变得很长,写起来也很不方便 ...

  6. MyBatis-学习笔记04【04.自定义Mybatis框架基于注解开发】

    Java后端 学习路线 笔记汇总表[黑马程序员] MyBatis-学习笔记01[01.Mybatis课程介绍及环境搭建][day01] MyBatis-学习笔记02[02.Mybatis入门案例] M ...

  7. 基于注解配置简单的SpringMVC+Mybatis

    SpringMVC+Mybatis框架配置过好几次了,现在貌似终于对这些东西有些了解,自己草草的总结一下,也是再学习的一个过程. 首先,准备jar包,这个就不在赘述了. 然后就来肉戏了,以下只是我的理 ...

  8. Mybatis:基于注解形式,传入List,返回List实体

    1)现在的需求是: 传入一个List<String>,返回查询到的List<Entity> 基于注解形式,我们采用in语句筛选. 2)现在的需求是: 传入一个List<S ...

  9. mybatis使用注解开发

    mybatis使用注解开发 面向接口编程 在之前我们是通过面向对象编程,但是在真正开发的时候我们会选择面向接口编程. 根本原因 : 解耦 , 可拓展 , 提高复用 , 分层开发中 , 上层不用管具体的 ...

最新文章

  1. python中文字符画、编写程序合理选取中文字符构造_编写程序,合理选取中文字符构造字符表,生成中文字符画。...
  2. 接口测试,接口协议以及常用接口测试工具介绍
  3. Blog_mini完整部署文档
  4. python:argparse命令行解析模块详解
  5. Interview with BOA
  6. flume+elasticsearch日志收集分析
  7. ServletConfig的详解
  8. Spring零配置之@Configuration注解详解
  9. 数据结构之外部排序:失败树
  10. 文后参考文献著录规则
  11. 干货 | 人工智能应用落地的关键成功要素
  12. Unity脚本:寻找血量最低的敌人
  13. 记TUP对话大师系列之-Jeffery Richter
  14. Linux 中设置计划任务(定时任务)
  15. 5W无线充SOC方案芯片FS68001封装SOP16和SOP8
  16. Python小爬虫之协程爬虫快速上手
  17. 如何在bat文件中切换盘符并执行命令
  18. 中国招聘网站调研报告
  19. Android 加载pdf文件
  20. matlab已知滤波器参数,求频响

热门文章

  1. MyBitis(iBitis)系列随笔之六:mybitis与spring集成
  2. mac下的抓包工具Charles
  3. MVC,三层架构,工厂模型,七层
  4. CentOS 6.0 VNC远程桌面配置
  5. Delphi中将DBGRID中的内容输出到WORD中
  6. CVE-2018-20169漏洞学习
  7. 【uva10829-求形如UVU的串的个数】后缀数组+rmq or 直接for水过
  8. 【转】Visio画用例模型图竟然没有include关系
  9. 待飞日记(第四天和第五天)
  10. Apache 'mod_accounting'模块SQL注入漏洞(CVE-2013-5697)