springDataJPA和spring的整合

   <properties>
        <spring.version>4.2.4.RELEASE</spring.version>
        <hibernate.version>5.0.7.Final</hibernate.version>
        <slf4j.version>1.6.6</slf4j.version>
        <log4j.version>1.2.12</log4j.version>
        <c3p0.version>0.9.1.2</c3p0.version>
        <mysql.version>5.1.6</mysql.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--spring start-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--spring end-->
        <!--hibernate start-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.1.Final</version>
        </dependency>
        <!--hibernate end-->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>${c3p0.version}</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.9.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>2.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.el</artifactId>
            <version>2.2.4</version>
        </dependency>
    </dependencies>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
    <!--1.数据源连接池    c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring-data-jpa?characterEncoding=UTF-8"/>
        <property name="user" value="root"/>
        <property name="password" value="MYSQL"/>
    </bean>
    <!--工厂类对象  本地容器实力管理工厂对象-->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>      <!--引用数据源-->
        <property name="packagesToScan" value="com.jpa.entity"/>  <!--扫描entity层对象-->
        <!--配置jpa供应商适配器-->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="true"/>     <!--打印sql语句-->
                <property name="generateDdl" value="true"/> <!--sql语句格式化-->
                <property name="database" value="MYSQL"/>   <!--使用MYSQL数据库-->
            </bean>
        </property>
    </bean>
    <!--配置事务    jpa事务管理-->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>   <!--引用工厂对象-->
    </bean>
            <!--事务驱动-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="get*" read-only="true"/>
                <tx:method name="find*" read-only="true"/>
                <tx:method name="*" propagation="REQUIRED"/>
            </tx:attributes>
    </tx:advice>

    <!--配置切面-->
    <aop:config>
            <aop:pointcut id="pointcut" expression="execution(* com.jpa.service.*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>
    <!--dao层-->
    <jpa:repositories base-package="com.jpa.dao"
                      transaction-manager-ref="transactionManager"
                      entity-manager-factory-ref="entityManagerFactory"
    />
</beans>
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<Custome,Long> {
}
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<Custome> customeList = customerDao.findAll();
         for (Custome custome:customeList){
             System.out.println(custome);
         }
    }
    @Test   //分页查询
    public void testPage(){
        PageRequest pageRequest = new PageRequest(0, 5);    //0第一页
        Page<Custome> customePage = customerDao.findAll(pageRequest);
        long totalElements = customePage.getTotalElements();
        System.out.println("总记录数:"+totalElements);
        int totalPages = customePage.getTotalPages();
        System.out.println("总页码数:"+totalPages);
        List<Custome> 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中查询

 

posted @ 2019-08-16 15:10  陌之殇  阅读(1666)  评论(0编辑  收藏  举报