美文网首页
spring mvc 的jpa JpaRepository数据层

spring mvc 的jpa JpaRepository数据层

作者: 前端混合开发 | 来源:发表于2019-01-29 15:09 被阅读0次

    1. Spring Data 应用场景

    Spring Data包含多个子项目:
    Spring Data JPA
    Spring Data Mongo DB
    Spring Data Redis
    Spring Data Solr

    2. JDBC和JPA的区别

    Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法

    JDBC: is a standard for Database Access (是Java的底层技术,包括也是很多database JPA的底层技术)
    JPA: is a standard for ORM. This can "hide" the SQL from the developer so that all they deal with are Java classes, and the provider allows you to save them and load them magically. The most famous JPA provider is Hibernate

    JDBC的一个例子:

        public String getAllEnglishWords() throws ClassNotFoundException, SQLException {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1434;databaseName=DailyEnglish", "username", "password");
             Statement stmt = con.createStatement(); 
             ResultSet rs = stmt.executeQuery("select * from Words"); 
             //while(rs.next()) {
                // System.out.println(rs.getString("english"));
                 //System.out.println(rs.getString("chinese"));
             //}
             String result = resultSetToJson(rs);
             System.out.println(result);
             rs.close();
             stmt.close();
             con.close();
            return result;
        }
    

    3. Spring Data JPA

    JPA (Java Persistence API)
    ORM:Object-Relational Mapping: 让你map entity class到sql table
    Class <-> Table
    当你用JAVA连接关系型数据库事,你需要JDBC,并且运行queries,然后得到结果后,把它转为对象;
    References:
    https://juejin.im/post/5aa9fb2a518825557e781bbf

    相关文章

      网友评论

          本文标题:spring mvc 的jpa JpaRepository数据层

          本文链接:https://www.haomeiwen.com/subject/kvocjqtx.html