springDataJPA和spring,springmvc做整合和初体验

简介

SpringData是Spring的子框架,而SpringDataJPA是SpringData的子项目。SpringDataJPA专门对JPA进行了封装,使用SpringDataJPA有以下好处:
①与JPA和Hibernate一样,可以跨数据库产品兼容(只需要配置方言即可)
②Dao层只需要写接口,不用写实现类了(和mybatis一样)
③用法与JPA一致,都非常简单,而且还修改了JPA的方法名称(persist和merge改成了save,remove改成了delete,find改成了findOne,getResult改成了findAll)

SpringDataJpa逆向工程:https://www.cnblogs.com/zyulike/p/11304285.html

SpringDataJpa集成之配置文件

  • applicationContext.xml
<?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:context="http://www.springframework.org/schema/context"
       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/context
       http://www.springframework.org/schema/context/spring-context.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">

    <!--配置包扫描器(可以加逗号)-->
    <context:component-scan base-package="com.jpa.service"/>

    <context:property-placeholder ignore-unresolvable="true" location="classpath*:jdbc.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="clone">
        <property name="driverClassName" value="${driverName}"/>
        <property name="url" value="${url}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
        <!-- 初始化连接数量 -->
        <property name="initialSize" value="${jdbc.initialSize}"/>
        <!-- 最大并发连接数量 -->
        <property name="maxActive" value="${jdbc.maxActive}"/>
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="${jdbc.minIdle}"/>
        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="${jdbc.maxWait}" />
        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="${jdbc.removeAbandoned}" />
        <!-- 超过时间限制多长 -->
        <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
        <!-- 用来检测连接是否有效的sql,要求是一个查询语句-->
        <property name="validationQuery" value="${jdbc.validationQuery}" />
        <!-- 申请连接的时候检测 -->
        <property name="testWhileIdle" value="${jdbc.testWhileIdle}" />
        <!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
        <property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
        <!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能  -->
        <property name="testOnReturn" value="${jdbc.testOnReturn}" />
        <property name="logAbandoned" value="true" />
        <!-- 配置监控统计拦截的filters,wall用于防止sql注入,stat用于统计分析 -->
        <property name="filters" value="stat" />
    </bean>


    <!-- 配置sql工厂-->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--配置扫描我们jpa中的注解-->
        <property name="packagesToScan" value="com.jpa.model"/>
        <!--配置JPA的实现[Hibernate对JPA的实现]-->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <!--方言-->
                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
                <!--建表策略,其中只有update,true是为update-->
                <property name="generateDdl" value="false"/>
                <!--是否显示sql-->
                <property name="showSql" value="true"/>
            </bean>
        </property>
    </bean>


    <!--配置事务-->
    <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="add*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete" propagation="REQUIRED"/>
            <tx:method name="remove*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="modify*" propagation="REQUIRED"/>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="query*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!--配置切面-->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.jpa.service..*.*(..))"/>
    </aop:config>

    <!--集成SpringDataJpa:注意一定是先集成JPA,然后才能集成SpringDataJpa
    其实这个配置就是告诉SpringDataJpa我们的dao层接口在哪个包,不能在使用原来的扫描包的方式了
    dao层只有接口没有实现类了,就利用动态代理技术自动生成一个实现类,然后再创建对象
    -->
    <jpa:repositories base-package="com.jpa.dao"/>
</beans>

SpringDataJpa具体使用

  • model实体类

逆向工程生成实体类后,直接加@Data注解就可以了啥都不用干

要让主键自增必须在主键的get方法上加 @GeneratedValue(strategy = GenerationType.IDENTITY)注解

@Entity
@Data
@Table(name = "person")
public class Person {
    @Id
    @Column(name = "person_id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer personId;
    @Basic
    @Column(name = "name")
    private String name;
    @Basic
    @Column(name = "gender")
    private Integer gender;
    @Basic
    @Column(name = "person_addr")
    private String personAddr;
    @Basic
    @Column(name = "birthday")
    private Date birthday;
}
  • dao层

SpringDataJpa的dao层接口,只需要继承JpaRepository接口即可,该接口中有两个泛型:
T:model实体类类型
ID:model实体类中主键字段的类型

集成SpringDataJpa:注意一定是先集成JPA,然后才能集成SpringDataJpa
其实这个配置就是告诉SpringDataJpa我们的dao层接口在哪个包,不能在使用原来的扫描包的方式了dao层只有接口没有实现类了,就利用动态代理技术自动生成一个实现类,然后再创建对象。springdatajpa的dao层没有实现类,创建对象的时候是利用动态代理技术自动生成一个实现类,然后再创建对象。

springdatajpa扫描到这个包内的接口之后,底层会使用动态代理技术帮我们自动生成一个类叫$Proxy28,并且这个类实现了我们自定义的接口

  • 实例代码
public interface PersonDao extends JpaRepository<Person,Integer> {
}

Springdatajpa的继承体系

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值