SpringBoot+Mysql开发系统,实体类保存到数据库的时间比系统当前时间晚了13小时

最近使用SpringBoot,Mysql开发一套web系统,实体类往数据库保存的时间比系统当前时间晚13小时。以下查阅问题的全过程,开始尝试的方法均失败,最后找到问题的原因是mysql-connector-java.jar驱动包的版本问题并解决。

以下是尝试的解决办法:

(1)修改Mysql数据库的timezone,修改为CST或者GMT+8,再重启mysql服务,(此方法失败!)

set global time_zone = '+8:00';
flush privileges; #立即生效

 

(2)修改Mysql的配置文件mysql.cnf,增加以下配置项。(重启mysql后,此方法成功!)

# vim /etc/my.cnf ##在[mysqld]区域中加上
default-time-zone = '+8:00'

 

(3)在项目的配置文件中修改jdbc的数据库连接url,后增加&serverTimezone=Asia/Shanghai(重启服务,此方法成功!

后来查看开发环境使用的mysql-connector-java.jar包版本是8.0.17,mysql-connector-java的8.0后的版本会有时区问题。

之前开发的系统使用5.14的驱动,是没有时区问题的,到此查明了原因,问题也解决!

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的积分系统的实现思路: 1. 创建一个MySQL数据库,包含两个表:用户表和积分表。用户表包含用户ID、用户名等基本信息,积分表包含用户ID、积分值等字段。 2. 使用SpringBoot框架来搭建Web应用程序,包含用户登录、积分查询、积分增加、积分减少等功能。 3. 在用户登录时,根据用户名和密码验证用户身份,并将用户ID保存到Session中。 4. 在积分查询时,根据Session中的用户ID查询积分表中的积分值。 5. 在积分增加或减少时,根据Session中的用户ID更新积分表中的积分值。 下面是一个简单的代码示例: 1. 创建MySQL数据库 ```sql CREATE DATABASE IF NOT EXISTS `score_system` DEFAULT CHARSET utf8 COLLATE utf8_general_ci; USE `score_system`; CREATE TABLE IF NOT EXISTS `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(64) NOT NULL DEFAULT '', `password` varchar(64) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `score` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL DEFAULT '0', `score_value` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ``` 2. 创建SpringBoot应用程序 首先,我们需要在pom.xml文件中添加依赖: ```xml <!-- SpringBoot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> ``` 接着,我们需要在application.properties文件中配置MySQL数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/score_system?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver mybatis.mapper-locations=classpath:mapper/*.xml mybatis.configuration.map-underscore-to-camel-case=true ``` 然后,我们需要创建User和Score两个实体类: ```java public class User { private int id; private String username; private String password; // getters and setters } public class Score { private int id; private int userId; private int scoreValue; // getters and setters } ``` 接着,我们需要创建UserMapper和ScoreMapper两个Mapper接口: ```java @Mapper public interface UserMapper { User selectUserByUsernameAndPassword(@Param("username") String username, @Param("password") String password); } @Mapper public interface ScoreMapper { int selectScoreByUserId(@Param("userId") int userId); int insertScore(Score score); int updateScoreByUserId(@Param("userId") int userId, @Param("scoreValue") int scoreValue); } ``` 最后,我们需要创建UserController和ScoreController两个Controller类,提供用户登录和积分查询、增加、减少等接口: ```java @RestController @RequestMapping("/user") public class UserController { @Autowired private UserMapper userMapper; @PostMapping("/login") public String login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) { User user = userMapper.selectUserByUsernameAndPassword(username, password); if (user == null) { return "login failed"; } session.setAttribute("userId", user.getId()); return "login success"; } } @RestController @RequestMapping("/score") public class ScoreController { @Autowired private ScoreMapper scoreMapper; @GetMapping("/query") public int query(HttpSession session) { int userId = (int) session.getAttribute("userId"); return scoreMapper.selectScoreByUserId(userId); } @PostMapping("/add") public int add(@RequestParam("scoreValue") int scoreValue, HttpSession session) { int userId = (int) session.getAttribute("userId"); Score score = new Score(); score.setUserId(userId); score.setScoreValue(scoreValue); scoreMapper.insertScore(score); return scoreMapper.selectScoreByUserId(userId); } @PostMapping("/reduce") public int reduce(@RequestParam("scoreValue") int scoreValue, HttpSession session) { int userId = (int) session.getAttribute("userId"); int oldScore = scoreMapper.selectScoreByUserId(userId); int newScore = Math.max(oldScore - scoreValue, 0); scoreMapper.updateScoreByUserId(userId, newScore); return newScore; } } ``` 以上就是一个简单的积分系统的实现思路,具体的实现细节还需要根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值