Skip to content

Commit 814baf9

Browse files
committed
init
0 parents  commit 814baf9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1881
-0
lines changed

.gitignore

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**
5+
!**/src/test/**
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
30+
### VS Code ###
31+
.vscode/

pom.xml

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.codingapi</groupId>
7+
<artifactId>simple-mybatis</artifactId>
8+
<version>1.0.0</version>
9+
<name>simple-mybatis</name>
10+
<description>simple mybatis project for Spring Boot</description>
11+
<packaging>pom</packaging>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
16+
<java.version>1.8</java.version>
17+
18+
<pagehelper-starter.version>1.2.7</pagehelper-starter.version>
19+
<org.projectlombok.version>1.18.0</org.projectlombok.version>
20+
<pagehelper.version>5.1.10</pagehelper.version>
21+
<mybatis.version>3.4.6</mybatis.version>
22+
<hibernate.jpa.api.version>1.0.0.Final</hibernate.jpa.api.version>
23+
<commons.beanutils.version>1.9.3</commons.beanutils.version>
24+
<commons.lang3.version>3.10</commons.lang3.version>
25+
<google.guava.version>28.1-jre</google.guava.version>
26+
</properties>
27+
28+
<dependencies>
29+
30+
<dependency>
31+
<groupId>org.hibernate.javax.persistence</groupId>
32+
<artifactId>hibernate-jpa-2.1-api</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>commons-beanutils</groupId>
37+
<artifactId>commons-beanutils</artifactId>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.mybatis</groupId>
42+
<artifactId>mybatis</artifactId>
43+
<scope>provided</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>com.github.pagehelper</groupId>
48+
<artifactId>pagehelper</artifactId>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>com.google.guava</groupId>
53+
<artifactId>guava</artifactId>
54+
</dependency>
55+
56+
57+
<dependency>
58+
<groupId>org.apache.commons</groupId>
59+
<artifactId>commons-lang3</artifactId>
60+
</dependency>
61+
62+
63+
</dependencies>
64+
65+
<dependencyManagement>
66+
<dependencies>
67+
68+
<dependency>
69+
<groupId>org.apache.commons</groupId>
70+
<artifactId>commons-lang3</artifactId>
71+
<version>${commons.lang3.version}</version>
72+
</dependency>
73+
74+
<dependency>
75+
<groupId>commons-beanutils</groupId>
76+
<artifactId>commons-beanutils</artifactId>
77+
<version>${commons.beanutils.version}</version>
78+
</dependency>
79+
80+
<dependency>
81+
<groupId>org.hibernate.javax.persistence</groupId>
82+
<artifactId>hibernate-jpa-2.1-api</artifactId>
83+
<version>${hibernate.jpa.api.version}</version>
84+
</dependency>
85+
86+
<dependency>
87+
<groupId>com.google.guava</groupId>
88+
<artifactId>guava</artifactId>
89+
<version>${google.guava.version}</version>
90+
</dependency>
91+
92+
<dependency>
93+
<groupId>com.github.pagehelper</groupId>
94+
<artifactId>pagehelper</artifactId>
95+
<version>${pagehelper.version}</version>
96+
</dependency>
97+
98+
<dependency>
99+
<groupId>org.mybatis</groupId>
100+
<artifactId>mybatis</artifactId>
101+
<version>${mybatis.version}</version>
102+
</dependency>
103+
104+
</dependencies>
105+
</dependencyManagement>
106+
107+
<build>
108+
<plugins>
109+
<plugin>
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-source-plugin</artifactId>
112+
<version>3.0.1</version>
113+
<executions>
114+
<execution>
115+
<id>attach-sources</id>
116+
<goals>
117+
<goal>jar</goal>
118+
</goals>
119+
</execution>
120+
</executions>
121+
</plugin>
122+
<plugin>
123+
<groupId>org.apache.maven.plugins</groupId>
124+
<artifactId>maven-compiler-plugin</artifactId>
125+
<configuration>
126+
<source>8</source>
127+
<target>8</target>
128+
</configuration>
129+
</plugin>
130+
</plugins>
131+
</build>
132+
133+
134+
</project>

readme.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# simple-mybatis
2+
3+
## 前言
4+
mybatis框架已经被大量使用,但是随着近些时间springboot的流行还有很多人在使用mybatis时采用xml的方式配置mapper.该框架将让Mybatis可以更方便的在springboot项目上使用。
5+
6+
## 框架说明
7+
* 框架依赖兵支持jpa注解的写法
8+
* 提供了常用操作的Mapper封装
9+
* 基于pagehelper提供了分页支持
10+
* 提供Query方式对查询语句的支持
11+
* 提供Tree数据接口的支持
12+
13+
## 使用说明
14+
15+
* jpa注解的使用
16+
```java
17+
@Data
18+
@Table(name = "t_demo")
19+
public class Demo implements ITree<Long>{ //ITree根据业务需要可不实现
20+
21+
//Id 注解
22+
@Id
23+
private Long id;
24+
25+
//数据库字段与属于想一致时可忽略@Column注解
26+
private Long superId;
27+
28+
private String name;
29+
30+
//字段名称
31+
@Column(name = "s_module")
32+
private String module;
33+
34+
private String myName;
35+
36+
//忽略字段
37+
@Transient
38+
private String test;
39+
40+
}
41+
42+
```
43+
* `CommandMapper` 提供了所有常用的写操作
44+
45+
```java
46+
47+
//保存数据,会返回主键Id
48+
int save(T t);
49+
50+
//批量保存
51+
int saveAll(@Param("list") List<T> list);
52+
53+
//修改数据,修改数据只会替换非null对象
54+
int update(T t);
55+
56+
//删除数据,删除条件为Id
57+
int delete(T t);
58+
59+
//批量删除,删除条件为Id
60+
int deleteAll(@Param("list") List<T> list);
61+
62+
//通过Id直接删除
63+
int deleteById(@Param("id") Object id);
64+
65+
//通过Id批量删除
66+
int deleteAllById(@Param("list") List id);
67+
```
68+
69+
* `QueryMapper`提供常用的查询操作
70+
```java
71+
72+
//通过Id查询
73+
T getById(@Param("id") Object id);
74+
75+
//查询属于数据
76+
List<T> findAll();
77+
78+
//通过Query查询数据
79+
List<T> query(@Param("query") Query query);
80+
81+
//通过Query查询试图数据,返回List Map对象
82+
List<Map<String,Object>> queryMap(@Param("query") Query query);
83+
84+
//通过Query查询试图数据,返回List Bean对象
85+
<V> List<V> queryView(Class<V> clazz, Query query);
86+
87+
```
88+
89+
* `SimpleMapper`提供了`QueryMapper``CommandMapper`的功能
90+
91+
* `IPageQuery` 提供基于MybatisHelper的分页支持
92+
93+
```java
94+
PageList<Demo> pageList = demoMapper.page(1, 5, demoMapper::findAll);
95+
log.info("page:{}",pageList);
96+
```
97+
98+
* `ITreeQuery` 提供树形数据结构组装
99+
100+
```java
101+
//实现ITree接口
102+
public class Demo implements ITree<Long>{
103+
//fields get set ...
104+
}
105+
106+
List<TreeList<Demo>> treeLists = demoMapper.tree( demoMapper::findAll,0L);
107+
log.info("treeLists:{}",treeLists);
108+
```
109+
110+
* 用户根据自己的Mapper来选择集成对应的功能接口
111+
112+
```java
113+
@Mapper
114+
public interface DemoMapper extends QueryMapper<Demo>,IPageQuery<Demo>,ITreeQuery<Demo,Long> {
115+
116+
117+
}
118+
119+
@Mapper
120+
public interface DemoMapper extends SimpleMapper<Demo> {
121+
122+
123+
}
124+
@Mapper
125+
public interface DemoMapper extends CommandMapper<Demo> {
126+
127+
128+
}
129+
130+
```
131+
132+
133+
* query使用说明
134+
query 通过QueryBuilder来创建,当查询返回的是表的数据,则不需要写select语句,直接拼接where()查询条件。
135+
若查询返回的是视图格式的数据则需要写select语句,select语句中可以写join关联.
136+
select语句中的字段可以用下划线,也可以直接处理成小驼峰。都可以转成java bean对象。
137+
```java
138+
@Test
139+
void viewList(){
140+
List<DemoView> list =
141+
demoMapper.queryView(
142+
DemoView.class,
143+
QueryBuilder.Build()
144+
.select("select d.name,d.super_id from t_demo d join t_test t on t.demo_id = d.id ")
145+
.where()
146+
.date("time","2020-04-12")
147+
.or()
148+
.equal("id",31)
149+
.and()
150+
.in("id",1,2,3,4,5,6,7,8,9,10)
151+
.and()
152+
.like("name","2")
153+
.orderBy("name desc")
154+
.builder());
155+
log.info("list:{}",list);
156+
}
157+
158+
@Test
159+
void queryList(){
160+
//select * from t_demo where name = '123'
161+
List<Demo> list = demoMapper.query(QueryBuilder.Build().where().equal("name","123").builder());
162+
log.info("list:{}",list);
163+
}
164+
```
165+
166+
## 支持
167+
issue
168+
169+
## 使用建议
170+
171+
设计数据库时可以先定义类,然后通过jap扫描这些entity来创建数据库,这样做不仅可以完成数据库的设计,也可以直接完成entity的定义,由于框架兼任了jpa注解也不会在框架使用这些entity上出现错误。
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.codingapi.simplemybatis.mapper;
2+
3+
/**
4+
* 所有mapper的定义
5+
*
6+
* @param <T>
7+
*/
8+
public interface BaseMapper<T> {
9+
10+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.codingapi.simplemybatis.mapper;
2+
3+
import com.codingapi.simplemybatis.provider.CommandProvider;
4+
import org.apache.ibatis.annotations.*;
5+
6+
import java.util.List;
7+
8+
/**
9+
* 常用的写操作
10+
*
11+
* @param <T>
12+
*/
13+
public interface CommonMapper<T> extends BaseMapper<T> {
14+
15+
@InsertProvider(type = CommandProvider.class, method = "save")
16+
@Options(useGeneratedKeys = true, keyProperty = "id")
17+
int save(T t);
18+
19+
@InsertProvider(type = CommandProvider.class, method = "saveAll")
20+
@Options(useGeneratedKeys = true, keyProperty = "id")
21+
int saveAll(@Param("list") List<T> list);
22+
23+
@UpdateProvider(type = CommandProvider.class, method = "update")
24+
int update(T t);
25+
26+
@DeleteProvider(type = CommandProvider.class, method = "delete")
27+
int delete(T t);
28+
29+
@DeleteProvider(type = CommandProvider.class, method = "deleteAll")
30+
int deleteAll(@Param("list") List<T> list);
31+
32+
@DeleteProvider(type = CommandProvider.class, method = "deleteById")
33+
int deleteById(@Param("id") Object id);
34+
35+
@DeleteProvider(type = CommandProvider.class, method = "deleteAllById")
36+
int deleteAllById(@Param("list") List id);
37+
38+
39+
}

0 commit comments

Comments
 (0)