spring boot 项目 ,spring data jpa 初使用

我使用的是 maven 构建工具,使用 spring data jpa 来实现简单的用户数据增删改查功能。
项目使用: spring boot、spring data jpa、mysql。
项目使用 postman 工具来测试功能是否实现。

代码

pom.xml 如下:



    4.0.0
    
    com.kaven
    spring-data-jpa
    0.0.1-SNAPSHOT
    jar

    spring-data-jpa
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.0.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            mysql
            mysql-connector-java
            5.1.31
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

项目结构如下图:
spring boot 项目 ,spring data jpa 初使用_第1张图片

user 表:

 create table user(
    id int auto_increment,
    age int not null,
    primary key(id)
 );

user 表结构如下图:
spring boot 项目 ,spring data jpa 初使用_第2张图片

application.yml 如下:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/kaven
    username: root
    password: 015036
  jpa:
    hibernate:
      ddl-auto: update #更新
    show-sql: true

show-sql: true
操作数据库中的数据时会生成相应的 sql 语句展示出来。
效果如下图:
在这里插入图片描述

User.java 如下:

package com.kaven.springdatajpa.entity;

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

/**
 * @program: spring-data-jpa
 * @description: 用户类
 * @author: Kaven
 * @create: 2018-11-16 12:50
 **/
@Entity
public class User {

    //用户唯一标识
    @Id
    @GeneratedValue
    private Integer id;

    //用户年龄
    private Integer age;

    public User() {}

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "{\n" +
                "      id: " + id +",\n"+
                "      age: " + age +
                "\n}";
    }
}

UserController.java 如下:

package com.kaven.springdatajpa.controller;

import com.kaven.springdatajpa.entity.User;
import com.kaven.springdatajpa.repository.UserRepository;
import com.kaven.springdatajpa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: spring-data-jpa
 * @description: 用户数据操作控制器
 * @author: Kaven
 * @create: 2018-11-16 12:53
 **/

@RestController
public class UserController {

    @Autowired
    private UserRepository UserRepository;

    @Autowired
    private UserService UserService;

    /**
    * 获取所有用户信息
    * */
    @GetMapping(value = "/users")
    public List<User> Users() {
        return UserRepository.findAll();
    }

    /**
     * 添加用户
    * */
    @PostMapping(value = "/user/add")
    public User addUser(User User) {
        return UserRepository.save(User);
    }

    /**
     * 根据用户 id 获取用户信息
     * */
    @PostMapping(value = "/user/get")
    public User getUser(@RequestParam("id") Integer id) {
        return UserRepository.findById(id).get();
    }

    /**
     * 更新用户信息
     * */
    @PutMapping(value = "/update")
    public void updateUser(@RequestParam("id") Integer id ,
                           @RequestParam("age") Integer age) {
        UserRepository.updateUser(id , age);
    }

    /**
     * 删除用户信息
     * */
    @DeleteMapping(value = "/user/delete")
    public void UserDelede(@RequestParam("id") Integer id) {
        UserRepository.deleteById(id);
    }

    /**
     * 通过用户年龄查询用户列表
     * */
    @GetMapping(value = "/users/findbyage")
    public List<User> UserListByAge(@RequestParam("age") Integer age) {
        return UserRepository.findByAge(age);
    }

    /**
     * 同时添加两个用户(只能同时添加成功或者同时添加失败)
     * */
    @PostMapping(value = "/users/inserttwo")
    public void insertTwoUsers() {
        UserService.insertTwo();
    }

    /**
     * 通过用户 id 获取用户的年龄
     */
    @GetMapping(value = "/user/id")
    public Integer getAge(@RequestParam("id") Integer id) throws Exception {
        return UserService.getAge(id);
    }
}

UserRepository.java 如下:

package com.kaven.springdatajpa.repository;

import com.kaven.springdatajpa.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: spring-data-jpa
 * @description: 用户数据仓库类
 * @author: Kaven
 * @create: 2018-11-16 13:04
 **/
@Component
public interface UserRepository extends JpaRepository<User, Integer> {

    /**
     * 通过年龄来查询用户列表,根据 spring data jpa 的命名规则来命名方法名
     */
    public List<User> findByAge(Integer age);

    /**
     * 也可以自己写 sql 语句实现
     * 更新用户
     * */
    @Transactional
    @Modifying
    @Query(value = "update User bean set bean.age=?2 where bean.id=?1")
    public void updateUser(Integer id , Integer age);
}

UserService.java 如下:

package com.kaven.springdatajpa.service;

import com.kaven.springdatajpa.entity.User;
import com.kaven.springdatajpa.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @program: spring-data-jpa
 * @description:  用户数据应用类
 * @author: Kaven
 * @create: 2018-11-16 12:57
 **/

@Service
public class UserService {

    @Autowired
    private UserRepository UserRepository;

    /**
     * 事务处理
     * 两个用户只能同时添加成功,或者同时添加失败
     * */
    @Transactional
    public void insertTwo() {
        User UserA = new User();
        UserA.setAge(19);
        UserRepository.save(UserA);

        User UserB = new User();
        UserB.setAge(22);
        UserRepository.save(UserB);
    }

    /**
     * 根据用户 id 来获取用户的年龄信息
     * */
    public Integer getAge(Integer id) throws Exception {
        User User = UserRepository.findById(id).get();
        Integer age = User.getAge();
        return age;
    }

    /**
     * 根据用户 id 来获取用户信息
     * */
    public User findOne(Integer id) {
        return UserRepository.findById(id).get();
    }
}

程序完成后,启动程序。
如下,说明启动成功:

F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=50884 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:F:\Install\CompilerInstall\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=50885:F:\Install\CompilerInstall\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\charsets.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\deploy.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\dnsns.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\jaccess.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\localedata.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\nashorn.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\sunec.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\ext\zipfs.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\javaws.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\jce.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\jfr.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\jfxswt.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\jsse.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\management-agent.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\plugin.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\resources.jar;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\lib\rt.jar;F:\Install\CompilerWorkspace\IDEA2\spring-data-jpa\target\classes;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-starter-data-jpa\2.1.0.RELEASE\spring-boot-starter-data-jpa-2.1.0.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-starter-aop\2.1.0.RELEASE\spring-boot-starter-aop-2.1.0.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-aop\5.1.2.RELEASE\spring-aop-5.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\aspectj\aspectjweaver\1.9.2\aspectjweaver-1.9.2.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-starter-jdbc\2.1.0.RELEASE\spring-boot-starter-jdbc-2.1.0.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\com\zaxxer\HikariCP\3.2.0\HikariCP-3.2.0.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-jdbc\5.1.2.RELEASE\spring-jdbc-5.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\javax\transaction\javax.transaction-api\1.3\javax.transaction-api-1.3.jar;F:\Install\Apache\apache-maven-3.5.4\repository\javax\xml\bind\jaxb-api\2.3.1\jaxb-api-2.3.1.jar;F:\Install\Apache\apache-maven-3.5.4\repository\javax\activation\javax.activation-api\1.2.0\javax.activation-api-1.2.0.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\hibernate\hibernate-core\5.3.7.Final\hibernate-core-5.3.7.Final.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;F:\Install\Apache\apache-maven-3.5.4\repository\javax\persistence\javax.persistence-api\2.2\javax.persistence-api-2.2.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\javassist\javassist\3.23.1-GA\javassist-3.23.1-GA.jar;F:\Install\Apache\apache-maven-3.5.4\repository\net\bytebuddy\byte-buddy\1.9.3\byte-buddy-1.9.3.jar;F:\Install\Apache\apache-maven-3.5.4\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\jboss\jandex\2.0.5.Final\jandex-2.0.5.Final.jar;F:\Install\Apache\apache-maven-3.5.4\repository\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\dom4j\dom4j\2.1.1\dom4j-2.1.1.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\hibernate\common\hibernate-commons-annotations\5.0.4.Final\hibernate-commons-annotations-5.0.4.Final.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\data\spring-data-jpa\2.1.2.RELEASE\spring-data-jpa-2.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\data\spring-data-commons\2.1.2.RELEASE\spring-data-commons-2.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-orm\5.1.2.RELEASE\spring-orm-5.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-context\5.1.2.RELEASE\spring-context-5.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-tx\5.1.2.RELEASE\spring-tx-5.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-beans\5.1.2.RELEASE\spring-beans-5.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-aspects\5.1.2.RELEASE\spring-aspects-5.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-starter-web\2.1.0.RELEASE\spring-boot-starter-web-2.1.0.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-starter\2.1.0.RELEASE\spring-boot-starter-2.1.0.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot\2.1.0.RELEASE\spring-boot-2.1.0.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-autoconfigure\2.1.0.RELEASE\spring-boot-autoconfigure-2.1.0.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-starter-logging\2.1.0.RELEASE\spring-boot-starter-logging-2.1.0.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;F:\Install\Apache\apache-maven-3.5.4\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\apache\logging\log4j\log4j-to-slf4j\2.11.1\log4j-to-slf4j-2.11.1.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;F:\Install\Apache\apache-maven-3.5.4\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-starter-json\2.1.0.RELEASE\spring-boot-starter-json-2.1.0.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\com\fasterxml\jackson\core\jackson-databind\2.9.7\jackson-databind-2.9.7.jar;F:\Install\Apache\apache-maven-3.5.4\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;F:\Install\Apache\apache-maven-3.5.4\repository\com\fasterxml\jackson\core\jackson-core\2.9.7\jackson-core-2.9.7.jar;F:\Install\Apache\apache-maven-3.5.4\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.7\jackson-datatype-jdk8-2.9.7.jar;F:\Install\Apache\apache-maven-3.5.4\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.7\jackson-datatype-jsr310-2.9.7.jar;F:\Install\Apache\apache-maven-3.5.4\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.7\jackson-module-parameter-names-2.9.7.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-starter-tomcat\2.1.0.RELEASE\spring-boot-starter-tomcat-2.1.0.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.12\tomcat-embed-core-9.0.12.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.12\tomcat-embed-el-9.0.12.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.12\tomcat-embed-websocket-9.0.12.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\hibernate\validator\hibernate-validator\6.0.13.Final\hibernate-validator-6.0.13.Final.jar;F:\Install\Apache\apache-maven-3.5.4\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-web\5.1.2.RELEASE\spring-web-5.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-webmvc\5.1.2.RELEASE\spring-webmvc-5.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-expression\5.1.2.RELEASE\spring-expression-5.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\mysql\mysql-connector-java\5.1.31\mysql-connector-java-5.1.31.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-core\5.1.2.RELEASE\spring-core-5.1.2.RELEASE.jar;F:\Install\Apache\apache-maven-3.5.4\repository\org\springframework\spring-jcl\5.1.2.RELEASE\spring-jcl-5.1.2.RELEASE.jar com.kaven.springdatajpa.SpringDataJpaApplication

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

2018-11-16 23:17:03.222  INFO 4504 --- [           main] c.k.s.SpringDataJpaApplication           : Starting SpringDataJpaApplication on DESKTOP-1EB3JGB with PID 4504 (F:\Install\CompilerWorkspace\IDEA2\spring-data-jpa\target\classes started by Kaven in F:\Install\CompilerWorkspace\IDEA2\spring-data-jpa)
2018-11-16 23:17:03.232  INFO 4504 --- [           main] c.k.s.SpringDataJpaApplication           : No active profile set, falling back to default profiles: default
2018-11-16 23:17:05.186  INFO 4504 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2018-11-16 23:17:05.306  INFO 4504 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 100ms. Found 1 repository interfaces.
2018-11-16 23:17:06.038  INFO 4504 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$d8d98785] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-16 23:17:07.452  INFO 4504 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-11-16 23:17:07.502  INFO 4504 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-11-16 23:17:07.502  INFO 4504 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.12
2018-11-16 23:17:07.512  INFO 4504 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\bin;F:\Install\JavaJDK\JDKInstall\jdk1.8.0_181\jre\bin;F:\Install\Apache\apache-tomcat-9.0.12\lib;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\MySQL\MySQL Utilities 1.6\;F:\Install\Apache\apache-maven-3.5.4\bin;C:\Program Files\MySQL\MySQL Server 5.7\bin;F:\Install\web\nodejsInstall\;F:\Install\VersionControlSystem\GitInstall\Git\cmd;F:\Install\DataBase\RedisInstall;C:\Users\Kaven\AppData\Local\Microsoft\WindowsApps;C:\Users\Kaven\AppData\Roaming\npm;.]
2018-11-16 23:17:08.082  INFO 4504 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-11-16 23:17:08.082  INFO 4504 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 4750 ms
2018-11-16 23:17:08.212  INFO 4504 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-11-16 23:17:08.232  INFO 4504 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-11-16 23:17:08.232  INFO 4504 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-11-16 23:17:08.232  INFO 4504 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'formContentFilter' to: [/*]
2018-11-16 23:17:08.232  INFO 4504 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-11-16 23:17:08.772  INFO 4504 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2018-11-16 23:17:09.111  INFO 4504 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2018-11-16 23:17:09.790  INFO 4504 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
	name: default
	...]
2018-11-16 23:17:10.075  INFO 4504 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.7.Final}
2018-11-16 23:17:10.085  INFO 4504 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2018-11-16 23:17:10.790  INFO 4504 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2018-11-16 23:17:11.182  INFO 4504 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-11-16 23:17:12.971  INFO 4504 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2018-11-16 23:17:14.665  INFO 4504 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2018-11-16 23:17:14.785  WARN 4504 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2018-11-16 23:17:15.184  INFO 4504 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-11-16 23:17:15.194  INFO 4504 --- [           main] c.k.s.SpringDataJpaApplication           : Started SpringDataJpaApplication in 12.94 seconds (JVM running for 15.21)

测试

添加用户
spring boot 项目 ,spring data jpa 初使用_第3张图片
spring boot 项目 ,spring data jpa 初使用_第4张图片
添加了两个用户,下面验证添加用户是否成功。

获取所有的用户信息
spring boot 项目 ,spring data jpa 初使用_第5张图片
成功添加了两个用户。

删除用户
spring boot 项目 ,spring data jpa 初使用_第6张图片

验证删除用户成功,如下图:
spring boot 项目 ,spring data jpa 初使用_第7张图片

根据用户 id 获取用户的年龄信息
spring boot 项目 ,spring data jpa 初使用_第8张图片

同时增加两个用户(事务处理)
spring boot 项目 ,spring data jpa 初使用_第9张图片
验证同时添加两个用户成功,如下图,失败的情况可以自己去测一测,我这里是可以的。
spring boot 项目 ,spring data jpa 初使用_第10张图片

根据用户年龄获取用户信息
spring boot 项目 ,spring data jpa 初使用_第11张图片

根据用户 id 获取用户信息
spring boot 项目 ,spring data jpa 初使用_第12张图片

更新用户信息
spring boot 项目 ,spring data jpa 初使用_第13张图片

验证用户数据是否更新,如下图:
spring boot 项目 ,spring data jpa 初使用_第14张图片

测试部分完成了,功能都能实现。
一个 spring data jpa 的简单应用。

你可能感兴趣的:(后端框架)