菜单
登录注册
欢 迎
登录
自动登录
忘记密码?
新朋友
注册
注册
老朋友
登录
笔记内容为空!
TA的笔记树 >>
Kotlin + SpringBoot + JPA(Hibernate) + Repository自定义方法
Spring
[TOC] ### Kotlin + SpringBoot + JPA(Hibernate) + Repository自定义方法 ``` JPA是java为整合对数据库操作定义的一系列接口方案, 操作非常简单方便. 但有时需要自定义复杂sql操作的时候, 则可能需要对JPA方法扩展. 但若直接实现JPARepository接口, 则需实现它一系列方法, 这显然是不可取的.本文则记录如何简单的扩展自定义方法 ``` #### `pom.xml` ```
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
1.0.22
2.9.3
1.8
4.12
1.2.10
5.1.29
1.3.1
1.5.9.RELEASE
1.3.8.RELEASE
true
UTF-8
UTF-8
junit
junit
${junit.version}
test
com.fasterxml.jackson.module
jackson-module-kotlin
${jackson-module-kotlin}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j
${spring-boot-starter-log4j.version}
org.springframework.boot
spring-boot-starter-test
test
org.jetbrains.kotlin
kotlin-stdlib-jre8
${kotlin.version}
org.jetbrains.kotlin
kotlin-reflect
${kotlin.version}
com.alibaba
druid
${druid.version}
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
${mysql.version}
${project.basedir}/src/main/kotlin
${project.basedir}/src/test/kotlin
kotlin-maven-plugin
org.jetbrains.kotlin
${kotlin.version}
spring
1.8
compile
compile
compile
test-compile
test-compile
test-compile
org.jetbrains.kotlin
kotlin-maven-allopen
${kotlin.version}
: PagingAndSortingRepository
{ } ``` ##### `UserRepositoryImpl.kt` ``` package com.qiqinote.repository.customize import com.qiqinote.entity.User /** * Created by vanki on 2018/1/23 14:44. */ interface UserCustomizeRepository { fun test(name: String): MutableList
? } ``` ##### `UserRepositoryImpl.kt` > ***注意*** >> 1. 实现 `UserCustomizeRepository` >> 2. 名称必须与对外接口名对应(加上`Impl`), 或者注解加上bean名称 `@Repository("userRepositoryImpl")` ``` package com.qiqinote.repository.customize.impl import com.qiqinote.entity.User import com.qiqinote.repository.customize.UserCustomizeRepository import org.springframework.stereotype.Repository import javax.persistence.EntityManager import javax.persistence.PersistenceContext /** * Created by vanki on 2018/1/23 14:38. */ @Repository class UserRepositoryImpl : UserCustomizeRepository { @PersistenceContext private lateinit var entityManager: EntityManager override fun test(name: String): MutableList
? { val sql = "select * from user where alias=?1" val query = this.entityManager.createNativeQuery(sql, User::class.java) query.setParameter(1, name) return query.resultList as MutableList
? } } ``` ##### `UserRepository` > 对外的接口 ``` package com.qiqinote.repository import com.qiqinote.entity.User import com.qiqinote.repository.base.BaseRepository import com.qiqinote.repository.customize.UserCustomizeRepository /** * Created by vanki on 2018/1/23 14:04. */ interface UserRepository : BaseRepository
, UserCustomizeRepository { } ``` #### Application.kt ``` package com.qiqinote import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.context.annotation.ComponentScan import org.springframework.context.annotation.PropertySource /** * Created by vanki on 2018/1/17 21:46. */ @SpringBootApplication @ComponentScan(basePackages = arrayOf("com.qiqinote.repository")) @PropertySource(value = "classpath:application-jpa.properties") class Application fun main(args: Array
) { SpringApplication.run(Application::class.java, *args); } ``` #### 测试类 ``` package com.qiqinote.repository import org.junit.Test import org.junit.runner.RunWith import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.context.junit4.SpringRunner /** * Created by vanki on 2018/1/18 18:00. */ @RunWith(SpringRunner::class) @SpringBootTest class TestUserRepository { @Autowired private lateinit var userRepository: UserRepository @Test fun t1() { val test = this.userRepository.test("name2") } } ```
vanki
相传胜利者肯定手持正义,那仅是因为正义可以被胜利者定义
浏览:
6717
创建:
2018-01-24 04:55:38
更新:
2018-09-15 09:09:01
TA的最新笔记
spring-boot配置redis多数据源
linux源修改(阿里)
python安装postgresql依赖
arthas使用
java基于spring的NamedParameterJdbcTemplate封装的获取sql工具类
Impala添加负载
S3常用使用
redis常用操作
hdfs相关命令
crontab使用
TA的最热笔记
java异步http请求工具类(org.asynchttpclient)
iTerm2主题配置与常用技巧
java基于spring.redisTemplate实现分布式锁工具类
Kotlin + SpringBoot + JPA(Hibernate) + Repository自定义方法
IDEA汉化
Sequel Pro连接mysql8打开数据库报错
centos-Hadoop2.7.3完全分布式搭建(HA)
SpringBoot上传文件报错(The temporary upload location [..] is not valid)
mac常用软件
kotlin对象属性值拷贝工具类