JFinal+JOOQ 简单整合

本文来自: fair-jm.iteye.com 转截请注明出处

 

JFinal 介绍自行移步到其官网:

http://www.jfinal.com/

 

简单来说JFinal的定位是接近全栈的 所以我这边硬生生把JOOQ拉进来有点违背他的用意

这篇文章也就当作一个记录好了 以免以后忘记什么东西

 

环境使用:

  • JDK1.8
  • Tomcat8
  • JFinal 1.8(1.9已经出了 但maven仓库里还没有 我就偷懒用了1.8)
  • JOOQ 3.4.4 

JOOQ的codegen使用很简单 可以参考官网手册

http://www.jooq.org/doc/3.4/manual-pdf/jOOQ-manual-3.4.pdf

这里不再累述

 

首先是maven的依赖项:

 

	<dependencies>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.33</version>
		</dependency>
		<dependency>
			<groupId>org.jooq</groupId>
			<artifactId>jooq-meta</artifactId>
			<version>3.4.4</version>
		</dependency>
		<dependency>
			<groupId>com.jfinal</groupId>
			<artifactId>jfinal</artifactId>
			<version>1.8</version>
		</dependency>
		<dependency>
			<groupId>org.jooq</groupId>
			<artifactId>jooq</artifactId>
			<version>3.4.4</version>
		</dependency>
		<dependency>
			<groupId>org.jooq</groupId>
			<artifactId>jooq-codegen</artifactId>
			<version>3.4.4</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.1.2</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-access</artifactId>
			<version>1.1.2</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.1.2</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.7</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.9</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>XX</finalName>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>3.2</version>
					<configuration>
						<source>1.8</source>
						<target>1.8</target>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

 

 

logback的配置:

 

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%date{yyyy-MM-dd HH:mm:ss ZZZZ} - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
		</encoder>
	</appender>

	<appender name="FILE"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>logs/application.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- Daily rollover with compression -->
			<fileNamePattern>application-log-%d{yyyy-MM-dd}.gz</fileNamePattern>
			<!-- keep 30 days worth of history -->
			<maxHistory>30</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>%date{yyyy-MM-dd HH:mm:ss ZZZZ} - [%level] - from %logger in %thread %n%message%n%xException%n</pattern>
		</encoder>
	</appender>
	
	<!-- ingore log -->
	<logger name="org.jooq.Constants" level="OFF" />
	<logger name="com.alibaba.druid.pool.DruidDataSource" level="OFF" />

	<root level="INFO">
		<appender-ref ref="STDOUT" />
		<appender-ref ref="FILE" />
	</root>

</configuration>

 

 

web-inf.xml的配置:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
  http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
	<filter>
		<filter-name>jfinal</filter-name>
		<filter-class>com.jfinal.core.JFinalFilter</filter-class>
		<init-param>
			<param-name>configClass</param-name>
			<param-value>com.cc.xx.config.Config</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>jfinal</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<display-name>Archetype Created Web Application</display-name>
</web-app>

 

 

Config.java内容:

 

package com.cc.xx.config;
... ...
public class Config extends JFinalConfig {

    @Override
    public void afterJFinalStart() {
        super.afterJFinalStart();
        DBTool.shutDown();
    }

    @Override
    public void beforeJFinalStop() {
        super.beforeJFinalStop();
        DBTool.getDataSource();
    }

    @Override
    public void configConstant(Constants me) {
        me.setDevMode(true);
        me.setViewType(ViewType.FREE_MARKER);
    }

    @Override
    public void configRoute(Routes me) {
        me.add("/test", Test.class);
    }
 ... ...

}

   

 

数据库属性配置:

 

db.url=jdbc:mysql://localhost:3306/xx
db.name=root
db.password=
ds.initialSize=1
ds.maxActive=20
ds.minIdle=10
ds.maxIdle=15
ds.maxWait=60000
ds.validationQuery=SELECT 'x'

 

 

连接池配置:

 

package com.cc.xx.db;

... ...
public class DBTool {

    private static Logger LOG = LoggerFactory.getLogger(DBTool.class);
    private volatile static DruidDataSource dataSource;

    public static DataSource getDataSource() {
        if (dataSource == null) {
            synchronized (DBTool.class) {
                if (dataSource == null) {
                    Properties prop = new Properties();
                    try {
                        prop.load(DBTool.class.getResourceAsStream("/db.properties"));
                    } catch (IOException e) {
                        LOG.error("getDataSource", e);
                    }
                    dataSource = new DruidDataSource();
                    dataSource.setUrl((String) prop.get("db.url"));
                    dataSource.setUsername((String) prop.get("db.name"));
                    dataSource.setPassword((String) prop.get("db.password"));
                    dataSource.setInitialSize(Integer.valueOf((String) prop.get("ds.initialSize")));
                    dataSource.setMinIdle(Integer.valueOf((String) prop.get("ds.minIdle")));
                    dataSource.setMaxActive(Integer.valueOf((String) prop.get("ds.maxActive")));
                    dataSource.setMaxWait(Long.valueOf((String) prop.get("ds.maxWait")));
                    dataSource.setValidationQuery((String) prop.get("ds.validationQuery"));
                }
            }
        }
        return dataSource;
    }

    public static void shutDown() {
        if (dataSource != null) {
            dataSource.close();
        }
    }

}

 

 

测试代码:

 

package com.cc.xx.controller;
... ...
public class Test extends Controller {

    public void index() {
        List<String> s = DSL.using(DBTool.getDataSource(), SQLDialect.MYSQL).select(FACTORY.ID).from(FACTORY).limit(0, 10)
                .fetch(r -> r.getValue(FACTORY.ID));
        renderJson(s);
    }
}

 

这样就好了

可以看到这和我在play2和slick2整合一样 是单独使用了连接池 类似一种外挂的形式放到原有框架中去

不过也就是简单的整合 具体使用以后会再补充

 

 

本文来自: fair-jm.iteye.com 转截请注明出处

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值