您好,登錄后才能下訂單哦!
這篇文章主要介紹了Spring boot2.x中集成H2數據庫代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
在spring boot中集成
1.添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>com.h3database</groupId> <artifactId>h3</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
2.添加H2相關配置,修改application.properties文件
spring.jpa.database=h3 spring.jpa.show-sql=true #ddl執行方式,update create 等 spring.datasource.url=jdbc:h3:./data/test;AUTO_SERVER=TRUE spring.jpa.hibernate.ddl-auto=update spring.datasource.username=sa spring.datasource.password=123456 spring.datasource.driverClassName=org.h3.Driver spring.h3.console.path=/h3-console spring.h3.console.enabled=true
說明:
spring.datasource.url
數據庫文件
(1)內存數據庫
jdbc:h3:mem:DBName
內存數據庫的數據存在內存中,當程序停止時,不會被保存會丟失
eg:
spring.datasource.url=jdbc:h3:mem:test
(2)文件數據庫
jdbc:h3:file:{FilePath} 也可以簡化為 jdbc:h3:{FilePath}
FilePath的格式
(3)遠程數據庫
jdbc:h3:tcp://<{IP|hostname}>[:{Port}]/[]<{dbName}>
附加參數:
3.代碼
domain層,即User類(entity)
package com.example.demo.domain; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.*; @Entity @Table(name = "user") @Data public class User { @Id @GeneratedValue(strategy= GenerationType.AUTO) private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
dao層,即UserRepository 接口
package com.example.demo.dao; import com.example.demo.domain.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface UserRepository extends JpaRepository<User,Integer> { List<User> getUsersByName(String Name); }
controller層,即Demo
package com.example.demo.controller; import com.example.demo.dao.UserRepository; import com.example.demo.domain.User; 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 Demo { @Autowired private UserRepository repo; @RequestMapping("find") public List<User> find() { return (List<User>) repo.findAll(); } }
編寫DemoApplication
package com.example.demo; import com.example.demo.dao.UserRepository; import com.example.demo.domain.User; import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class DemoApplication { @Bean InitializingBean saveData(UserRepository repo){ return ()->{ User u = new User(); u.setName("abc"); repo.save(u); User u1 = new User(); u1.setName("zyx"); repo.save(u1); }; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
啟動項目,打開瀏覽器訪問http://localhost:8080/find
訪問http://localhost:8080/h3-console/
連接上后查詢數據
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。