Spring boot + JPA H2 数据库

本文介绍:spring boot 使用数据库范例

H2 内存数据库

1 pom.xml 添加依赖


        org.springframework.boot
        spring-boot-starter-data-jpa
 

            com.h2database
            h2
            runtime

2 配置属性:

  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
  datasource:
    data-username: sa
    data-password: 123456
    driver-class-name: org.h2.Driver
    url: jdbc:h2:./test;;AUTO_SERVER=TRUE
  h2:
    console:
      path: /h2-console
      enabled: true

3 添加对象模型

package com.demo.reid.entity;

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

@Entity
@Table(name="deviceinfo")
public class DeviceInfo {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String name;

    public DeviceInfo() {
    }

    private String ipaddress;

    public DeviceInfo(String name, String ipaddress) {
        this.name = name;
        this.ipaddress = ipaddress;
    }

    public long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getIpaddress() {
        return ipaddress;
    }

    public void setIpaddress(String ipaddress) {
        this.ipaddress = ipaddress;
    }


}

4 添加对象数据库持久化对象

package com.demo.reid.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.demo.reid.entity.DeviceInfo;
@Repository
public interface DeviceInfoRespository extends  JpaRepository{

}

5 添加服务对象

package com.demo.reid.Service;

import com.demo.reid.entity.DeviceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo.reid.dao.DeviceInfoRespository;
import java.util.List;
@Service
public class DeviceInfoService {
    @Autowired
    private DeviceInfoRespository deviceRepository;

    public void save(DeviceInfo device){
        deviceRepository.save(device);
    }
    public List list(){
        return deviceRepository.findAll();
    }
    public void deleteall(){
         deviceRepository.deleteAll();
    }
}

6 添加controller类

package com.demo.reid.Controller;

import com.demo.reid.Service.DeviceInfoService;
import com.demo.reid.entity.DeviceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class DeviceInfoController {
    @Autowired
    private DeviceInfoService deviceInfoService;

    @RequestMapping("/add")
    public String add(){
        DeviceInfo device = new DeviceInfo("Router","10.10.10.1");
        deviceInfoService.save(device);
        return "success";
    }

    @RequestMapping("/findall")
    public List findall(){
        return deviceInfoService.list();
    }

    @RequestMapping("/deleteall")
    public String deleteall(){
        deviceInfoService.deleteall();
        return "Delete all data";
    }
}

7 运行:

 

你可能感兴趣的:(Spring,Boot)