IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

SpringBoot教程(二):Spring Boot连接MySQL数据库

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

上篇Spring Boot实战(一):只需两步!Eclipse+Maven快速构建第一个Spring Boot项目已经构建了一个Spring Boot项目,本文在此基础上使用Hibernate进行连接MySQL数据库的操作。

1. pom.xml添加依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>   

2. application.properties添加数据库配置

spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true

如果数据库连接写成spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot ,由于MySQL版本的问题,可能会有以下的错误,在后面加上“?serverTimezone=GMT%2B8”,设置下时区,解决。

123_1.png

设置驱动,spring.datasource.driver-class-name=com.mysql.jdbc.Driver会有下面红色的警告信息。说的是com.mysql.jdbc.Driver被弃用了,要使用新的驱动com.mysql.cj.jdbc.Driver1,改成com.mysql.cj.jdbc.Driver以后一切正常。

Loading class com.mysql.jdbc.Driver’. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

123_2.png

3. 添加实体类

@Entity代表这是一个实体类,@Table(name=”user”)用来对应数据库中的use表,@Id用来表达主键,@Column(name=”id”)表明一个id属性。

@GeneratedValue使主键自增,如果还有疑问,可参考@GeneratedValue源码解析

package com.example.demo.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long id;
    @Column(name = "username")
    private String userName;
    @Column(name = "password")
    private String passWord;

    public User() {
        super();
    }

    public User(String userName, String passWord) {
        super();
        this.userName = userName;
        this.passWord = passWord;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWord() {
        return passWord;
    }

    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

}

4. 添加Dao

Dao层主要用来实现对数据库的增、删、查、改。 dao只要继承JpaRepository类就可以,几乎可以不用写方法,可以根据方法名来自动的生产SQL,比如findByUserName 会自动生产一个以 userName 为参数的查询方法。

package com.example.demo.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.domain.User;

public interface UserRepository extends JpaRepository<User, Long> {
    User findByUserName(String userName);

}

5. 添加Controller

package com.example.demo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dao.UserRepository;
import com.example.demo.domain.User;

@RestController
@RequestMapping("user")
public class UserController {

   @Autowired
   private UserRepository userRepository;

   @RequestMapping("/getAllUser")
   @ResponseBody
   public List<User> findAll() {
       List<User> list = new ArrayList<User>();
       list = userRepository.findAll();
       return list;
   }

   @RequestMapping("/getByUserName")
   @ResponseBody
   public User getByUserName(String userName) {
       User user = userRepository.findByUserName(userName);
       return user;
   }

}

工程添加文件后工程结构图:

123_3.png

6. 新建数据库

新建数据库mysql://localhost:3306/spring_boot ,必须的一个步骤。hibernate虽然会自动新建表,但是数据库还是要手动建好的。

使用Navicat新建本地数据库,连接名上面右键- >新建数据库 ->填写数据库信息 – > 确定。

123_4.png

在user表中,插入两条测试数据:

123_5.png

7. 测试

启动项目。用Postman发送请求进行测试:

http://localhost:8080//user/getAllUser :

123_6.png

http://localhost:8080//user/getByUserName?userName=Turing :

123_7.png

文章永久链接:https://tech.souyunku.com/?p=35985


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(69) 打赏



未经允许不得转载:搜云库技术团队 » SpringBoot教程(二):Spring Boot连接MySQL数据库

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367