Springbot整合JPA----03---66/67

社么是Spring Data

JPA是SpringData的模块。

是对Hibernate。。。。。。的再封装。

-------------------------------------------------------------------------------------------------------------------------------------------------------

参照文档

整合JPA

代码的github地址:https://github.com/FandyWw/spring-boot-06-data-jpa

扫描二维码关注公众号,回复: 5817659 查看本文章

选择模块。

配置yml文件:

spring:
  datasource:
    url: jdbc:mysql://192.168.244.130/jpa
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

JPA:ORM(Object Relational Mapping)

1)编写一个实体类(bean)和数据表进行映射,配置映射关系。

package com.atguigu.springboot.entity;


import javax.persistence.*;

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

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

    @Column(name = "last_name",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;
    }
}

注意这个实体类,先配置映射关系:注意上面的注解的使用。

2)编写一个Dao接口操作实体类对应的数据表,在springboot中名字为(repository)。

package com.atguigu.springboot.repository;

import com.atguigu.springboot.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

//继承JpaRepository来完成对数据库的操作
public interface UserRepository extends JpaRepository<User,Integer> {
}

   

注意上面类的继承关系,既有crud功能也有排序分页功能。

public interface UserRepository extends JpaRepository<User,Integer>

    注意这个类的泛型:前者传的是实体类,后者传的是实体类的id的类型,表示其是可以序列化的。

@NoRepositoryBean
public interface JpaRepository<T, ID extends Serializable>
		extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {

	/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.CrudRepository#findAll()
	 */
	List<T> findAll();

	/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)

在yml上做下基本配置:

spring:
  datasource:
    url: jdbc:mysql://192.168.244.130/jpa
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  jpa:
    hibernate:
#     更新或者创建数据表结构
      ddl-auto: update
#    控制台显示SQL
    show-sql: true

启动应用:表已经创建出来了。

简单的增删改查。

package com.atguigu.springboot.controller;

import com.atguigu.springboot.entity.User;
import com.atguigu.springboot.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;

@RestController
public class UserController {

    @Autowired
    UserRepository userRepository;

    @GetMapping("/user/{id}")
    public User getUser(@PathVariable("id") Integer id){
        User user = userRepository.findOne(id);
        return user;
    }

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

}

访问:

http://localhost:8080/user?lastName=zhangsan&email=aa

http://localhost:8080/user/1

猜你喜欢

转载自blog.csdn.net/qq_28764557/article/details/88932318
今日推荐