JPA

》#千锋逆战#
Java持久性API(JPA)是Java的一个规范。 它用于在Java对象和关系数据库之间保存数据。 JPA充当面向对象的领域模型和关系数据库系统之间的桥梁。

由于JPA只是一个规范,它本身不执行任何操作。 它需要一个实现。 因此,像Hibernate,TopLink和iBatis这样的ORM工具实现了JPA数据持久性规范。
pom.xml

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.28</version>
        </dependency>
        <!--
            添加spring-data-jpa的依赖
        -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.11.0.RELEASE</version>
        </dependency>
        <!--
            spring-data-jpa依赖于hibernate-entitymanager
        -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.10.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.6.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.6</version>
        </dependency>
    </dependencies>

spring-jpa.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: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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <context:property-placeholder location="classpath:db.properties"/>

    <context:component-scan base-package="com.qfedu.service"/>
    <context:component-scan base-package="com.qfedu.dao"/>

    <bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${url}"/>
        <property name="driverClassName" value="${driver}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${pass}"/>
    </bean>

    <!--
        配置 HibernateJpaVendorAdapter,用来分别设置数据库的方言和是否显示sql语句
    -->
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" id="adapter">
        <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="showSql" value="true"/>
    </bean>

    <!--
        配置EntityManagerFactoryBean
    -->
    <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="emf">
        <property name="dataSource" ref="ds"/>
        <property name="packagesToScan" value="com.qfedu.entity"/>
        <property name="jpaVendorAdapter" ref="adapter"/>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>

    <!--
        配置jpa的事务管理器
    -->
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="jtx">
        <property name="entityManagerFactory" ref="emf"/>
    </bean>
    <!--配置事务驱动-->
    <tx:annotation-driven proxy-target-class="false" transaction-manager="jtx"/>
    <jpa:repositories base-package="com.qfedu.dao" entity-manager-factory-ref="emf" transaction-manager-ref="jtx"/>

</beans>

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/supermarket?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=TRUE
user=root
pass=123456

entity

package com.qfedu.entity;

import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;

/**
 * (GoodType)实体类
 *
 * @author makejava
 * @since 2020-04-01 23:18:54
 */
@Entity
@Table(name = "good_type")
@Data
public class GoodType {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer gtid;
    
    private String gtname;


}

dao

package com.qfedu.dao;

import com.qfedu.entity.GoodType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
@Repository
public interface IGoodsTypeDao extends JpaRepository<GoodType, Serializable> {
}

service

package com.qfedu.service;

import com.qfedu.entity.GoodType;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

public interface IGoodsTypeService {
    List<GoodType> getAllType();
    void saveType(GoodType gt);
}

service.impl

package com.qfedu.service.impl;

import com.qfedu.dao.IGoodsTypeDao;
import com.qfedu.entity.GoodType;
import com.qfedu.service.IGoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class GoodsTypeServiceImpl implements IGoodsTypeService {
    @Resource
    private IGoodsTypeDao goodsTypeDao;
    public List<GoodType> getAllType() {
        return goodsTypeDao.findAll();
    }

    public void saveType(GoodType gt) {
        goodsTypeDao.saveAndFlush(gt);
    }
}

test

package com.qfedu.test;

import com.qfedu.entity.GoodType;
import com.qfedu.service.IGoodsTypeService;
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 javax.annotation.Resource;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-jpa.xml")
public class TestService {
    @Resource
    private IGoodsTypeService goodsTypeService;
    @Test
    public void getAllType(){
        List<GoodType> allType = goodsTypeService.getAllType();
        for (GoodType type : allType) {
            System.out.println(type);

        }
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容