SpringbootData中实现JPA
2020-12-24 00:29
标签:tomcat7 date() jpa 图片 mod pil onclick lse open
* JpaRepository 封装基本的curd操作
*
* JpaSpecificationExecutor 封装了分页,排序等复杂操作
*
* 查询方式
* 1、findById或者findOne等jpa定义的方式进行查询
* 2、引入jps语言查询例如:queryById
* 3、使用原生语言进行sql查询,需要自己写sql ,例如:queryByLike
* 4、在接口中自己写接口,通过jpa的编写规则进行写,例如:findByNameLikeAndAddressLike
* 5、动态查询
* -----条件的不同sql不同
* -----Optional
* 1、Entity声明是一个实体类对象
* 2、Table声明数据表与实体的对应关系
* 2、@Id 指定主键
* 3、@GeneratedValue 指定主键策略
* GenerationType.TABLE:使用一个特定的数据库表格来保存主键。
* GenerationType.SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。
* GenerationType.IDENTITY:主键由数据库自动生成(主要是自动增长型)
* GenerationType.AUTO:主键由程序控制。
* 3、@Column 字段属性对应关系
SpringbootData中实现JPA 标签:tomcat7 date() jpa 图片 mod pil onclick lse open 原文地址:https://www.cnblogs.com/niunafei/p/13213012.htmlspringDataJpa使用
0、引入依赖jar
xml version="1.0" encoding="UTF-8"?>
project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
parent>
artifactId>test_codeartifactId>
groupId>city.albertgroupId>
version>1.0-SNAPSHOTversion>
parent>
modelVersion>4.0.0modelVersion>
artifactId>jpaartifactId>
packaging>warpackaging>
dependencies>
dependency>
groupId>junitgroupId>
artifactId>junitartifactId>
version>4.12version>
dependency>
dependency>
groupId>org.springframework.datagroupId>
artifactId>spring-data-jpaartifactId>
version>2.1.9.RELEASEversion>
dependency>
dependency>
groupId>javax.elgroupId>
artifactId>javax.el-apiartifactId>
version>3.0.0version>
dependency>
dependency>
groupId>javax.elgroupId>
artifactId>el-apiartifactId>
version>2.2version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-aopartifactId>
version>5.2.5.RELEASEversion>
dependency>
dependency>
groupId>org.aspectjgroupId>
artifactId>aspectjweaverartifactId>
version>1.9.5version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-contextartifactId>
version>5.2.5.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-context-supportartifactId>
version>5.1.12.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-ormartifactId>
version>5.1.8.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-beansartifactId>
version>5.2.5.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-coreartifactId>
version>5.2.5.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-testartifactId>
version>5.2.5.RELEASEversion>
dependency>
dependency>
groupId>org.hibernategroupId>
artifactId>hibernate-ehcacheartifactId>
version>5.3.10.Finalversion>
dependency>
dependency>
groupId>org.hibernategroupId>
artifactId>hibernate-entitymanagerartifactId>
version>5.3.10.Finalversion>
dependency>
dependency>
groupId>org.hibernategroupId>
artifactId>hibernate-enversartifactId>
version>5.3.10.Finalversion>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
version>5.1.47version>
dependency>
dependency>
groupId>com.alibabagroupId>
artifactId>druidartifactId>
version>1.1.21version>
dependency>
dependencies>
build>
finalName>jpafinalName>
plugins>
plugin>
groupId>org.apache.maven.pluginsgroupId>
artifactId>maven-compiler-pluginartifactId>
version>3.3version>
configuration>
encoding>utf-8encoding>
source>1.8source>
target>1.8target>
compilerArgs>
arg>-parametersarg>
compilerArgs>
configuration>
plugin>
plugin>
groupId>org.apache.tomcat.mavengroupId>
artifactId>tomcat7-maven-pluginartifactId>
version>2.2version>
configuration>
port>8080port>
path>/path>
uriEncoding>UTF-8uriEncoding>
server>tomcat7server>
configuration>
plugin>
plugins>
build>
repositories>
repository>
id>centralid>
name>aliyun mavenname>
url>http://maven.aliyun.com/nexus/content/groups/public/url>
layout>defaultlayout>
releases>
enabled>trueenabled>
releases>
snapshots>
enabled>falseenabled>
snapshots>
repository>
repositories>
project>
1、配置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:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
context:component-scan base-package="city.albert"/>
context:property-placeholder location="classpath*:jdbc.properties"/>
bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
property name="driverClassName" value="${classDr}"/>
property name="url" value="${url}"/>
property name="username" value="${username}"/>
property name="password" value="${password}"/>
bean>
bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
property name="dataSource" ref="dataSource"/>
property name="packagesToScan" value="city.albert.entity"/>
property name="persistenceProvider" >
bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
property>
property name="jpaDialect">
bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
property>
property name="jpaVendorAdapter">
bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
property name="generateDdl" value="false"/>
property name="database" value="MYSQL"/>
property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
property name="showSql" value="true"/>
bean>
property>
bean>
bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
property name="entityManagerFactory" ref="entityManagerFactory"/>
bean>
jpa:repositories base-package="city.albert.dao"
entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>
tx:annotation-driven/>
beans>
2、配置dao接口
package city.albert.dao;
import city.albert.entity.ResumeEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
/**
* @author niunafei
* @function
* @email niunafei0315@163.com
* @date 2020/6/25 8:52 PM
*
4、配置实体类
package city.albert.entity;
import javax.persistence.*;
/**
* 简历实体类 简历属性与字段映射关系
*
* @author niunafei
* @function
* @email niunafei0315@163.com
* @date 2020/6/25 8:09 PM
*
5、测试
package city.albert;
import city.albert.dao.ResumeDao;
import city.albert.entity.ResumeEntity;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.persistence.criteria.*;
import java.util.List;
import java.util.Optional;
/**
* @author niunafei
* @function
* @email niunafei0315@163.com
* @date 2020/6/25 9:10 PM
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class JpaTest {
@Autowired
private ResumeDao dao;
/**
* 查询
* 1、findById或者findOne等jpa定义的方式进行查询
* 2、引入jps语言查询例如:queryById
* 3、使用原生语言进行sql查询,需要自己写sql ,例如:queryByLike
* 4、在接口中自己写接口,通过jpa的编写规则进行写,例如:findByNameLikeAndAddressLike
* 5、动态查询
* -----条件的不同sql不同
*/
@Test
public void test() {
findById();
findOne();
saveOrUpdate();
findAll();
//查询jpa语法
ResumeEntity entity = dao.queryById(2L);
System.out.println(entity);
//原生sql,手动编写
ResumeEntity query = dao.queryByLike("%3%", "%3%");
System.out.println(query);
//根据规则进行查询
ResumeEntity f = dao.findByNameLikeAndAddressLike("%3%", "%3%");
System.out.println(f);
//实现动态sql拼接
Specification
文章标题:SpringbootData中实现JPA
文章链接:http://soscw.com/index.php/essay/37800.html