Jfinal+SQLite+Jetty 启动

发布于:2025-07-17 ⋅ 阅读:(18) ⋅ 点赞:(0)
<dependency>
    <groupId>org.xerial</groupId>
    <artifactId>sqlite-jdbc</artifactId>
    <version>3.49.1.0</version>
</dependency>
<dependency>
    <groupId>com.jfinal</groupId>
    <artifactId>jfinal</artifactId>
    <version>5.2.2</version>
</dependency>
<dependency>
    <groupId>com.jfinal</groupId>
    <artifactId>jetty-server</artifactId>
    <version>2019.3</version>
</dependency>
<build>
    <finalName>project</finalName>
<resources>
    <resource>
        <directory>src/main/webapp</directory>
        <targetPath>webapp</targetPath>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <targetPath>.</targetPath>
        <filtering>false</filtering>
    </resource>
</resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
        <plugin><!--核心打成jar包插件-->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.ncjk.bases.config.App</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>
@Override
public void configPlugin(Plugins me) {
    // SQLite
    String url = "jdbc:sqlite:D:\\database\\ticp.sqlite";
    try {
        Class.forName("org.sqlite.JDBC");
        LogKit.info("SQLite JDBC driver loaded successfully.");
    } catch (ClassNotFoundException e) {
        LogKit.error("SQLite JDBC driver not found.");
        throw new RuntimeException("SQLite JDBC driver not found.", e);
    }

    // 配置数据库连接池
    DruidPlugin druidPlugin = new DruidPlugin(url, "", "");
    me.add(druidPlugin);

    // 配置 ActiveRecord 插件
    ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
    Sqlite3Dialect d3 = new Sqlite3Dialect();
    d3.setModelBuilder(new MyModelBuilder());
    d3.setRecordBuilder(new MyRecordBuilder());
    arp.setDialect(d3);
    me.add(arp);
    _MappingKit.mapping(arp);

    // 增加定时任务配置文件
    me.add(new QuartzPlugin());
}
package com.ncjk.bases.config;

import com.jfinal.plugin.activerecord.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author 
 * @date 2025/7/3 13:44
 * @desc
 */
@SuppressWarnings("unchecked")
public class MyRecordBuilder extends RecordBuilder {
    @Override
    public List<Record> build(Config config, ResultSet rs, Function<Record, Boolean> func) throws SQLException {
        List<Record> result = new ArrayList<>();
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        String[] labelNames = new String[columnCount + 1];
        int[] types = new int[columnCount + 1];
        buildLabelNamesAndTypes(rsmd, labelNames, types);
        while (rs.next()) {
            Record record = new Record();
            CPI.setColumnsMap(record, config.getContainerFactory().getColumnsMap());
            Map<String, Object> columns = record.getColumns();
            for (int i = 1; i <= columnCount; i++) {
                Object value;
                if (types[i] < Types.BLOB) {
                    value = rs.getObject(i);
                } else {
                    if (types[i] == Types.CLOB) {
                        value = ModelBuilder.me.handleClob(rs.getClob(i));
                    } else if (types[i] == Types.NCLOB) {
                        value = ModelBuilder.me.handleClob(rs.getNClob(i));
                    } else if (types[i] == Types.BLOB) {
                        value = rs.getBytes(i);
                    } else {
                        value = rs.getObject(i);
                    }
                }

                columns.put(labelNames[i], value);
            }

            if (func == null) {
                result.add(record);
            } else {
                if (!func.apply(record)) {
                    break;
                }
            }
        }
        return result;
    }
}
package com.ncjk.bases.config;

import com.jfinal.plugin.activerecord.CPI;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.ModelBuilder;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author
 * @date 2025/7/3 13:41
 * @desc
 */
@SuppressWarnings("deprecation")
public class MyModelBuilder extends ModelBuilder {
    @SuppressWarnings({"unchecked"})
    public <T> List<T> build(ResultSet rs, Class<? extends Model> modelClass, Function<T, Boolean> func) throws SQLException, ReflectiveOperationException {
        List<T> result = new ArrayList<>();
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        String[] labelNames = new String[columnCount + 1];
        int[] types = new int[columnCount + 1];
        buildLabelNamesAndTypes(rsmd, labelNames, types);
        while (rs.next()) {
            Model<?> ar = modelClass.newInstance();
            Map<String, Object> attrs = CPI.getAttrs(ar);
            for (int i=1; i<=columnCount; i++) {
                Object value;
                if (types[i] < Types.BLOB) {
                    value = rs.getObject(i);
                } else {
                    if (types[i] == Types.CLOB) {
                        value = handleClob(rs.getClob(i));
                    } else if (types[i] == Types.NCLOB) {
                        value = handleClob(rs.getNClob(i));
                    } else if (types[i] == Types.BLOB) {
                        value = rs.getBytes(i);
                    } else {
                        value = rs.getObject(i);
                    }
                }

                attrs.put(labelNames[i], value);
            }

            if (func == null) {
                result.add((T)ar);
            } else {
                if ( ! func.apply((T)ar) ) {
                    break ;
                }
            }
        }
        return result;
    }

}

网站公告

今日签到

点亮在社区的每一天
去签到