0
# Bootstrap and Configuration
1
2
Framework initialization, global configuration, and multi-datasource setup for MyBatis-Flex applications.
3
4
## Capabilities
5
6
### MybatisFlexBootstrap
7
8
Main entry point for initializing the MyBatis-Flex framework with datasource configuration and mapper registration.
9
10
```java { .api }
11
/**
12
* Main bootstrap class for MyBatis-Flex framework initialization
13
*/
14
public class MybatisFlexBootstrap {
15
/**
16
* Get singleton instance of the bootstrap
17
* @return MybatisFlexBootstrap instance
18
*/
19
public static MybatisFlexBootstrap getInstance();
20
21
/**
22
* Set the primary datasource
23
* @param dataSource DataSource instance
24
* @return bootstrap instance for chaining
25
*/
26
public MybatisFlexBootstrap setDataSource(DataSource dataSource);
27
28
/**
29
* Add named datasource for multi-datasource scenarios
30
* @param key datasource key/name
31
* @param dataSource DataSource instance
32
* @return bootstrap instance for chaining
33
*/
34
public MybatisFlexBootstrap addDataSource(String key, DataSource dataSource);
35
36
/**
37
* Register mapper class
38
* @param mapperClass mapper interface class
39
* @return bootstrap instance for chaining
40
*/
41
public MybatisFlexBootstrap addMapper(Class<?> mapperClass);
42
43
/**
44
* Start the MyBatis-Flex framework
45
* @return bootstrap instance
46
*/
47
public MybatisFlexBootstrap start();
48
49
/**
50
* Get mapper instance by class
51
* @param mapperClass mapper interface class
52
* @return mapper instance
53
*/
54
public <T> T getMapper(Class<T> mapperClass);
55
56
/**
57
* Get MyBatis Configuration object
58
* @return Configuration instance
59
*/
60
public Configuration getConfiguration();
61
}
62
```
63
64
**Basic Usage:**
65
66
```java
67
import com.mybatisflex.core.MybatisFlexBootstrap;
68
import com.zaxxer.hikari.HikariDataSource;
69
70
// Create datasource
71
HikariDataSource dataSource = new HikariDataSource();
72
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/testdb");
73
dataSource.setUsername("root");
74
dataSource.setPassword("password");
75
76
// Initialize MyBatis-Flex
77
MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance()
78
.setDataSource(dataSource)
79
.addMapper(UserMapper.class)
80
.addMapper(OrderMapper.class)
81
.start();
82
83
// Get mapper instances
84
UserMapper userMapper = bootstrap.getMapper(UserMapper.class);
85
OrderMapper orderMapper = bootstrap.getMapper(OrderMapper.class);
86
```
87
88
**Multi-DataSource Setup:**
89
90
```java
91
// Multiple datasources
92
MybatisFlexBootstrap.getInstance()
93
.setDataSource(primaryDataSource) // Primary datasource
94
.addDataSource("read", readOnlyDataSource) // Read-only replica
95
.addDataSource("analytics", analyticsDataSource) // Analytics DB
96
.addMapper(UserMapper.class)
97
.start();
98
```
99
100
### FlexGlobalConfig
101
102
Global configuration management for MyBatis-Flex behavior including database types, key generation, and framework features.
103
104
```java { .api }
105
/**
106
* Global configuration for MyBatis-Flex framework
107
*/
108
public class FlexGlobalConfig {
109
/**
110
* Get default global configuration
111
* @return default FlexGlobalConfig instance
112
*/
113
public static FlexGlobalConfig getDefaultConfig();
114
115
/**
116
* Set configuration for specific environment
117
* @param configId configuration identifier
118
* @param config FlexGlobalConfig instance
119
* @param isDefault whether this is the default config
120
*/
121
public static void setConfig(String configId, FlexGlobalConfig config, boolean isDefault);
122
123
/**
124
* Get database type
125
* @return DbType enum value
126
*/
127
public DbType getDbType();
128
129
/**
130
* Set database type for SQL dialect
131
* @param dbType database type
132
*/
133
public void setDbType(DbType dbType);
134
135
/**
136
* Get key generation configuration
137
* @return KeyConfig instance
138
*/
139
public KeyConfig getKeyConfig();
140
141
/**
142
* Set key generation configuration
143
* @param keyConfig key generation settings
144
*/
145
public void setKeyConfig(KeyConfig keyConfig);
146
147
/**
148
* Get entity listeners configuration
149
* @return entity listeners
150
*/
151
public EntityListeners getEntityListeners();
152
153
/**
154
* Set entity listeners for lifecycle events
155
* @param entityListeners listeners configuration
156
*/
157
public void setEntityListeners(EntityListeners entityListeners);
158
159
/**
160
* Check if optimistic locking is enabled
161
* @return true if enabled
162
*/
163
public boolean isOptimisticLockEnabled();
164
165
/**
166
* Enable or disable optimistic locking
167
* @param enabled enable flag
168
*/
169
public void setOptimisticLockEnabled(boolean enabled);
170
}
171
```
172
173
**Configuration Example:**
174
175
```java
176
import com.mybatisflex.core.FlexGlobalConfig;
177
import com.mybatisflex.core.dialect.DbType;
178
import com.mybatisflex.core.keygen.KeyGenerators;
179
180
// Create custom configuration
181
FlexGlobalConfig config = new FlexGlobalConfig();
182
config.setDbType(DbType.MYSQL);
183
184
// Configure key generation
185
config.getKeyConfig().setKeyGenerator(KeyGenerators.snowFlakeId);
186
187
// Set as default configuration
188
FlexGlobalConfig.setConfig("default", config, true);
189
```
190
191
### KeyConfig
192
193
Configuration for primary key generation strategies.
194
195
```java { .api }
196
/**
197
* Key generation configuration
198
*/
199
public static class KeyConfig {
200
/**
201
* Get key generator
202
* @return IKeyGenerator instance
203
*/
204
public IKeyGenerator getKeyGenerator();
205
206
/**
207
* Set key generator strategy
208
* @param keyGenerator key generator implementation
209
*/
210
public void setKeyGenerator(IKeyGenerator keyGenerator);
211
212
/**
213
* Check if key generation before insert is enabled
214
* @return true if enabled
215
*/
216
public boolean isKeyGeneratorBefore();
217
218
/**
219
* Set whether to generate keys before insert
220
* @param keyGeneratorBefore enable flag
221
*/
222
public void setKeyGeneratorBefore(boolean keyGeneratorBefore);
223
}
224
```
225
226
### EntityListeners
227
228
Entity lifecycle event listeners for insert, update, and set operations.
229
230
```java { .api }
231
/**
232
* Entity lifecycle listeners
233
*/
234
public static class EntityListeners {
235
/**
236
* Get insert listener
237
* @return InsertListener instance
238
*/
239
public InsertListener getInsertListener();
240
241
/**
242
* Set insert listener for before/after insert events
243
* @param insertListener listener implementation
244
*/
245
public void setInsertListener(InsertListener insertListener);
246
247
/**
248
* Get update listener
249
* @return UpdateListener instance
250
*/
251
public UpdateListener getUpdateListener();
252
253
/**
254
* Set update listener for before/after update events
255
* @param updateListener listener implementation
256
*/
257
public void setUpdateListener(UpdateListener updateListener);
258
259
/**
260
* Get set listener
261
* @return SetListener instance
262
*/
263
public SetListener getSetListener();
264
265
/**
266
* Set field value listener
267
* @param setListener listener implementation
268
*/
269
public void setSetListener(SetListener setListener);
270
}
271
```
272
273
## Types
274
275
```java { .api }
276
// Listener interfaces
277
public interface InsertListener {
278
void onInsert(Object entity);
279
}
280
281
public interface UpdateListener {
282
void onUpdate(Object entity);
283
}
284
285
public interface SetListener {
286
Object onSet(Object entity, String property, Object value);
287
}
288
289
// Key generation
290
public interface IKeyGenerator {
291
Object generate(Object entity, String keyProperty);
292
}
293
```