》#千鋒逆戰(zhàn)#
Java持久性API(JPA)是Java的一個(gè)規(guī)范训堆。 它用于在Java對(duì)象和關(guān)系數(shù)據(jù)庫(kù)之間保存數(shù)據(jù)。 JPA充當(dāng)面向?qū)ο蟮念I(lǐng)域模型和關(guān)系數(shù)據(jù)庫(kù)系統(tǒng)之間的橋梁白嘁。
由于JPA只是一個(gè)規(guī)范坑鱼,它本身不執(zhí)行任何操作。 它需要一個(gè)實(shí)現(xiàn)。 因此鲁沥,像Hibernate呼股,TopLink和iBatis這樣的ORM工具實(shí)現(xiàn)了JPA數(shù)據(jù)持久性規(guī)范。
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画恰,用來(lái)分別設(shè)置數(shù)據(jù)庫(kù)的方言和是否顯示sql語(yǔ)句
-->
<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的事務(wù)管理器
-->
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="jtx">
<property name="entityManagerFactory" ref="emf"/>
</bean>
<!--配置事務(wù)驅(qū)動(dòng)-->
<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)實(shí)體類
*
* @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);
}
}
}