最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解在Spring Boot中使用Mysql和JPA

 更新時間:2017年04月18日 11:36:54   作者:qiyadeng  
本文向你展示如何在Spring Boot的Web應用中使用Mysq數(shù)據(jù)庫,也充分展示Spring Boot的優(yōu)勢

本文向你展示如何在Spring Boot的Web應用中使用Mysq數(shù)據(jù)庫,也充分展示Spring Boot的優(yōu)勢(盡可能少的代碼和配置)。數(shù)據(jù)訪問層我們將使用Spring Data JPA和Hibernate(JPA的實現(xiàn)之一)。

1.Maven pom.xml文件

在你的項目中增加如下依賴文件

<dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
 </dependency>
 <dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
 </dependency>
</dependencies>

2.屬性配置文件application.properties

在src/main/resources/application.properties中設置數(shù)據(jù)源和jpa配置。

spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

全部的配置都在如上的文件中了,不需要另外的XML配置和Java配置。

上文中的數(shù)據(jù)庫配置,你需要換成你的數(shù)據(jù)庫的地址和用戶名密碼。

hibernate的ddl-auto=update配置表名,數(shù)據(jù)庫的表和列會自動創(chuàng)建(根據(jù)Java實體的熟悉), 這里 可以看到更多得hibernate配置。

3.User實體

創(chuàng)建一個User實體,User包含三個屬性id,email和name。User實體和Mysql數(shù)據(jù)庫的users表相對應。

@Entity
@Table(name = "users")
public class User {
 // ==============
 // PRIVATE FIELDS
 // ==============
 // An autogenerated id (unique for each user in the db)
 @Id
 @GeneratedValue(strategy = GenerationType.AUTO)
 private long id;
 // The user email
 @NotNull
 private String email;
 // The user name
 @NotNull
 private String name;
 // ==============
 // PUBLIC METHODS
 // ==============
 public User() { }
 public User(long id) {
  this.id = id;
 }
 // Getter and setter methods
 // ...
} // class User

4.User實體的數(shù)據(jù)訪問層UserDao

本例中UserDao非常簡單,只需要繼承CrudRespositroy即可,CrudRespositroy已經(jīng)實現(xiàn)了save,delete,deleteAll,findOne和findAll.(比較神奇的時這些方法其實CrudRespositroy中其實并沒有實現(xiàn),并且通過對dao方法的命名還可以實現(xiàn)新的方法)

@Transactional
public interface UserDao extends CrudRepository<User, Long> {
 public User findByEmail(String email);
} 

5.測試的控制器UserController

新建一個查詢控制器UserController

@Controller
public class UserController {
  @RequestMapping("/get-by-email")
  @ResponseBody
  public String getByEmail(String email) {
   String userId;
   User user = userDao.findByEmail(email);
   if (user != null) {
    userId = String.valueOf(user.getId());
    return "The user id is: " + userId;
   }
   return "user " + email + " is not exist.";
  }
 }

你可以使用瀏覽器訪問url http://127.0.0.1:8080/get-by-email?email=qiyadeng@gmail.com,可以獲得用戶的Id(你可以先在Mysql數(shù)據(jù)庫中新增一條記錄)。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • MyBatis-Plus代碼生成器的使用詳解

    MyBatis-Plus代碼生成器的使用詳解

    這篇文章主要介紹了MyBatis-Plus代碼生成器的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • Java設計模式之享元模式

    Java設計模式之享元模式

    這篇文章主要為大家詳細介紹了Java設計模式之享元模式的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 淺談java分頁三個類 PageBean ResponseUtil StringUtil

    淺談java分頁三個類 PageBean ResponseUtil StringUtil

    下面小編就為大家?guī)硪黄獪\談java分頁三個類 PageBean ResponseUtil StringUtil。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • 在Mybatis @Select注解中實現(xiàn)拼寫動態(tài)sql

    在Mybatis @Select注解中實現(xiàn)拼寫動態(tài)sql

    這篇文章主要介紹了在Mybatis @Select注解中實現(xiàn)拼寫動態(tài)sql,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 基于java中泛型的總結(jié)分析

    基于java中泛型的總結(jié)分析

    本篇文章介紹了,在java中泛型的總結(jié)分析。需要的朋友參考下
    2013-05-05
  • Spring Cloud Feign接口返回流的實現(xiàn)

    Spring Cloud Feign接口返回流的實現(xiàn)

    這篇文章主要介紹了Spring Cloud Feign接口返回流的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • java去除重復對象的簡單實例

    java去除重復對象的簡單實例

    下面小編就為大家?guī)硪黄猨ava去除重復對象的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Trie樹(字典樹)的介紹及Java實現(xiàn)

    Trie樹(字典樹)的介紹及Java實現(xiàn)

    Trie樹,又稱字典樹或前綴樹,關(guān)于它的結(jié)構(gòu)就不詳細介紹了。Trie樹在單詞統(tǒng)計、前綴匹配等很多方面有很大用處。下面這篇文章主要介紹了Trie樹,以及Java實現(xiàn)如何Trie樹,有需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報錯問題及解決方法(親測可用)

    IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報

    這篇文章主要介紹了IDEA下因Lombok插件產(chǎn)生的Library source does not match the bytecode報錯問題及解決方法,親測試過好用,需要的朋友可以參考下
    2020-04-04
  • springboot動態(tài)加載Echarts柱狀圖

    springboot動態(tài)加載Echarts柱狀圖

    這篇文章主要為大家詳細介紹了springboot動態(tài)加載Echarts柱狀圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12

最新評論

河津市| 青浦区| 繁峙县| 彰化县| 巴彦县| 郓城县| 汝阳县| 江西省| 信宜市| 虞城县| 东阳市| 曲阜市| 当阳市| 永丰县| 灌南县| 保定市| 留坝县| 大理市| 苍梧县| 金湖县| 聂荣县| 东安县| 怀柔区| 个旧市| 漳平市| 土默特右旗| 石棉县| 龙游县| 惠来县| 玛纳斯县| 安西县| 甘孜县| 灵川县| 宽甸| 渑池县| 且末县| 五河县| 福贡县| 邮箱| 修武县| 从江县|