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源码案例相关推荐

  1. java制作扫雷游戏中埋雷的难点_java 扫雷游戏源码案例项目

    代码如下import java.awt.*;import javax.swing.*;//图形计数器JCounter三位class JCounter extends JPanel {        p ...

  2. java 获取spring对象数组_解析Java中如何获取Spring中配置的bean

    解析Java中如何获取Spring中配置的bean Java中如何获取Spring中配置的bean?下面是由百分网小编为大家整理的解析Java中如何获取Spring中配置的bean,喜欢的可以收藏一下 ...

  3. 原生java读取properties与spring中@value、@ConfigurationProperties读取配置文件

    原生java读取properties与spring中@value.@ConfigurationProperties读取配置文件 1.properties类 Properties 继承于 Hashtab ...

  4. Redis在Java中的使用及连接数据库(附源码)

    Redis在Java中的使用及连接数据库(附源码) 引言: 本文主要分享了Redis如何在IDEA中部署,运行:模拟加入Redis的操作: 文章目录 Redis在Java中的使用及连接数据库(附源码) ...

  5. Java中ConcurrentHashMap底层实现原理(JDK1.8)源码分析2

    https://blog.csdn.net/programmer_at/article/details/79715177 https://blog.csdn.net/qq_41737716/categ ...

  6. python删除链表中重复的节点_Java编程删除链表中重复的节点问题解决思路及源码分享...

    一. 题目 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 二. 例子 输入链表:1->2->3->3->4->4-&g ...

  7. 3000门徒内部训练绝密视频(泄密版)第3课:Scala中函数式编程彻底精通及Spark源码阅读

    Scala中函数式编程彻底精通及Spark源码阅读 函数可以不依赖于类,函数可以作为函数的参数,函数可以作为函数的返回值 =>表明对左面的参数进行右面的加工 函数赋值给变量需要在函数名后面加空格 ...

  8. python处理回显_Python中getpass模块无回显输入源码解析

    本文主要讨论了python中getpass模块的相关内容,具体如下. getpass模块 昨天跟学弟吹牛b安利Python标准库官方文档的时候偶然发现了这个模块.仔细一看内容挺少的,只有两个主要api ...

  9. chrome浏览器开发者工具F12中某网站的sources下的源码如何批量保存?

    目录 chrome浏览器 开发者工具F12中某网站的sources下的源码如何批量保存 1. 常用保存Sources源码的两种方法 1.1单个文件 1.2 单个页面 2. 问题 3.解决方案 chro ...

最新文章

  1. 极限编程阅读笔记--第二篇
  2. 树莓派使用神经计算棒1代的准备(Tensorflow)
  3. 转:使用 Tkprof 分析 ORACLE 跟踪文件
  4. recorder_将Java Flight Recorder与OpenJDK 11一起使用
  5. mysql 插入中文 ERROR 1366 (HY000): Incorrect string value: '\xE7\x8E\x9E\x97' for column
  6. 【转】VS2005 CTP 版本这个CTP是什么意思
  7. Retrofit学习入门
  8. 【王道考研计算机网络】—计算机网络的概念 组成 功能 分类
  9. java遍历hashMap、hashSet、Hashtable
  10. 数据分析师如何正确地提意见?
  11. 面试官:MyBatis的SQL执行流程说这么详细,网上抄的吧!
  12. 安装ie11提示计算机安装了更新的版本,离线安装IE11浏览器提示quot;获取更新quot;解决方法 - 191路由网...
  13. 邮件、社交营销最全“勾搭”策略
  14. 交通强国试点方案密集落地!智慧交通成为关键词
  15. Roslyn 入门:使用 Roslyn 静态分析现有项目中的代码
  16. CRA图像 Clean Random Access (CRA) Pictures
  17. 云计算时代的域名解析
  18. linux 设置创建用户设置密码
  19. bootstrap的carousel-indicators是怎么实现垂直居中的
  20. 多种汉语方言语音落地应用,微软智能语音解锁更多交互场景

热门文章

  1. tplink 2.4g弱信号剔除_wifi信号差,网速慢?可能是你没有配置好2.4G和5G WiFi
  2. 深度研究微点主动防御
  3. Android中集成QQ登陆和QQ空间分享
  4. VMware虚拟机下安装win7系统详解
  5. MDPI的bib参考文献不显示会议地址
  6. 哪些东西,年轻人千万不能碰
  7. 输入数字或字母间隙变大
  8. java入门(List、Set、数据结构、Collections)
  9. 计算机在游戏过程中怎么返回页面,游戏时怎么切换到桌面,电脑打游戏时怎么切换到桌面...
  10. 敏捷-细说产品路线图(转)