springboot mysql踩过的坑

本文总结了SpringBoot应用在连接MySQL时遇到的一些配置问题,包括sql_mode不兼容、字符集编码冲突、数据插入量过大导致的错误及SSL验证问题,并提供了相应的解决办法。

服务的环境经常变更,水土不服导致程序跑不起来。所有问题均来自配置问题。

1、this is incompatible with sql_mode=only_full_group_by......

MySQL版本问题

https://blog.youkuaiyun.com/qq_42175986/article/details/82384160

修改配置文件:

sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
       重启mysql服务,顺利解决。 

或者临时修改:

select @@GLOBAL.sql_mode;

set@@GLOBAL.sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

 

2、java.sql.SQLException: Illegal mix of collations (gb2312_chinese_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='

MySQL内部字符集编码不统一

https://blog.youkuaiyun.com/hqyhqyhq/article/details/9278773

搞不定就手动改表吧,utf8是最安全的。

 

3、SQLErrorCodes loaded: [DB2, Derby, H2, HDB, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]

MySQL数据插入量过大

MySQL优化:设置max_allowed_packet变量
https://www.cnblogs.com/gmq-sh/p/7096687.html

数据库服务器最好独立,内存吃不消的时候,它会自己改默认配置。。。。。

4、javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors

mysql SSL验证配置问题;

将useSSL=true改为useSSL=false

https://blog.youkuaiyun.com/wandrong/article/details/83347267

 

 


 

<think> 嗯,用户想查找基于Spring Boot开发考研论坛的教程、代码和架构设计信息。从用户的提问来看,ta可能是个计算机专业的学生或开发者,正在准备开发类似的毕业设计或项目。用户引用了多篇关于考研平台设计的文献,说明ta需要的是具体的技术实现方案。 用户实际需求应该是:获取从零开始搭建Spring Boot考研论坛的完整技术路线,包括核心模块实现和架构设计要点。用户可能还希望得到可运行的示例代码和关键问题的解决方案。 从技术角度看,这类系统需要重点解决几个问题:用户权限管理(特别是学生/管理员角色)、帖子与评论的树形结构存储、学习资料的上传下载机制、以及高性能的全文检索功能。前端用Vue.js配合Axios异步加载数据会是不错的选择。 注意到用户提供的引用[3]提到系统包含论坛、个人中心、资料交易等模块,这个需求比较完整。在实现时需要特别注意: 1. Spring Security配置RBAC权限模型时,要区分普通用户发帖权限和版主的管理权限 2. 帖子表设计建议采用path枚举方式存储树形评论,比闭包表更节省空间 3. 文件存储最好整合MinIO对象存储服务 用户可能遇到的难点会是实时消息推送和复杂查询优化。WebSocket消息推送可以参考Spring的STOMP实现,而查询优化可以用Elasticsearch做二级索引。示例代码里特意给出了全文搜索的Repository写法,因为这是论坛系统的性能关键点。 考虑到用户可能刚接触Spring Boot,在回复中补充了Maven依赖示例和配置文件要点。特别提醒了application.yml里要配置MySQL连接池和MyBatis扫描路径,这是新手常。另外加入Redis缓存配置建议,因为论坛类应用必须考虑高并发场景。 最后提出的延伸问题覆盖了系统扩展性(微服务化)、安全性(JWT加固)和部署方案(Docker),这些都是实际开发中必然要面对的挑战。如果用户需要,后续可以深入讲解OAuth2.0社交登录集成或压力测试方案。 </think> 基于Spring Boot开发考研论坛的系统设计及实现方案如下,结合主流技术栈和最佳实践: ### 一、技术架构设计 ```mermaid graph TD A[前端 Vue.js] -->|Axios请求| B[Spring Boot后端] B --> C[Spring Security 鉴权] B --> D[MySQL 数据库] B --> E[Redis 缓存] F[MinIO 文件存储] --> B G[Elasticsearch 搜索] --> B ``` ### 二、核心模块实现 #### 1. 领域模型设计(MySQL) ```sql -- 用户表 CREATE TABLE `user` ( `id` BIGINT PRIMARY KEY AUTO_INCREMENT, `username` VARCHAR(50) UNIQUE, `password` VARCHAR(100), -- BCrypt加密 `role` ENUM('STUDENT','ADMIN') ); -- 帖子表 CREATE TABLE `post` ( `id` BIGINT PRIMARY KEY AUTO_INCREMENT, `title` VARCHAR(100), `content` TEXT, `user_id` BIGINT, `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP ); -- 评论表(支持二级评论) CREATE TABLE `comment` ( `id` BIGINT PRIMARY KEY AUTO_INCREMENT, `content` TEXT, `post_id` BIGINT, `parent_id` BIGINT DEFAULT NULL, -- 父评论ID `user_id` BIGINT ); ``` #### 2. Spring Boot 核心代码示例 **实体类 (Post.java)**: ```java @Entity public class Post { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(nullable = false) private String title; @Lob @Column(columnDefinition = "TEXT") private String content; @ManyToOne @JoinColumn(name = "user_id") private User user; @OneToMany(mappedBy = "post", cascade = CascadeType.ALL) private List<Comment> comments = new ArrayList<>(); } ``` **服务层 (PostService.java)**: ```java @Service public class PostService { private final PostRepository postRepo; @Cacheable(value = "posts", key = "#id") public Post getPostById(Long id) { return postRepo.findById(id) .orElseThrow(() -> new ResourceNotFoundException("Post not found")); } @Transactional public Post createPost(PostRequest request, User author) { Post post = new Post(); post.setTitle(request.getTitle()); post.setContent(request.getContent()); post.setUser(author); return postRepo.save(post); } } ``` **安全配置 (SecurityConfig.java)**: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests() .antMatchers("/api/posts/**").permitAll() .antMatchers("/api/admin/**").hasRole("ADMIN") .anyRequest().authenticated() .and() .addFilter(new JwtAuthenticationFilter(authenticationManager())) .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); } } ``` ### 三、关键功能实现方案 1. **权限控制** - 采用JWT+Spring Security实现无状态鉴权 - 注解控制接口权限:`@PreAuthorize("hasRole('ADMIN')")` 2. **帖子全文搜索** ```java // 集成Elasticsearch @Repository public interface PostSearchRepository extends ElasticsearchRepository<PostEs, Long> { Page<PostEs> findByTitleOrContent(String title, String content, Pageable pageable); } ``` 3. **文件上传(学习资料)** ```java @PostMapping("/upload") public String uploadFile(@RequestParam("file") MultipartFile file) { String objectName = minioClient.putObject( PutObjectArgs.builder() .bucket("study-materials") .object(UUID.randomUUID().toString()) .stream(file.getInputStream(), file.getSize(), -1) .build() ); return fileService.generateUrl(objectName); } ``` ### 四、部署优化方案 1. **容器化部署** ```dockerfile # Dockerfile示例 FROM openjdk:17 COPY target/forum-0.0.1-SNAPSHOT.jar app.jar ENTRYPOINT ["java","-jar","/app.jar"] ``` 2. **性能优化配置** ```yaml # application-prod.yml spring: datasource: hikari: maximum-pool-size: 20 redis: cache: time-to-live: 1h # 缓存1小时 ``` ### 五、学习资源推荐 1. **教程文档** - [Spring Boot官方文档](https://spring.io/projects/spring-boot) - [Vue+Spring Boot前后端分离实战](https://www.bilibili.com/video/BV1PE411i7CV) 2. **开源参考项目** - 论坛系统:[Discourse](https://github.com/discourse/discourse)(Ruby) - Spring Boot实现:[community](https://github.com/codedrinker/community)(Java) 3. **代码生成工具** - 使用 [Spring Initializr](https://start.spring.io/) 快速初始化项目 - MyBatis代码生成器:[mybatis-generator](https://mybatis.org/generator/) > 注:完整项目代码建议参考GitHub开源项目 **spring-boot-forum** 或 **vue-springboot-bbs**,包含用户管理、权限控制、帖子/评论模块等完整实现[^1][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值