springboot简单demo实践(IDEA+springBo

2019-08-01  本文已影响0人  花椒胡椒小辣椒

参考链接
因为参考上面的博文发现有几个问题,解决问题之后把自己的完整步骤整理如下,在搭建项目时遇到的问题也整理出来。我用的springboot的版本是2.1,参考博文应该是1.5,版本不一样有些api就不一样了,遇到问题可以看看参考。

springboot项目搭建(更新中)

环境:mac系统
所需工具:IDEA(jdk1.8)+mysql+postman
下载地址:ideamysqlmysql workbenchpostman

  1. 创建项目
    在idea界面通过file—>new—>projects,选择Spring Initializr(是Spring 官方提供的一个用来初始化一个Spring boot 项目的工具),选择已有的jdk,next,输入Group & Artifact(Group和Artifact等的含义),dependencies选择所需的依赖包,基本的如图所示,注意图片最右侧一栏(如果建好项目了才发现少添加了包,在pom.xml配置文件中添加即可;这些包具体的作用请自行百度)。
    选择依赖包
    一路next直至finish,项目结构如图所示:
    项目结构
  1. 修改配置文件application.properties
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demoTest?characterEncoding=UTF-8&&useSSL=false&useUnicode=true
spring.datasource.username=root
spring.datasource.password=password

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
到mysql workbench中新建一个数据库,命名为demoTest,表不需要创建,会通过注解自动创建。此时启动项目,会在控制台中看到启动成功。 启动成功
  1. 新建User类
    在DemoApplication的父文件夹下分别新建entity、controller、dao包,在entity包下新建user类(注意这里DemoApplication和各个包的位置是有讲究的,具体原因可以自行百度,我也不是特别清楚原理,等我搞懂再来更新这个吧。还有为了代码规范,各个包具体用来放什么功能的代码,还是需要更进一步学习(涉及到mvc模式等等))
package com.fn.example.entity;

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

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

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;
    private Integer age;

    public User(){
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

各个注解的作用可百度springboot data jpa注解,详细学习(待更新)。
数据库的表的名称和代码中@Table注解name的值对应。
重新启动项目,可以看到在本地数据库中自动生成了数据库的表user


数据库user表
  1. 新建UserRepository接口
    在dao包下面新建UserRepository接口
    (idea新建interface:new-Java Class-在kind下拉框选择interface)
package com.jianshu.demo.dao;

import com.jianshu.demo.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User,Integer> {
}
  1. 新建UserController类
    在controller包下新建UserController类
package com.jianshu.demo.controller;

import com.jianshu.demo.dao.UserRepository;
import com.jianshu.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

     /*
     select all data
      */
    @GetMapping(value = "/all")
    public List<User> getUserList(){
        return userRepository.findAll();
    }

    /*
    add a user
     */
    @PostMapping(value = "/add")
    public User addUser(@RequestParam("name") String name,@RequestParam("age") Integer age){
        User user = new User();
        user.setName(name);
        user.setAge(age);

        return userRepository.save(user);
    }

    /*
    select a user based on id
     */
    @GetMapping(value = "all/{id}")
    public User getUser(@PathVariable("id") Integer id){
        return userRepository.findById(id).orElse(null);
    }

    /*
    delete a user based on id
     */
    @DeleteMapping(value = "del/{id}")
    public void deleteUser(@PathVariable("id") Integer id){
        userRepository.deleteById(id);
    }
}
  1. 测试
    get方法可以直接用浏览器输入,这里我们都用postman来模拟网页请求。
add a user
数据库更新
此时再查看http://localhost:8080/all,可以看到数据已返回。
all

return girlRepository.findOne(id);

要改成:

return userRepository.findById(id).orElse(null);

上一篇下一篇

猜你喜欢

热点阅读