spring-boot2(使用mysql)

官方文档

目录

一.pom添加数据库依赖

二.添加数据库配置application.yaml

三.编写dao层repository

四.编写Entity实体

五.controller层应用

六.接口测试

一.pom添加数据库依赖

mysql的版本可以自己加定义限制,只需要添加<version>定义就行

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

二.添加数据库配置application.yaml

需要设置服务器时区

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

三.编写dao层repository

注意点:定义和repository底层一样的方法时会报错,需要找解决办法

package com.bllon.boot.dao;

import com.bllon.boot.domain.Posts;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostsRepository extends JpaRepository<Posts, Long> {

}

四.编写Entity实体

package com.bllon.boot.domain;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "wordpress_posts")
public class Posts implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @Column(name = "post_title")
    private String title;

    @Column(name = "post_content")
    private String content;

    public Posts() {
        super();
    }

    public Posts(String title, String content) {
        super();
        this.title = title;
        this.content = content;
    }

    public Long getId() {
        return id;
    }

    public String getTitle() {
        return this.title;
    }

    public String getContent() {
        return this.content;
    }
}

五.controller层应用

注意点:需要添加注解@Autowired,不然无法引入repository

package com.bllon.boot.controller;

import com.bllon.boot.dao.PostsRepository;
import com.bllon.boot.domain.Posts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
public class HelloController {

    @Autowired
    private PostsRepository postsRepository;

    @RequestMapping("/hello")
    public String Handle01() {
        return "Hello, spring boot 2!";
    }

    @RequestMapping("/allPost")
    public List<Posts> Posts() {
        List<Posts> list = new ArrayList<Posts>();
        list = postsRepository.findAll();
        return list;
    }

    @RequestMapping("/post")
    public Optional<Posts> Posts(Long id) {
        Optional<Posts> posts = postsRepository.findById(id);
        return posts;
    }
}

六.接口测试

 以上数据用的本地wordprss的数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

end for time

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值