[[toc]]
Write Sql In Groovy Script.
See the 中文文档 for Chinese README.
- Maven
<dependency>
<groupId>io.github.taylorxian</groupId>
<artifactId>mybatis-groovy-boot-starter</artifactId>
<version>1.0-RC</version>
</dependency>
- Maven
<dependency>
<groupId>io.github.taylorxian</groupId>
<artifactId>mybatis-groovy</artifactId>
<version>1.0-RC</version>
</dependency>
- Mapper class: UserMapper.java
@Mapper
public interface UserMapper {
@Select("UserMapperSql#selectByCondition")
@Lang(GroovyLangDriver.class)
List<UserVO> selectByCondition(UserVO userVO);
}
- Script: UserMapperSql.groovy
def selectByCondition(Object parameter) {
'''
SELECT id, name, age, addr
FROM `user`
''' + where { c ->
if (parameter?.id) c += 'id = #{id} '
if (parameter?.name) c += ' AND name = #{name} '
c
}
}
- Mapper class: UserMapper.java
@Mapper
public interface UserMapper {
@Select("UserMapperSelectByName")
@Lang(GroovyLangDriver.class)
UserVO selectByName(String name);
}
- Script: UserMapperSelectByName.groovy
'''select * from user where name = #{name}'''