zoukankan      html  css  js  c++  java
  • 注解

    MyBatis注解的基本使用方法 

       1)介绍

           MyBatis的注解方式就是将SQL语句直接写在接口上。这种方式的优点是,对于需求比较简单的系统,效率较高。缺点是,当SQL有变化时需要重新编译代码,一般情况下不建议使用注解方式。

       2@Select注解

              以下案例使用了案例1中的UserMapper接口:

    1、UserMapper.xml文件中的select节点注释(id为getUserById)

    2、UserMapper.java接口中的getUserById方法上使用@Select注解

    3、运行Demo测试代码中的selectUser方法

              通过以上案例可以看出,在简单的SQL操作中,使用注解无疑使程序变得更简单。

       3@Select注解与@Results注解的联用    

    XML中的resultMap元素有一个对应Java的注解@Results,使用这个注解来实现属性映射。

          

    1、StudentMapper.xml文件中的select节点和resultMap注释掉  

    2、StudentMapper.java接口中添加@Select注解和@Results注解

            3、在使用@Results注解的时候,大家可能会担心,是不是要在每一个方法上都 这么写。完全不需要,我们可以设置@Results注解的id属性,在其它方法中使用 @ResultMap注解引入既可

          4、注意点:我们使用注解的方式将原本在StudentMapper.xml文件中的SQL字句 转放到StudentMapper.java接口中,那么,我们在configuration.xml文件中 <mapper>节点的指向需要指向StudentMapper.java接口。

       否则:将会抛出如下异常。

          Type interface com.cmy.mapper.StudentMapper is not known to the MapperRegistry.

          接口StudentMapper没有在MapperRegistry中注册!

     

       4@Insert@Delete@Update注解的使用

              以下代码均在案例1的基础之上完成。

              1、注释掉UserMapper.xml中的insert、delete、update节点  

              2、将configuration.xml文件中mapper节点指向到UserMapper.java接口

  • 相关阅读:
    函数
    python操作文件
    POJ-2689-Prime Distance(素数区间筛法)
    POJ-2891-Strange Way to Express Integers(线性同余方程组)
    POJ-2142-The Balance
    POJ-1061-青蛙的约会(扩展欧几里得)
    Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing
    Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer
    Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes
    Educational Codeforces Round 75 (Rated for Div. 2) A. Broken Keyboard
  • 原文地址:https://www.cnblogs.com/ws1149939228/p/11730176.html
Copyright © 2011-2022 走看看