IDEA 中創(chuàng)建Spring Data Jpa 項目的示例代碼
一、IDEA 創(chuàng)建工程
使用IDEA 創(chuàng)建工程的過程,使用文字做簡單描述。
選擇工程類別【Spring Initializr】。 設(shè)置工程的元數(shù)據(jù)【Metadata】,根據(jù)自己的情況填寫即可。 設(shè)置工程的依賴;在【W(wǎng)eb】中選擇“Spring Web”;在【SQL】中選中“Spring Data JPA”、“Spring Data JDBC”、“MySQL Driver”、“JDBC API”。選中的可能有些多,如果多了自己刪除掉。做完最后一步,工程就創(chuàng)建完成了。
PS:在配置 IEAD 中的 Maven 功能時,將“Always update snapshots” 選中,這樣就會及時更新測試包,并且不緩存Maven 的錯誤信息。
1.1、Pom 文件中的依賴信息
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency></dependencies>
二、編輯配置文件
在 resources 目錄中創(chuàng)建配置文件:application.yml;添加數(shù)據(jù)源等信息。
spring: datasource: url: jdbc:mysql://dbserver.com:3306/user?useUnicode=true&characterEncoding=utf-8 username: username password: password driver-class-name: com.mysql.cj.jdbc.Driverserver: port: 8080 compression: enabled: true servlet: context-path: /
三、編寫實體類
創(chuàng)建User的實體類 Entity/User.java(省略了getter&setter方法;如果不寫的話,會對數(shù)據(jù)操作失敗)。
/** * 使用JPA注解配置映射關(guān)系 */@Entity //告訴JPA這是一個實體類(和數(shù)據(jù)表映射的類)@Table(name = 'tbl_user') //@Table來指定和哪個數(shù)據(jù)表對應;如果省略,默認表名就是該類名的小寫:userpublic class User { @Id //表示這個屬性是數(shù)據(jù)表中的主鍵 @GeneratedValue(strategy = GenerationType.IDENTITY) //還是一個自增的主鍵 private Integer id; @Column(name = 'last_name',length = 50) //這是和數(shù)據(jù)表對應的一個列,可以定義其在數(shù)據(jù)表中的名字及長度 private String lastName; @Column //省略的情況,默認列名就是屬性名 private String email;}
使用JPA的一個好處就是,它能夠根據(jù)實體類自動創(chuàng)建數(shù)據(jù)庫表,只需簡單配置幾步即可:
在application.yml文件中增加(jpa 也是spring下的一個屬性,所以它也是在spring這個層級下的):
jpa: hibernate: ddl-auto: update #定義數(shù)據(jù)表的生成策略。update:更新或者創(chuàng)建數(shù)據(jù)表結(jié)構(gòu) show-sql: true #控制臺顯示sql語句
運行主程序,控制臺會報錯找不到對應的表,但是這實際上是正常的,去數(shù)據(jù)庫看就可以發(fā)現(xiàn)名為tbl_user的表已經(jīng)創(chuàng)建成功了,而且是根據(jù)實體類中的設(shè)置創(chuàng)建的。
四、持久層接口
JPA中有許多封裝好的對數(shù)據(jù)庫進行操作的方法,不需要我們再寫sql語句,而是直接調(diào)用其中的方法,就可以完成對數(shù)據(jù)的操作了。而這時,持久層只需繼承JpaRepository類就可以了。
/** * Dao/UserRepository.java * 繼承JpaRepository來完成對數(shù)據(jù)庫的操作 * JpaRepository<T,id>,T表示要進行操作的實體類,id表示類的主鍵的類型 */public interface UserRepository extends JpaRepository<User,Integer> {}
五、控制層
編寫控制層代碼來測試一下是否成功。因為沒有界面,所以這邊直接用@RestController注解來返回json串就好了,把持久層接口自動注入進來,直接調(diào)用其中對應的方法。
@RestController@EnableAutoConfigurationpublic class UserController { @Autowired UserRepository userRepository; @GetMapping('/user/{id}') public User getUser(@PathVariable('id') Integer id) { Optional<User> optional = userRepository.findById(id); if (optional.isPresent()) { User user = optional.get(); return user; } else { return new User(); } } @GetMapping('/user') public User insertUser(User user) { User save = userRepository.save(user); return save; }}
六、測試
1.首先插入一條數(shù)據(jù):
地址欄輸入 :http://localhost:8080/user?lastName=zhangsan&email=aa
2、查詢:
地址欄輸入 :http://localhost:8080/user/1
到此這篇關(guān)于IDEA 中創(chuàng)建Spring Data Jpa 項目的示例代碼的文章就介紹到這了,更多相關(guān)IDEA 創(chuàng)建Spring Data Jpa 項目內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. html中的form不提交(排除)某些input 原創(chuàng)3. bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼4. 網(wǎng)頁中img圖片使用css實現(xiàn)等比例自動縮放不變形(代碼已測試)5. CSS3中Transition屬性詳解以及示例分享6. python 如何在 Matplotlib 中繪制垂直線7. vue使用moment如何將時間戳轉(zhuǎn)為標準日期時間格式8. js select支持手動輸入功能實現(xiàn)代碼9. jsp文件下載功能實現(xiàn)代碼10. 開發(fā)效率翻倍的Web API使用技巧
