站长信息
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")
在实体类字段加上该注解