This is a microformat block with images (taken from Getting started with Compose Multiplatform tutorial).
Create a Spring Boot project with Kotlin
Add a data class to the Spring Boot project
Add database support for Spring Boot project
Use Spring Data CrudRepository for database access>
plugins {
kotlin("kapt") version "%kotlinVersion%"
}
plugins {
id "org.jetbrains.kotlin.kapt" version "%kotlinVersion%"
}
plugins {
kotlin("plugin.noarg") version "%kotlinVersion%"
}
plugins {
id "org.jetbrains.kotlin.plugin.noarg" version "%kotlinVersion%"
}
Some text here and a codeblock:
plugins {
kotlin("plugin.noarg") version "%kotlinVersion%"
}
Just a codeblock:
import java.util.*
@Service
class MessageService(val db: MessageRepository) {
fun findMessages(): List<Message> = db.findAll().toList()
fun findMessageById(id: String): List<Message> = db.findById(id).toList()
fun save(message: Message) {
db.save(message)
}
fun <T : Any> Optional<out T>.toList(): List<T> =
if (isPresent) listOf(get()) else emptyList()
}
package com.example.demo
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
@RestController
class MessageController {
@GetMapping("/")
fun index(@RequestParam("name") name: String) = "Hello, $name!"
}
{initial-collapse-state="collapsed"}
data class User(val name: String, val id: Int)
fun main() {
val user = User("Alex", 1)
//sampleStart
// Automatically uses toString() function so that output is easy to read
println(user)
// User(name=Alex, id=1)
//sampleEnd
}
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
Primitive-type array | Equivalent in Java |
---|---|
BooleanArray |
boolean[] |
ByteArray |
byte[] |
CharArray |
char[] |
DoubleArray |
double[] |
FloatArray |
float[] |
IntArray |
int[] |
LongArray |
long[] |
ShortArray |
short[] |
Last modified on | December 2023 |
Next update | June 2024 |
Simple table:
Before | Now |
kotlin {
targets {
configure(['windows',
'linux']) {
}
}
} |
kotlin {
targets {
configure([findByName('windows'),
findByName('linux')]) {
}
}
} |
More complex table:
Before | Now | |
Dependencies of the jvmMain compilation |
jvm<Scope> |
jvmCompilation<Scope> |
dependencies {
add("jvmImplementation",
"foo.bar.baz:1.2.3")
} |
dependencies {
add("jvmCompilationImplementation",
"foo.bar.baz:1.2.3")
} |
|
Dependencies of the jvmMain source set |
jvmMain<Scope> |
|
Dependencies of the jvmTest compilation |
jvmTest<Scope> |
jvmTestCompilation<Scope> |
Dependencies of the jvmTest source set |
jvmTest<Scope> |
-
One
-
Two
-
Three
- Three point 1
- Three point 2
- Three point 3
- Three point 1 and 1
-
With a codeblock inside:
jvmTest<Scope>
-
First bullet
-
Second bullet
-
Third bullet
- One more
- Another one
- Wow, one more
-
With a codeblock inside:
jvmTest<Scope>
The return type of the findById()
function in the CrudRepository
interface is an instance of the Optional
class. However, it would be convenient to return a List
with a single message for consistency. For that, you need to unwrap the Optional
value if it’s present, and return a list with the value. This can be implemented as an extension function to the Optional
type.
In the code, Optional<out T>.toList()
, .toList()
is the extension function for Optional
. Extension functions allow you to write additional functions to any classes, which is especially useful when you want to extend functionality of some library class.
This function works with an assumption that the new object doesn’t have an id in the database. Hence, the id should be null for insertion.
If the id isn’t null, CrudRepository
assumes that the object already exists in the database and this is an update operation as opposed to an insert operation. After the insert operation, the id
will be generated by the data store and assigned back to the Message
instance. This is why the id
property should be declared using the var
keyword.
- Bold text
- italic text
inline code
- internal anchor
- internal link
- external link
- emojis ❌✅🆕
- variable using: lastest Kotlin version is %kotlinVersion%