创建controller后 需要标记注解
@RestController 标记以后springboot会纳入管理
@RequestMapping 标记路由
全局的路由配置前缀api可以放在application.properties中 server.servlet.context-path=/api
这样访问的地址前面就可以加上http://server:port/context-path/swagger-ui.html
如果想使用自己定义的config文件例如: config.properties
在java类中如果想使用@value注解 需要
//PropertySourcesPlaceholderConfigurer是一个用于处理属性文件中占位符的 Bean。在Spring应用中,当我们使用@Value注解来注入属性值时,如果属性值是由占位符定义的,那么就需要用到PropertySourcesPlaceholderConfigurer来进行占位符的解析和替换。 application.properties 不用写这个是因为springboot默认会加载这个
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
配置这个
@Component
@Configuration
@PropertySource("classpath:config.properties")
public class SysConfig {
@Value("${config.debug}")
public boolean debug;
@Value("${config.keyId}")
public String keyId;
//测试属性注入的正确性
@Value("${config.haha}")
public String haha;
//@Value() 使用注意要
//PropertySourcesPlaceholderConfigurer是一个用于处理属性文件中占位符的 Bean。在Spring应用中,当我们使用@Value注解来注入属性值时,如果属性值是由占位符定义的,那么就需要用到PropertySourcesPlaceholderConfigurer来进行占位符的解析和替换。 application.properties 不用写这个是因为springboot默认会加载这个
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
然后在使用的类中比如Utility中注入
static SysConfig config;
//依赖注入
@Autowired
public Utility(SysConfig sysConfig) {
config=sysConfig;
}这样就可以使用 config.appId 调用自定义的内容
2--------------------------------引入spring.doc
首先pom.xml
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
然后新建config配置类
package com.gas;
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.info.License;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.servers.Server;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.context.annotation.Configuration;
/**
* @author admin
*/
@OpenAPIDefinition(
tags = {
@Tag(name = "用户管理", description = "用户模块操作"),
@Tag(name = "角色管理", description = "角色模块操作")
},
info = @Info(
title = "用户接口 API 文档",
description = "用户数据管理......",
version = "1.0.0",
contact = @Contact(name = "luyuxin", email = "luyuxinlyx@163.com", url = ""),
license = @License(name = "Apache 2.0", url = "http://www.apache.org/licenses/LICENSE-2.0.html")
),
servers = {
@Server(description = "生产环境服务器", url = "https://xxxx.com/api/v1"),
@Server(description = "测试环境服务器", url = "https://test.xxxx.com/api/v1")
},
security = @SecurityRequirement(name = "Oauth2"),
externalDocs = @ExternalDocumentation(
description = "项目编译部署说明",
url = "http://localhost/deploy/README.md"
)
)
@Configuration
public class SwaggerConfig {
}
一定要记得纳入管理
sqlserver 我用得是微软的这个数据库,然后说一下占位符 ?
执行存储过程
String sql = “{call IsExist_Sys_Users_Sp(?, ?)}”;
stmt.setString(1, loginName); // 设置值
赋值需要根据位置 从1开始 不是从0开始
使用try-withs-resources 可以避免忘记关闭链接的问题
try (CallableStatement stmt = conn.prepareCall(sql)) {
// 如果使用 ?占位符 赋值时需要用索引
// String sql = “{call IsExist_Sys_Users_Sp(?, ?)}”;
// stmt.setString(1, loginName);
// stmt.setString(2, password);
// stmt.registerOutParameter(3, Types.INTEGER); // 注册输出参数 // 获取输出参数值
// int outputParam = stmt.getInt(3);
// 如果使用的命名参数的方式 则需要用命名参数的方式赋值
stmt.setString(1, loginName); // 使用命名参数设置值
stmt.setString(2, password); // 使用命名参数设置值
// 执行存储过程
stmt.execute();
// 获取结果集
// 使用try-with-resources 不用手动关闭 resultSet.Close();
try (ResultSet resultSet = stmt.getResultSet()) {
if (resultSet.next()) {
do {
user.setOptUserId(resultSet.getInt("OptUserID"));
user.setLoginName(resultSet.getString("LoginName"));
user.setRealName(resultSet.getString("RealName"));
user.setPassword(resultSet.getString("Password"));
user.setRoleId(resultSet.getString("RoleID"));
user.setEmpId(resultSet.getInt("EmpID"));
user.setCompanyId(resultSet.getInt("CompanyID"));
user.setCompanyName(resultSet.getString("CompanyName"));
user.setDeptId(resultSet.getInt("deptID"));
user.setDepartmentName(resultSet.getString("departmentName"));
} while (resultSet.next());
} else {
return BusinessResponse.fail("01","登录失败");
}
// while (resultSet.next()) {
//
// }
} catch (SQLException e) {
return BusinessResponse.fail("99",e.getMessage());
}
} catch (SQLException e) {
// 处理异常
return BusinessResponse.fail("99",e.getMessage());
}