springboot: kotlin + gradle + jdbc(mysql) demo
目錄
spring.io文件
1. kotlin
2. kotlin-demo
spring-boot-applications-with-kotlin
3. jdbc
4. jdbc-demo
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
- demo1: https://gitlab.com/intbird/demo-spring-java-mvn
- demo2: https://gitlab.com/intbird/demo-spring-kotlin-gradle
- demo2 running service: https://intbird.net/spring/book/books
2.安裝mysql
1.安裝
之前的筆記: https://editor.csdn.net/md/?articleId=109116879
2.軟體
3.連線
1.服務機開啟3306埠或使用代理地址
2.客戶端遠端連線許可權管理(見上面筆記)
3.記住遠端登入地址,埠號,使用者名稱,密碼
3.spring程式碼編寫
1.專案依賴
- 使用線上模板: https://start.spring.io/
- 使用Idea Spring Initalizr建立時選擇依賴
- 在idea中手動查詢mvn庫
2. java+maven
-
包含: java + maven + jdbc(mysql)
3. kotlin+gradle
-
包含: kotlin + gradle + jdbc(mysql)
4.請求編寫
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..."
}
application.properties
新增資料庫連線配置
spring.datasource.url=jdbc:mysql://intbird.ml:3306/demo_book
spring.datasource.username=intbird
spring.datasource.password=intbird
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)
}
}
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
-
orm資料庫持久化工具
JPA : https://spring.io/projects/spring-data-jpa
MyBatis: https://mybatis.org/mybatis-3/zh/index.html -
demo running service: https://intbird.net/spring/book/books
end.
文章來自:http://blog.csdn.net/intbird 轉載請說明出處
相關文章
- 【SpringBoot Demo】MySQL + JPA + Hibernate + Springboot + Maven DemoSpring BootMySqlMaven
- springboot gradle demo (使用 Gradle 構建的 Spring Boot專案)Spring BootGradle
- Gradle Kotlin DSL 1.0GradleKotlin
- 安卓Toolbar使用 Demo(Kotlin)安卓Kotlin
- Gradle Kotlin DSL遷移指南GradleKotlin
- 【譯】當 Kotlin 遇見 GradleKotlinGradle
- jdbc連線elasticsearch6.3.0demo展示JDBCElasticsearch
- Gradle Kotlin DSL , 你知道它嗎?GradleKotlin
- Spark JDBC To MySQLSparkJDBCMySql
- JDBC-MySqlJDBCMySql
- SpringBoot簡單DemoSpring Boot
- 【Java】【Gradle】Gradle構建SpringBoot專案,Gradle模組化管理JavaGradleSpring Boot
- Gradle指南之從Groovy遷移到KotlinGradleKotlin
- 用Kotlin寫Android Gradle指令碼KotlinAndroidGradle指令碼
- SpringBoot 中使用 JDBC TempletSpring BootJDBC
- Mysql入門【JDBC】MySqlJDBC
- JDBC連線mysqlJDBCMySql
- SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫分表實踐Spring BootMyBatisMySqlJDBC
- [老實李] Gradle學習筆記-使用kotlin編寫gradle指令碼Gradle筆記Kotlin指令碼
- 【MySQL】com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:CommunicationslinkfailureMySqlJDBCExceptionAI
- Gradle構建SpringBoot專案GradleSpring Boot
- idea使用gradle搭建SpringBootIdeaGradleSpring Boot
- SpringBoot 與 Kotlin 完美交融Spring BootKotlin
- Maven構建springBoot Demo案例MavenSpring Boot
- MySQL的JDBC下載MySqlJDBC
- Java JDBC連線MYsqlJavaJDBCMySql
- 《SpringBoot實戰開發》——基於Gradle+Kotlin的企業級應用開發最佳實踐Spring BootGradleKotlin
- 使用IDEA的SpringBoot整合JDBCIdeaSpring BootJDBC
- 使用gradle構建springboot專案GradleSpring Boot
- java~springboot~gradle裡的docker整合JavaSpring BootGradleDocker
- 基於SpringBoot的策略模式demoSpring Boot模式
- JDBC for MySQL 最新jar包下載JDBCMySqlJAR
- SpringBoot--SpringBoot 讀取Properties檔案(結合JDBC)Spring BootJDBC
- MySQL5.7: sql script demoMySql
- Springboot整合mybatis框架(含例項Demo)Spring BootMyBatis框架
- 【JDBC + MySQL】〖程式碼〗JDBCUtils工具類(MySQL8)JDBCMySql
- jdbc 最新mysql jar包下載方法JDBCMySqlJAR
- MySQL-07——JDBC、Properties、DBCP、PreparedstatementMySqlJDBC