• 一个简单jpa例子


    package com.example.demo.entity;
    
    import javax.persistence.*;
    
    /*使用jpa注解配置映射关系*/
    /*告诉jpa这是一个实体类和(数据表映射的类)*/
    @Entity
    @Table(name = "tal_user")
    public class User {
        @Id /*这是主键*/
        @GeneratedValue(strategy = GenerationType.IDENTITY) /*主键自增*/
        private Integer id;
        @Column(name = "lastName",length = 50)/*这是和数据表对应的一个列*/
        private String lastName;
        @Column  /*省略就代表默认属性名就是列名*/
        private String email;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getLastName() {
            return lastName;
        }
    
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    }
    package com.example.demo.repository;
    
    import com.example.demo.entity.User;
    import org.springframework.data.jpa.repository.JpaRepository;
    /*继承jpaRepository来完成对数据库的操作 参数是实体类型和实体类中的Id类型*/
    public interface UserRepository extends JpaRepository<User, Integer> {
    
    }
    package com.example.demo.Controller;
    
    import com.example.demo.entity.User;
    import com.example.demo.repository.UserRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    
    @RestController
    public class UserController {
        @Autowired
        UserRepository userRepository;
        @GetMapping("/user/{id}")
        public List<User> getUser(@PathVariable("id") Integer id){
            User user = userRepository.getOne(id);
    
            final List<User> users = userRepository.findAll();
            return users;
        }
        @GetMapping("/user")
        public User insertUser(User user){
            User save = userRepository.save(user);
            return save;
        }
    
    }
    spring:
      datasource:
        username: root
        password: root
        url: jdbc:mysql://localhost:3306/jpa?serverTimezone=UTC
        driver-class-name: com.mysql.cj.jdbc.Driver
      jpa:
        hibernate:
    #      更新或者创建表结构
          ddl-auto: update
    #      控制台显示sql
        show-sql: true
      <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>8.0.12</scope>
            </dependency>
  • 相关阅读:
    uoj207 共价大爷游长沙 子树信息 LCT + 随机化 + 路径覆盖
    bzoj4542 [Hnoi2016]大数 莫队+同余
    bzoj2959: 长跑 LCT+并查集+边双联通
    bzoj4819 [Sdoi2017]新生舞会 分数规划+最大费用最大流
    bzoj5020 & loj2289 [THUWC 2017]在美妙的数学王国中畅游 LCT + 泰勒展开
    bzoj1195 [HNOI2006]最短母串 AC 自动机+状压+bfs
    hdu 多校 2019 选做
    bzoj3510 首都 LCT 维护子树信息+树的重心
    bzoj4011 [HNOI2015]落忆枫音 拓扑排序+DP
    bzoj3091 城市旅行 LCT + 区间合并
  • 原文地址:https://www.cnblogs.com/zhulina-917/p/10222290.html
Copyright © 2020-2023  润新知