README.md 9.2 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1
# MyBatis JPA Extra
M
MaxKey 已提交
2 3
**MyBatis JPA Extra**对MyBatis扩展JPA功能
   
M
3.0  
MaxKey 已提交
4
1.Jakarta JPA 3注释**简化CUID操作**;
M
MaxKey 已提交
5
    
M
MaxKey 已提交
6
2.Interceptor实现数据库**SELECT分页查询**;
M
MaxKey 已提交
7
    
M
MaxKey 已提交
8
3.**链式**Query查询条件构造器;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
9

M
MaxKey 已提交
10
4.提供starter,**简化SpringBoot集成**;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
11

M
MaxKey 已提交
12 13 14 15 16 17 18 19 20 21 22
5.数据库支持
| 数据库          |   支持  |
| :-----          | :----   |
| **MySQL**       | ✔      | 
| **PostgreSQL**  | ✔      | 
| **Oracle**      | ✔      |
| **SqlServer**   | ✔      |
| **DB2**         | ✔      |
| **Derby**       | ✔      |


M
MaxKey 已提交
23
## 1、JPA 3注释
M
MaxKey 已提交
24

M
readme  
MaxKey 已提交
25
## 1.1、注释
M
MaxKey 已提交
26

MaxKey单点登录官方's avatar
v2.2  
MaxKey单点登录官方 已提交
27
> * @Entity
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
28 29
> * @Table
> * @Column
MaxKey单点登录官方's avatar
v2.2  
MaxKey单点登录官方 已提交
30
> * @Id
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
31
> * @GeneratedValue
MaxKey单点登录官方's avatar
v2.2  
MaxKey单点登录官方 已提交
32
> * @Transient 
M
MaxKey 已提交
33 34
> * @Temporal
> * @PartitionKey
M
MaxKey 已提交
35 36
> * @ColumnDefault
> * @ColumnLogic
MaxKey单点登录官方's avatar
v2.2  
MaxKey单点登录官方 已提交
37

M
readme  
MaxKey 已提交
38
## 1.2、主键策略
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
39

M
readme  
MaxKey 已提交
40
支持3种主键策略
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
41

M
readme  
MaxKey 已提交
42 43
| 序号    | 策略      |   支持  |
| --------| :-----        | :----   |
M
MaxKey 已提交
44
| 1     | **AUTO**          | 4种主键自动填充策略<br>snowflakeid(雪花ID-默认)<br>uuid(UUID)<br>uuid.hex(UUID十六进制)<br>serial(JPA Extra序列)   | 
M
readme  
MaxKey 已提交
45 46
| 2     | **SEQUENCE**      | 数据库序列生成,generator值为数据库序列名 | 
| 3     | **IDENTITY**      | 数据库表自增主键  |
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
47

M
readme  
MaxKey 已提交
48
## 1.3、Java Bean 注释
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
49

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
50
```java
MaxKey单点登录官方's avatar
v2.2  
MaxKey单点登录官方 已提交
51
@Entity
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
52
@Table(name = "STUDENTS")  
M
MaxKey 已提交
53
public class Students extends JpaEntity implements Serializable{
M
MaxKey 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    @Id
    @Column
    @GeneratedValue
    private String id;
    @Column
    private String stdNo;
    @Column
    private String stdName;
    @Column
    @ColumnDefault("'M'")
    private String stdGender;
    @Column
    private int stdAge;
    @Column
    private String stdMajor;
    @Column
    private String stdClass;
    @Column
    private byte[] images;
    @Column(insertable = false)
    @GeneratedValue
    @Temporal(TemporalType.TIMESTAMP)
M
MaxKey 已提交
76
    private LocalDateTime modifyDate;
M
MaxKey 已提交
77 78 79 80
    @ColumnLogic
    @Column(name ="is_deleted")
    private int isDeleted;
    //getter setter
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
81 82
}
```
M
readme  
MaxKey 已提交
83
## 2、基本操作
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
84

M
readme  
MaxKey 已提交
85
## 2.1、CURD
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
86 87

```java
M
MaxKey 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    //新增数据
    @Test
    void insert() throws Exception{
        Students student = new Students();
        student.setStdNo("10024");
        student.setStdGender("M");
        student.setStdName("司马昭");
        student.setStdAge(20);
        student.setStdMajor("政治");
        student.setStdClass("4");
        service.insert(student);
    }
    //查询数据实体并更新
    @Test
    void update() throws Exception{
        Students student = service.get("317d5eda-927c-4871-a916-472a8062df23");
        student.setStdMajor("政治");
        service.update(student);
    }
    //根据实体查询并更新
    @Test
    void merge() throws Exception{
        Students student = new Students();
        student.setStdMajor("政治");
        student.setStdClass("4");
        service.merge(student);
    }
    //根据ID查询
    @Test
    void get() throws Exception{
        Students student = service.get("317d5eda-927c-4871-a916-472a8062df23");
    }
    //根据实体查询
    @Test
    void query() throws Exception{
        Students student = new Students();
        student.setStdGender("M");
        List<Students> listStudents = service.query(student);
    }
    //查询所有记录
    @Test
    void findAll() throws Exception{
        List<Students> listStudents = service.findAll();
    }
    //根据ID删除
    @Test
    void remove() throws Exception{
        service.remove("921d3377-937a-4578-b1e2-92fb23b5e512");
    }
    //根据ID集合批量删除
    @Test
    void batchDelete() throws Exception{
        List<String> idList = new ArrayList<String>();
        idList.add("8584804d-b5ac-45d2-9f91-4dd8e7a090a7");
        idList.add("ab7422e9-a91a-4840-9e59-9d911257c918");
        //...
        service.deleteBatch(idList);
    }
M
MaxKey 已提交
146 147 148 149 150 151
    //根据ID批量删除
    @Test
    void batchDeleteByIds() throws Exception{
        service.deleteBatch("2");
        service.deleteBatch("2,639178432667713536");
    }
M
readme  
MaxKey 已提交
152
```
M
readme  
MaxKey 已提交
153 154 155
## 2.2、逻辑删除
```java    
    //根据ID删除或者ID字符串分隔符,批量逻辑删除
M
MaxKey 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    @Test
    void logicDelete() throws Exception{
        service.logicDelete("2");
        service.logicDelete("2,639178432667713536");
    }
    //根据IDS批量逻辑删除
    @Test
    void logicBatchDelete() throws Exception{
        List<String> idList=new ArrayList<String>();
        idList.add("8584804d-b5ac-45d2-9f91-4dd8e7a090a7");
        idList.add("ab7422e9-a91a-4840-9e59-9d911257c918");
        //...
        service.logicDelete(idList);
    }
    //根据IDS字符串和分割符批量逻辑删除
    @Test
    void logicDeleteSplit() throws Exception{
        service.logicDeleteSplit("2,639178432667713536",",");
    }
M
readme  
MaxKey 已提交
175 176

```
M
readme  
MaxKey 已提交
177

M
MaxKey 已提交
178
## 2.3、Find查询和Qruey构造器
M
readme  
MaxKey 已提交
179 180

```java
M
MaxKey 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    //SpringJDBC的查询方式 where StdNo = '10024' or StdNo = '10004'
    @Test
    void find() throws Exception{
        List<Students> listStudents = service.find(" StdNo = ? or StdNo = ?  ",
                            new Object[]{"10024","10004"},
                            new int[]{Types.VARCHAR,Types.INTEGER}
                        );
    }
    //根据链式条件构造器查询
    //WHERE (stdMajor = '政治' and STDAGE > 30 and stdMajor in ( '政治' , '化学' )  or  ( stdname = '周瑜' or stdname = '吕蒙' ) )
    @Test
    void queryByCondition() throws Exception{
        List<Students> listStudents = service.query(
                new Query().eq("stdMajor", "政治").and().gt("STDAGE", 30).and().in("stdMajor", new Object[]{"政治","化学"})
                .or(new Query().eq("stdname", "周瑜").or().eq("stdname", "吕蒙")));
    }
M
readme  
MaxKey 已提交
197 198
```

M
MaxKey 已提交
199
## 2.4、单表分页查询
M
readme  
MaxKey 已提交
200 201

```java
M
readme  
MaxKey 已提交
202 203 204
    //根据实体分页查询
    @Test
    void fetch() throws Exception{
M
MaxKey 已提交
205
         JpaPage page = new JpaPage(1);
M
readme  
MaxKey 已提交
206 207 208 209
         Students student = new Students();
         student.setStdGender("M");
         JpaPageResults<Students>  results = service.fetch(page,student);
    }
M
readme  
MaxKey 已提交
210
    //根据Query条件分页查询 where stdMajor = '政治' and STDAGE > 30
M
readme  
MaxKey 已提交
211 212
    @Test
    void fetchByCondition() throws Exception{
M
MaxKey 已提交
213
         JpaPage page = new JpaPage(1,20);
M
readme  
MaxKey 已提交
214 215 216
         Query condition = new Query().eq("stdMajor", "政治").and().gt("STDAGE", 30);
         JpaPageResults<Students>  results = service.fetch(page,condition);
    }
M
MaxKey 已提交
217
```
M
MaxKey 已提交
218

M
MaxKey 已提交
219 220 221
## 2.5、根据mapper的xml分页查询

```java
M
readme  
MaxKey 已提交
222
    //根据Mapper xml配置fetchPageResults分页查询
M
MaxKey 已提交
223 224 225
    @Test
    void fetchPageResults() throws Exception{
         Students student=new Students();
M
MaxKey 已提交
226
         student.setStdGender("M");
M
MaxKey 已提交
227
         student.setPageNumber(1);
M
MaxKey 已提交
228 229
         JpaPageResults<Students>  results = service.fetchPageResults(student);
    }
M
readme  
MaxKey 已提交
230
    //根据Mapper xml id分页查询,fetchPageResults1在mapper的xml中配置
M
MaxKey 已提交
231 232 233 234
    @Test
    void fetchPageResultsByMapperId() throws Exception{
         Students student=new Students();
         student.setStdGender("M");
M
MaxKey 已提交
235
         student.setPageNumber(1);
M
MaxKey 已提交
236 237
         JpaPageResults<Students> results = service.fetchPageResults("fetchPageResults1",student);
    }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
238 239
```

M
readme  
MaxKey 已提交
240
## 3、mapper配置
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
241

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
242 243
```xml
<mapper namespace="org.apache.mybatis.jpa.test.dao.persistence.StudentsMapper" >
M
MaxKey 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    <sql id="sql_condition">
        WHERE   1   =   1
        <if test="id != null">
            AND ID  =   '${id}'
        </if>
        <if test="stdName != null  and stdName != '' ">
            AND STDNAME like '%${stdName}%'
        </if>
        <if test="stdGender != null  and stdGender != '' ">
            AND STDGENDER   =   #{stdGender}
        </if>
        <if test="stdMajor != null">
            <![CDATA[AND    STDMAJOR    = #{stdMajor}]]>
        </if>
    </sql>
    
M
MaxKey 已提交
260
    <select id="fetchPageResults" parameterType="Students" resultType="Students">
M
MaxKey 已提交
261
        SELECT 
M
MaxKey 已提交
262
			id , stdno , stdname ,stdgender , stdage , stdmajor , stdclass 
M
MaxKey 已提交
263 264
        FROM STUDENTS 
        <include refid="sql_condition"/>
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
265 266
    </select>
 
M
MaxKey 已提交
267
     <select id="fetchPageResults1" parameterType="Students" resultType="Students">
M
MaxKey 已提交
268
        SELECT 
M
MaxKey 已提交
269
            id , stdno , stdname ,stdgender , stdage , stdmajor , stdclass 
M
MaxKey 已提交
270 271
        FROM STUDENTS 
        <include refid="sql_condition"/>
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
272 273
    </select>
       
MaxKey单点登录官方's avatar
rm  
MaxKey单点登录官方 已提交
274
     <select id="queryBy" parameterType="Students" resultType="Students">
M
MaxKey 已提交
275
        SELECT 
M
MaxKey 已提交
276
            id , stdno , stdname ,stdgender , stdage , stdmajor , stdclass 
M
MaxKey 已提交
277 278
        FROM ROLES 
        <include refid="sql_condition"/>
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
279 280 281
    </select>
  
    <delete id="delete" parameterType="Students" >
M
MaxKey 已提交
282
        DELETE FROM STUDENTS WHERE ID=#{id}
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
283 284 285 286
    </delete>
```


M
MaxKey 已提交
287
##  4、SpringBoot配置
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
288

MaxKey单点登录官方's avatar
v2.2  
MaxKey单点登录官方 已提交
289 290 291 292 293 294 295 296
```ini
#
spring.datasource.username=root
spring.datasource.password=maxkey
spring.datasource.url=jdbc:mysql://localhost/test?autoReconnect=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

M
readme  
MaxKey 已提交
297 298 299
mybatis.dialect=mysql
mybatis.type-aliases-package=org.apache.mybatis.jpa.test.entity
mybatis.mapper-locations=classpath*:/org/apache/mybatis/jpa/test/dao/persistence/xml/${mybatis.dialect}/*.xml
MaxKey单点登录官方's avatar
v2.2  
MaxKey单点登录官方 已提交
300 301
mybatis.table-column-escape=true
#mybatis.table-column-escape-char=`
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
302
```
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
303

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
304

M
MaxKey 已提交
305 306 307 308 309 310
##  5、相关资源

[MyBatis网站][1]

[MyBatis GitHub源码][2]

M
MaxKey 已提交
311 312 313 314
[MaxKey单点登录认证系统 GitHub源码][3]

[MaxKey单点登录认证系统 Gitee源码][4]

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
315 316
  [1]: http://www.mybatis.org/mybatis-3/
  [2]: https://github.com/mybatis/mybatis-3/
M
MaxKey 已提交
317 318
  [3]: https://github.com/dromara/MaxKey
  [4]: https://gitee.com/dromara/MaxKey