MyBatis注解sql及映射

MyBatis支持使用注解来配置映射的sql语句,这样可以省掉映射器xml文件

一、映射语句
1、insert
比如入门实例中的:

public int insert(User user) throws Exception;

<insert id="insert"  parameterType="twm.mybatisdemo.pojo.User" useGeneratedKeys="true" keyProperty="id"><!-- 将插入数据的主键返回,返回到user对象中 -->insert into user (username,address,email) values (#{username},#{address},#{email})
</insert>

改成注解来配置映射:

@Insert("insert into user (username,address,email) values (#{username},#{address},#{email})")
@Options(useGeneratedKeys = true, keyProperty = "id")
public int insert(User user) throws Exception;

这里使用@Insert注解来定义一个INSERT映射语句。
并且使用@Options注解的userGeneratedKeys 和keyProperty属性,让数据库auto_increment生成的主键值,赋值到keyProperty标记的属性id中

还有一种获取主键的方法(oracle要用SELECT SEQ.NEXTVAL FROM DUAL ,且order设为before)

2、select

@Select("select * from user where id=#{id}")
public User selectById(int id) throws Exception;

返回的是一个User对象,因此如果select语句返回多行记录,就会出现TooManyResultsException异常。

3、update

@Update("update user set username=#{username},address=#{address},email=#{email} where id=#{id}")
public int update(User user) throws Exception;

4、delete

@Delete("delete from user where id=#{id}")
public int delete(int id) throws Exception;

二、结果映射

在xml配置文件中,将查询结果和JavaBean属性映射起来的标签是。对应的是@Results注解

@Select("select * from user")
@Results({ @Result(id = true, column = "id", property = "id"),@Result(column = "username", property = "user_name"),@Result(column = "city", property = "city") }) public List<User> selectAll() throws Exception;

但是@Results注解没办法复用。
在接口文件同目录下新建一个userMapper.xml文件,并定义一个名为userMap的resultMap。

<mapper namespace="twm.mybatisdemo.mapper.UserMapper"><!-- 自定义返回结果集 --><resultMap id="userMap" type="twm.mybatisdemo.pojo.User"><id column="id" property="id" jdbcType="INTEGER" /><result property="user_name" column="username"></result><result property="city" column="city"></result></resultMap>
</mapper>

在userMapper.java中,使用@ResultMap引用名为userMap的resultMap,实现复用。

@Select("select * from user where id=#{id}")
@ResultMap("twm.mybatisdemo.mapper.UserMapper.userMap")
public User selectById(int id) throws Exception;@Select("select * from user")
@ResultMap("twm.mybatisdemo.mapper.UserMapper.userMap")
public List<User> selectAll() throws Exception;

三、关联关系

四、动态SQL

可见官网
https://mybatis.org/mybatis-3/zh/dynamic-sql.html

MyBatis注解sql及映射相关推荐

  1. Mybatis 注解sql 中使用like 和if 判断关键字

    SpringBoot 结婚mybatisplus 进行数据操作出现的问题 1. 插入中文乱码问题? jdbc:mysql://127.0.0.1:3306/xxxuseSSL=false&ze ...

  2. mybatis注解sql使用判断if踩坑

    mybatis使用注解SQL语句的方式 在增删改查时还是很方便的 但是复杂SQL多了之后就不太友好了 我这里写了一个多表的SQL 废了好半天的劲才改出来 先给大家看错误的语句 怎么调都不好用 就是报错 ...

  3. Mybatis关联映射;Mybatis注解

    Mybatis关联映射 Mybatis关联映射的用途: 在实际的开发过程中,对于数据库的操作除了单表外往往会涉及到多张表,这些操作在面向对象中就涉及到了对象与对象之间的关联关系.针对多表之间的操作,M ...

  4. Mybatis注解的基本用法

    Mybatis注解方式就是将SQL语句直接写在接口上,而省略了去XML文件中写查询的SQL语句.这种方法的优点是对于需求比较简单的系统,效率较高.缺点是当SQL语句有变化时都需要重新编译代码.所以一般 ...

  5. SpringBoot + MyBatis(注解版),常用的SQL方法

    一.新建项目及配置 1.1 新建一个SpringBoot项目,并在pom.xml下加入以下代码 <dependency> <groupId>org.mybatis.spring ...

  6. 14. 使用MyBatis注解配置SQL

    14. 使用MyBatis注解配置SQL 1.MyBatis常用注解 @Insert:实现新增,代替了 @Delete:实现删除,代替了 @Update:实现更新,代替了 @Select:实现查询,代 ...

  7. Mybatis 注解开发 + 动态SQL

    Hello 大家好我是橙子同学,今天分享注解Mybatis注解开发+动态sql 目录 每文一铺垫(今天有小插曲哦) 注解开发 添加 @Insert 删除 @Delete 查询 @Select 修改 @ ...

  8. MyBatis学习 之 二、SQL语句映射文件(1)resultMap

    二.SQL语句映射文件(1)resultMap SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对应的接口类的路径.写好SQL语句映射文件后,需要在MyB ...

  9. Mybatis注解实现一对多关联映射(@Many)

    mybatis注解实现一对多关联映射 @Select("<script> \n" +"\t\tselect a.*,p.punishment_money, p ...

最新文章

  1. python程序员又叫什么-Python程序员都知道的入门知识の四
  2. pythonweb开发-一步步教你开始使用Python开发Web应用
  3. b站上java和python视频可以吗_b站有哪些好的java视频?
  4. 3DSlicer27:OpenIGTLinkIF
  5. 汇编和可执行文件(Debug和Release)
  6. 如何用css进行网页布局,_技术笔记_课程笔记
  7. 控制台应用程序《石头剪刀布》——新手,
  8. KORG Polysix for mac(四VCO单声道/多声道合成器)
  9. LeetCode:每日一题(2020.4.8)
  10. 知道是骗子 !好要撞上去!别太得瑟,没啥么好
  11. fastjson list转json
  12. word被锁定无法编辑怎么处理
  13. 这是啥SQL,室友看了人傻了
  14. prusai3打印机使用教程_【打印虎原创】Prusa_i3_3D打印机校准图解教程-基础篇
  15. 如何通过CND指令创建一个新的Maven 项目,已经如何部署到tomcat 的webapps目录下
  16. 【转】manifest
  17. JZOJ4883. 【NOIP2016提高A组集训第12场11.10】灵知的太阳信仰
  18. 883. 三维形体投影面积(javascript)883. Projection Area of 3D Shapes
  19. python定向爬取wallhaven壁纸
  20. 阿里云9月1日安骑士升级故障真相

热门文章

  1. oracle认证大师时间,oracle认证考试时间在什么时候
  2. Java中的继承 与 多态(中)
  3. php手机号码隐藏,php如何隐藏手机号
  4. vue中的mixins的介绍和使用
  5. python-docx 切分runs时会将整个词切分成多个的问题
  6. [Garmin]小米2成功运行v大GARMIN2.14,详细总结_我是亲民_新浪博客
  7. 深圳50个值得一吃的地方(转载)
  8. 拼命——不一定就能到达终点
  9. php定义成绩的变量,php - 变量
  10. was控制台java虚拟机默认值_JVM 参数设置