《柒柒架构》DDD领域驱动设计--源代码分享
前言
前面介绍了一些柒柒架构的设计思想,以及部分代码,这篇文章将部分代码路径展示出来。
代码以及上传至github,并且基础架构以及上传至maven中心仓库。如若使用,请使用最新版本。
由于本人即将入职阿里,所以后续更新速度可能会减缓。
(目前maven最新版本是1.0.5-RELEASE,以后会不定期更新)
代码及maven地址
测试代码:qiqiframeworktest
https://github.com/well410/qiqiframeworktest.git
《柒柒架构》代码:
https://github.com/well410/qiqiframework.git
maven引用地址:
<dependency>
<groupId>io.github.well410</groupId>
<artifactId>dddframework</artifactId>
<version>1.0.5-RELEASE</version>
</dependency>
使用前置
- maven的pom.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.well410.qiqiframework</groupId>
<artifactId>qiqiframework-testcase</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.github.well410</groupId>
<artifactId>dddframework</artifactId>
<version>1.0.5-RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.4.2.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.14.0</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<showWeaveInfo/>
<complianceLevel>${java.version}</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>io.github.well410</groupId>
<artifactId>dddframework</artifactId>
</aspectLibrary>
</aspectLibraries>
<aspectDirectory>
/src/main/java/**/*AspectjAop.java
</aspectDirectory>
<forceAjcCompile>true</forceAjcCompile>
<sources/>
<weaveDirectories>
<weaveDirectory>${project.build.outputDirectory}</weaveDirectory>
</weaveDirectories>
</configuration>
</plugin>
</plugins>
</build>
</project>
其中,插件部分无需改动,如若改动后,柒柒架构失效,请按照上述配置改回。
- idea编译方式修改
settings->build->compiler->java compiler->use compiler 修改为javac(使用aspectj,默认会改成ajc,需要改回javac)
使用方式
- 启动类上添加相应注解
@SpringBootApplication
@EnableTransactionManagement
@EnableQiQiDDDCommon //自定义启动工具包,支持LogAop
@EnableQiQiDDDSwagger //使其支持swagger
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
为使事务生效,需添加@EnableTransactionManagement;
为使swagger生效,需添加@EnableQiQiDDDSwagger
重要:为使柒柒架构生效,需添加@EnableQiQiDDDCommon
- 用户接口层
和正常controller没有多少变化,可以配置swagger,使用柒柒架构提供的@LogAnnotation配置日志打印
@PostMapping("/query")
@ApiOperation(value = "用户信息查询", notes = "用户信息查询")
@LogAnnotation
public Response<CustomInfoDto> query(@RequestBody CustomInfoQuery customInfoQuery) {
CustomInfo customInfo = customInfoApplication.query(customInfoQuery.getUserId());
CustomInfoDto customInfoDto = PropertiesCopyUtil.deepCopyProperties(customInfo, CustomInfoDto.class);
return Response.success(customInfoDto, customInfoQuery);
}
- 应用层
需要继承Application(注意是柒柒架构的Application类),以及在相应的事件驱动入口注解入口的正则表达式。
@Service
public class CustomInfoApplication implements Application {
@Autowired
private CustomInfoService customInfoService;
public CustomInfo query(String id) {
return BaseRepository.find(id, CustomInfo.class);
}
public void insert(CustomInfo customInfo) {
BaseRepository.save(customInfo, CustomInfo.class);
}
public void delete(String id) {
BaseRepository.remove(id, CustomInfo.class);
}
public void update(CustomInfo customInfo) {
BaseRepository.save(customInfo, CustomInfo.class);
}
@EventDrivenPattern("transCash.success")
public void doSomething(String id) {
CustomInfo customInfo = BaseRepository.find(id, CustomInfo.class);
customInfo.handle1();
}
public void transCash(int cashMoney, String id1, String id2) {
CustomInfo customInfo1 = BaseRepository.find(id1, CustomInfo.class);
CustomInfo customInfo2 = BaseRepository.find(id2, CustomInfo.class);
customInfoService.transCash(cashMoney, customInfo1, customInfo2);
}
}
- 领域层
聚合需继承Aggregate,实体需继承Entity,值对象需继承VO,领域服务需继承AggregateService接口。
具体详见代码。
注意目录结构,以及业务验证类DddValidation的使用。 - 基础设施层
5.1 config:
如果需要数据库,需要配置DataSource数据源:
MybatisPlusConfig:
@Configuration
@Slf4j
@Data
public class MybatisPlusConfig {
/**
* 必须要要进行注入 依赖actable.model.pack配置指定PO的位置,自动生成MapperLocation
*/
/*@Autowired
private Resource[] resolveMapperLocations;*/
@Bean(name = "hikariConfig")
@ConfigurationProperties(prefix = "spring.datasource")
public HikariConfig hikariConfig() {
HikariConfig hikariConfig = new HikariConfig();
return hikariConfig;
}
@Bean(name = "dataSource")
public DataSource dataSource(@Qualifier("hikariConfig") HikariConfig hikariConfig) {
return new HikariDataSource(hikariConfig);
}
}
柒柒架构已经实现了SQLSession,因此只需要配置DataSource类即可。如果需要自己的sqlsession,则可以定义自己的sqlsession。
5.2 po
定义PO对象,需要继承PO,按照如下格式:
@Data
@Table(name = "user")
@TableName(value = "user")
public class UserPo implements Po {
@TableId
@Column(type = MySqlTypeConstant.CHAR, length = 11, isKey = true, comment = "用户Id")
private String userId;
@Column(type = MySqlTypeConstant.CHAR, length = 11, comment = "用户姓名")
private String userName;
@Column(type = MySqlTypeConstant.INT, length = 11, comment = "财产")
private int cash;
}
柒柒架构实现了根据注解配置,实现数据表的自动创建。建议开发环境开启自动创建,测试生产环境手动创建,这样可以保证数据源和代码的一致性。在application.yml中进行配置:
actable:
table:
auto: update
5.3 poAssemble
可以缺省,没有配置的话,底层将使用PropertiesCopyUtil对象自动映射。
如果自己定义,建议使用mapstruct。
如下示例:(必须使用Spring注入方式)
@Mapper(componentModel = "spring") //使用通用处理仓库时,必须使用spring管理
// 注意这里,在使用通用处理仓库对象处理时,必须继承DomainDoAssemble接口
public interface UserPoAssemblerImpl {
UserPo toPo(User user);
// 如果字段没有不一致,一行注解都不需要
User toEntity(UserPo userPo);
}
或者自定义实现,但必须继承PoAssemble接口。
public interface PoAssemble {
Entity toEntity(Po po);
Po toPo(Entity entity);
}
5.4 Mapper的实现
《柒柒架构》底层使用了mybatis-plus的BaseMapper实现了各个PO对象对应得Mapper,在《柒柒架构》下是足够使用的了。
《柒柒架构》在应用启动时,会自动编译PO对应得Mapper对象,需要再像mybatis-plus那样再手动定义。
5.5 repositoryImpl 实现
《柒柒架构》底层实现了通用的repositoryImpl ,如果自己想复写,可以使用${Aggregate}RepositoryImpl 的命名方式进行命名,并且继承RepositorySupport抽象类,并将其注入Spring容器。
原则上,也无需自己实现。
5.6 防腐层
此章节,后续详细说明。
小结
作者对柒柒架构进行了开源,大家可以一起学习。但是使用柒柒架构,还请注明出处。
目前还有一个问题,待解决。
aspectj与lombok有一些兼容性问题,目前没有很好的解决方案,导致项目在编译时,可能会遇到一些问题。
如果遇到问题。请将idea的use compiler配置(如上)改成javac,重新执行maven compile,即能解决问题。