mybatis 注解方式操作 sql

前言:注解的方式在某些查询的时候还是比较方便的

mapUnderscoreToCamelCase 配置

别名设置,mapUnderscoreToCamelCase 配置 配置可以将 带下划线 sql 字段转化为带驼峰结构的 java 属性设置

具体配置如下

//在mybatis-plus中
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: true
//在mybatis中: 
mybatis:
  configuration:
    map-underscore-to-camel-case: true

此设置相当于 xml 中的 result sql 字段于 java 实体类属性的关系映射

也可以在注解中使用 result 进行关系映射,如果使用result 则放在xml 使用,注解可以使用一些短sql,简单查询,删除,短sql更新等操作不适合长sql操作

@Select 注解

@Select ({select id, role name roleName, enabled, 
create_by createBy, 
create time createTime ” 
” from sys_role ”, 
” where id= #{id } ” }) 
SysRole selectByid(Long id) ;
也可以写成下面这种形式。
@Select ({select id, role name roleName , enabled, 
create by createBy, 
create time createTime 
from sys_role 
where id = #{id } ” }) 
SysRole selectByid(Long id) ;

如果没有配置 别名,使用result

@Results({ 
@Result(property = ” i d ”, column = ” id”, id = true) , 
@Result(property = ” roleName ”, column =”role name ”), 
@Result(property =”enabled”, column = ” enabled”), 
@Result(property = ” createBy”, column = ” create_by”), 
@Result(property = ” createTime”, column = " create_time ”) 
} ) 
@Select (”s elect id,role name , enabled, create by , create time 
from sys_role where id = #{id )”) 
SysRole selectByid2(Long id);

@Insert 注解

不需要返回主键

@Insert ({”insert into sys role (id, role name, enabled, create by, create time )”, 
” values(#{id}, #{roleName}, #{enabled}, #{createBy } ,”, #{ createTime , jdbcType=TIMESTAMP })”}) 
int insert(SysRole sysRole);

返回自增主键

@Insert ({”insert into sys_role (role_name, enabled, create_by, create_ time )”, 
” values(#{roleName}, #{enabled}, #{createBy },”, #{ createTime, jdbcType=TIMESTAMP })”}) 
@Options(useGeneratedKeys =true, keyProperty =”id”) 
int insert2(SysRole sysRole);

和上面的 insert 方法相比, insert2 方法中的 SQL 中少了 id 列,注解多了
@Options ,我们在这个注解中设置了 useGeneratedKeys keyProperty 属性,用法和
XML 相同, 当需要配置多个列时,这个注解也提供了 ke yColumn 属性,可以像 ML 那样
配置使用。

@Delete 注解 和 @Update 注解

@Update {{ ” update sys role ”, 
}) 
”set role name = #{roleName },”, 
” enabled = #{enabled },”, 
” create by = #{createBy },”, 
” create time = #{createTime, jdbcType=TIMESTAMP }”, 
” where id = #{id }” 
int updateByid{SysRole sysRole); 

@Delete {” delete from sys role where id = #{id }”) 
int deleteByid{Long id);

@Provider 注解

除了上面 种注解可以使用简单的 SQL 外, MyBatis 还提供了 Provider 注解,分别
@SelectProvider 、@ InsertProvider 、@ Update Provider 和@ DeleteProvider
们同样可以实现查询、插入、更新、删除操作。
下面通过@ SelectProvider 用法来了解 Provider 注解方式的基本用法

public class PrivilegeProv der { 
	public String selectByid(final Long id) { 
			return new SQL{) { 
			SELECT (”id, privilege name, privilege url”); 
			FROM (”sys privilege”); 
			WHERE (id= #{id }”); 
	} . toString () ; 
}
public String selectByid(final Long id) { 
returnselect id, privilege name, privilege url ”+ 
” from sys_privilege where id = #{id }”;
}

本文大量引用这本书的实例,有问题可以阅读原文
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,关于mybatis注解方式sql in,可以使用@Select注解,并在其中使用SQL语句的动态参数标签进行in操作。具体的示例代码如下: @Select("SELECT * FROM table WHERE id IN (#{idList})") List<Table> findByIds(@Param("idList") List<Long> idList); 其中,List<Long> idList 为入参,传入多个id值,可以根据实际情况进行调整。希望以上内容能够帮到您,如果还有其他相关问题,欢迎继续咨询我。 ### 回答2: Mybatis注解方式sql in可以通过使用@Select注解,并在注解SQL语句中使用动态SQL的foreach标签来实现。 具体步骤如下: 1. 在需要执行in查询的Mapper接口中,使用@Select注解声明SQL语句。例如: ``` @Select("SELECT * FROM table_name WHERE column_name IN (#{ids})") List<Entity> selectByIds(@Param("ids") List<Integer> ids); ``` 2. 在注解SQL语句中,使用动态SQL的foreach标签来循环遍历传入的参数列表。例如: ``` @Select("<script>" + "SELECT * FROM table_name" + "WHERE column_name IN" + "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>" + "#{item}" + "</foreach>" + "</script>") List<Entity> selectByids(@Param("ids") List<Integer> ids); ``` 其中,item代表集合中的每个对象,index代表循环的下标。 3. 在调用Mapper接口的地方传入参数列表即可实现in查询。例如: ``` List<Integer> ids = Arrays.asList(1, 2, 3); List<Entity> result = mapper.selectByids(ids); ``` 通过以上步骤,就可以使用Mybatis注解方式实现sql in查询。 ### 回答3: MyBatis注解方式SQL in时,可以通过@Select注解来实现。使用注解的好处是可以直接在Java代码中书写SQL语句,方便快捷。 首先,在需要执行in查询的Mapper接口中,使用@Select注解定义SQL语句。例如: @Select("SELECT * FROM table_name WHERE column_name IN (${ids})") List<TableName> findDataByIds(@Param("ids") String ids); 其中,@Select表示该方法执行的是一个查询操作,括号内的内容为SQL语句。"SELECT * FROM table_name WHERE column_name IN (${ids})"表示查询表名为table_name中指定列名为column_name的数据,数据的值在ids参数中传入。ids参数使用${}包裹,表示该参数在SQL语句中的占位符。 接下来,在调用该方法时,需要将实际的参数传入,例如: String ids = "1,2,3,4,5"; List<TableName> data = mapper.findDataByIds(ids); 在查询时,将需要查询的数据的id以字符串形式传入,多个id之间用逗号分隔。如上述示例中,ids为"1,2,3,4,5"。 最后,执行查询后,将查询结果存入List集合中,可以对查询结果进行进一步的处理和使用。 需要注意的是,使用注解方式SQL in时,由于SQL语句直接嵌入到Java代码中,所以要确保传入的参数值是安全可靠的,以防止SQL注入攻击的发生。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值