JPA扩展(自定义sql)

pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>per.qiao</groupId>
<artifactId>springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootdemo</name>
<description>sprnigboot学习</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 扩展JPA包 -->
<dependency>
<groupId>com.slyak</groupId>
<artifactId>spring-data-jpa-extra</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
得益于spring-data-jpa-extra 包
先编写两个文件
@Configuration
@EnableConfigurationProperties(SpringJpaExtraProperties.class)
@AutoConfigureAfter({ DataSourceAutoConfiguration.class })
public class JpaExtraAutoConfiguration {
@Autowired
private SpringJpaExtraProperties springJpaProperties;
@Bean
protected FreemarkerSqlTemplates freemarkerSqlTemplates() {
FreemarkerSqlTemplates sqlTemplates = new FreemarkerSqlTemplates();
String templateBasePackage = springJpaProperties.getTemplateBasePackage();
if (templateBasePackage != null) {
sqlTemplates.setTemplateBasePackage(templateBasePackage);
}
String templateLocation = springJpaProperties.getTemplateLocation();
if (templateLocation != null) {
sqlTemplates.setTemplateLocation(templateLocation);
}
// 默认是xml
sqlTemplates.setSuffix(".sftl");
return sqlTemplates;
}
}
@ConfigurationProperties(prefix = "spring.jpa.extra")
public class SpringJpaExtraProperties {
/**
* 源码看 FreemarkerSqlTemplates.resolveSqlResource
* 例如 templateLocation:classpath:/sqltemplates 那么 扫描路径为 classpath:/sqltemplates/** /*.sftl
* templateLocation:classpath:/sqltemplates/Test.sftl 那么将只扫描这个一个文件
* 例如 templateBasePackage:sqltemplates.mysql 那么扫描路径为 classpath*: sqltemplates/sql/** /*.sftl
*
* 两个属性可以共存
*/
private String templateLocation;
private String templateBasePackage;
public String getTemplateLocation() {
return templateLocation;
}
public void setTemplateLocation(String templateLocation) {
this.templateLocation = templateLocation;
}
public String getTemplateBasePackage() {
return templateBasePackage;
}
public void setTemplateBasePackage(String templateBasePackage) {
this.templateBasePackage = templateBasePackage;
}
}
再写一个facoties文件
META-INF/spring.facotries
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
per.qiao.config.JpaExtraAutoConfiguration
yml文件
debug: false
spring:
main:
banner-mode: "off"
jpa:
database: mysql
show-sql: true
hibernate:
ddl-auto: update
naming:
#命名策略
strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
extra:
#源码看 FreemarkerSqlTemplates.resolveSqlResource
#templateLocation: classpath: sqltemplates
templateBasePackage: sqltemplates
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8
username: root
password: 123456
Dao文件
public interface TestRepository extends GenericJpaRepository<Test, Long>, JpaSpecificationExecutor {
@TemplateQuery
List<Test> getData();
@TemplateQuery
List<Test> getList(@Param("id") Integer id);
@TemplateQuery
List<Test> getListByPage(Test test, Pageable pageable);
@TemplateQuery
List<Map<String, Object>> getListMap(@Param("id") Integer id);
@TemplateQuery
Page<Test> findByName(String name, Pageable pageable);
// 这个是JPA默认的支持@Query使用原生sql
@Query(nativeQuery = true, value = "select * from test where name like ?1")
List<Test> findTest(String name);
}
sql的文件 Test.sftl
该文件使用freemarker的语法 FreeMarker基础语法
--getData
select id, name, subject from test where 1 = 1
<#--<#if content??>-->
<#--AND id = ${id}-->
<#--</#if>-->
--getList
select * from test where 1 = 1
<#if id??>
AND id = ${id}
</#if>
--getListByPage
select * from test where 1 = 1
<#if id??>
AND id = ${id}
</#if>
--getListMap
select name, birthday from test t left join test2 t2 on t.id = t2.id where 1 = 1
<#if id??>
And t.id = ${id}
</#if>
--findByName
select * from test
<#if name??>
And name = ${name}
</#if>
entity
@Entity
@ToString
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Test implements Serializable {
@Id
private Long id;
private String name;
private String subject;
private Integer score;
}
spirngBoot启动类上要加上@EnableJpaRepositories
@SpringBootApplication
@RestController
@ComponentScan({"per.qiao.entity"})
@EnableJpaRepositories(
basePackages = {"per.qiao.**.dao"},
repositoryFactoryBeanClass = GenericJpaRepositoryFactoryBean.class,
repositoryBaseClass = GenericJpaRepositoryImpl.class)
public class App {
调用
public void say() {
// getData
List<Test> data = testRepository.getData();
//
List<Test> tests = testRepository.getList(1);
//
Test test = Test.builder().id(1L).build();
PageRequest pageRequest = new PageRequest(1, 2, new Sort(Sort.Direction.ASC, "id"));
List<Test> pageDatas = testRepository.getListByPage(test, pageRequest);
System.out.println(pageDatas);
//
List<Map<String, Object>> listMap = testRepository.getListMap(1);
//
Page<Test> page = testRepository.findByName("张三", pageRequest);
List<Test> content = page.getContent();
System.out.println(content);
// 使用jpa的默认@Query注解
List<Test> tests = testRepository.findTest("张%");
System.out.println(tests);
}
注意sftl配置文件的名字要与实体类的名字一样
可以自定义修改一些东西,比如文件名与类名一样这点,关键类在FreemarkerTemplateQuery
源码地址: spring-data-jpa-extra
JPA扩展(自定义sql)的更多相关文章
- mybatis的sqlprovider用法扩展自定义sql
SqlProvider使用 public class MemberRewardSqlProvider { private static final Logger log = LoggerFactory ...
- JPA中自定义的插入、更新、删除方法为什么要添加@Modifying注解和@Transactional注解?
前几天,有个同事在使用JPA的自定义SQL方法时,程序一直报异常,捣鼓了半天也没能解决,咨询我的时候,我看了一眼他的程序,差不多是这个样子的: @Repository public interface ...
- JPA 开发写SQL时候遇见的困难点
官方文档 https://docs.spring.io/spring-data/jpa/docs/1.11.16.RELEASE/reference/html/#repositories.specia ...
- spring-jpa通过自定义sql执行修改碰到的问题
在编写自定义SQL的时候需要注意 @Query 注解只能用来查询,想要进行添加.修改和删除操作需要配合 @Modifying 注解一同使用 @Modifying @Query("update ...
- phpcmsv9自定义sql语句查询模型实现
在phpcmsv9中,自定义sql语句查询可不太好实现,传入sql语句查询很容易被内部转入生成一系列莫名其妙的sql语句,比如最佳前缀等等,直接造成sql语句查询错误,在此也提供两种解决办法,1修改底 ...
- thinkjs中自定义sql语句
一直以为在使用thinkjs时,只能是它自带的sql语句查询,当遇到类似于这样的sql语句时,却不知道这该怎样来写程序,殊不知原来thinkjs可以执行自定义sql语句 SELECT * from a ...
- 11月16日《奥威Power-BI基于SQL的存储过程及自定义SQL脚本制作报表》腾讯课堂开课啦
上周的课程<奥威Power-BI vs微软Power BI>带同学们全面认识了两个Power-BI的使用情况,同学们已经迫不及待想知道这周的学习内容了吧!这周的课程关键词—— ...
- springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用
springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用 原文链接:https://www.cnblogs.com/blog5277/p/10661 ...
- django不定义model,直接执行自定义SQL
如果不想定义model,直接执行自定义SQL,可如下操作: 1. 通过 connections获取db连接,如果是多个数据库,connections['dbName'] 来选择 2. 获取游标 cur ...
- Python Django 之 直接执行自定义SQL语句(二)
转载自:https://my.oschina.net/liuyuantao/blog/712189 一般来说,最好用 Django 自带的模型来实现这些操作.这里仅仅只是为了学习使用原始 SQL 而做 ...
随机推荐
- OR(Convex_Optimization_读书杂记)
今天心血来潮,看了一下ConvexOptimization,看到一开始描述直线的部分,真的感觉翻译得很棒.$$ y = \theta x_1 + (1-\theta) x_2 \qquad x1, ...
- pom.xml中properties作用
可以在properties下声明相应的版本信息,然后在dependency下引用的时候用${spring-version}就可以引入该版本jar包了 <properties> <sp ...
- PyTorch Tutorials 2 AUTOGRAD: AUTOMATIC DIFFERENTIATION
%matplotlib inline Autograd: 自动求导机制 PyTorch 中所有神经网络的核心是 autograd 包. 我们先简单介绍一下这个包,然后训练第一个简单的神经网络. aut ...
- 记一次被DDoS敲诈的历程 糖果LUA FreeBuf 今天 0x01 背景
记一次被DDoS敲诈的历程 糖果LUA FreeBuf 今天 0x01 背景
- Qt编写Onvif搜索及云台控制工具
一.前言 这个工具很早以前大概在2013年就想做了,后面杂七杂八的事情一再耽搁,记得当时最初用的是soap类来搜索和解析的,后面发现太大了,每次编译都要等好久,光源码文件加起来都快10MB了,而且函数 ...
- 【Leetcode_easy】653. Two Sum IV - Input is a BST
problem 653. Two Sum IV - Input is a BST 参考 1. Leetcode_easy_653. Two Sum IV - Input is a BST; 完
- iOS从App跳转至系统设置菜单各功能项
跳到系统设置里的WiFi界面 info里面设置: 在项目中的info.plist中添加 URL types 并设置一项URL Schemes为prefs,如下图 代码: 复制代码 代码如下: NSUR ...
- 静态链表过程演示及代码实现(A - B) U (B - A)
静态链表说明 使用数组来实现链式存储结构,目的是方便在不设指针类型的高级程序设计语言中使用链式结构 c语言定义数据结构 #define MAX_SIZE 1000 // 所有的类型都统一定义为Elem ...
- 最新 开创java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.开创等10家互联网公司的校招Offer,因为某些自身原因最终选择了开创.6.7月主要是做系统复习.项目复盘.LeetCode ...
- MAC OS brew的使用
brew 是 Mac 下的一个包管理工具,类似于 centos 下的 yum,可以很方便地进行安装/卸载/更新各种软件包,例如:nodejs, elasticsearch, kibana, mysql ...