发表于: 2018-09-10 22:59:35

1 399


今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin) 

一 . mybatis 逆向生成---maven插件

1. maven添加plugin

 <!-- mybatis 逆向工程maven工具 -->
   <plugin>
       <groupId>org.mybatis.generator</groupId>
       <artifactId>mybatis-generator-maven-plugin</artifactId>
       <version>1.3.2</version>
       <dependencies>
           <dependency>
               <groupId>mysql</groupId>
               <artifactId>mysql-connector-java</artifactId>
               <version>${mysql.version}</version>
           </dependency>
       </dependencies>
       <configuration>
           <!--配置文件的路径-->
           <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
           <overwrite>true</overwrite>
       </configuration>
   </plugin>
</plugins>

2. generatorConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
       PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
       "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
   <context id="test" targetRuntime="MyBatis3">
       <!-- 一些工具 -->
       <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
       <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
       <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
       <commentGenerator>
           <!-- 插入一个日期字段 -->
           <property name="suppressDate" value="true" />
           <!-- 注释 -->
           <!-- 是否去除自动生成的注释 true:是 : false:否 -->
           <property name="suppressAllComments" value="false" />
       </commentGenerator>
       <!--数据库链接 参数 -->
       <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                       connectionURL="jdbc:mysql://localhost:3306/test"
                       userId="root"
                       password="123456">
       </jdbcConnection>
       <javaTypeResolver>
           <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,
       为 true时把JDBC DECIMAL和NUMERIC类型解析为java.math.BigDecimal -->
           <!-- This property is used to specify whether MyBatis Generator should
               force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
           <property name="forceBigDecimals" value="false" />
       </javaTypeResolver>
       <!-- 生成模型的包名和位置 -->
       <javaModelGenerator targetPackage="com.example.mybatisnixiang.model"
                           targetProject="src/main/java">
       <!-- enableSubPackages:是否让schema作为包的后缀 -->
           <property name="enableSubPackages" value="true" />
           <!-- 从数据库返回的值被清理前后的空格 -->
           <property name="trimStrings" value="true" />
       </javaModelGenerator>
       <!-- 生成映射文件的包名和位置 -->
       <sqlMapGenerator targetPackage="com.example.mybatisnixiang.mappers"
                        targetProject="src/main/java">
           <property name="enableSubPackages" value="true" />
       </sqlMapGenerator>
       <!-- 生成DAO的包名和位置 -->
       <javaClientGenerator type="XMLMAPPER"
                            targetPackage="com.example.mybatisnixiang.dao" targetProject="src/main/java">
           <property name="enableSubPackages" value="true" />
       </javaClientGenerator>

       <!-- 要生成哪些表 -->
       <!--tableName写表名,domainObjectName写实体类的名称-->
       <table tableName="taskone" domainObjectName="User"
              enableCountByExample="false" enableUpdateByExample="false"
              enableDeleteByExample="false" enableSelectByExample="false"
              selectByExampleQueryId="false">
           <!--domain字段的命名规则,false:默认为驼峰命名 true:按数据库真实命名  -->
           <property name="useActualColumnNames" value="true"/>
       </table>
   </context>
</generatorConfiguration>

下列配置不生成UserExample.java文件

enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false


二. swagger 简单使用

  • @ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”, response = “接口返回参数类型”, notes = “接口发布说明”;其他参数可参考源码;
  • @ApiImplicitParams:用在方法上包含一组参数说明
  • @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
    • paramType:参数放在哪个地方
    • header-->请求参数的获取:@RequestHeader
    • query-->请求参数的获取:@RequestParam
    • path(用于restful接口)-->请求参数的获取:@PathVariable
    • body(不常用)
    • form(不常用)
    • name:参数名
    • dataType:参数类型
    • required:参数是否必须传
    • value:参数的意思
    • defaultValue:参数的默认值
  • @ApiResponses:用于表示一组响应
  • @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
    • code:数字,例如400
    • message:信息,例如"请求参数没填好"
    • response:抛出异常的类
  • @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
  • @ApiModelProperty:描述一个model的属性

例子

@ApiOperation("获取用户信息")     @ApiImplicitParams({         @ApiImplicitParam(paramType="header",name="username",dataType="String",required=true,value="用户的姓名",defaultValue="zhaojigang"),         @ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="用户的密码",defaultValue="wangna")     })     @ApiResponses({        @ApiResponse(code=400,message="请求参数没填好"),        @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")     })     @RequestMapping(value="/getUser",method=RequestMethod.GET)     

public User getUser(@RequestHeader("username") String username, @RequestParam("password") String password) {         return userService.getUser(username,password);     }

访问地址:http://项目实际地址/swagger-ui.html

例子: http://localhost:8082/swagger-ui.html#/


swaggar 配置

@Bean
   public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//当前包路径
               .apis(RequestHandlerSelectors.basePackage("com.example.jpa.web"))
.paths(PathSelectors.any())
.build();
   }


//    构建 api文档的详细信息函数,注意这里的注解引用的是哪个
//    访问地址:http://项目实际地址/swagger-ui.html
   private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")//接口大标题
               .description("api根地址:http://com.example.jpa:8082/")
.termsOfServiceUrl("https:///")//服务说明url
   //            .contact("小莫")  接口作者联系方式
               .version("1.0")
.build();
   }
}



web

@ApiOperation(value = "获取用户列表", notes = "获取所有用户信息")
@RequestMapping(value = {""}, method = RequestMethod.GET)
public List<User> hello() {
users.add(new User("逻辑", "luoji"));
   users.add(new User("叶文杰", "yewenjie"));
   return users;
}



@ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
@ApiResponses({ @ApiResponse(code = 400, message = "无效的用户信息") })
@RequestMapping(value = "/create", method = RequestMethod.POST)
public User postUser(User user) {

return user;
}

@ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
@RequestMapping(value = "getUser/{id}", method = RequestMethod.GET)

public User getUser(@ApiParam(value = "用户id", required = true) //[注意] @ApiParam与 Controller中方法并列使用,也可以省略的
                   @PathVariable(value = "id") String id) {

return new User(id, "itguang", "123456");
}


访问 http://localhost:8082/swagger-ui.html




返回列表 返回列表
评论

    分享到