整合springboot-mybatis时,MySQL数据库无法连接问题
解决步骤
先手动停止MySQL服务,在cmd后的控制台输入services.msc
找到MySql停止服务
修改配置文件,跳过验证
修改MySQL安装目录下的my.ini配置文件,使登录时跳过权限检查;
使用命令进入MySQL
mysql -u root -p
然后通过SQL语句修改root用户的密码;
USE mysql;
UPDATE user SET password=PASSWORD(‘此处填写新的密码’)WHERE user=‘root’;
使用下方命令之后,即可重新连接MySQL
flush privileges;
接下来为点击MySQL用户,并且为root用户添加可以操作的权限,选择对应的字段进行打勾后保存
再检查appliciation.xml文件配置
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8username: root //用户名password: root //密码
接下来创建@Test案例,创建teacher类,写入数据库对应的字段属性,和teachermapper内容空的接口继承BaseMapper,即可测试springboot+mybatis连接查询数据库中的数据,查询到结果为响应成功
package com.yase.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
@Data
@AllArgsConstructor
@NoArgsConstructor
//增加链式编程
@Accessors(chain = true)
@TableName("teacher")
public class Teacher {//Id自增处理@TableId(type = IdType.AUTO)private Integer id;//指定数据库对应字段名@TableField("teacher_name")private String teacherName;@TableField("gender")private String sex;//这个字段在查询时,不返回值@TableField(select = false)private Integer age;//addr在数据库中字段不存在,防止报错,也无法插入@TableField(exist = false)private String addr;//3.3.0version后,不需要写,逻辑删除TableLogic注解@TableLogicprivate Integer deleted;
}
package com.yase.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yase.entity.Teacher;public interface TeacherMapper extends BaseMapper<Teacher> {
}
@Resourceprivate TeacherMapper mapper;//查询所有数据@Testvoid selectList() {List<Teacher> list = mapper.selectList(null);list.forEach(System.out::println);}