Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JdbcTemplate in Kotlin

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

这篇文章给大家介绍JdbcTemplate怎么在Kotlin中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

import

import org.springframework.beans.factory.annotation.Autowiredimport org.springframework.jdbc.core.namedparam.MapSqlParameterSourceimport org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate

声明NamedParameterJdbcTemplate类型的成员变量

@Autowired private lateinit var jt: NamedParameterJdbcTemplate

各种用法

fun queryStudentList(): List { return jt.jdbcTemplate.query("""SELECT id, name | FROM t_student""".trimMargin()) { rs, _ -> Student(id = rs.getInt(1), name = rs.getString(2)) } } // 命名参数 fun queryStudentList2(gender: String): List { return jt.query("""SELECT id, name | FROM t_student | WHERE gender=:gender""".trimMargin(), MapSqlParameterSource("gender", gender)) { rs, _ -> Student(id = rs.getInt(1), name = rs.getString(2)) } } // 参数? fun queryStudentList3(gender: String): List { return jt.jdbcTemplate.query("""SELECT id, name | FROM t_student | WHERE gender=?""".trimMargin(), arrayOf(gender)) { rs, _ -> Student(id = rs.getInt(1), name = rs.getString(2)) } } fun queryStudent(id: Int): Student? { return jt.jdbcTemplate.query("""SELECT id, name | FROM t_student | WHERE id=?""".trimMargin(), arrayOf(id)) { rs -> if (rs.next()) { Student(id = rs.getInt(1), name = rs.getString(2)) } else null } } fun queryStudent2(id: Int): Student? { return jt.query("""SELECT id, name | FROM t_student | WHERE id=:id""".trimMargin(), mapOf("id" to id)) { rs -> if (rs.next()) { Student(id = rs.getInt(1), name = rs.getString(2)) } else null } } fun doesStudentExist(id: Int): Boolean { return jt.jdbcTemplate.query("""SELECT id | FROM t_student | WHERE id=?""".trimMargin(), arrayOf(id), ResultSet::next)!! }关于JdbcTemplate怎么在Kotlin中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report