SpringBoot-配置文件与数据库操作

一.springboot配置文件

1.推荐使用yml配置文件

server:

  #配置端口
  port: 8080
  servlet:
    #配置项目访问名称
    context-path: /springdemo

user:
  name: zhansan
  age: 18

1.配置文件中属性映射成对象

  • 定义对应bean
package com.gdy.springboot1.domain;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "user")
public class User {
    private String name;
    private Integer age;

    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;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  • controller
package com.gdy.springboot1;

import com.gdy.springboot1.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @Autowired
    private User user;

    @RequestMapping("find")
    public String getUser(){
        return user.toString();
    }
}
  • 启动访问结果
    这里写图片描述

2. jpa 操作数据库

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

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.4</version>
</dependency>
  • 配置数据库信息 与 jpa信息
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/lianxi
    username: root
    password: 123
  jpa:
    hibernate:
      #create 程序运行时 重新创建一张表
      #update 创建表 但不会删除表中的数据
      #create-drop 应用停止时, 删除表
      #none
      #validate 验证类中的属性是否与表结构一致,不一致 会报错
      ddl-auto: create
    show-sql: true
  • 与数据库结构一致的类
package com.gdy.springboot1.domain;

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

@Entity
public class UserInfo {

    @Id  //id
    @GeneratedValue //自增
    private Integer id;
    private String name;
    private Integer age;

    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;
    }
}
  • 运行程序自动生成表结构

3.restful操作数据库

  • dao层
package com.gdy.springboot1;

import com.gdy.springboot1.domain.UserInfo;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface UserDao extends JpaRepository<UserInfo , Integer> {
    public List<UserInfo> findByAge(Integer age);
}
  • controller

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

    @Autowired
    private UserDao userDao;



    /**
     * 查找所有用户信息
     * @return
     */
    @RequestMapping("findAlls")
    public List<UserInfo> findAlls(){
        return userDao.findAll();
    }

    /**
     * 根据id查找用户信息
     * @return
     */
//    @GetMapping("find/{id}")
//    public UserInfo findAlls(@PathVariable("id") Integer id){
//        return userDao.findOne(id);
//    }



    /**
     * 现在用户信息
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("addUser")
    public UserInfo addUser(@RequestParam("name") String name , @RequestParam("age")Integer age){
        UserInfo userInfo = new UserInfo();
        userInfo.setAge(age);
        userInfo.setName(name);

        return userDao.save(userInfo);
    }


}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014136910/article/details/79955007
个人分类: 框架-SpringBoot
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭