# icustom-web **Repository Path**: zgdong/icustom-web ## Basic Information - **Project Name**: icustom-web - **Description**: 定制版本java框架,持续维护 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-06-15 - **Last Updated**: 2023-07-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # icustom-web ## 介绍 本项目采用spring boot 2.7.x(含mvc)加mybatis为基础框架,增加spring-boot-starter-data-jpa实现数据库自动建表,主要支持java项目的定制项目,后续会持续扩展框架功能减少开发重复代码与工作量定制版本java框架,持续维护 ## 软件架构 软件架构说明 ## 安装教程 1. 项目打包 - [ ] 使用mvn clean package -DskipTest 打包 - [ ] 将target/icustom-web-xxxxx.jar包上传至服务器准备编译docker。 2. 编译docker - [ ] 将jar包放置docker-build.sh同级目录; - [ ] 运行docker-build.sh; - [ ] 编译完成后,可运行docker images 查看docker; - [ ] 可使用docker-save.sh将docker保存为.tar文件,移至最终服务器发布。 3. 运行docker - [ ] 修改docker-start.sh数据库参数,运行时需要传递数据库IP、端口、用户名、密码。 - [ ] 运行docker-start.sh 4. 调试docker - [ ] 查看运行日志:docker logs -f --tail 200 icustom-web - [ ] 调试运行容器:docker exec -it icustom-web bash 5. 其它说明 安装中各步骤指令都已经包装为.sh指令集,其中jar包也可直接发布运行,也可使用docker发布,docker部分可省略保存tar步骤,实际情况根据各自相对环境调整。 如果是学习框架用户,下载代码可用20230711为日期截止下载代码;开发用户可持续更新代码。 ## 使用说明 ### 添加日志定制 主要是使用logback输出日志,使用log4j定制日志类,对日志类包装. 使用参考: **导入:** ``` import com.gwtjs.icustom.log.ICustomLogger; import com.gwtjs.icustom.log.ICustomLoggerFactory; ``` **定义:** ``` private static final ICustomLogger log = ICustomLoggerFactory .getLogger(PageInterceptor.class); ``` **输出:** ``` log.info("sql id:", sqlId); ``` ### 添加分页拦截器 ##### -封装分页VO### PageVO主要包含mysql和oracle分页查询时需要的参数,包括总页数,当前页,每页多少条记录,针对 mysql的mysqlStartIndex、mysqlEndIndex等 。并且实现VO内部计算总页数。 ##### -包装返回对象### ResultWrapper 主要包含请求成功、失败、状态码,请求状态,请求返回的信息和数据. PagedResult 主要包含两个属性: 1. PageVO 分页的参数; 2. List 查询出来的具体数据条目。 ##### -包装MyBatis拦截器### 实现自动分页,不用设置分页参数了。 主要涉及 1. MyBatisConfig 分页拦截器配置类; 2. PageInterceptor、ProgramInterceptor 分页拦截器; 3. ExtendedSqlSource mybatis扩展sql 其中MyBatisConfig中加载了mybatis中扫描的pojo包及Mapper的配置。 注意application中需要配置以下属性: ``` mybatis.type-aliases-package=com.gwtjs.**.pojo mybatis.mapper-locations=classpath:mybatis/mapper/*Mapper.xml ## 这里根据自己项目的包修改,扫描到自己的*xml文件,分页插件需要 mybatis-plus.mapper-locations=${mybatis.mapper-locations} ``` #### 分页参考: ##### controller层: ``` @GetMapping("/queryUserList/{mysqlStartIndex}/{mysqlEndIndex}") public PagedResult queryUserListPage(User record, PageVO page){ PagedResult pagedResult = userMapper.findUserListPage(record,page); logger.info("Paged Result:",pagedResult.toString()); return pagedResult; } ``` ##### UserMyMapper.java: ``` PagedResult findUserListPage(@Param("vo")User record,@Param("page")PageVO page); int findUserListPageCount(@Param("vo")User record,@Param("page")PageVO page); ``` ##### UserMyMapper.xml 从代码可以看出,java类不用再做分页的设置了,实现的分页代码的充分 简洁简化,在大量代码的情况下,可省去项目中不计基数的分页代码。 mapper的代码有一个小规划,查询记录和统计的名称有一个Count的区别。 记录的查询 名称为xxxxx,而统计为xxxxxCount,如上:findUserListPage findUserListPageCount 拦截器会根据分页查询的方法名称查找增加Count的统计方法,并将统计查询 结果塞入分页对象相应的totalRows属性中。 ### 添加Swagger2 #### pom.xml ``` io.springfox springfox-swagger2 2.9.2 io.springfox springfox-swagger-ui 2.9.2 com.github.xiaoymin swagger-bootstrap-ui 1.8.7 ``` #### application.properties ``` ###### # 仅用于swagger2 扫描显示 测试接口 spring.contBasePath.swagger2=com.gwtjs.icustom.controller spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER ``` #### Swagger2 配置类 @Configuration @EnableSwagger2 public class Swagger2 { @Value("${spring.application.name}") private String appName; @Value("${spring.contBasePath.swagger2}") private String contBasePath; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage(contBasePath)).paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title(appName+" Swagger2 RESTful APIs").description("仅用于开发环境") .termsOfServiceUrl("http://").contact(appName).version("1.0").build(); } } ### 添加生产环境application.properties 复制了一份application.properties为application-prod.preperties 修改了其中数据库连接属性: ``` spring.datasource.username=${DB_USER} spring.datasource.password=${DB_PASSWORD} spring.datasource.url=jdbc:mysql://${DB_HOST}:${DB_PORT}/bootcustom?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false ``` 其中有${DB_USER}、${DB_PASSWORD}、${DB_HOST}、${DB_PORT}四个属性修改为外部传入。 spring-boot启动时,指定spring.profiles.active Maven启动指定Profile通过-P, 如mvn spring-boot:run -Ptest 但这是Maven的Profile。 如果要指定spring-boot的spring.profiles.active, spring-boot 1.x 使用mvn spring-boot:run -Drun.profiles=test, spring-boot 2.x 使用mvn spring-boot:run -Dspring-boot.run.profiles=test。 ### 开发docker应用,发布应用到docker #### Dockerfile ``` ######## 基础镜像使用java,先走完整体流程,后续再瘦身 FROM jre8:slim-buster MAINTAINER aGuang <8538191@qq.com> ADD icustom-0.0.1-SNAPSHOT.jar /opt/app.jar ######## 运行端口 EXPOSE 11530 RUN cd /opt ###### 启动命令,指定环境配置文件 ENTRYPOINT ["java","-jar","-Dspring.profiles.active=prod","/opt/app.jar"] ####### 设置所属时区 ENV TZ=Asia/Shanghai ###### 创建本地和容器的连接 RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone ``` #### docker-build.sh ``` #!/bin/bash docker build -t flash8627/icustom-web . ``` #### docker-save.sh ``` #!/bin/bash docker save -o icustom-web.tar flash8627/icustom-web ``` #### docker-start.sh ``` #!/bin/bash set -e tar_name="icustom-web.tar" node_type="icustom-web" if [ -z "$node_type" ]; then echo ">>>>>>>> node type is empty >> " exit 0 fi workspace=$(pwd) echo "ready to stop container $node_type" docker stop $node_type || echo "stop container $node_type failed" echo "ready to rm container $node_type" docker rm $node_type || echo "rm container $node_type failed" echo "rm container $node_type complete" img=`docker load -i $workspace/$tar_name | awk -F " " '{print $3}'` echo "load image $img success" echo "$img====================================================" ## echo 111 > $(pwd)/data/myid docker run -d --name $node_type \ --restart=always \ -p 8080:8080 \ -e DB_USER=root \ -e DB_PASSWORD=rootroot123 \ -e DB_HOST=192.168.152.100 \ -e DB_PORT=13308 \ -v $(pwd)/logs:/target/log \ $img echo "sleep 2s" ``` 项目使用8080端口映射到docker启动,数据库用户名、密码、主机、端口在启动docker时指定。 到这里,项目从新建到docker的发布,已经是个完整的流程了,后续也可以将类似分页这样可移植的代码独立出来成为子项目,用jar包的形式导入到项目中,便于开发中简洁代码目录结构 。 ### 添加自动生成数据表 #### 添加依赖: com.mysql mysql-connector-j runtime org.springframework.boot spring-boot-starter-data-jpa javax.validation validation-api #### 添加application.properties ``` spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.open-in-view=false ``` #### 实体类添加注解: 单表参考: /icustom-web/src/main/java/com/gwtjs/icustom/pojo/User.java 一对多参考: /icustom-web/src/main/java/com/gwtjs/icustom/pojo/UserEntity.java /icustom-web/src/main/java/com/gwtjs/icustom/pojo/ClassEntity.java ### 添加测试 #### 单元测试 待续...... #### Apifox测试 | 说明 | 协议 | 地址 | 参数 | | -------- | ------ | ------------------------------------------------------------ | ------------------------------------------------------------ | | 分页测试 | GET | /user/my/queryUserList/
{mysqlStartIndex}/{mysqlEndIndex} | {mysqlStartIndex}:mysql开始索引
{mysqlEndIndex}:mysql结束索引 | | 查询ID | GET | /user/my/{id} | 数据ID | | 新增 | POST | /user/my/add | { "account": "flash8627","password": "flash8627","username": "flash8627", "address": "flash8627" } | | 更新 | PUT | /user/my/update | {"id":3,"account": "old_8627", "password": "flash8627old", "username": "flash8627old", "address": "flash8627old" } | | 删除 | DELETE | /user/my/delete | [{"id":1}..........] | ## 下个功能 1. 添加controller层测试 6. 开发通用dao 6. 开发代码生成器 ## 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 aGuang flash8627 4. 新建 Pull Request ## 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)