Skip to content

Commit 4bda9d5

Browse files
committed
Merge branch 'dev'
2 parents 9c9788f + c427511 commit 4bda9d5

2 files changed

Lines changed: 93 additions & 206 deletions

File tree

README.md

Lines changed: 46 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -22,108 +22,55 @@ The primary goal of the [Spring Data](http://projects.spring.io/spring-data) pro
2222
* Possibility to integrate custom repository code
2323
* Easy Spring integration with custom namespace
2424

25-
## Why choose [Ebean ORM](https://ebean-orm.github.io)
25+
#### Scenarios implemented ####
26+
1. Fetch single entity based on primary key
27+
2. Fetch list of entities based on condition
28+
3. Save new single entity and return primary key
29+
4. Batch insert multiple entities of the same type and return generated keys
30+
5. Update single existing entity - update all fields of entity at once
31+
6. Fetch many-to-one relation (Company for Department)
32+
7. Fetch one-to-many relation (Departments for Company)
33+
8. Update entities one-to-many relation (Departments in Company) - add two items, update two items and delete one item - all at once
34+
9. Complex select - construct select where conditions based on some boolean conditions + throw in some JOINs
35+
10. Execute query using JDBC simple Statement (not PreparedStatement)
36+
11. Remove single entity based on primary key
2637

27-
[CLICK HERE TO SEE](http://ebean-orm.github.io/architecture/compare-jpa)
38+
## Why choose [Ebean ORM](https://ebean-orm.github.io)
39+
Conditions on frameworks which I choose for consideration:
40+
1. The framework should embrace - not hide - SQL language and RDBMS we are using
41+
2. The framework can implement DDD
42+
3. Can utilize JPA annotations, but must not be full JPA implementation
43+
4. The framework must be mature enough for "enterprise level" use
44+
45+
#### Subjective pros/cons of each framework
46+
**Hibernate/JPA**
47+
* [Compare to JPA](http://ebean-orm.github.io/architecture/compare-jpa)
48+
49+
**MyBatis**
50+
* Pros
51+
* Writing SQL statements in XML mapper file feels good - it's easy to work with parameters
52+
* Cons
53+
* Quite a lot of files for single DAO implementation
54+
* Can't run batch and non-batch operations in single SqlSession
55+
. Can't implement DDD
56+
57+
**EBean**
58+
* Pros
59+
* Everything looks very nice - all the scenarios are implemented by very readable code
60+
* Super simple batch operations (actually it's only about using right method :) )
61+
* Although there are methods which make CRUD operations and Querying super simple, there are still means how to execute plain SQL and even a way how to get the basic JDBC Transaction object, which you can use for core JDBC stuff. That is really good.
62+
* Cons
63+
* Not found any
2864

2965
## Quick Start ##
3066

31-
Create maven project,recommend to use spring boot to build web project.
32-
33-
If using spring-boot-starter-data-ebean, see example [spring-boot-data-ebean-samples](https://github.com/hexagonframework/spring-boot-data-ebean-samples)
34-
35-
else following:
36-
download the jar through Maven:
37-
38-
```xml
39-
<dependency>
40-
<groupId>io.github.hexagonframework.data</groupId>
41-
<artifactId>spring-data-ebean</artifactId>
42-
<version>{current version}</version>
43-
</dependency>
44-
```
67+
Create maven project,recommend to use spring boot and [spring-data-ebean-spring-boot](https://github.com/hexagonframework/spring-data-ebean-spring-boot.git) to build web project.
4568

46-
If using maven to compile, package, run,should add:
47-
48-
```xml
49-
<build>
50-
<plugins>
51-
<plugin>
52-
<groupId>io.repaint.maven</groupId>
53-
<artifactId>tiles-maven-plugin</artifactId>
54-
<version>2.8</version>
55-
<extensions>true</extensions>
56-
<configuration>
57-
<tiles>
58-
<tile>org.avaje.tile:java-compile:1.1</tile>
59-
<tile>io.ebean.tile:enhancement:5.1</tile>
60-
</tiles>
61-
</configuration>
62-
</plugin>
63-
</plugins>
64-
</build>
65-
```
66-
67-
If run with ide, should install, enable ebean enhancement plugin.
68-
69-
The simple Spring Data Ebean configuration with Java-Config looks like this:
70-
```java
71-
@Configuration
72-
@EnableEbeanRepositories(value = "org.springframework.data.ebean.sample")
73-
@EnableTransactionManagement
74-
public class SampleConfig {
75-
@Bean
76-
public PlatformTransactionManager transactionManager(DataSource dataSource) {
77-
return new DataSourceTransactionManager(dataSource);
78-
}
79-
80-
@Bean
81-
public EbeanQueryChannelService ebeanQueryChannelService(EbeanServer ebeanServer) {
82-
return new EbeanQueryChannelService(ebeanServer);
83-
}
84-
85-
@SuppressWarnings("SpringJavaAutowiringInspection")
86-
@Bean
87-
@Primary
88-
public ServerConfig defaultEbeanServerConfig() {
89-
ServerConfig config = new ServerConfig();
90-
91-
config.setDataSource(dataSource());
92-
config.addPackage("org.springframework.data.ebean.sample.domain");
93-
config.setExternalTransactionManager(new SpringJdbcTransactionManager());
94-
95-
config.loadFromProperties();
96-
config.setDefaultServer(true);
97-
config.setRegister(true);
98-
config.setAutoCommitMode(false);
99-
config.setExpressionNativeIlike(true);
100-
101-
config.setCurrentUserProvider(new CurrentUserProvider() {
102-
@Override
103-
public Object currentUser() {
104-
return "test"; // just for test, can rewrite to get the currentUser from threadLocal
105-
}
106-
});
107-
108-
return config;
109-
}
110-
111-
@Bean
112-
public DataSource dataSource() {
113-
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
114-
}
115-
116-
@Bean
117-
@Primary
118-
public EbeanServer defaultEbeanServer(ServerConfig defaultEbeanServerConfig) {
119-
return EbeanServerFactory.create(defaultEbeanServerConfig);
120-
}
121-
}
122-
```
69+
Examples: [spring-boot-data-ebean-samples](https://github.com/hexagonframework/spring-boot-data-ebean-samples)
12370

124-
Create an table entity or sql entity:
71+
1. Create modal as table entity or sql entity or DTO:
12572

126-
Table entity
73+
Table entity:
12774
```java
12875
@Entity
12976
public class User {
@@ -140,7 +87,7 @@ public class User {
14087
// equals / hashcode
14188
}
14289
```
143-
Sql entity or DTO(The feature to replace MyBatis)
90+
Sql entity:
14491

14592
Sql entity:
14693
```java
@@ -165,7 +112,7 @@ public class UserDTO {
165112
private String emailAddress;
166113
}
167114
```
168-
Create a repository interface in `org.springframework.data.ebean.sample`:
115+
2. Create a repository interface:
169116

170117
```java
171118
public interface UserRepository extends EbeanRepository<User, Long> {
@@ -200,7 +147,7 @@ public interface UserRepository extends EbeanRepository<User, Long> {
200147
}
201148
```
202149

203-
Create a named query config in `resources/ebean.xml`:
150+
3. Options to create a named query config in `resources/ebean.xml` when using named query:
204151

205152
```xml
206153
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -238,7 +185,7 @@ Create a named query config in `resources/ebean.xml`:
238185
</ebean>
239186
```
240187

241-
Write a test client:
188+
4. Write your code to use model and repository(FOR DDD CURD) or `EbeanQueryChannelService`(FOR DTO QUERY):
242189

243190
`UserRepositoryIntegrationTests.java`
244191
```java

README_zh.md

Lines changed: 47 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -24,109 +24,61 @@
2424
* 方便的与Spring集成
2525
* 支持MySQL、Oracle、SQL Server、H2、PostgreSQL等数据库
2626

27+
#### 实现的场景 ####
28+
1. Fetch single entity based on primary key
29+
2. Fetch list of entities based on condition
30+
3. Save new single entity and return primary key
31+
4. Batch insert multiple entities of the same type and return generated keys
32+
5. Update single existing entity - update all fields of entity at once
33+
6. Fetch many-to-one relation (Company for Department)
34+
7. Fetch one-to-many relation (Departments for Company)
35+
8. Update entities one-to-many relation (Departments in Company) - add two items, update two items and delete one item - all at once
36+
9. Complex select - construct select where conditions based on some boolean conditions + throw in some JOINs
37+
10. Execute query using JDBC simple Statement (not PreparedStatement)
38+
11. Remove single entity based on primary key
39+
2740
## 为什么选择[Ebean ORM](https://ebean-orm.github.io)
2841

2942
基于JPA注解的轻量级ORM实现,支持Mybatis不支持的实体关联,但相比Hibernate/JPA具有Mybatis的查询灵活性,支持查询[partial objects](https://ebean-orm.github.io/docs/query/partialobjects)
3043
对于实现领域模型仓储接口的聚合根实体保存(保存聚合根实体同时保存聚合根上的关联实体、值对象)和partial objects等技术要求,Ebean都非常适用。
31-
[查看更多详情](http://ebean-orm.github.io/architecture/compare-jpa)
44+
45+
我选择关系型数据持久化框架的基本原则:
46+
1. 拥抱SQL而非隐藏
47+
2. 可以实现面向领域编程
48+
3. 可以利用JPA注解,但不能是JPA的完整实现(这点我偏向于Ebean)
49+
4. 足够成熟以应对企业级应用(Ebean和Hibernate同时期作品,资格老,而且持续更新以满足更高需求)
50+
51+
#### 框架优缺点比较
52+
**Hibernate/JPA**
53+
* [Compare to JPA](http://ebean-orm.github.io/architecture/compare-jpa)
54+
* 反正比Hibernate/JPA好
55+
56+
**MyBatis**
57+
* 优点
58+
* 在XML映射文件里写SQL语句很爽
59+
* 缺点
60+
* 实现一个DAO、仓储要写很多文件,方法多了比较繁琐
61+
* 无法在一个方法里做批处理,无法级联加载
62+
* 无法面向对象,无法实现DDD
63+
64+
**EBean**
65+
* 优点
66+
* 所有场景都实现非常完美,代码可读性高
67+
* 实现批处理超级简单
68+
* ORM查询、sql查询、DTO查询都非常简单
69+
* 缺点
70+
* 还没发现
3271

3372
## 快速开始 ##
3473

3574
建立maven项目,建议使用spring boot建立web项目
3675

37-
使用spring-boot-starter-data-ebean,参考[spring-boot-data-ebean-samples](https://github.com/hexagonframework/spring-boot-data-ebean-samples)
38-
39-
如果不使用spring-boot-starter-data-ebean,步骤如下:
40-
41-
通过Maven引入依赖包:
42-
43-
```xml
44-
<dependency>
45-
<groupId>io.github.hexagonframework.data</groupId>
46-
<artifactId>spring-data-ebean</artifactId>
47-
<version>{current version}</version>
48-
</dependency>
49-
```
50-
51-
如果使用Maven编译、打包、运行,需要在pom文件中加入如下插件对实体类进行字节码加强,如果直接通过IDE运行需要安装、开启ebean enhancement插件
76+
实例:[spring-boot-data-ebean-samples](https://github.com/hexagonframework/spring-boot-data-ebean-samples)
5277

53-
```xml
54-
<build>
55-
<plugins>
56-
<plugin>
57-
<groupId>io.repaint.maven</groupId>
58-
<artifactId>tiles-maven-plugin</artifactId>
59-
<version>2.8</version>
60-
<extensions>true</extensions>
61-
<configuration>
62-
<tiles>
63-
<tile>org.avaje.tile:java-compile:1.1</tile>
64-
<tile>io.ebean.tile:enhancement:5.1</tile>
65-
</tiles>
66-
</configuration>
67-
</plugin>
68-
</plugins>
69-
</build>
70-
```
71-
72-
增加配置,最简单的通过Java注解配置的Spring Data Ebean 配置如下所示:
73-
```java
74-
@Configuration
75-
@EnableEbeanRepositories(value = "org.springframework.data.ebean.sample")
76-
@EnableTransactionManagement
77-
public class SampleConfig {
78-
@Bean
79-
public PlatformTransactionManager transactionManager(DataSource dataSource) {
80-
return new DataSourceTransactionManager(dataSource);
81-
}
82-
83-
@Bean
84-
public EbeanQueryChannelService ebeanQueryChannelService(EbeanServer ebeanServer) {
85-
return new EbeanQueryChannelService(ebeanServer);
86-
}
87-
88-
@SuppressWarnings("SpringJavaAutowiringInspection")
89-
@Bean
90-
@Primary
91-
public ServerConfig defaultEbeanServerConfig() {
92-
ServerConfig config = new ServerConfig();
93-
94-
config.setDataSource(dataSource());
95-
config.addPackage("org.springframework.data.ebean.sample.domain");
96-
config.setExternalTransactionManager(new SpringJdbcTransactionManager());
97-
98-
config.loadFromProperties();
99-
config.setDefaultServer(true);
100-
config.setRegister(true);
101-
config.setAutoCommitMode(false);
102-
config.setExpressionNativeIlike(true);
103-
104-
config.setCurrentUserProvider(new CurrentUserProvider() {
105-
@Override
106-
public Object currentUser() {
107-
return "test"; // just for test, can rewrite to get the currentUser from threadLocal
108-
}
109-
});
110-
111-
return config;
112-
}
113-
114-
@Bean
115-
public DataSource dataSource() {
116-
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
117-
}
118-
119-
@Bean
120-
@Primary
121-
public EbeanServer defaultEbeanServer(ServerConfig defaultEbeanServerConfig) {
122-
return EbeanServerFactory.create(defaultEbeanServerConfig);
123-
}
124-
}
125-
```
12678

127-
创建一个表格实体类或SQL实体类:
79+
1、创建一个表格实体类或SQL实体类或DTO类:
12880

129-
表格实体
81+
表格实体
13082
```java
13183
@Entity
13284
public class User {
@@ -143,7 +95,7 @@ public class User {
14395
// equals / hashcode
14496
}
14597
```
146-
SQL实体或POJO DTO(注意:这个是替代MyBatis的重要特性!!!)
98+
SQL实体:
14799

148100
Sql实体:
149101
```java
@@ -168,7 +120,7 @@ public class UserDTO {
168120
}
169121
```
170122

171-
创建一个仓储接口,使用包名 `org.springframework.data.ebean.sample`
123+
2、创建一个仓储接口
172124
```
173125
public interface UserRepository extends EbeanRepository<User, Long> {
174126
@Query("where emailAddress = :emailAddress order by id desc")
@@ -208,7 +160,7 @@ public interface UserRepository extends EbeanRepository<User, Long> {
208160
}
209161
```
210162

211-
对于使用到的命名sql查询、命名orm查询,编写XML文件`resources/ebean.xml`
163+
3、 对于使用到的命名sql查询、命名orm查询,编写XML文件`resources/ebean.xml`
212164

213165
```xml
214166
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
@@ -246,7 +198,7 @@ public interface UserRepository extends EbeanRepository<User, Long> {
246198
</ebean>
247199
```
248200

249-
编写一个测试用例:
201+
4、 编写你的使用代码:
250202

251203
`UserRepositoryIntegrationTests.java`
252204
```java
@@ -429,15 +381,3 @@ public class EbeanQueryChannelServiceIntegrationTests {
429381

430382
}
431383
```
432-
433-
运行
434-
435-
1、使用IntelliJ编译执行Test(推荐)
436-
437-
执行前需要安装ebean enhancement的IntelliJ插件,安装完后要在Build菜单下勾选Enhance 10.x Enhancement。
438-
然后就可以运行Test
439-
440-
2、使用MAVEN编译执行Test
441-
```
442-
mvn test
443-
```

0 commit comments

Comments
 (0)