當(dāng)前位置:
                    首頁 >
                            前端技术
>                            javascript
>内容正文                
                        
                    javascript
SpringBoot集成JPA
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                SpringBoot集成JPA
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                2019獨角獸企業(yè)重金招聘Python工程師標準>>>
1.創(chuàng)建新的maven項目
(本文由開源中國-千里明月-原創(chuàng) ,https://my.oschina.net/u/3490860/blog/write/1610121。如有雷同,純屬抄襲。)
2. 添加必須的依賴
<!--springboot的必須依賴--><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version></parent><dependencies><!--啟動springmvc的相關(guān)配置,springboot的自動配置--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--jpa--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!--mysql驅(qū)動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency></dependencies>3. 新建springboot啟動類
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class Application {public static void main(String[] args) {SpringApplication.run(Application.class,args);} }4. 在resources跟目錄下新建application.properties
#建立/更新數(shù)據(jù)表的配置 spring.jpa.hibernate.ddl-auto=update #數(shù)據(jù)庫地址 spring.datasource.url=jdbc:mysql://localhost:3306/qian?useUnicode=true&characterEncoding=utf-8 #數(shù)據(jù)庫用戶名 spring.datasource.username=root #數(shù)據(jù)庫密碼 spring.datasource.password=123- update:Hibernate根據(jù)給定的Entity結(jié)構(gòu)改變數(shù)據(jù)庫。
 - create: 每次都會創(chuàng)建數(shù)據(jù)庫,關(guān)閉時不會刪除
 - none: mysql的默認設(shè)置 , 不改變數(shù)據(jù)結(jié)構(gòu)
 - create-drop: 創(chuàng)建數(shù)據(jù)庫,但是每次sessionFactory關(guān)閉后都會刪除
 
5. 新建實體類User
這個時候其實已經(jīng)可以啟動springboot, 但是不會生成數(shù)據(jù)表,因為還沒有配置實體類的jpa
先新建user.java
import org.hibernate.annotations.GenericGenerator; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;/*** Created by Andy on 2018/1/20.*/ //表明這是個需要生成數(shù)據(jù)表的類 @Entity public class User { // 定義主鍵id@Id // 聲明一個策略通用生成器,name為”system-uuid”,策略strategy為”uuid”。@GenericGenerator(name = "system-uuid", strategy ="uuid") // 用generator屬性指定要使用的策略生成器。@GeneratedValue(generator = "system-uuid")private String id;private String name;private Integer age;private Boolean sex;public String getId() {return id;}public void setId(String 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;}public Boolean getSex() {return sex;}public void setSex(Boolean sex) {this.sex = sex;} }這時候啟動項目,就會在指定位置下生成一個user數(shù)據(jù)表
6. 實現(xiàn)CRUD
CrudRepository是一個提供了普通增刪改查方法的接口,由spring內(nèi)部提供,我們只需調(diào)用即可
@NoRepositoryBean public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> {<S extends T> S save(S var1);<S extends T> Iterable<S> save(Iterable<S> var1);T findOne(ID var1);boolean exists(ID var1);Iterable<T> findAll();Iterable<T> findAll(Iterable<ID> var1);long count();void delete(ID var1);void delete(T var1);void delete(Iterable<? extends T> var1);void deleteAll(); }新建UserRepository.java
@S public interface UserRepository extends CrudRepository<User, String> {}7. 實現(xiàn)controller控制
新建UserController.java
@RestController public class UserController {@Autowiredprivate UserRepository userRepository;@RequestMapping("/add")public User add(String name){User user = new User();user.setName(name);return userRepository.save(user);}@RequestMapping("/list")public Iterable<User> list(){Iterable<User> all = userRepository.findAll();return all;} }轉(zhuǎn)載于:https://my.oschina.net/u/3490860/blog/1610121
總結(jié)
以上是生活随笔為你收集整理的SpringBoot集成JPA的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: python的subprocess模块执
 - 下一篇: Ubuntu 开发环境搭建