【java_web】idea中spring boot 配置 mysql5数据库

  • 操作系统: win7
  • 数据库:MySQL8 + MySQL5.7.25
  • 架构:idea 下配置 spring boot(web+jpa+mysql)
  • MySQL5.7.25官方下载地址

https://dev.mysql.com/downloads/mysql/
在这里插入图片描述
在这里插入图片描述

  • 解压到文件夹

E:\mysql_5.7.25
复制目录E:\mysql_5.7.25\bin加入系统环境变量path内

  • E:\mysql_5.7.25下新建my.ini文件
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8 
[mysqld]
#设置3306端口
port = 3306 
# 设置mysql的安装目录
basedir=E:\\mysql_5.7.25
# 设置mysql数据库的数据的存放目录
datadir=E:\\mysql_5.7.25\\data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
[client]
port=3306
default-character-set=utf8
  • cmd进入bin文件夹下执行

mysqld --initialize --console

输出信息中有:A temporary password is generated for root@localhost: XXXX   记录下XXXX,这个是数据库用户的初始密码
  • 安装服务

mysqld --install [服务名]

如果系统已经存在其他数据库服务,则服务名需要设置, 这里设置为mysql5
  • 启动服务

net start mysql5

  • 以root身份登录,并修改密码

mysql -u root -p
密码为初始密码,再修改
ALTER USER ‘root’@‘localhost’ IDENTIFIED WITH mysql_native_password BY ‘新密码’;

  • 创建用户:

CREATE USER ‘mysql5’ IDENTIFIED WITH mysql_native_password BY ‘密码’;

  • 为用户设置远程连接的权限(便于运行mysql workbench进行可视化操作)

GRANT ALL PRIVILEGES ON *.* TO 'mysql5';

  • 使用mysql workbench创建数据库(schems)和表
    在这里插入图片描述在这里插入图片描述
  • 进入idea配置application.properties
// spring boot 的端口号为8888
server.port=8888
// mysql5 的端口号为3306  sign是上面创建的schems的命名
spring.datasource.url = jdbc:mysql://localhost:3306/sign?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
spring.datasource.username = mysql5
spring.datasource.password = 123456
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  • 搭建MVC架构
    在这里插入图片描述
  • 智能生成Entity
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  • get操作,相关的函数式编程、JPA的使用省略
package com.example.demo.controller;

import com.example.demo.model.UserInfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {

    // 控制器
    @Autowired
    private UserService userService;

    @RequestMapping("/user")
    private UserInfo getUser(@RequestParam int id) {
           return userService.getById(id);
        }
}

  • 运行项目
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/chenghan_yang/article/details/88378528