之前学习的时候,看到别人在使用mybatis时,用到@Select、@Insert、@Delete、@Param这几个注解,故楼主研究了一下,在这里与大家分享

当使用这几个注解的时候,可以省去写Mapper.xml等一系列配置文件

首先来看个例子:

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import com.xwj.entity.UserEntity; public interface UserMapper { /**
* 查询
*/
@Select("SELECT id, last_name lastName, email, age FROM xwj_user WHERE id = #{id} and last_name like '%${lastName}%' ")
UserEntity findById(@Param("id") String id, @Param("lastName") String name); /**
* 新增
*/
@Insert("INSERT INTO xwj_user(id, last_name, age) VALUES(#{id}, #{lastName}, #{age})")
int addUser(@Param("id") String id, @Param("lastName") String name, @Param("age") Integer age); /**
* 更新
*/
@Update("UPDATE xwj_user SET last_name = #{lastName} WHERE id = ${id}")
int updateUser(@Param("id") String id, @Param("lastName") String name); /**
* 删除
*/
@Delete("DELETE FROM xwj_user WHERE id = ${id}")
int deleteUser(@Param("id") String id);

还有实体类:

public class UserEntity {

    private String id;

    private String lastName;

    private String email;

    private int age;

    //TODO set跟get方法略。。。

}

这里解释一下:

  1、@Select(...)注解的作用就是告诉mybatis框架,执行括号内的sql语句

  2、id, last_name lastName, email, age  对于实体类字段与数据库字段表不一致时,得加上别名。如last_name是数据库字段,lastName是实体类字段

   这段代码的作用就是实现数据库字段名和实体类属性的一一映射。如果没有加别名,则在查询出的entity中,这个字段是null

  3、WHERE id = #{id} and last_name like '%${lastName}%' 表示sql语句要接受2个参数:id跟lastName。#{..}(或${..})中的名称得跟@Param(..)中的名称对应。

    这就是@Param注解的妙用,正确的将参数传入sql语句中

  4、使用了@Param注解来声明参数时,使用 #{} 或 ${}来接收参数的方式都可以

  5、@Insert、@Update、@Delete的用法跟@Select类似

  楼主在使用的过程中,发现就insert语句写在@Update注解中也是可以的

  虽然直接使用注解很方便,不过楼主发现,如果在添加查询语句时,如a字段为空则不添加,有值则添加的场景,处理起来很不方便

mybatis之@Select、@Insert、@Delete、@Param的更多相关文章

  1. mybatis select/insert/update/delete

    这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...

  2. 关于MyBatis mapper的insert, update, delete返回值

    这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...

  3. Use Select To Generate Any Insert/Delete/Update Statement

    If you don't have the permission to generate script according to an existing db, but you have the re ...

  4. C++使用Mysql的详细步骤及各个常用方法的代码演示:select,insert,update,delete

    这几天一直在学习C++下使用Mysql的方法及其中各种的问题,也看了很多Mysql的API函数,当然自己看的还是很基础的.其实对于每种数据库的操作,基本的方法都是非常类似的,大多都是connect,s ...

  5. using the library to generate a dynamic SELECT or DELETE statement mysqlbaits xml配置文件 与 sql构造器 对比

    https://github.com/mybatis/mybatis-dynamic-sql MyBatis Dynamic SQL     What Is This? This library is ...

  6. mybatis源码专题(2)--------一起来看下使用mybatis框架的insert语句的源码执行流程吧

    本文是作者原创,版权归作者所有.若要转载,请注明出处.本文以简单的insert语句为例 1.mybatis的底层是jdbc操作,我们先来回顾一下insert语句的执行流程,如下 执行完后,我们看下数据 ...

  7. 带有OUTPUT的INSERT,DELETE,UPDATE

    原文地址:http://blog.sina.com.cn/s/blog_71460d950100nld2.html OUTPUT是SQL SERVER2005的新特性.可以从数据修改语句中返回输出.可 ...

  8. mybatis 使用@Select 注解,因为字符编码不一致导致mybatis 报错

    使用 mybatis 的@Select 注解, @Select({ "<script>select " + ALL_COLUMNS + " from &quo ...

  9. sqlserver触发器insert,delete,update

    Create Trigger [dbo].[upemployee_kefyu_sale] on [dbo].[employee] for update as if update(FullName) b ...

  10. OGG for sqlserver engryption && insert/delete

    OGG for sqlserver engryption && insert/delete 1. 源端操作 1.1 获取key 作为数据库用户密码加密 d:\GoldenGate\gg ...

随机推荐

  1. supervisord 启动失败 Error: Another program is already listening on a port that one of our HTTP serve...

    Linux系统中 Supervisor 配置守护进程: 启动Supervisor 服务语句: supervisord -c /etc/supervisor/supervisord.conf 这个过程可 ...

  2. C#一些代码小结--文件对话框

    C# 一些代码小结--文件对话框 查看文件完整路径 try { Config cfg = new Config(); var file = ""; if (saveFileDial ...

  3. DateTimeField如何自动设置为当前时间并且能被修改 ——django日期时间字段的使用

    参考于:https://www.cnblogs.com/huchong/p/7895263.html 创建django的model时,有DateTimeField.DateField和TimeFiel ...

  4. docker和定时任务

    查看linux信息 cat /etc/issue 以id运行容器docker start 1c3339d7f9a8通过id结束容器 docker kill 1c3339d7f9a8 Ubuntu 安装 ...

  5. 编写 ES6 的 7 个实用技巧

    无脑翻译走一波~ Hack #1 - 变量交换 使用数组解构交换变量的值 let a = 'world', b = 'hello' [a, b] = [b, a] console.log(a) // ...

  6. linux 服务器性能监控(一)

    这篇文章主要介绍一些常用的linux服务器性能监控命令,包括命令的常用参数.指标的含义以及一些交互操作. 几个问题 命令本身并不复杂,关键是你对操作系统基础知识的掌握和理解,先来看看下面几个问题: C ...

  7. 读Lock-Free论文实践

    论文地址:implementing Lock-Free Queue 论文大体讲的意思是:Lock-Base的程序的performance不好,并且a process inside the critic ...

  8. java求三角形面积以及周长---封装

    /*时间: 2012-10-08作者: 烟大程序要求: 1.封装一类三角形对象Triangle,该类对象具有三条边的属性, 具有初始化三角形的功能.修改边长的功能.判断三条边能否构成三角形的功能. 求 ...

  9. OC 中的属性

    自动合成 (autosynthesis) @property 语法,会做下面两件事情 自动生成存取方法 由编译器生成,编辑器里不会看到这些方法. 向类中添加适当类型的实例变量 在属性前加下划线,作为实 ...

  10. day 33js 后续 函数.对象

    前情提要: 今天学习的是js的函数以及简单的类的使用 一:函数的初识别 <!DOCTYPE html> <html lang="en"> <head& ...