SpringData JPA 搭建 xml的 配置方式

SpringData JPA 搭建 xml的 配置方式_第1张图片 

1.导入版本管理依赖 到父项目里


  
    
      org.springframework.data
      spring-data-bom
      2021.1.10
      import
      pom
    
  

2.导入spring-data-jpa 依赖 在子模块

  
             
        
            junit
            junit
            4.12
            test
        

        
        
            org.hibernate
            hibernate-entitymanager
            5.4.32.Final
        
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
        
            org.springframework.data
            spring-data-jpa
        
        
        
            com.alibaba
            druid
            1.2.8
        

        
        
            org.springframework
            spring-test
            5.3.10
            test
        
    

3.创建实体类

package com.kuang.pojo;

import javax.persistence.*;

@Entity//作为 hibernate实体类
@Table(name = "tb_customer")//映射的表名
public class Customer {

    /**
     * @Id: 声明主键的配置
     * @GeneratedValue: 配置主键的生成策略
     *        strategy :
     *            1. GenerationType.IDENTITY :自增 mysql
     *               底层数据库必须支持自动增长 (底层数据库支持的自动增长方式,对id自增)
     *            2. GenerationType.SEQUENCE : 序列 ,oracle
     *               底层书库必须支持序列
     *            3. GenerationType.TABLE : jpa 提供的一种机制, 通过一张数据库表的形式帮助我们完成主键的配置
     *            4. GenerationType.AUTO : 由程序自动的帮助我们选择主键生成策略
     *   @Column(name = "cust_id") 配置属性和字段的映射关系
     *       name: 数据库表中字段的名称
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "cust_id")
    private Long custId;//客户的主键

    @Column(name = "cust_name")
    private String custName;//客户的名称

    @Column(name = "cust_address")
    private String custAddress;

    public Long getCustId() {
        return custId;
    }

    public void setCustId(Long custId) {
        this.custId = custId;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public String getCustAddress() {
        return custAddress;
    }

    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "custId=" + custId +
                ", custName='" + custName + '\'' +
                ", custAddress='" + custAddress + '\'' +
                '}';
    }
}

4.创建spring配置文件




    
    

    
    
        
            
            
                
                
                
                
            
        
        
        

        
    


    
        
        
        
        
    


    
          
    

    

5.创建Repository接口

package com.kuang.repositories;

import com.kuang.pojo.Customer;
import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository {

}

6.测试通过主键查询

package com.kuang.test;

import com.kuang.pojo.Customer;
import com.kuang.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Optional;

@ContextConfiguration("/spring.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringDataJpaTest {

    @Autowired
    private CustomerRepository customerRepository;

    @Test
    public void select() {
        Optional byId = customerRepository.findById(1L);
        Customer customer = byId.get();
        System.out.println(customer);
    }
}

package com.kuang.test;

import com.kuang.pojo.Customer;
import com.kuang.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Optional;

@ContextConfiguration("/spring.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringDataJpaTest {

    @Autowired
    private CustomerRepository customerRepository;

    @Test
    public void select() {
        Optional byId = customerRepository.findById(1L);
        Customer customer = byId.get();
        System.out.println(customer);
    }

    @Test
    public void insert() {
        Customer customer = new Customer();
        customer.setCustAddress("南环路");
        customer.setCustName("豪哥");
        Customer save = customerRepository.save(customer);
        save.setCustAddress("刘备");
        System.out.println(customer);
    }

    @Test
    public void update() {
        Customer customer = new Customer();
        customer.setCustId(1L);
        customer.setCustAddress("栖霞路");
        customer.setCustName("张飞");
        Customer save = customerRepository.save(customer);
    }

    @Test
    public void remove() {
        Customer customer = new Customer();
        customer.setCustId(1L);

         customerRepository.delete(customer);
    }
}

 

你可能感兴趣的:(hibernate,java)