mybatis之@Select、@Insert、@Delete、@Param
之前学习的时候,看到别人在使用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的更多相关文章
- mybatis select/insert/update/delete
这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...
- 关于MyBatis mapper的insert, update, delete返回值
这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...
- 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 ...
- C++使用Mysql的详细步骤及各个常用方法的代码演示:select,insert,update,delete
这几天一直在学习C++下使用Mysql的方法及其中各种的问题,也看了很多Mysql的API函数,当然自己看的还是很基础的.其实对于每种数据库的操作,基本的方法都是非常类似的,大多都是connect,s ...
- 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 ...
- mybatis源码专题(2)--------一起来看下使用mybatis框架的insert语句的源码执行流程吧
本文是作者原创,版权归作者所有.若要转载,请注明出处.本文以简单的insert语句为例 1.mybatis的底层是jdbc操作,我们先来回顾一下insert语句的执行流程,如下 执行完后,我们看下数据 ...
- 带有OUTPUT的INSERT,DELETE,UPDATE
原文地址:http://blog.sina.com.cn/s/blog_71460d950100nld2.html OUTPUT是SQL SERVER2005的新特性.可以从数据修改语句中返回输出.可 ...
- mybatis 使用@Select 注解,因为字符编码不一致导致mybatis 报错
使用 mybatis 的@Select 注解, @Select({ "<script>select " + ALL_COLUMNS + " from &quo ...
- sqlserver触发器insert,delete,update
Create Trigger [dbo].[upemployee_kefyu_sale] on [dbo].[employee] for update as if update(FullName) b ...
- OGG for sqlserver engryption && insert/delete
OGG for sqlserver engryption && insert/delete 1. 源端操作 1.1 获取key 作为数据库用户密码加密 d:\GoldenGate\gg ...
随机推荐
- c# .net中的简单Job
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- svn 连接超时,连接失败解决办法
1.确认服务是否开启 2.Windows防火墙是否开启,如开启则关闭防火墙 3.安全软件是否将3306与443端口关闭. 关闭后无法连接
- reids 安装
第一步:在VMware中安装CentOS(参考Linux教程中的安装虚拟机) 第二步:在Linux下安装gcc环境 [root@hadoop ~]#yum install gcc-c++ 第三步:将下 ...
- perl(ExtUtils::Embed)依赖包
perl(ExtUtils::Embed) 被 ****需要 yum install perl-ExtUtils-Embed即可
- Sentinel 哨兵 实现redis高可用
本文链接:http://www.cnblogs.com/zhenghongxin/p/8885879.html 我们知道redis是有主从复制的,例如下图: 但如果master主进程挂掉之后,没有sl ...
- KMP Demo
The key of Kmp is to build a look up table that records the match result of prefix and postfix. Valu ...
- 【转】目标检测之YOLO系列详解
本文逐步介绍YOLO v1~v3的设计历程. YOLOv1基本思想 YOLO将输入图像分成SxS个格子,若某个物体 Ground truth 的中心位置的坐标落入到某个格子,那么这个格子就负责检测出这 ...
- Mac 设置终端中使用 sublime 打开文件
在Mac下用终端创建文件等比较方便,但如果配合Sublime用,最好是可以直接在终端中输入命令就可以用Sublime打开指定文件或文件夹. #检查用户目录下有没有bin目录,如里没有就创建一个(目的是 ...
- 五,mysql优化——sql语句优化小技巧
1,大批量插入数据 (1)对于MyISAM: alter table table_name disable keys; loading data; alter table table_name ena ...
- centos6.2 下安装并配置hadoop1.0.4(32位安装)
一,环境准备 1. 集群内所有电脑需要设置防火墙,下面以master为例说明: 切换到root用户下,执行setup指令: