# spring-data-mybatis-mini
**Repository Path**: cyrs/spring-data-mybatis-mini
## Basic Information
- **Project Name**: spring-data-mybatis-mini
- **Description**: spring data jdbc with mybatis template dynamic query
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 7
- **Created**: 2020-06-11
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
## spring-data-mybatis-mini
[简体中文-more](README.md)
**Is equivalent to spring data jdbc + mybatis template dynamic query**
**Make It Easy**
1. discard cumbersome XML only use mybatis template engine namely
dynamic SQL,SQL written in markdown file easier to write and read,SQL
can be unified management
2. sql query based on spring jdbc,not mybatis
3. CRUD,Bulkupdate insert,support Page,Multiple data sources,Separation
of reading and writing
4. Simplify the dynamic SQL usage of mybatis (mix and use - the same as
mybatis) eg:
```
[@and id in idList] 等于
and id in #{item}
```

== Getting Started
1. Add the Maven dependency
2. @EnableMybatisMini
3. extends BaseRepository or extends
BaseQueryRepository(only query)
4. demo[spring-data-mybatis-mini-demo](https://github.com/VonChange/spring-data-mybatis-mini-demo/blob/master/src/test/java/com/vonchange/nine/demo/dao/UserBaseRepositoryTest.java)
Here is a quick teaser of an application using Spring Data mybatis mini
in Java:
=== Maven configuration
Add the Maven dependency:
```
com.vonchange.common
spring-data-mybatis-mini
2.2.3
org.springframework.data
spring-data-commons
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
8.0.15
```
```
//add @EnableMybatisMini
@EnableMybatisMini
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
```
import org.springframework.data.mybatis.mini.jdbc.repository.query.ConfigLocation;
import org.springframework.data.mybatis.mini.jdbc.repository.support.BaseRepository;
import org.springframework.data.repository.query.Param;
public interface UserBaseRepository extends BaseRepository {
@ReadDataSource
List findList(@Param("userName") String userName,
@Param("createTime") Date createTime);
Page findList(Pageable pageable, @Param("userName") String userName,@Param("createTime") Date createTime);
String findUserName(@Param("userName") String userName);
List findListByIds(@Param("userName") String userName,
@Param("createTime") Date createTime,@Param("idList")List idList);
int updateIsDelete(@Param("isDelete") Integer isDelete,@Param("id") Long id);
}
```
> The same name under the default SQL package UserBaseRepository.md
> Identify the method with the same name defined at the beginning and
> the end of ```
> eg[UserBaseRepository.md](https://github.com/VonChange/spring-data-mybatis-mini/blob/master/UserBaseRepository.md)
> domain entity
```
import javax.persistence.Id;
import javax.persistence.Table;
@Data
@Table(name = "user_base")
public class UserBaseDO {
@Id
private Long id;
private String userName;
private String firstPhone;
}
```
```
@Service
public class MyService {
@Resource
private final UserBaseRepository userBaseRepository;
public void doWork() {
List userBaseDOList = userBaseRepository.findList("test",new Date());
}
}
```
> Simplify the dynamic SQL usage of mybatis, \[@ begin
> \[@and id in idList] 等于
```
and id in #{item}
```
> \[@and user_name <> userName] Is equivalent to
```
and user_name <>
#{userName}
```
3. in List \[@and id in userList:id]
4. like
```
[@and user_name like userName] ] is equivalent to and user_name like CONCAT('%',?,'%')
[@and user_name like userName%] is equivalent to and user_name like CONCAT(?,'%')
[@and user_name like userName%] is equivalent to and user_name like CONCAT('%','test')
```
5. other
```
[@AND C.DESCRIPTION LIKE #{bean.description:like} or C.title like #{bean.description:like}]
is equivalent to
AND C.DESCRIPTION LIKE CONCAT('%',#{bean.description},'%') or C.title like CONCAT('%',#{bean.description},'%')
[@AND content -> '$.account' = #{bean.account}]
is equivalent to
AND content -> '$.account' = #{bean.account}
```
6. \[@sql XX] XX markdown XX named sql fragment