日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

java中jooq,在Spring中使用jOOQ源码案例

發布時間:2024/3/24 javascript 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中jooq,在Spring中使用jOOQ源码案例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring專題

在Spring中使用jOOQ源碼案例

雖然ORM大部分性能問題是由開發者自己引起的,以只讀方式使用ORM是不值得的,現在有一種其他可選方式,使用JOOQ,jOOQ從您的數據庫生成Java代碼,并允許您通過它流暢的API構建類型安全的SQL查詢。

依賴包:

Spring Framework?3.2.6. 使用其?aop,?beans,?core,?context,?context-support,?jdbc, 和?tx?模塊.

BoneCP?0.8.0. 作為數據庫連接池

jOOQ?3.2.2.

H2?1.3.174. 作為案例的數據庫.

pom.xml的配置:

org.springframework

spring-aop

3.2.6.RELEASE

org.springframework

spring-beans

3.2.6.RELEASE

org.springframework

spring-core

3.2.6.RELEASE

org.springframework

spring-context

3.2.6.RELEASE

org.springframework

spring-context-support

3.2.6.RELEASE

org.springframework

spring-expression

3.2.6.RELEASE

org.springframework

spring-jdbc

3.2.6.RELEASE

org.springframework

spring-tx

3.2.6.RELEASE

cglib

cglib

3.1

com.jolbox

bonecp

0.8.0.RELEASE

org.jooq

jooq

3.2.2

com.h2database

h2

1.3.174

DB 連接配置:

#Database Configuration

db.driver=org.h2.Driver

db.url=jdbc:h2:target/jooq-example

db.username=sa

db.password=

#jOOQ Configuration

jooq.sql.dialect=H2

#DB Schema

db.schema.script=schema.sql

負責持久的PersistenceContext?:

@Configuration

@ComponentScan({"net.petrikainulainen.spring.jooq.todo"})

@EnableTransactionManagement

@PropertySource("classpath:application.properties")

public?class?PersistenceContext?{

@Autowired

private?Environment?env;

@Bean(destroyMethod?=?"close")

public?DataSource dataSource()?{

BoneCPDataSource dataSource?=?new?BoneCPDataSource();

dataSource.setDriverClass(env.getRequiredProperty("db.driver"));

dataSource.setJdbcUrl(env.getRequiredProperty("db.url"));

dataSource.setUsername(env.getRequiredProperty("db.username"));

dataSource.setPassword(env.getRequiredProperty("db.password"));

return?dataSource;

}

@Bean

public?LazyConnectionDataSourceProxy lazyConnectionDataSource()?{

return?new?LazyConnectionDataSourceProxy(dataSource());

}

@Bean

public?TransactionAwareDataSourceProxy transactionAwareDataSource()?{

return?new?TransactionAwareDataSourceProxy(lazyConnectionDataSource());

}

@Bean

public?DataSourceTransactionManager transactionManager()?{

return?new?DataSourceTransactionManager(lazyConnectionDataSource());

}

@Bean

public?DataSourceConnectionProvider connectionProvider()?{

return?new?DataSourceConnectionProvider(transactionAwareDataSource());

}

@Bean

public?JOOQToSpringExceptionTransformer jooqToSpringExceptionTransformer()?{

return?new?JOOQToSpringExceptionTransformer();

}

@Bean

public?DefaultConfiguration configuration()?{

DefaultConfiguration jooqConfiguration?=?new?DefaultConfiguration();

jooqConfiguration.set(connectionProvider());

jooqConfiguration.set(new?DefaultExecuteListenerProvider(

jooqToSpringExceptionTransformer()

));

String?sqlDialectName?=?env.getRequiredProperty("jooq.sql.dialect");

SQLDialect dialect?=?SQLDialect.valueOf(sqlDialectName);

jooqConfiguration.set(dialect);

return?jooqConfiguration;

}

@Bean

public?DefaultDSLContext dsl()?{

return?new?DefaultDSLContext(configuration());

}

@Bean

public?DataSourceInitializer dataSourceInitializer()?{

DataSourceInitializer initializer?=?new?DataSourceInitializer();

initializer.setDataSource(dataSource());

ResourceDatabasePopulator populator?=?new?ResourceDatabasePopulator();

populator.addScript(

new?ClassPathResource(env.getRequiredProperty("db.schema.script"))

);

initializer.setDatabasePopulator(populator);

return?initializer;

}

}

將jOOQ Exceptions 傳遞到 Spring DataAccessExceptions

import?org.jooq.ExecuteContext;

import?org.jooq.SQLDialect;

import?org.jooq.impl.DefaultExecuteListener;

import?org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;

import?org.springframework.jdbc.support.SQLExceptionTranslator;

import?org.springframework.jdbc.support.SQLStateSQLExceptionTranslator;

public?class?JOOQToSpringExceptionTransformer?extends?DefaultExecuteListener?{

@Override

public?void?exception(ExecuteContext ctx)?{

SQLDialect dialect?=?ctx.configuration().dialect();

SQLExceptionTranslator translator?=?(dialect?!=?null)

??new?SQLErrorCodeSQLExceptionTranslator(dialect.name())

:?new?SQLStateSQLExceptionTranslator();

ctx.exception(translator.translate("jOOQ", ctx.sql(), ctx.sqlException()));

}

}

以上案例的源碼下載:at Github.

總結

以上是生活随笔為你收集整理的java中jooq,在Spring中使用jOOQ源码案例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。