mybatis注解动态sql实现
mybatis动态sql 有2种实现方式。
1:script标签
@Select("<script> " + "SELECT id, name, email,password " + "FROM user " + " <where> " + " <if test=\"email != null\">id=#{email}</if> " + " <if test=\"name != null\"> AND name=#{name}</if> " + " </where> " + " </script> ")
同时 对于LIKE 也是不能够直接使用的 可以借助concat函数实现
@Select("SELECT name from user WHERE email LIKE concat(#{prefix},'%') limit 5")
2使用复杂注解类 。如:@InsertProvider,@UpdateProvider,@DeleteProvider和@SelectProvider,这些都是建立动态语言和让MyBatis执行这些语言。
现在让我们来看一下如何使用@ SelectProvider来创建简单的SELECT映射的例子。创建一个TutorDynaSqlProvider.Java类,带有findTutorByIdSql()的方法。
package com.owen.mybatis.sqlproviders; import org.apache.ibatis.jdbc.SQL; public class TutorDynaSqlProvider { public String findTutorByIdSql(int tutorId) { return "SELECT TUTOR_ID AS tutorId, NAME, EMAIL FROM TUTORS WHERE TUTOR_ID=#{tutorId}"; } }
@SelectProvider(type=TutorDynaSqlProvider.class, method="findTutorByIdSql") Tutor findTutorById(int tutorId);
参考:
写道
相关推荐
chenjiazhu 2020-07-08
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
dongxurr 2020-08-08
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
Dullonjiang 2020-07-30
jimgreatly 2020-07-27
liqiancao 2020-07-26
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26