JOOQ和Spring
有没有人试过在Spring框架中使用JOOQ,还是我开辟了新天地?
http://www.jooq.org
是的,很多人(现在)。 jOOQ手册包含一个关于如何开始使用jOOQ ,Spring,Spring-TX和BoneCP的教程:
Petri Kainulainen还有一个非常好的教程,解释了建立项目的每一步,在这里:
- 在Spring中使用jOOQ:配置
- 使用jOOQ和Spring:代码生成
- 使用jOOQ和Spring:CRUD
- 使用jOOQ with Spring:排序和分页
我希望使用jOOQ作为构建器库,为Spring的JdbcTemplate和相关类提供查询。 不幸的是,jOOQ似乎将两个概念组合到同一组类中:SQL生成和查询执行。 在我的情况下,我想要前者但是想让Spring处理后者。 但它确实有效。 例如,你可以这样做(使用jOOQ 2.x API):
Factory create = new Factory(null, SQLDialect.ORACLE); getJdbcTemplate().query( create.select(create.field(ID_COL), create.field(VALUE_COL)) .from(FOO_TABLE) .where(create.field(ID_COL).equals("ignored")) .getSQL(), myRowMapper, id);
所有你需要做/知道使jOOQ与spring一起工作:
- 获取事务管理器绑定到线程的
java.sql.Connection
。 - 通过exception翻译正确处理交易
- 了解jOOQ Factory对象(尽管名称)不是线程安全的。 因此需要在每次使用时实例化一个新对象(不要做其他答案 )。
因此,对于第一个和第二个案例,我提供了这个要点: https : //gist.github.com/3669307 ,这是Lukas推荐的 。
对于第三种情况,您可以基本上创建工厂的工厂(包含DataSource
),也可以使用spring组件中的有线DataSource
在每个方法中实例化一个新的Factory
对象。
@Service public class MyDaoOrService { @Autowired private void DataSource dataSource; @Transactional public void doSomeJooq(){ Settings s = new Settings(); //You could instead put this jooq configuration xml s.getExecuteListeners().add("com.snaphop.jooq.SpringExceptionTranslationExecuteListener"); MyGeneratedFactory f = new MyGeneratedFactory(dataSource, s); f.select(); //etc } }
至于设置监听器,您可以使用JOOQ的配置支持来避免程序化创建。
我不会介绍如何在Spring中设置DataSource
,因为它涵盖在无数其他/更好的地方。
使用jOOQ运行spring事务要简单得多(除非我忘了):
只需将您的数据源包装进去
org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
可选 :延迟打开jdbc连接,直到第一个实际的sql语句发生使用
org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
所以这样做是为了创建一个应用了’transactions’和’lazyness’的jOOQ工厂
DataSource rawDS = /* your actual data source */ // (optional) make access lazy final DataSource lazyDS = new LazyConnectionDataSourceProxy(rawDataSource); // make spring transactions available in plain jdbc context final DataSource txDS = new TransactionAwareDataSourceProxy(lazyDS); // create jOOQ factory Factory jooq = new Factory(txDS, /* dialect */, /* settings */) // voila!
希望这对某人有帮助….
Spring应用程序上下文配置
SYSTEM_PROPERTIES_MODE_OVERRIDE true getConnection
它将使用给定的连接自动填充公共工厂(是的,它可以是池连接,自动关闭等,请参阅DriverManagerDataSource类以获取更详细的配置)。 而现在,publicFactory。 注意:无需修改jOOQ生成的原始公共工厂。
/** * This class is generated by jOOQ */ package dbLayer.db; /** * This class is generated by jOOQ. */ @javax.annotation.Generated(value = {"http://www.jooq.org", "2.0.5"}, comments = "This class is generated by jOOQ") public class PublicFactory extends org.jooq.util.h2.H2Factory { private static final long serialVersionUID = -1930298411; /** * Create a factory with a connection * * @param connection The connection to use with objects created from this factory */ public PublicFactory(java.sql.Connection connection) { super(connection); } /** * Create a factory with a connection and some settings * * @param connection The connection to use with objects created from this factory * @param settings The settings to apply to objects created from this factory */ public PublicFactory(java.sql.Connection connection, org.jooq.conf.Settings settings) { super(connection, settings); } }
最后,只需致电工厂。
PublicFactory vs = (PublicFactory) SpringLoader.getBean("publicFactory"); SimpleSelectQuery sq = vs.selectQuery(dbLayer.db.tables.Version.VERSION); VersionRecord v = null; try { v = sq.fetchAny(); } catch (Exception e) { log.warn("Seems that version table does not exists!", e); }
完成!
假设您使用Spring构建Web应用程序,您可能希望执行以下操作:
try { Connection conn = dataSource.getConnection(); try { // Do something with JOOQ // No need to use a JdbcTemplate! } finally { if (conn != null) { conn.close(); } } } catch (SQLException e) { // your error handling }
您可能希望通过Spring的dependency injection来获取DataSource,因为您的Web容器Tomcat或者whathaveyou正在提供DataSource并进行连接池。 在你的一个spring配置文件中你会有类似的东西
上面代码所在的对象(或者使用数据源提供此代码的某个对象)可以在spring文件中配置以使用数据源对其进行实例化,例如
字符串“jdbc / datasource”的部分将对应于Web容器中配置的资源名称。 这有所不同,但对于Tomcat,它可能是Tomcat home下conf / Catalina / localhost中的上下文文件,例如,
最简单的方法,(我发现)使用jOOQ的Spring Transactions,这里给出: http ://blog.liftoffllc.in/2014/06/jooq-and-transactions.html
请查看此答案以获得更好的解释: https : //stackoverflow.com/a/24380508/542108
对于Java配置(Spring Boot的默认配置),您可以使用以下代码:
/* JOOQ Configuration */ @Bean public DataSourceConnectionProvider dataSourceConnectionProvider() { return new DataSourceConnectionProvider(dataSource()); } @Bean public DefaultConfiguration defaultConfiguration() { DefaultConfiguration defaultConfiguration = new DefaultConfiguration(); defaultConfiguration.setConnectionProvider(dataSourceConnectionProvider()); defaultConfiguration.setSQLDialect(SQLDialect.POSTGRES); return defaultConfiguration; } @Bean public DSLContext dslContext() { return new DefaultDSLContext(defaultConfiguration()); }