h2database为我们提供了十分轻量,十分快捷方便的内嵌式数据库
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
server:
port: 8089
spring:
datasource:
url: jdbc:h2:~/test
driver-class-name: org.h2.Driver
username: sa
password: 123456
# schema: classpath:db/schema.sql
# data: classpath:db/data.sql
jpa:
database: h2
hibernate:
ddl-auto: update
show-sql: true
h2:
console:
path: /h2-console
enabled: true
package com.springboot.demo.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Location {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String type;
private double latitude;
private double longtitude;
}
package com.springboot.demo.repository;
import com.springboot.demo.entity.Location;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface LocationRepository extends JpaRepository<Location,Long> {
List<Location> getLocationsByType(String type);
}
package com.springboot.demo.controller;
import com.springboot.demo.entity.Location;
import com.springboot.demo.repository.LocationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class HelloContraller {
@Autowired
private LocationRepository locationRepository;
@ResponseBody
@RequestMapping("/hello")
public List<Location> hello(){
return locationRepository.findAll();
}
}
package com.springboot.demo;
import com.springboot.demo.entity.Location;
import com.springboot.demo.repository.LocationRepository;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication {
@Bean
InitializingBean saveData(LocationRepository repo){
return ()->{
repo.save(new Location((long) 1,"1",38.998064, 117.317267));
repo.save(new Location((long)2,"2",38.997793, 117.317069));
repo.save(new Location((long)3,"3",38.998006, 117.317101));
repo.save(new Location((long)4,"4",38.997814, 117.317332));
};
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
http://localhost:8089/h2-console
输入用户名sa,密码123456
SELECT * FROM USER
点击上面的Run执行。
原文:https://www.cnblogs.com/mizersy/p/10698409.html