我在我的项目中使用Spring 5。直到今天,还有方法CrudRepository#findOne
。
但是在下载最新快照后它突然消失了!有没有提到该方法现在不可用?
我的依赖列表:
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-validation'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-data-jpa'
runtime 'org.springframework.boot:spring-boot-devtools'
runtime 'com.h2database:h2:1.4.194'
compile 'org.projectlombok:lombok:1.16.14'
compile 'org.modelmapper:modelmapper:0.7.5'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'org.codehaus.groovy:groovy-all:2.4.10'
testCompile 'cglib:cglib:3.2.5'
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
}
更新
似乎此方法已替换为CrudRepository#findById
答案 0 :(得分:114)
请参阅与DATACMNS-944相关联的this commit,该https://developer.android.com/studio/publish/app-signing.html#google-play-app-signing具有以下重命名
╔═════════════════════╦═══════════════════════╗
║ Old name ║ New name ║
╠═════════════════════╬═══════════════════════╣
║ findOne(…) ║ findById(…) ║
╠═════════════════════╬═══════════════════════╣
║ save(Iterable) ║ saveAll(Iterable) ║
╠═════════════════════╬═══════════════════════╣
║ findAll(Iterable) ║ findAllById(…) ║
╠═════════════════════╬═══════════════════════╣
║ delete(ID) ║ deleteById(ID) ║
╠═════════════════════╬═══════════════════════╣
║ delete(Iterable) ║ deleteAll(Iterable) ║
╠═════════════════════╬═══════════════════════╣
║ exists() ║ existsById(…) ║
╚═════════════════════╩═══════════════════════╝
答案 1 :(得分:80)
请注意,findById
并非findOne
的确切替代,而是返回Optional
而不是null
。
我不太熟悉新的java事情,我花了一点时间才弄明白,但这会将findById
行为变成findOne
行为:
return rep.findById(id).orElse(null);
答案 2 :(得分:20)
我们有数百种旧的import os
import math
import random
import time
#set up screen
screen = turtle.Screen()
screen.bgcolor("green")
screen.title("Pong")
# set up border
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
#set score to 0
score = 0
#set time to zero
time = 0
seconds = 0
#Draw score
score_pen = turtle.Turtle()
score_pen.speed(0)
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290, 310)
scorestring = "Score %s" %score
score_pen.write(scorestring, False, align="left", font= ("Arial", 14, "normal"))
score_pen.hideturtle()
#Draw timer
time_pen = turtle.Turtle()
time_pen.speed(0)
time_pen.color("white")
time_pen.penup()
time_pen.setposition(260, 310)
timestring = "Time %s" %time
time_pen.write(timestring, False, align="left", font= ("Arial", 14, "normal"))
time_pen.hideturtle()
#create the player turtle
player = turtle.Turtle()
player.color("blue")
player.shape("square")
player.shapesize(0.5, 4)
player.penup()
player.speed(0)
player.setposition(-280,-250)#(x,y)
player.setheading(90)
playerspeed = 15
#create the AIplayer turtle
AIplayer = turtle.Turtle()
AIplayer.color("black")
AIplayer.shape("square")
AIplayer.shapesize(0.5, 4)
AIplayer.penup()
AIplayer.speed(0)
AIplayer.setposition(280,250)#(x,y)
AIplayer.setheading(90)
AIplayerspeed = 15
#create the pong
pong = turtle.Turtle()
pong.color("red")
pong.shape("circle")
pong.shapesize(0.5, 0.5)
pong.penup()
pong.speed(10)
pong.setposition(0,0)#(x,y)
pongspeed = 15
pong.goto(0, 265)
pong.dy = -5
pong.dx = 5
#Move player up and down
def move_up():
y = player.ycor()
y += playerspeed
if y > 265:
y = 260
player.sety(y)
def move_down():
y = player.ycor()
y -= playerspeed
if y < -265:
y = -260
player.sety(y)
#keyboard bindings
turtle.listen()
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")
#turtle.onkey(fire_bullet, "space")
def isCollision(t1, t2):
distance = math.sqrt(math.pow(t1.xcor()- t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
if distance < 20:
return True
else:
return False
#main game loop
while True:
#move pong ball
pong.sety(pong.ycor() +pong.dy)
pong.setx(pong.xcor() +pong.dx)
#check for bounce and redirect it
if pong.ycor() < -300:
pong.dy *= -1
if pong.ycor() > 300:
pong.dy *= -1
if pong.xcor() < -300:
pong.dx *= -1
print("Game Over")
exit()
if pong.xcor() > 300:
pong.dx *= -1
#move AI paddle (might speed up pong movement)
y = pong.ycor()
y += AIplayerspeed
AIplayer.sety(y)
if AIplayer.ycor() > 265:
AIplayerspeed *= -1
if AIplayer.ycor() < -250:
AIplayerspeed *= -1
#collision pong and player
if isCollision(pong, player):
pong.dy *= -1
pong.dx *= -1
#Update the score
score += 10
scorestring = "Score: %s" %score
score_pen.clear()
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
#collision pong and AIplayer
if isCollision(pong, AIplayer):
pong.dy *= -1
pong.dx *= -1
#updates timer and increases ball speed
if seconds > 29:
pong.dy *= -2
pong.dx *= -2
if seconds > 59:
pong.dy *= -3
pong.dx *= -3
#displays timer but makes game laggy
# seconds += 0.1
# time = seconds
# timestring = "Time: %s" %time
# time_pen.clear()
# time_pen.write(timestring, False, align="Left", font=("Arial", 14, "normal"))
方法的用法。我们没有进行庞大的重构,而是创建了以下中间接口,并让我们的存储库扩展了它,而不是直接扩展findOne()
JpaRepository
答案 3 :(得分:3)
实用转型
旧方式:
Entity aThing = repository.findOne(1L);
新方式:
Optional<Entity> aThing = repository.findById(1L);
答案 4 :(得分:0)
另一种方式。添加一个@Query。即使我更喜欢使用Optional:
@Repository
public interface AccountRepository extends JpaRepository<AccountEntity, Long> {
@Query("select a from AccountEntity a where a.id=?1")
public AccountEntity findOne(Long id);