Grails如果不想用Hibernate的东东,可以直接用Sql
配置部分可以参考——
?
[url=http://www.iteye.com/topic/11506?page=3]http://www.iteye.com/topic/11506?page=3[/url]
?
使用的代码:(配置、类似spring transactionTemplate、Closure = Callback)
?
?
?
?
?
?
?
?
配置部分可以参考——
?
[url=http://www.iteye.com/topic/11506?page=3]http://www.iteye.com/topic/11506?page=3[/url]
?
使用的代码:(配置、类似spring transactionTemplate、Closure = Callback)
?
?
<bean id="defaultDsTarget" lazy-init="true"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${db_local_driver}</value></property>
<property name="url"><value>${db_local_url}</value></property>
<property name="username"><value>${db_local_user}</value></property>
<property name="password"><value>${db_local_pwd}</value></property>
</bean>
<bean id="defaultDs" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg>
<ref bean="defaultDsTarget" />
</constructor-arg>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="defaultDsTarget"/>
</property>
</bean>?
?
?
static DataSource getDs(){
final dsBeanName = 'defaultDs'
def context = AppContext.getContextBiz()
return context.getBean(dsBeanName)
}
// use spring platform indenpendent TransactionManager
static DataSourceTransactionManager getTm(){
def context = AppContext.getContextBiz()
return context.getBean('transactionManager')
}
// not available in weblogic
static void withTrans(Closure callback){
def tm = getTm()
def transDef = new DefaultTransactionDefinition()
def status = tm.getTransaction(transDef)
try {
callback.call()
tm.commit(status)
}catch (ex) {
log.error('DSManager withTrans failed!', ex)
tm.rollback(status)
throw ex
}
}?
DSManager.withTrans{
def dao = new YourDAO() // use defaultDs from spring beanfactory
}?
?

本文详细介绍了如何在Grails中不使用Hibernate,而是直接通过配置数据库连接和事务管理,利用Spring框架来实现数据库操作。通过具体代码示例展示了如何设置数据库源、创建事务管理器和使用事务进行数据库操作。
5万+

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



