# mybatis-querymethods
**Repository Path**: gdouyang/mybatis-querymethods
## Basic Information
- **Project Name**: mybatis-querymethods
- **Description**: 参考spring data jpa的Query Methods的mybatis版本
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: https://gitee.com/gdouyang/mybatis-querymethods
- **GVP Project**: No
## Statistics
- **Stars**: 7
- **Forks**: 0
- **Created**: 2020-04-18
- **Last Updated**: 2025-05-23
## Categories & Tags
**Categories**: database-dev
**Tags**: None
## README
在使用spring data jpa的时候可以通过方法名来动态的创建查询语句,于是有了想把这个功能移植到mybaits的想法
# 通过方法名来动态生成sql查询
- 通过QueryMethodsHelper来重新生成sqlSource
- 通过QueryMethodsInterceptor来生成where条件
地址:`https://mvnrepository.com/artifact/com.github.gdouyang/mybatis-querymethods`
## 使用方式
```
com.github.gdouyang
mybatis-querymethods
${version}
```
tkmapper版本
```
tk.mybatis
mapper
4.1.5
```
### spring boot方式(tkmapper)
```
@tk.mybatis.spring.annotation.MapperScan(
factoryBean = querymethods.tkmapper.QueryMethodsMapperFactoryBean.class
)
@EnableTransactionManagement
@SpringBootApplication
public class QuickDuckApplication {
public static void main(String[] args) {
SpringApplication.run(QuickDuckApplication.class, args);
}
}
# 配置application.yml
mybatis:
config-location: classpath:mybatis-config.xml
```
### spring boot方式(mybatis-plus)
```
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan(basePackages = "com.example.demo", factoryBean = querymethods.mybatisplus.QueryMethodsMapperFactoryBean.class)
@SpringBootApplication
public class QuickDuckApplication {
public static void main(String[] args) {
SpringApplication.run(QuickDuckApplication.class, args);
}
}
# 配置application.yml
mybatis-plus:
config-location: classpath:mybatis-config.xml
```
`mybatis-config.xml`配置(放在`src/main/resources`下)
```
```
### spring mvc方式(tkmapper)
```
# 配置拦截器
```
### 样例
在Mapper使用`Select`注解,给空字符串, 没有注解的需要在xml文件中配置
```
create table if not exists customer (
id bigint primary key not null,
first_name varchar(255),
last_name varchar(255),
create_time_ datetime,
active tinyint
)
```
```
public interface CustomerMapper extends Mapper {
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id = ? and
* first_name = ? ) )
*/
@Select("")
Customer findByIdAndFirstName(Integer id, String name);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id is null ) or (
* first_name = ? ) )
*/
@Select("")
Customer findByIdOrFirstName(Integer id, String name);
/** SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id = ? ) ) */
@Select("")
Customer findById(Integer id);
/** SELECT COUNT(id) FROM customer WHERE ( ( id = ? ) ) */
@Select("")
Integer countById(Integer id);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( first_name = ? ) )
* order by id ASC
*/
@Select("")
List findByFirstNameOrderByIdAsc(String name);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( first_name = ? ) )
* order by create_time_ ASC
*/
@Select("")
List findByFirstNameOrderByCreateTimeAsc(String name);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( first_name like
* 'ABC%' ) )
*/
@Select("")
List findByFirstNameStartingWith(String name);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( first_name like
* '%ABC' ) )
*/
@Select("")
List findByFirstNameEndingWith(String name);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( first_name like
* '%ABC%' ) )
*/
@Select("")
List findByFirstNameContaining(String name);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( first_name not like
* '%ABC%' ) )
*/
@Select("")
List findByFirstNameNotContaining(String name);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( first_name like
* 'ABC' ) )
*/
@Select("")
List findByFirstNameLike(String name);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( first_name not like
* 'ABC' ) )
*/
@Select("")
List findByFirstNameNotLike(String name);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id in ( ? , ? , ? )
* ) or ( id = ? ) )
*/
@Select("")
List findByIdInOrId(List idList, Integer id);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id in ( ? , ? , ? )
* ) )
*/
@Select("")
List findByIdIn(List idList);
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id not in ( ? , ? ,
* ? ) ) )
*/
@Select("")
List findByIdNotIn(List idList);
/** SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id is null ) ) */
@Select("")
List findByIdIsNull();
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id is not null ) )
*/
@Select("")
List findByIdIsNotNull();
/** SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( active = 1 ) ) */
@Select("")
List findByActiveTrue();
/**
* SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id between ? and ? )
* )
*/
@Select("")
List findByIdBetween(Integer from, Integer to);
/** SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id > ? ) ) */
@Select("")
List findByIdAfter(Integer from);
/** SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id > ? ) ) */
@Select("")
List findByIdGreaterThan(Integer from);
/** SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id >= ? ) ) */
@Select("")
List findByIdGreaterThanEqual(Integer from);
/** SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id < ? ) ) */
@Select("")
List findByIdBefore(Integer from);
/** SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id < ? ) ) */
@Select("")
List findByIdLessThan(Integer from);
/** SELECT id,first_name,last_name,active,create_time_ FROM customer WHERE ( ( id <= ? ) ) */
@Select("")
List findByIdLessThanEqual(Integer from);
/** SELECT first_name FROM customer WHERE ( ( id = ? ) ) */
@Select("")
String findFirstNameById(Integer id);
/** SELECT distinct first_name FROM customer WHERE ( ( id = ? ) ) */
@Select("")
String findDistinctFirstNameById(Integer id);
/** DELETE FROM customer WHERE ( ( first_name = ? ) ) */
@Delete("")
int deleteByFirstName(String name);
/** DELETE FROM customer WHERE ( ( first_name = ? and id = ? ) ) */
@Delete("")
int deleteByFirstNameAndId(String name, Integer id);
}
```