美文网首页
SpringBoot学习笔记三 | 整合JPA

SpringBoot学习笔记三 | 整合JPA

作者: 殷俊杰 | 来源:发表于2018-03-19 11:47 被阅读0次

    JPA全称(Java Persistence API),JPA 的目标之一是制定一个可以由很多供应商实现的API,不了解的小伙伴...emm虽然没人看,,可以看一下我的JPA系列学习笔记,JPA学习笔记emm..日后再改。
    整个pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.yjj</groupId>
        <artifactId>sb-first</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>sb-first</name>
        <description>Demo project for Spring Boot</description>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- You can easily generate your own configuration metadata file
             from items annotated with @ConfigurationProperties by using the
             spring-boot-configuration-processor jar.
            -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-configuration-processor</artifactId>
                <optional>true</optional>
            </dependency>
    
            <!-- mysql连接-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <!-- jpa -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa
                </artifactId>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    
    </project>
    

    配置文件application.yml加入

    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
        username: root
        password: 123456
    
      jpa:
        hibernate:
          ddl-auto: update  # 第一次简表create  后面用update
        show-sql: true
    

    注意,如果通过jpa在数据库中建表,将jpa.hibernate,ddl-auto改为create,建完表之后,要改为update,要不然每次重启工程会删除表并新建。

    创建实体类

    通过@Entity 表明是一个映射的实体类, @Id表明id, @GeneratedValue 字段自动生成

    package com.yjj.sbfirst.entity;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Table(name="account")
    @Entity
    public class Account {
        @Id
        @GeneratedValue
        private int id;
        private String name;
        private double money;
    
        //省略getter setter
    }
    

    service层

    package com.yjj.sbfirst.repository;
    import com.yjj.sbfirst.entity.Account;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    public interface AccountRepository extends JpaRepository<Account,Integer>{
    
    }
    
    

    Controller层

    package com.yjj.sbfirst.controller;
    
    import com.yjj.sbfirst.entity.Account;
    import com.yjj.sbfirst.repository.AccountRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    @RestController
    @RequestMapping(value = "/account")
    public class AccountController {
        @Autowired
        AccountRepository accountRepository;
    
        @RequestMapping(value="/list")
        public List<Account> getAccounts(){
            return accountRepository.findAll();
        }
    
        @RequestMapping(value = "/{id}",method = RequestMethod.GET)
        public String getAccountById(@PathVariable("id") int id){
            return accountRepository.findById(0).get().toString();
        }
    
        @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
        public String updateAccount( @PathVariable("id") int id,@RequestParam(value="name",required = true) String name,
                                    @RequestParam(value="money",required = true) double money){
            Account account=new Account();
            account.setMoney(money);
            account.setName(name);
            account.setId(id);
            Account account1=accountRepository.saveAndFlush(account);
            return account1.toString();
        }
    
    
    }
    
    

    以上代码均测试过,就是有的没通过。。emm。。findOne和findByID返回Optional是怎么搞啊。。
    查询全部和更新是可以的。嗯,暂时到这里

    相关文章

      网友评论

          本文标题:SpringBoot学习笔记三 | 整合JPA

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