springboot: kotlin + gradle + jdbc(mysql) demo

intbird發表於2020-10-29

spring.io文件

1. kotlin

spring-boot-restful

2. kotlin-demo

spring-boot-applications-with-kotlin

3. jdbc

spring-data-jdbc

4. jdbc-demo

Data access with JDBC

5.orm

JPA : https://spring.io/projects/spring-data-jpa
MyBatis: https://mybatis.org/mybatis-3/zh/getting-started.html


1. 執行產物

running service: https://intbird.net/spring/book/books

1. 前臺執行

java -jar ./intbird-spring-0.0.1-SNAPSHOT.war --server.port=8082

2. 後臺執行

nohup java -jar ./intbird-spring-0.0.1-SNAPSHOT.war --server.port=8082 >/root/springboot/spring_log.log 2>&1 &

3. 檢視執行

curl https://intbird.net/spring/
curl https://intbird.net/spring/book/books

在這裡插入圖片描述

4.demo

  1. demo1: https://gitlab.com/intbird/demo-spring-java-mvn
  2. demo2: https://gitlab.com/intbird/demo-spring-kotlin-gradle
  3. demo2 running service: https://intbird.net/spring/book/books

2.安裝mysql

1.安裝

之前的筆記: https://editor.csdn.net/md/?articleId=109116879

2.軟體

mysql workbench等

3.連線

1.服務機開啟3306埠或使用代理地址
2.客戶端遠端連線許可權管理(見上面筆記)
3.記住遠端登入地址,埠號,使用者名稱,密碼

3.spring程式碼編寫

1.專案依賴

  1. 使用線上模板: https://start.spring.io/
  2. 使用Idea Spring Initalizr建立時選擇依賴
  3. 在idea中手動查詢mvn庫

2. java+maven

  1. demo: https://gitlab.com/intbird/demo-spring-java-mvn

  2. 包含: java + maven + jdbc(mysql)

3. kotlin+gradle

  1. demo: https://gitlab.com/intbird/demo-spring-kotlin-gradle

  2. service: https://intbird.net/spring/book/books

  3. 包含: kotlin + gradle + jdbc(mysql)

  1. kotlin: https://www.kotlincn.net/docs/tutorials/spring-boot-restful.html
  2. gradle: https://scans.gradle.com/?_ga=2.145011444.1952171845.1603939257-899236739.1603939257
  3. jdbc: https://spring.io/projects/spring-data-jdbc

4.請求編寫

  1. ServletInitializer (@RestController 和 @RequestMapping )
@RestController
class ServletInitializer : SpringBootServletInitializer() {

    override fun configure(application: SpringApplicationBuilder): SpringApplicationBuilder {
    	// 也可以配置一些java設定
        return application.sources(SpringbootApplication::class.java)
    }

    @RequestMapping("/")
    fun home() = "intbird spring-root running..."
}

  1. application.properties 新增資料庫連線配置
spring.datasource.url=jdbc:mysql://intbird.ml:3306/demo_book
spring.datasource.username=intbird
spring.datasource.password=intbird
  1. BookController
@RestController
@RequestMapping("/book")
class BookController(var bookService: IBookService) {

    // http://127.0.0.1:8080/book/books
    @GetMapping(value = ["/books"])
    fun getAllBook(): List<Book>? {
        return bookService.getAllBook()
    }

    // http://127.0.0.1:8080/book/search?id=2
    @GetMapping(value = ["/search"])
    fun searchBook(id: Long): Book? {
        return bookService.searchBook(id)
    }

    // POST
    // http://127.0.0.1:8080/book/insert?id=3&name=intbird3&writer=intbird3
    @PostMapping(value = ["/insert"])
    fun insertBook(@ModelAttribute book: Book): Int? {
        return bookService.insertBook(book)
    }

    // PATCH
    // http://127.0.0.1:8080/book/update/1?name=intbird123
    @PatchMapping(value = ["/update/{id}"])
    fun updateBook(@PathVariable id: Long, @ModelAttribute book: Book): Int? {
        return bookService.updateBook(id,book)
    }

    // DELETE
    // http://127.0.0.1:8080/book/delete/3/
    @DeleteMapping(value = ["/delete/{id}"])
    fun deleteBook(@PathVariable id: Long): Int? {
        return bookService.deleteBook(id)
    }
}
  1. BookServiceImpl 自動實現 IBookService
    @Service java的serviceLoader方法
    關於Service使用看這裡: https://github.com/intbird/AutoServiceLoader
@Service
class BookServiceImpl(val bookDao: IBookDao) : IBookService {
    override fun getAllBook(): List<Book>? {
        return bookDao.getAllBook()
    }

    override fun searchBook(id: Long): Book? {
        return bookDao.searchBook(id)
    }

    override fun insertBook(book: Book): Int? {
        return bookDao.insertBook(book)
    }

    override fun updateBook(id: Long,book: Book): Int? {
        return bookDao.updateBook(id,book)
    }

    override fun deleteBook(id: Long): Int? {
        return bookDao.deleteBook(id)
    }
}

4 BookDaoImpl 自動實現 ‘IBookDao’
@Repository 實現JdbcTemplate引數自動初始化

@Repository
class BookDaoImpl(val jdbcTemplate: JdbcTemplate) : IBookDao {

    override fun getAllBook(): List<Book>? {
        val sql = "SELECT id, name, writer FROM t_book"
        return jdbcTemplate.query(sql, BeanPropertyRowMapper(Book::class.java))
    }

    override fun searchBook(id: Long): Book? {
        val sql = "SELECT id, name, writer FROM t_book WHERE id=?"
        return jdbcTemplate?.queryForObject(sql, arrayOf(id), BeanPropertyRowMapper(Book::class.java, true))
    }

    override fun insertBook(book: Book): Int? {
        val sql = "INSERT INTO t_book(id,name,writer) VALUES(?,?,?)"
        return jdbcTemplate?.update(sql, book.id, book.name, book.writer)
    }

    override fun updateBook(id: Long, book: Book): Int? {
        val sql = "UPDATE t_book SET id=?, name=?, writer=? WHERE id=?"
        return jdbcTemplate?.update(sql, book.id, book.name, book.writer, id)
    }

    override fun deleteBook(id: Long): Int? {
        val sql = "DELETE FROM t_book WHERE id=?"
        return jdbcTemplate?.update(sql, id)
    }
}

5.Book bean
簡化@Configuration @Bean
kotlin data 並不能自動Serializable,所以需要自己處理

data class Book(var id: Long = 0, var name: String = "", var writer: String = "") : Serializable
  1. orm資料庫持久化工具
    JPA : https://spring.io/projects/spring-data-jpa
    MyBatis: https://mybatis.org/mybatis-3/zh/index.html

  2. demo1: https://gitlab.com/intbird/demo-spring-java-mvn

  3. demo2: https://gitlab.com/intbird/demo-spring-kotlin-gradle

  4. demo running service: https://intbird.net/spring/book/books

end.

文章來自:http://blog.csdn.net/intbird 轉載請說明出處

相關文章