无锡哪里有做网站的,怎样免费安装wordpress,泰州网站设计,大连html5开发尊贵的Spring玩家#xff0c;是不允许动脑思考的#xff0c;所以我们要学会复制粘贴 1.生成类与映射文件
背景#xff1a;在项目编写初期#xff0c;我们已经设计好了表#xff0c;后面就需要根据表来撰写实体类(model)和对应的sql语句(dao和mapper)。如果一个项目中是不允许动脑思考的所以我们要学会复制粘贴 1.生成类与映射文件
背景在项目编写初期我们已经设计好了表后面就需要根据表来撰写实体类(model)和对应的sql语句(dao和mapper)。如果一个项目中表有很多很多单单是花在上面的时间估计就会占很大的一个比重。
所以我们可以借助一些mybatis提供的一些工具来自动生成。下面介绍如何使用工具。
一共四大步配置pom文件、编写xml配置类、双击生成、配置扫描路径和yml
1配置pom文件 对于pom文件有两步。可以直接复制使用无需修改 在properties标签中加入版本号
mybatis-generator-plugin-version1.4.1/mybatis-generator-plugin-version 在build -- plugins标签下加入下面的配置
!-- mybatis ⽣成器插件 --
plugingroupIdorg.mybatis.generator/groupIdartifactIdmybatis-generator-maven-plugin/artifactIdversion${mybatis-generator-plugin-version}/versionexecutionsexecutionidGenerate MyBatis Artifacts/idphasedeploy/phasegoalsgoalgenerate/goal/goals/execution/executions!-- 相关配置 --configuration!-- 打开⽇志 --verbosetrue/verbose!-- 允许覆盖 --overwritetrue/overwrite!-- 配置⽂件路径 --configurationFilesrc/main/resources/mybatis/generatorConfig.xml/configurationFile/configuration
/plugin
位置 2编写xml配置类 这一步工作量最大需要该的地方最多大家先复制好下面的文件再按照步骤进行修改成自己项目中的配置 第一步创建generatorConfig.xml 在resources目录下创建一个mybatis目录然后在mybatis目录下创建generatorConfig.xml负责然后点击File生产即可
问题答疑为什么要在这个目录下起这个名字就是因为前面配置的pom文件已经规定了。 第二步负责下面这段代码到generatorConfig.xml中
?xml version1.0 encodingUTF-8?
!DOCTYPE generatorConfigurationPUBLIC -//mybatis.org//DTD MyBatis Generator Configuration 1.0//ENhttp://mybatis.org/dtd/mybatis-generator-config_1_0.dtdgeneratorConfiguration!-- 驱动包路径location中路径替换成⾃⼰本地路径 --classPathEntry locationD:\Maven\.m2\repository\mysql\mysql-connector-java\5.1.49\mysql-connector-java-5.1.49.jar/context idDB2Tables targetRuntimeMyBatis3!-- 禁⽤⾃动⽣成的注释 --commentGeneratorproperty namesuppressAllComments valuetrue/property namesuppressDate valuetrue//commentGenerator!-- 连接配置 --jdbcConnection driverClasscom.mysql.jdbc.DriverconnectionURLjdbc:mysql://127.0.0.1:3306/forum_db?characterEncodingutf8amp;useSSLfalseuserIdrootpassword2003/jdbcConnectionjavaTypeResolver!-- ⼩数统⼀转为BigDecimal --property nameforceBigDecimals valuefalse//javaTypeResolver!-- 实体类⽣成位置 --javaModelGenerator targetPackageorg.ljy.forum6.model targetProjectsrc/main/javaproperty nameenableSubPackages valuetrue/property nametrimStrings valuetrue//javaModelGenerator!-- mapper.xml⽣成位置 --sqlMapGenerator targetPackagemapper targetProjectsrc/main/resourcesproperty nameenableSubPackages valuetrue//sqlMapGenerator!-- DAO类⽣成位置 --javaClientGenerator typeXMLMAPPER targetPackageorg.ljy.forum6.dao targetProjectsrc/main/javaproperty nameenableSubPackages valuetrue//javaClientGenerator!-- 配置⽣成表与实例, 只需要修改表名tableName, 与对应类名domainObjectName 即可--table tableNamet_article domainObjectNameArticleenableSelectByExamplefalseenableDeleteByExamplefalse enableDeleteByPrimaryKeyfalseenableCountByExamplefalseenableUpdateByExamplefalse!-- 类的属性⽤数据库中的真实字段名做为属性名, 不指定这个属性会⾃动转换 _ 为驼峰命名规则--property nameuseActualColumnNames valuetrue//tabletable tableNamet_article_reply domainObjectNameArticleReplyenableSelectByExamplefalseenableDeleteByExamplefalse enableDeleteByPrimaryKeyfalseenableCountByExamplefalseenableUpdateByExamplefalseproperty nameuseActualColumnNames valuetrue//tabletable tableNamet_board domainObjectNameBoardenableSelectByExamplefalse enableDeleteByExamplefalseenableDeleteByPrimaryKeyfalse enableCountByExamplefalseenableUpdateByExamplefalseproperty nameuseActualColumnNames valuetrue//tabletable tableNamet_message domainObjectNameMessageenableSelectByExamplefalseenableDeleteByExamplefalse enableDeleteByPrimaryKeyfalseenableCountByExamplefalseenableUpdateByExamplefalseproperty nameuseActualColumnNames valuetrue//tabletable tableNamet_user domainObjectNameUserenableSelectByExamplefalse enableDeleteByExamplefalseenableDeleteByPrimaryKeyfalse enableCountByExamplefalseenableUpdateByExamplefalseproperty nameuseActualColumnNames valuetrue//table/context
/generatorConfiguration
先别管那么多先复制到进去再说下面再说修改的地方
第三步修改对应值
1修改驱动包路径
classPathEntry locationD:\Maven\.m2\repository\mysql\mysql-connector-java\5.1.49\mysql-connector-java-5.1.49.jar/ 这个需要找到本地maven仓库里面存放关于mysql的jar包路径也就是本地存放jar的包路径在maven学习阶段也就是配置本地镜像时的知识点
寻找方法 最后加上jar包进行替换原路径即可
2修改数据库连接配置
位置 修改的地方有三个你的数据库名字数据库用户名、数据库密码如果是纯数字需要加上单/双引号
3实体类生产位置
位置 改框起来的修改成自己的路径后面model包不需要改会自己生产
4修改dao类生成路径
位置 这个和上面一样修改成自己的包路径后面的dao包会自己生产 5注意点
下面就是一些数据表的名字需要同步 3生成运行
先生成一个mapper目录 运行插件
修改完pom文件记得先刷新然后点开maven双击运行即可生成 生成的效果与后续注意
生成的三个包下 注意点这些是系统生成的特别是xml跟原有的dao不要去修改它最好的方式就是另起接口。后续也不要再双击maven可能会生成不可控的东西。
生成完还需要自己手动添加注解等
4配置路径和yml
配置包扫描路径 输入以下代码
Configuration
MapperScan(org.ljy.forum6.dao)
public class MybatisConfig {}
其中dao前面的路径需要修改成自己的
配置yml
# mybatis 相关配置单独配置顶格写
mybatis:mapper-locations: classpath:mapper/**/*.xml # 指定 xxxMapper.xml的扫描路径 至此所有工作已完成 2.实现API自动生成
背景在写项目的过程中需要我们测试的接口非常的多。如果我们借助postman一个个进行输入路径和参数进行测试那也是一个非常重的体力活。我们作为cv程序猿怎么能允许这种事情发生呢所以接下来跟我学习如何偷懒
下面分成三大步配置pom文件、写配置类、配置yml文件
1配置pom文件
在properties标签中加入版本号
springfox-boot-starter.version3.0.0/springfox-boot-starter.version
在dependencies标签中加入以下文件
!-- API⽂档⽣成基于swagger2 --
dependencygroupIdio.springfox/groupIdartifactIdspringfox-boot-starter/artifactIdversion${springfox-boot-starter.version}/version
/dependency
!-- SpringBoot健康监控 --
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId
/dependency
配置完记得进行刷新
2配置类 在org.ljy.forum6包下新建SwaggerConfig.java 加入以下的代码
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;import java.util.ArrayList;
import java.util.Collection;
import java.util.List;/*** Swagger配置类** Author 比特就业课*/
// 配置类
Configuration
// 开启Springfox-Swagger
EnableOpenApi
public class SwaggerConfig {/*** Springfox-Swagger基本配置* return*/Beanpublic Docket createApi() {Docket docket new Docket(DocumentationType.OAS_30).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage(org.ljy.forum6.controller)).paths(PathSelectors.any()).build();return docket;}// 配置API基本信息private ApiInfo apiInfo() {ApiInfo apiInfo new ApiInfoBuilder().title(论坛系统API).description(论坛系统前后端分离API测试).contact(new Contact(Bit Tech, https://edu.bitejiuyeke.com, 2742676336qq.com)).version(1.0).build();return apiInfo;}/*** 解决SpringBoot 2.6.0以上与Swagger 3.0.0 不兼容的问题* 复制即可**/Beanpublic WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,ServletEndpointsSupplier servletEndpointsSupplier,ControllerEndpointsSupplier controllerEndpointsSupplier,EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,WebEndpointProperties webEndpointProperties, Environment environment) {ListExposableEndpoint? allEndpoints new ArrayList();CollectionExposableWebEndpoint webEndpoints webEndpointsSupplier.getEndpoints();allEndpoints.addAll(webEndpoints);allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());String basePath webEndpointProperties.getBasePath();EndpointMapping endpointMapping new EndpointMapping(basePath);boolean shouldRegisterLinksMapping this.shouldRegisterLinksMapping(webEndpointProperties, environment,basePath);return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),shouldRegisterLinksMapping, null);}private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment,String basePath) {return webEndpointProperties.getDiscovery().isEnabled() (StringUtils.hasText(basePath)|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));}}
需要修改的地方
位置1 controller前面的包修改成自己的。一般我们都是测试controller接口所以就这么写。
位置2 这些信息自己配置即可不写也是OK的 3配置yml文件
在Spring节点下进行配置
spring:mvc:pathmatch:matching-strategy: ANT_PATH_MATCHER #Springfox-Swagger兼容性配置
以上就是配置的全部工作了这只是准备功能要生产api还得靠自己运用注解
访问的网站
http://127.0.0.1:13145/swagger-ui/index.html
其中端口号改成自己项目中配置的
4使用API注解
这里有五大注解分别运用在不同的地方。
五大注解说明
1API作用在Controller上对控制类的说明。比如Api(tags 我是一个controller)
2ApiModel作用在响应的类上对返回响应数据的说明
3ApiModelProerty作用在类的属性上对属性的说明
4ApiOperation作用在具体方法上对API接口的说明
5ApiParam作用在方法的每一个参数上对参数的属性进行说明
其中最常用的就是第一、四、五个所以其他的就不演示了
效果展示
首先是运用在代码上的效果 接口效果 一定复制下面的网址http://127.0.0.1:13145/swagger-ui/index.html并且修改端口号最后启动项目才能出现上面的效果。
接口使用 导入postman
不仅可以直接生成接口测试我们还能将他们导入postman中进入永久保存
第一复制上面的网址
http://127.0.0.1:13145/swagger-ui/index.html
第二输入下面的位置 如果页面和上述不一样请自行搜索关于postman导入的文章。