java 初学记录

本文介绍了如何在SpringBoot项目中创建带有@RestController和@RequestMapping注解的控制器,配置全局路由和使用PropertySourcesPlaceholderConfigurer处理配置文件中的占位符。同时,还展示了如何集成SpringDoc和Swagger生成API文档,以及在SQLServer中使用命名参数和处理异常的方法。
摘要由CSDN通过智能技术生成

创建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());
        }
  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卢小亦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值