Skip to content

Instantly share code, notes, and snippets.

@cheeseonamonkey
Last active August 4, 2022 21:50
Show Gist options
  • Save cheeseonamonkey/4417504268ad7ba3203f50eaf6ed8f70 to your computer and use it in GitHub Desktop.
Save cheeseonamonkey/4417504268ad7ba3203f50eaf6ed8f70 to your computer and use it in GitHub Desktop.
## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url = jdbc:mysql://localhost:3306/sendapi?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
spring.datasource.username = root
spring.datasource.password = root
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
package com.smol.send.api.mvc.controllers
import com.smol.send.api.repositories.MessageRepository
import com.smol.send.api.mvc.models.Message
import org.springframework.web.bind.annotation.*
import javax.validation.Valid
@RestController
@RequestMapping("/api")
class MessageController(private val r : MessageRepository) {
@GetMapping("/messages/{id}")
fun getMessage(@PathVariable("value = id") id:Long,) : Message =
r.getById(id)
@GetMapping("/messages")
fun getAllMessages() : List<Message> =
r.findAll()
@PostMapping("/messages")
fun createMessage(@Valid @RequestBody msg : Message) : Message =
r.save(msg)
}
package com.smol.send.api.mvc.models
import org.springframework.stereotype.Component
import javax.persistence.*
//@Component
@Entity
data class Message(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
val id : Long = 0,
val content: String = ""
) {}
package com.smol.send.api.repositories
import com.smol.send.api.mvc.models.Message
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.stereotype.Repository
@Repository
interface MessageRepository :JpaRepository<Message,Long>
/*
@Query("select * from MESSAGES")
fun all() : List<Message>
@Query("select * from MESSAGES") // where id = ?1
fun get(id : String) : Message
//no post fun in Repo
*/
package com.smol.send.api
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.autoconfigure.domain.EntityScan
import org.springframework.boot.runApplication
import org.springframework.context.annotation.ComponentScan
//@EntityScan()
@SpringBootApplication
class SendApiApplication
fun main(args: Array<String>) {
runApplication<SendApiApplication>(*args)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment