站长信息
jeffery.xu
jeffery.xu

软件工程师

欢迎访问我的个人笔记网站!我是一名热爱技术的开发者,专注于Web开发和技术分享。

811495111@qq.com
18521510875
筛选

个人笔记

application.yml
java学习
spring:
autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.security.servlet.UserDetailsServiceAutoConfiguration
- org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration
datasource:
url: jdbc:mysql://localhost:3306/knowledge_base?useSSL=false&serverTimezone=UTC
username: jeffery
password: Password01!
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
maximum-pool-size: 20 # 最大连接数
minimum-idle: 5 # 最小空闲连接数
idle-timeout: 600000 # 空闲连接超时时间(毫秒)
max-lifetime: 1800000 # 连接最大存活时间(毫秒)
connection-timeout: 30000 # 获取连接超时时间(毫秒)
data:
redis:
url: redis://127.0.0.1:6379
timeout: 2000ms
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: -1ms
main:
allow-bean-definition-overriding: true
mybatis:
mapper-locations: classpath*:com/mobizone/kb/mapper/*.xml
type-aliases-package: com.mobizone.kb.entity
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
cache-enabled: true
lazy-loading-enabled: true
aggressive-lazy-loading: false
local-cache-scope: STATEMENT
logging:
level:
org.apache.ibatis: WARN
com.mobizone.kb.mapper: DEBUG
io.netty: ERROR
io.netty.util.internal: ERROR
安装redis
java学习

sudo apt update &&sudo apt upgrade -y
sudo apt install redis-server

sudo systemctl start redis-server

sudo systemctl status redis-server

转发接口,使得WSL中的redis也能被访问

netsh interface portproxy add v4tov4 listenport=6379 listenaddress=127.0.0.1 connectport=6379 connectaddress=<WSL2_IP>
验证:
netsh interface portproxy show v4tov4
初始化数据写入
java学习
package com.mobizone.kb.config;

import com.mobizone.kb.entity.Users;
import com.mobizone.kb.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class DataInitializerConfig implements CommandLineRunner {

    @Autowired
    private IUsersService usersService;
    @Override
    public void run(String... args) throws Exception {
        // 检查是否存在admin用户
        if (usersService.userExists("admin") == false) {
            Users admin = new Users();
            admin.setUsername("admin");
            // 加密密码
            admin.setPassword("123456");
            usersService.addUser(admin);
            System.out.println("管理员账号创建成功");
        }
    }
}

CommandLineRunner是Spring Boot提供的一个接口,用于在应用程序完全启动后执行特定的代码逻辑。


CommandLineRunner的作用
1. 启动后执行


在Spring Boot应用完全启动后自动执行
所有Bean都已初始化完成
可以安全地访问数据库和其他服务
2. 初始化数据
常用于项目启动时的数据初始化
创建默认用户、角色等基础数据
数据库预填充

执行时机
启动顺序:
Spring容器启动
所有Bean创建和初始化
应用上下文完全准备就绪
CommandLineRunner.run()方法执行 ← 这里
应用可以接收请求

Mybatis 查询替换字段
java学习
<resultMap id="userMap" type="com.mobizone.kb.entity.Users">
<result property="passwordHash" column="password"/>
<!-- 其他字段映射 -->
</resultMap>
<select id="selectByUserName" parameterType="string" resultMap="userMap">
select * from users where username=#{username}
</select>
Mybatis Exists判断
java学习
<select id="existsByUserName" parameterType="string" resultType="boolean">
select count(1) > 0 from users where username = #{username}
</select>
IDEA禁用拼写检查
java学习

打开设置 (Ctrl+Alt+S)
导航到 Editor -> Inspections
找到 Spelling 检查项
取消勾选即可禁用拼写检查

json和实体类关联
java学习
@JsonProperty("status")
在实体类字段加上该注解
@Bean注解详解
编程技巧

@Bean注解是Spring框架中的核心注解,用于将方法返回的对象注册为Spring容器管理的Bean。


@Bean的核心作用
1. Bean注册


将方法返回的对象注册到Spring的IoC容器中
成为Spring管理的组件,可以被依赖注入
2. 生命周期管理


Spring负责Bean的创建、初始化和销毁
默认为单例模式(Singleton)
3. 依赖注入
可以在任何Spring管理的类中通过@Autowired注入使用

@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

执行过程:
Spring启动时扫描到这个方法
调用方法创建BCryptPasswordEncoder实例
将实例注册到容器中,Bean名称为passwordEncoder
全应用共享这一个实例

常见的Bean作用域
singleton(默认):全局单例
prototype:每次注入创建新实例
request:每个HTTP请求一个实例
session:每个HTTP会话一个实例

@Bean本质上是告诉Spring:"这个方法返回的对象请帮我管理,其他地方需要时直接注入给它们"。
Mysql强行清空有外键的表
编程技巧
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE users;
初步pom配置
编程技巧
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mobizone</groupId>
    <artifactId>kb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>kb</name>
    <description>kb</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>24</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>
    <dependencies>
    <!--    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>-->
<!--        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
<!--        <dependency>-->
<!--            <groupId>com.mysql</groupId>-->
<!--            <artifactId>mysql-connector-j</artifactId>-->
<!--            <scope>runtime</scope>-->
<!--        </dependency>-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>3.0.5</version>
        </dependency>
        <!-- JWT -->
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-api</artifactId>
            <version>0.11.5</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-impl</artifactId>
            <version>0.11.5</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt-jackson</artifactId>
            <version>0.11.5</version>
            <scope>runtime</scope>
        </dependency>
        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.38</version>
            <optional>true</optional>
        </dependency>
        <!-- Swagger (springdoc-openapi) -->
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

总结几个问题
1. springboot 3.5.4  Lombok要版本高
2. mybatis 版本要新
3. 其他的暂时没碰到问题