31 SpringBoot与JPA整合

版权声明:看什么?6,你和我,走一波! https://blog.csdn.net/qq_31323797/article/details/84954908

jpa维护困难,不建议使用(个人拙见)

1 依赖导入

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-data-jpa</artifactId>
	</dependency>

	<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>

	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<scope>runtime</scope>
	</dependency>
</dependencies>

2 实体类

package com.gp6.springboot26.bean;

import javax.persistence.*;

/*
    使用JPA注解配置映射关系
        @Entity : 告诉JPA这是一个实体类(和数据表映射的类)
        @Table  : 来指定和哪个数据表对应;如果省略默认表名就是user
        @Id     : 标明主键
 */
@Entity
@Table(name = "m_user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)//自增主键
    private Integer id;

    // 和数据表对应的一个列
    @Column(name = "user_name",length = 50)
    private String userName;

    // 省略,默认列名就是属性名
    @Column
    private String email;

    public Integer getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

3 application.properties

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# update: 更新或创建  数据表结构
# 在项目启动时,会在数据库中根据实体类自动生成或更新表
spring.jpa.hibernate.ddl-auto=update

# 控制台显示SQL
spring.jpa.show-sql=true

4 Controller

package com.gp6.springboot26.controller;

import com.gp6.springboot26.bean.User;
import com.gp6.springboot26.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.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
public class UserController {

    @Autowired
    UserRepository userRepository;

    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Integer id){
		// getOne查询结果为null,原因未知
        User user1 = userRepository.getOne(id);
        Optional<User> user = userRepository.findById(id);	
        return user.get();
    }

    @PostMapping("/user")
    public User insertUser(User user){
        User save = userRepository.save(user);
        return save;
    }

}

猜你喜欢

转载自blog.csdn.net/qq_31323797/article/details/84954908
31
31)
今日推荐