Spring与Grails开发技术详解
1. Spring JDBC模板
Spring的 JdbcTemplate 可以通过数据源获取连接、语句和结果集,并在使用完毕后关闭它们。它还能将受检查的 SQLException 转换为运行时异常,方便错误处理。以下是使用 JdbcTemplate 的示例代码:
import misc.Person
import org.springframework.jdbc.core.RowMapper
import org.springframework.jdbc.core.JdbcTemplate
import java.sql.ResultSet
import java.sql.SQLException
def dataSource = ctx.dataSource
def template = new JdbcTemplate(dataSource)
long id = ...
Person person = template.queryForObject(
"select first_name, initial, last_name from person where id = ?",
[id] as Object[],
new RowMapper<Person>() {
Person mapRow(ResultSet rs, int rowNum) throws SQLException {
def p = new Person(fi
超级会员免费看
订阅专栏 解锁全文
91

被折叠的 条评论
为什么被折叠?



