spring boot 系列之四:spring boot 整合JPA

上一篇我们讲了spring boot 整合 JdbcTemplate来进行数据的持久化,

这篇我们来说下怎么通过 spring boot 整合 JPA 来实现数据的持久化。

一、代码实现 

  1. 修改 pom,引入依赖
      <!-- 引入 jpa 依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
  2. 修改 application.properties,配置相关信息
    #修改 tomcat 默认端口号
    server.port=8090
    #修改 context path
    server.context-path=/test
    

    #配置数据源信息
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/test
    spring.datasource.username=root
    spring.datasource.password=root
    #配置 jpa
    spring.jpa.hibernate.ddl-auto=update
    spring.jpa.show-sql=true
    spring.jackson.serialization.indent_output=true

  3. 创建实体类
    package com.study.entity;
    

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Entity
    @Table(name="t_user")
    public class User {

    @Id @GeneratedValue(strategy</span>=<span style="color: rgba(0, 0, 0, 1)">GenerationType.AUTO)
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Integer id;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String userName;
    </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String password;
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Integer getId() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> id;
    }
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setId(Integer id) {
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.id =<span style="color: rgba(0, 0, 0, 1)"> id;
    }
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getUserName() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> userName;
    }
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setUserName(String userName) {
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.userName =<span style="color: rgba(0, 0, 0, 1)"> userName;
    }
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getPassword() {
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> password;
    }
    
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setPassword(String password) {
        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.password =<span style="color: rgba(0, 0, 0, 1)"> password;
    }
    

    }

  4. 创建 repository 接口并继承 CrudRepository
    package com.study.repository;
    

    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.data.repository.query.Param;

    import com.study.entity.User;

    /**

    • 注意:

    • 1. 这里这里是 interface,不是 class

    • 2.CrudRepository 里面的泛型,第一个是实体类,第二个是主键的类型

    • 3. 由于 crudRepository 里面已经有一些接口了,如 deleteAll,findOne 等, 我们直接调用即可

    • 4. 当然,我们也可以根据自己的情况来实现自己的接口, 如下面的 getUser() 方法,jpql 语句和 hql 语句差不多

    • */
      public interface UserRepository extends CrudRepository<User, Integer> {

      /**

      • 我们这里只需要写接口,不需要写实现,spring boot 会帮忙自动实现
      • */

      @Query("from User where id =:id")
      public User getUser(@Param("id")Integer id);
      }

  5. 创建 service
    1. 接口
      package com.study.service;
      

      import com.study.entity.User;

      public interface UserService {
      public User getUser(Integer id);
      }

    2. 实现
      package com.study.service.impl;
      

      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;

      import com.study.entity.User;
      import com.study.repository.UserRepository;
      import com.study.service.UserService;

      @Service
      public class UserServiceImpl implements UserService {

      @Autowired
      UserRepository repository;
      
      @Override
      </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> User getUser(Integer id) {
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">有两种方式:
          </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">1.调用crudRepository的接口
      

      // return repository.findOne(id);
      //2. 调用我们自己写的接口
      return repository.getUser(id);
      }

      }

  6. 创建 controller
    package com.study.controller;
    

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import com.study.entity.User;
    import com.study.service.UserService;

    @RestController
    public class UserController {
    @Autowired
    UserService service;

    @RequestMapping(</span>"/getUser/{id}"<span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">public</span> User getUser(@PathVariable("id"<span style="color: rgba(0, 0, 0, 1)">) Integer id){
        
        </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> service.getUser(id);
    }
    

    }

  7. 测试,页面以 json 格式显示数据库值

    

二、知识点引申

关于 Repository 知识点,可以去看下下面这篇文章

https://segmentfault.com/a/1190000012346333