springDataJPA和spring的整合

   
        4.2.4.RELEASE
        5.0.7.Final
        1.6.6
        1.2.12
        0.9.1.2
        5.1.6
        1.8
        1.8
    

    
        
            junit
            junit
            4.12
            test
        
        
        
            org.aspectj
            aspectjweaver
            1.6.8
        
        
            org.springframework
            spring-aop
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            org.springframework
            spring-context-support
            ${spring.version}
        
        
            org.springframework
            spring-orm
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
        
        
            org.hibernate
            hibernate-core
            ${hibernate.version}
        
        
            org.hibernate
            hibernate-entitymanager
            ${hibernate.version}
        
        
            org.hibernate
            hibernate-validator
            5.2.1.Final
        
        
        
            c3p0
            c3p0
            ${c3p0.version}
        
        
            log4j
            log4j
            ${log4j.version}
        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
        
        
            org.springframework.data
            spring-data-jpa
            1.9.0.RELEASE
        
        
            org.springframework
            spring-test
            4.2.4.RELEASE
        
        
            javax.el
            javax.el-api
            2.2.4
        
        
            org.glassfish.web
            javax.el
            2.2.4
        
    


       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop" 
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd 
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd 
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop.xsd 
       http://www.springframework.org/schema/data/jpa 
       http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    
    class="com.mchange.v2.c3p0.ComboPooledDataSource">
        
        
        
        
    
    
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
              
          
        
        
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                     
                 
                   
            
        
    
    
    class="org.springframework.orm.jpa.JpaTransactionManager">
               
    
            
    
            
                
                
                
            
    

    
    
            
            
    
    
    package="com.jpa.dao"
                      transaction-manager-ref="transactionManager"
                      entity-manager-factory-ref="entityManagerFactory"
    />
import javax.persistence.*;

@Entity         //jpa 的实体类
@Table(name = "custome")    //配饰实体类和数据库的关系, name对应表名
public class Custome {

    @GeneratedValue(strategy = GenerationType.IDENTITY) //主键自动增持
    @Id

    @Column(name="cust_id")
    private long custId;

    @Column(name="cust_name")
    private String custName;

    @Column(name="cust_level")
    private String custLevel;

    @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 getCustLevel() {
        return custLevel;
    }

    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }

    public String getCustAddress() {
        return custAddress;
    }

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

    @Override
    public String toString() {
        return "Custome{" +
                "custId=" + custId +
                ", custName='" + custName + '\'' +
                ", custLevel='" + custLevel + '\'' +
                ", custAddress='" + custAddress + '\'' +
                '}';
    }
import com.jpa.entity.Custome;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CustomerDao extends JpaRepository {
}
import com.jpa.dao.CustomerDao;
import com.jpa.entity.Custome;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestCustomer {

    @Autowired
    private CustomerDao customerDao;

    @Test   //添加数据
    public void addCustomer(){
       for (int i=0;i<20;i++){
           Custome custome = new Custome();
           custome.setCustName("张三");
           custome.setCustAddress("中华大陆");
           custome.setCustLevel("vip");
           customerDao.save(custome);
       }
    }
    @Test       //根据id删除数据
    public void deleteCustomer(){
        customerDao.delete(2l);
    }
    @Test       //根据id查询数据
    public void findById(){
        Custome custome = customerDao.findOne(7l);
        System.out.println(custome);
    }

    @Test
    @Transactional      //加上事务注解,在方法结束 之前不会断
    public void findById2(){
        Custome custome = customerDao.findOne(7L);
        System.out.println("---------------------");
        System.out.println(custome);
    }

    @Test   //查询全部
    public void findAll(){
        List customeList = customerDao.findAll();
         for (Custome custome:customeList){
             System.out.println(custome);
         }
    }
    @Test   //分页查询
    public void testPage(){
        PageRequest pageRequest = new PageRequest(0, 5);    //0第一页
        Page customePage = customerDao.findAll(pageRequest);
        long totalElements = customePage.getTotalElements();
        System.out.println("总记录数:"+totalElements);
        int totalPages = customePage.getTotalPages();
        System.out.println("总页码数:"+totalPages);
        List content = customePage.getContent();   //该页内容
        for (Custome custome:content){
            System.out.println(custome);
        }
    }
}
1 需求 使用springDataJpa  实现数据库的CRUD

​        2 创建数据库  Customer

​        3 开发步骤

​            a   创建Maven 工程

​            b   添加依赖

​                mysql 

​                c3p0

​                Hibernate

​                springDataJpa

​                Spring 相关jar包

##      2SpringDataJpa中查询

 

转载于:https://www.cnblogs.com/dragonyl/p/11364046.html

你可能感兴趣的:(springDataJPA和spring的整合)