Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MaxKey单点登录官方(MaxKeyTop)
Mybatis Jpa Extra
提交
8f4befd7
M
Mybatis Jpa Extra
项目概览
MaxKey单点登录官方(MaxKeyTop)
/
Mybatis Jpa Extra
大约 1 年 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
Mybatis Jpa Extra
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8f4befd7
编写于
4月 23, 2021
作者:
M
MaxKey
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
readme
上级
dd18c2d9
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
184 addition
and
169 deletion
+184
-169
.gitignore
.gitignore
+9
-2
README.md
README.md
+175
-167
mybatis-jpa-extra-2.3.jar
mybatis-jpa-extra-2.3.jar
+0
-0
mybatis-jpa-extra-spring-boot-starter-2.3.jar
mybatis-jpa-extra-spring-boot-starter-2.3.jar
+0
-0
未找到文件。
.gitignore
浏览文件 @
8f4befd7
*.class
*/target/*
.metadata
.classpath
/.metadata/
# Log file
*.log
...
...
@@ -7,3 +9,8 @@
*/logs/*
*/jdk/*
jdk/*
.settings
*/.settings
.project
*/.project
*/.classpath
README.md
浏览文件 @
8f4befd7
# MyBatis JPA Extra
**MyBatis JPA Extra**
对MyBatis
进行了JPA扩展,旨在基于JPA 2.1的注释简化对单表CUID操作,根据JPA注释动态生成SQL语句;使用Interceptor实现数据库SELECT分页查询,适配多种数据库
;另外提供mybatis-jpa-extra-spring-boot-starter简化SpringBoot集成。
**MyBatis JPA Extra**
对MyBatis
扩展JPA功能,旨在基于JPA 2.1的注释简化对单表CUID操作;用Interceptor实现数据库SELECT分页查询
;另外提供mybatis-jpa-extra-spring-boot-starter简化SpringBoot集成。
相关资源
...
...
@@ -24,6 +24,8 @@
1.
**AUTO**
snowflakeid
uuid
uuid.hex
...
...
@@ -40,16 +42,37 @@
```
java
package
org.apache.mybatis.jpa.test.domain
;
/*
* Copyright [2021] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.mybatis.jpa.test.entity
;
import
java.io.Serializable
;
import
javax.persistence.Column
;
import
javax.persistence.Entity
;
import
javax.persistence.GeneratedValue
;
import
javax.persistence.GenerationType
;
import
javax.persistence.Id
;
import
javax.persistence.Table
;
import
org.apache.mybatis.jpa.persistence.JpaBaseDomain
;
import
org.apache.mybatis.jpa.persistence.JpaBaseEntity
;
/*
ID varchar(40) not null,
...
...
@@ -67,7 +90,7 @@ import org.apache.mybatis.jpa.persistence.JpaBaseDomain;
*/
@Entity
@Table
(
name
=
"STUDENTS"
)
public
class
Students
extends
JpaBase
Domain
implements
Serializable
{
public
class
Students
extends
JpaBase
Entity
implements
Serializable
{
/**
*
*/
...
...
@@ -75,7 +98,7 @@ public class Students extends JpaBaseDomain implements Serializable{
@Id
@Column
@GeneratedValue
(
strategy
=
GenerationType
.
AUTO
,
generator
=
"s
erial
"
)
@GeneratedValue
(
strategy
=
GenerationType
.
AUTO
,
generator
=
"s
nowflakeid
"
)
//@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="SEQ_MYBATIS_STUD")
//@GeneratedValue(strategy=GenerationType.IDENTITY,generator="SEQ_MYBATIS_STUD")
private
String
id
;
...
...
@@ -92,78 +115,130 @@ public class Students extends JpaBaseDomain implements Serializable{
@Column
private
String
stdClass
;
@Column
private
byte
[]
images
;
public
Students
()
{
super
();
}
public
String
getStdNo
()
{
return
stdNo
;
}
public
void
setStdNo
(
String
stdNo
)
{
this
.
stdNo
=
stdNo
;
}
public
String
getStdName
()
{
return
stdName
;
}
public
void
setStdName
(
String
stdName
)
{
this
.
stdName
=
stdName
;
}
public
String
getStdGender
()
{
return
stdGender
;
}
public
void
setStdGender
(
String
stdGender
)
{
this
.
stdGender
=
stdGender
;
}
public
int
getStdAge
()
{
return
stdAge
;
}
public
void
setStdAge
(
int
stdAge
)
{
this
.
stdAge
=
stdAge
;
}
public
String
getStdMajor
()
{
return
stdMajor
;
}
public
void
setStdMajor
(
String
stdMajor
)
{
this
.
stdMajor
=
stdMajor
;
}
public
String
getStdClass
()
{
return
stdClass
;
}
public
void
setStdClass
(
String
stdClass
)
{
this
.
stdClass
=
stdClass
;
}
public
String
getId
()
{
return
id
;
}
public
void
setId
(
String
id
)
{
this
.
id
=
id
;
}
public
byte
[]
getImages
()
{
return
images
;
}
public
void
setImages
(
byte
[]
images
)
{
this
.
images
=
images
;
}
@Override
public
String
toString
()
{
return
"Students [
stdNo="
+
stdNo
+
", stdName="
+
stdName
+
", stdgender="
+
stdGender
+
", stdAge="
+
stdAge
+
", stdMajor="
+
stdMajor
+
", stdClass="
+
stdClass
+
"]"
;
return
"Students [
id="
+
id
+
", stdNo="
+
stdNo
+
", stdName="
+
stdName
+
", stdGender="
+
stdGender
+
", std
Age="
+
stdAge
+
", std
Major="
+
stdMajor
+
", stdClass="
+
stdClass
+
"]"
;
}
}
```
## 2、单表新增、修改、删除、查询
## 2、单表新增、修改、删除、查询
、分页查询
```
java
/*
* Copyright [2021] [MaxKey of copyright http://www.maxkey.top]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.mybatis.jpa.test
;
import
java.text.SimpleDateFormat
;
...
...
@@ -171,7 +246,7 @@ import java.util.ArrayList;
import
java.util.Date
;
import
java.util.List
;
import
org.apache.mybatis.jpa.test.dao.service.StudentsService
;
import
org.apache.mybatis.jpa.test.
domain
.Students
;
import
org.apache.mybatis.jpa.test.
entity
.Students
;
import
org.apache.mybatis.jpa.util.WebContext
;
import
org.junit.Before
;
import
org.junit.Test
;
...
...
@@ -181,15 +256,18 @@ import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext
;
public
class
MyBatisTestRunner
{
private
static
final
Logger
_logger
=
LoggerFactory
.
getLogger
(
MyBatisTestRunner
.
class
);
public
static
ApplicationContext
context
;
public
static
StudentsService
service
;
@Test
public
void
insert
()
throws
Exception
{
_logger
.
info
(
"insert..."
);
Students
student
=
new
Students
();
//student.setId("10024");
student
.
setStdNo
(
"10024"
);
student
.
setStdGender
(
"M"
);
student
.
setStdName
(
"司马昭"
);
...
...
@@ -197,137 +275,64 @@ public class MyBatisTestRunner {
student
.
setStdMajor
(
"政治"
);
student
.
setStdClass
(
"4"
);
service
.
insert
(
student
);
Thread
.
sleep
(
1000
);
service
.
remove
(
student
.
getId
());
}
@Test
public
void
get
()
throws
Exception
{
_logger
.
info
(
"get..."
);
Students
student
=
service
.
get
(
"921d3377-937a-4578-b1e2-92fb23b5e512"
);
_logger
.
info
(
"Students "
+
student
);
}
@Test
public
void
remove
()
throws
Exception
{
_logger
.
info
(
"remove..."
);
Students
student
=
new
Students
();
student
.
setId
(
"921d3377-937a-4578-b1e2-92fb23b5e512"
);
service
.
remove
(
student
.
getId
());
}
@Test
public
void
batchDelete
()
throws
Exception
{
_logger
.
info
(
"batchDelete..."
);
List
<
String
>
idList
=
new
ArrayList
<
String
>();
idList
.
add
(
"8584804d-b5ac-45d2-9f91-4dd8e7a090a7"
);
idList
.
add
(
"ab7422e9-a91a-4840-9e59-9d911257c918"
);
idList
.
add
(
"12b6ceb8-573b-4f01-ad85-cfb24cfa007c"
);
idList
.
add
(
"dafd5ba4-d2e3-4656-bd42-178841e610fe"
);
service
.
batchDelete
(
idList
);
}
@Test
public
void
findAll
()
throws
Exception
{
_logger
.
info
(
"findAll..."
);
_logger
.
info
(
"findAll "
+
service
.
findAll
());
}
@Before
public
void
initSpringContext
(){
if
(
context
!=
null
)
return
;
_logger
.
info
(
"init Spring Context..."
);
SimpleDateFormat
sdf_ymdhms
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
String
startTime
=
sdf_ymdhms
.
format
(
new
Date
());
try
{
MyBatisTestRunner
runner
=
new
MyBatisTestRunner
();
runner
.
init
();
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
_logger
.
info
(
"-- --Init Start at "
+
startTime
+
" , End at "
+
sdf_ymdhms
.
format
(
new
Date
()));
}
//Initialization ApplicationContext for Project
public
void
init
(){
_logger
.
info
(
"init ..."
);
context
=
new
ClassPathXmlApplicationContext
(
new
String
[]
{
"spring/applicationContext.xml"
});
Thread
.
sleep
(
1000
);
_logger
.
info
(
"insert id "
+
student
.
getId
());
//service.remove(student.getId());
WebContext
.
applicationContext
=
context
;
service
=(
StudentsService
)
WebContext
.
getBean
(
"studentsService"
);
}
}
```
## 3、支持分页查询
```
java
package
org.apache.mybatis.jpa.test
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.apache.mybatis.jpa.test.dao.service.StudentsService
;
import
org.apache.mybatis.jpa.test.domain.Students
;
import
org.apache.mybatis.jpa.util.WebContext
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.test.context.junit4.SpringRunner
;
@RunWith
(
SpringRunner
.
class
)
@SpringBootTest
(
classes
=
MybatisJpaApplication
.
class
)
public
class
MybatisJpaApplicationTest
{
private
static
final
Logger
_logger
=
LoggerFactory
.
getLogger
(
MybatisJpaApplicationTest
.
class
);
@Autowired
StudentsService
studentsService
;
@Autowired
org
.
apache
.
ibatis
.
session
.
SqlSessionFactory
SqlSessionFactory
;
@Autowired
private
ApplicationContext
applicationContext
;
@Before
public
void
before
()
{
_logger
.
info
(
"---------------- before"
);
WebContext
.
applicationContext
=
applicationContext
;
}
@Test
public
void
insert
()
throws
Exception
{
_logger
.
info
(
"
insert
..."
);
public
void
merge
()
throws
Exception
{
_logger
.
info
(
"
merge
..."
);
Students
student
=
new
Students
();
//student.setId("10024");
student
.
setStdNo
(
"10024"
);
student
.
setStdGender
(
"M"
);
student
.
setStdName
(
"司马昭"
);
student
.
setStdAge
(
20
);
student
.
setStdMajor
(
"政治"
);
student
.
setStdClass
(
"4"
);
s
tudentsService
.
insert
(
student
);
s
ervice
.
merge
(
student
);
Thread
.
sleep
(
1000
);
studentsService
.
remove
(
student
.
getId
());
_logger
.
info
(
"insert id "
+
student
.
getId
());
//service.remove(student.getId());
}
@Test
public
void
get
()
throws
Exception
{
_logger
.
info
(
"get..."
);
Students
student
=
studentsService
.
get
(
"921d3377-937a-4578-b1e2-92fb23b5e512"
);
Students
student
=
service
.
get
(
"317d5eda-927c-4871-a916-472a8062df23"
);
System
.
out
.
println
(
"Students "
+
student
);
_logger
.
info
(
"Students "
+
student
);
}
@Test
public
void
update
()
throws
Exception
{
_logger
.
info
(
"get..."
);
Students
student
=
service
.
get
(
"317d5eda-927c-4871-a916-472a8062df23"
);
System
.
out
.
println
(
"Students "
+
student
);
_logger
.
info
(
"Students "
+
student
);
_logger
.
info
(
"update..."
);
student
.
setImages
(
null
);
service
.
update
(
student
);
_logger
.
info
(
"updateed."
);
student
.
setImages
(
"ssss"
.
getBytes
());
service
.
update
(
student
);
_logger
.
info
(
"updateed2."
);
}
@Test
public
void
find
()
throws
Exception
{
_logger
.
info
(
"find..."
);
Students
student
=
service
.
find
(
Students
.
class
,
"317d5eda-927c-4871-a916-472a8062df23"
);
System
.
out
.
println
(
"Students "
+
student
);
_logger
.
info
(
"Students "
+
student
);
...
...
@@ -340,7 +345,7 @@ public class MybatisJpaApplicationTest{
_logger
.
info
(
"remove..."
);
Students
student
=
new
Students
();
student
.
setId
(
"921d3377-937a-4578-b1e2-92fb23b5e512"
);
s
tudentsS
ervice
.
remove
(
student
.
getId
());
service
.
remove
(
student
.
getId
());
}
...
...
@@ -352,7 +357,7 @@ public class MybatisJpaApplicationTest{
idList
.
add
(
"ab7422e9-a91a-4840-9e59-9d911257c918"
);
idList
.
add
(
"12b6ceb8-573b-4f01-ad85-cfb24cfa007c"
);
idList
.
add
(
"dafd5ba4-d2e3-4656-bd42-178841e610fe"
);
s
tudentsS
ervice
.
batchDelete
(
idList
);
service
.
batchDelete
(
idList
);
}
@Test
...
...
@@ -364,9 +369,9 @@ public class MybatisJpaApplicationTest{
student
.
setStdGender
(
"M"
);
//student.setStdMajor(政治");
student
.
setPageSize
(
10
);
student
.
setPageNumber
(
2
);
student
.
setPageNumber
(
1
);
List
<
Students
>
allListStudents
=
s
tudentsS
ervice
.
queryPageResults
(
student
).
getRows
();
service
.
queryPageResults
(
student
).
getRows
();
for
(
Students
s
:
allListStudents
)
{
_logger
.
info
(
"Students "
+
s
);
}
...
...
@@ -383,53 +388,55 @@ public class MybatisJpaApplicationTest{
student
.
setPageNumber
(
2
);
List
<
Students
>
allListStudents
=
s
tudentsS
ervice
.
queryPageResults
(
"queryPageResults1"
,
student
).
getRows
();
service
.
queryPageResults
(
"queryPageResults1"
,
student
).
getRows
();
for
(
Students
s
:
allListStudents
)
{
_logger
.
info
(
"Students "
+
s
);
}
}
@Test
public
void
findAll
()
{
_logger
.
info
(
"---------------- ALL"
);
List
<
Students
>
allListStudents
=
studentsService
.
findAll
();
@Test
public
void
findAll
()
throws
Exception
{
_logger
.
info
(
"findAll..."
);
List
<
Students
>
allListStudents
=
service
.
findAll
();
for
(
Students
s
:
allListStudents
)
{
_logger
.
info
(
"Students "
+
s
);
}
}
}
package
org.apache.mybatis.jpa.test
;
import
com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder
;
import
javax.sql.DataSource
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Primary
;
@Before
public
void
initSpringContext
(){
if
(
context
!=
null
)
return
;
_logger
.
info
(
"init Spring Context..."
);
SimpleDateFormat
sdf_ymdhms
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
);
String
startTime
=
sdf_ymdhms
.
format
(
new
Date
());
@Configuration
public
class
MybatisJpaConfig
{
private
int
port
;
try
{
MyBatisTestRunner
runner
=
new
MyBatisTestRunner
();
runner
.
init
()
;
public
int
getPort
()
{
return
port
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
()
;
}
public
void
setPort
(
int
port
)
{
this
.
port
=
port
;
_logger
.
info
(
"-- --Init Start at "
+
startTime
+
" , End at "
+
sdf_ymdhms
.
format
(
new
Date
()));
}
@Bean
@Primary
@ConfigurationProperties
(
"spring.datasource"
)
public
DataSource
dataSource
()
{
return
DruidDataSourceBuilder
.
create
().
build
();
//Initialization ApplicationContext for Project
public
void
init
(){
_logger
.
info
(
"Application dir "
+
System
.
getProperty
(
"user.dir"
));
context
=
new
ClassPathXmlApplicationContext
(
new
String
[]
{
"spring/applicationContext.xml"
});
WebContext
.
applicationContext
=
context
;
service
=(
StudentsService
)
WebContext
.
getBean
(
"studentsService"
);
}
}
```
...
...
@@ -502,7 +509,6 @@ public class MybatisJpaConfig {
## 5、SpringBoot配置
```
ini
spring.main.web-application-type
=
NONE
#
spring.datasource.username
=
root
spring.datasource.password
=
maxkey
...
...
@@ -510,10 +516,12 @@ spring.datasource.url=jdbc:mysql://localhost/test?autoReconnect=true&characterEn
spring.datasource.driver-class-name
=
com.mysql.jdbc.Driver
spring.datasource.type
=
com.alibaba.druid.pool.DruidDataSource
mybatis.type-aliases-package
=
org.apache.mybatis.jpa.test.
domain
mybatis.mapper-locations
=
classpath*:/org/apache/mybatis/jpa/test/dao/persistence/xml/
mysql
/*.xml
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
mybatis.table-column-escape
=
true
mybatis.table-column-snowflake-datacenter-id
=
1
mybatis.table-column-snowflake-machine-id
=
1
#
mybatis.table-column-escape-char
=
`
```
...
...
mybatis-jpa-extra-2.3.jar
浏览文件 @
8f4befd7
无法预览此类型文件
mybatis-jpa-extra-spring-boot-starter-2.3.jar
浏览文件 @
8f4befd7
无法预览此类型文件
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录