软件编程
位置:首页>> 软件编程>> java编程>> Spring Boot整合mybatis使用注解实现动态Sql、参数传递等常用操作(实现方法)

Spring Boot整合mybatis使用注解实现动态Sql、参数传递等常用操作(实现方法)

作者:章为忠  发布时间:2023-03-17 07:09:32 

标签:Spring,Boot,mybatis,动态Sql

前面介绍了Spring Boot 整合mybatis 使用注解的方式实现数据库操作,介绍了如何自动生成注解版的mapper 和pojo类。 接下来介绍使用mybatis 常用注解以及如何传参数等数据库操作中的常用操作。

其实,mybatis 注解方式 和 XML配置方式两者的使用基本上相同,只有在构建 SQL 脚本有所区别,所以这里重点介绍两者之间的差异,以及增删改查,参数传递等注解的常用操作。

详解SpringBoot 快速整合Mybatis(去XML化+注解进阶)已经介绍过了,不清楚的朋友可以看看之前的文章:https://www.jb51.net/article/127473.htm

注解介绍

mybatis 注解方式的最大特点就是取消了Mapper的XML配置,具体的 SQL 脚本直接写在 Mapper 类或是 SQLProvider 中的方法动态生成。
mybatis 提供的常用注解有:@Insert 、@Update 、@Select、 @Delete等标签,这些注解其实就是 MyBatis 提供的来取代其 XML配置文件的。

1、@Select 注解

@Select,主要在查询的时候使用,查询类的注解,一般简单的查询可以使用这个注解。


@Select({
"select",
"id, company_id, username, password, nickname, age, sex, job, face_image, province, ",
"city, district, address, auth_salt, last_login_ip, last_login_time, is_delete, ",
"regist_time",
"from sys_user",
"where id = #{id,jdbcType=VARCHAR}"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
@Result(column="company_id", property="companyId", jdbcType=JdbcType.VARCHAR),
@Result(column="face_image", property="faceImage", jdbcType=JdbcType.VARCHAR),
@Result(column="auth_salt", property="authSalt", jdbcType=JdbcType.VARCHAR),
@Result(column="last_login_ip", property="lastLoginIp", jdbcType=JdbcType.VARCHAR),
@Result(column="last_login_time", property="lastLoginTime", jdbcType=JdbcType.TIMESTAMP),
@Result(column="is_delete", property="isDelete", jdbcType=JdbcType.INTEGER),
@Result(column="regist_time", property="registTime", jdbcType=JdbcType.TIMESTAMP)
})
User selectByPrimaryKey(String id);

注意:如果是多个参数,需要将 #后面的参数和传入的变量名保持一致。

2、@Insert 注解

@Insert,插入数据时使用,直接传入数据实体类,mybatis 会属性自动解析到对应的参数。所以需要将 #后面的参数和实体类属性保持一致。


@Insert({
 "insert into sys_user (id, company_id, ",
 "username, password, ",
 "nickname, age, sex, ",
 "job, face_image, ",
 "province, city, ",
 "district, address, ",
 "auth_salt, last_login_ip, ",
 "last_login_time, is_delete, ",
 "regist_time)",
 "values (#{id,jdbcType=VARCHAR}, #{companyId,jdbcType=VARCHAR}, ",
 "#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, ",
 "#{nickname,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}, #{sex,jdbcType=INTEGER}, ",
 "#{job,jdbcType=INTEGER}, #{faceImage,jdbcType=VARCHAR}, ",
 "#{province,jdbcType=VARCHAR}, #{city,jdbcType=VARCHAR}, ",
 "#{district,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, ",
 "#{authSalt,jdbcType=VARCHAR}, #{lastLoginIp,jdbcType=VARCHAR}, ",
 "#{lastLoginTime,jdbcType=TIMESTAMP}, #{isDelete,jdbcType=INTEGER}, ",
 "#{registTime,jdbcType=TIMESTAMP})"
})
int insert(User record);

注意:需要将 #后面的参数和实体类属性保持一致。

3、@Update 注解

@Update,一般数据更新操作可以使用 @Update注解实现。


@Update({
 "update sys_user",
 "set company_id = #{companyId,jdbcType=VARCHAR},",
  "username = #{username,jdbcType=VARCHAR},",
  "password = #{password,jdbcType=VARCHAR},",
  "nickname = #{nickname,jdbcType=VARCHAR},",
  "age = #{age,jdbcType=INTEGER},",
  "sex = #{sex,jdbcType=INTEGER},",
  "job = #{job,jdbcType=INTEGER},",
  "face_image = #{faceImage,jdbcType=VARCHAR},",
  "province = #{province,jdbcType=VARCHAR},",
  "city = #{city,jdbcType=VARCHAR},",
  "district = #{district,jdbcType=VARCHAR},",
  "address = #{address,jdbcType=VARCHAR},",
  "auth_salt = #{authSalt,jdbcType=VARCHAR},",
  "last_login_ip = #{lastLoginIp,jdbcType=VARCHAR},",
  "last_login_time = #{lastLoginTime,jdbcType=TIMESTAMP},",
  "is_delete = #{isDelete,jdbcType=INTEGER},",
  "regist_time = #{registTime,jdbcType=TIMESTAMP}",
 "where id = #{id,jdbcType=VARCHAR}"
})
int updateByPrimaryKey(User record);

4、@Delete 注解
@Delete 数据删除的注解


@Delete({
 "delete from sys_user",
 "where id = #{id,jdbcType=VARCHAR}"
})
int deleteByPrimaryKey(String id);

5、@Results和@Result注解

@Results 和 @Result 主要作用是,当有一些特殊的场景需要处理,查询的返回结果与期望的数据格式不一致时,可以将将数据库中查询到的数值自动转化为具体的属性或类型,,修饰返回的结果集。比如查询的对象返回值属性名和字段名不一致,或者对象的属性中使用了枚举等。如果实体类属性和数据库属性名保持一致,就不需要这个属性来修饰。


@Select({
"select",
"id, company_id, username, password, nickname, age, sex, job, face_image, province, ",
"city, district, address, auth_salt, last_login_ip, last_login_time, is_delete, ",
"regist_time",
"from sys_user",
"where id = #{id,jdbcType=VARCHAR}"
})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.VARCHAR, id=true),
@Result(column="company_id", property="companyId", jdbcType=JdbcType.VARCHAR),
@Result(column="face_image", property="faceImage", jdbcType=JdbcType.VARCHAR),
@Result(column="auth_salt", property="authSalt", jdbcType=JdbcType.VARCHAR),
@Result(column="last_login_ip", property="lastLoginIp", jdbcType=JdbcType.VARCHAR),
@Result(column="last_login_time", property="lastLoginTime", jdbcType=JdbcType.TIMESTAMP),
@Result(column="is_delete", property="isDelete", jdbcType=JdbcType.INTEGER),
@Result(column="regist_time", property="registTime", jdbcType=JdbcType.TIMESTAMP)
})
User selectByPrimaryKey(String id);

上面的例子可以看到,数据库中的company_id 字段和实体类中定义的 companyId 属性的名称不一致,需要Result 转换。

来源:https://www.cnblogs.com/zhangweizhong/p/13118410.html

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com