Spring Boot入门系列(十九)整合mybatis,使用注解实现动态Sql、参数传递等常用操作!

2020-08-24 00:00:00 参数 注解 方式 属性 常用

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

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

Spring Boot 整合mybatis 使用xml配置版之前已经介绍过了,不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/1657780.html。

 

注解介绍

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);
				
	

相关文章